PDFOne Java可以在创建PDF时或者是对存在的PDF文件进行字体以及颜色的设置,开发人员可以轻松使用 PdfFont对象来实现该功能,具体可以参考下面的部分代码:
// Create a PdfDocument instance
PdfDocument doc = new PdfDocument();
try {
// Load an existing PDF document
doc.load("sample_doc1.pdf");
// Create font objects
PdfFont fontArialItalic =
PdfFont.create("Arial", // name of installed font
PdfFont.ITALIC | PdfFont.STROKE_AND_FILL,
18,
PdfEncodings.WINANSI);
PdfFont fontTahomaNormal =
PdfFont.create("tahoma.ttf", // pathname of a font file
PdfFont.STROKE_AND_FILL,
48,
PdfEncodings.WINANSI);
// Write text on page 1 using the Arial font
doc.writeText("Hello again, World!",
fontArialItalic, // font
100, 50);
// Change font properties
fontTahomaNormal.setStrokeWidth(2);
fontTahomaNormal.setStrokeColor(Color.RED);
fontTahomaNormal.setColor(Color.ORANGE);
// Write more text on page 1 using the Tahoma font
doc.writeText("Hello again, World!",
fontTahomaNormal, // font
100, 100);
// Write modified document to output file
doc.save("sample_doc3.pdf");
doc.close();
} catch (IOException | PdfException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}