我想利用 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-manipulate-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 …Run Code Online (Sandbox Code Playgroud)