gpt4 book ai didi

java - itext 中 PDFFontFactory 类的新常量是什么?

转载 作者:行者123 更新时间:2023-12-02 01:26:24 28 4
gpt4 key购买 nike

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

该程序使用 iText7(库也使用 slf4j)。我尝试创建自己的字体,但这使用了 Font 类,我不确定应该从哪里导入常量(第一次尝试是 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

关于java - itext 中 PDFFontFactory 类的新常量是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56853541/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com