itext 中 PDFFontFactory class 的新常量是什么?

What are the new constants for the PDFFontFactory class in itext?

我想使用 iText7 中的 PdfFontFactory 库;但是,我不能再使用 FontConstants(即 FontConstants.COURIER)。这些常量已被标记为已弃用,但我找不到应该用什么替换它。

此程序使用 iText7(库也使用 slf4j)。我曾尝试创建自己的字体,但这使用了字体 class,我不确定应该从哪里导入常量(第一次尝试是 java.awt,但没有成功)。我也尝试过为参数创建我自己的值,并且我尝试过使用您在代码前面看到的无参数版本。我从 iText 教程中获得了这段代码和常量:https://itextpdf.com/en/resources/books/itext-7-jump-start-tutorial-java/chapter-5-manipulating-existing-pdf-document

PdfDocument pdfDoc = null;
        try {
            pdfDoc = new PdfDocument(new PdfReader(sourcePDF), new PdfWriter(destPDF));

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        PdfAnnotation ann = new PdfTextAnnotation(new Rectangle(400, 795, 0, 0))
                .setTitle(new PdfString("iText"))
                .setContents("Please, fill out the form.");
            pdfDoc.getFirstPage().addAnnotation(ann);
            PdfCanvas canvas = new PdfCanvas(pdfDoc.getFirstPage());
            canvas.beginText().setFontAndSize(
                    PdfFontFactory.createFont(), 12)
                    .moveText(265, 597)
                    .showText("I agree to the terms and conditions.")
                    .endText();
            PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
            Map<String, PdfFormField> fields = form.getFormFields();
            fields.get("language").setValue("English");
            fields.get("experience1").setValue("Yes");
            fields.get("experience2").setValue("Yes");
            fields.get("experience3").setValue("Yes");
            fields.get("shift").setValue("Any");
            PdfFont courier = PdfFontFactory.createFont(FontConstants.COURIER);
            fields.get("info")
                .setValue("I was 38 years old when I became a 007 agent.", courier, 7);
            pdfDoc.close();

当 运行 这段代码时,我没有收到任何错误,但是由于不推荐使用的值,eclipse 抛出了一些警告。

itext 已弃用原始 FontConstants.java 并将所有与字体相关的常量移至 com.itextpdf.io.font.constants 因此您可以使用以下选项代替 FontConstants.java .

https://api.itextpdf.com/iText7/7.1.2/com/itextpdf/io/font/constants/StandardFontFamilies.html

https://api.itextpdf.com/iText7/7.1.2/com/itextpdf/io/font/constants/StandardFonts.html

您现在可以使用 StandardFonts 而不是 FontConstants

PdfFont courier = PdfFontFactory.createFont(StandardFonts.COURIER);