为什么以前在Itext7中没有使用内容流将字体嵌入到PDF中? [英] Why font is not being embedded to the PDF using content stream before in Itext7?

查看:106
本文介绍了为什么以前在Itext7中没有使用内容流将字体嵌入到PDF中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用IText示例网站(

I am trying to add a watermark text with font Helvitica to a simple PDF using one of the examples provided in the IText examples site (https://developers.itextpdf.com/examples/stamping-content-existing-pdfs/clone-watermark-examples), but for some reason the PDF is not showing the font properly in the PDF.

我查看了pdf属性字体,似乎该字体未嵌入到PDF中.

I looked at the pdf property fonts and it seems the font is not embedded to the PDF.

我正在使用itext 7.0.8版本.

I am using itext 7.0.8 version.

我在这里做错什么了吗

我的代码:

import java.io.FileNotFoundException;
import java.io.IOException;
import com.itextpdf.io.font.FontConstants;
import com.itextpdf.io.font.FontProgramFactory;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfResources;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Canvas;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.layout.property.VerticalAlignment;

public class AddTextToPDF {
    public static void main(String[] args) throws FileNotFoundException, IOException {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader("c:\\Development\\test.pdf"),
                new PdfWriter("c:\\Development\\test_result.pdf"));
        PdfCanvas under = new PdfCanvas(pdfDoc.getFirstPage().newContentStreamBefore(), new PdfResources(), pdfDoc);
        PdfFont font = PdfFontFactory.createFont(FontProgramFactory.createFont(FontConstants.HELVETICA));
        Paragraph p = new Paragraph("This watermark is added UNDER the existing content")
                .setFont(font).setFontSize(15);
        new Canvas(under, pdfDoc, pdfDoc.getDefaultPageSize())
                .showTextAligned(p, 297, 550, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
        pdfDoc.close();
    }
}

如果我更改行:

PdfCanvas under = new PdfCanvas(pdfDoc.getFirstPage().newContentStreamBefore(), new PdfResources(), pdfDoc);

收件人

PdfCanvas over = new PdfCanvas(pdfDoc.getFirstPage());

字体被嵌入到PDF中.

the font is being embedded to the PDF..

推荐答案

发现自己后,问题与以下行有关:

As you found out yourself, the issue is related to this line:

PdfCanvas under = new PdfCanvas(pdfDoc.getFirstPage().newContentStreamBefore(), new PdfResources(), pdfDoc);

问题是您在这里使用了new PdfResources()对象,此对象以后将不再使用.

The problem is that you use a new PdfResources() object here which is not used for anything later on.

在此PdfCanvas构造函数中提供的资源对象是放置在画布上所需的新资源的地方,例如新字体资源.

The resources object you provide in this PdfCanvas constructor is where new resources are put that are required for what you draw on the canvas, e.g. new font resources.

因此,在您的情况下,新字体将添加到新的资源对象中,然后再无处添加,因此最终不会出现在最终的pdf中.因此字体丢失了.

Thus, in your case the new font is added to a new resources object which then is added nowhere and so does not end up in the final pdf at all. So the font is lost.

要解决此问题,请改用页面资源.

To fix this use the resources of the page instead.

这篇关于为什么以前在Itext7中没有使用内容流将字体嵌入到PDF中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆