我在一个程序中写信并以 PDF 格式导出。我最近实现了一个小型控制台程序,它应该遍历每个页面并将我的特殊设计的字母(包含我的地址等)设置为背景。
问题是,我的信件是用 Calibri 写的。在使用 pdf 压模之前,原始 PDF 看起来很棒,在设置背景之后,(未修改的文本)的样式看起来有点不同。
看截图,我的意思。
我无法向我解释,因此我认为这可能是一个错误。你有什么建议吗?
我的代码在这里。
public static void main(String[] args) throws IOException, DocumentException {
PdfBackgroundSetter.setBackgroundToPdf(args[0], args[1], args[2]);
}
public static void setBackgroundToPdf(String inputContentPdfPath, String outputPdfPath, String inputBackgroundPdfPath) throws IOException, DocumentException {
PdfReader inputContentReader = new PdfReader(inputContentPdfPath);
PdfStamper outputStamper = new PdfStamper(inputContentReader, new FileOutputStream(outputPdfPath));
PdfReader inputBackgroundReader = new PdfReader(inputBackgroundPdfPath);
PdfImportedPage backgroundPage = outputStamper.getImportedPage(inputBackgroundReader, 1);
int numberOfPages = inputContentReader.getNumberOfPages();
for (int i = 1; i <= numberOfPages; i++) {
outputStamper.getUnderContent(i).addTemplate(backgroundPage, 0, 0);
}
outputStamper.close();
inputContentReader.close();
inputBackgroundReader.close();
}
稍微修改(匿名)的 PDF 文件可以在这里找到: