使用PDFBox将页眉添加到现有的PDF文件 [英] Adding Header to existing PDF File using PDFBox

查看:748
本文介绍了使用PDFBox将页眉添加到现有的PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将标头添加到现有的PDF文件中.它可以工作,但是现有PDF中的表格标题会因字体更改而变得混乱.如果删除设置字体,则标题不会显示.这是我的代码:

I am trying to add a Header to an existing PDF file. It works but the table header in the existing PDF are messed up by the change in the font. If I remove setting the font then the header doesn't show up. Here is my code:

    // the document
    PDDocument doc = null;
    try
    {
        doc = PDDocument.load( file );

        List allPages = doc.getDocumentCatalog().getAllPages();
        //PDFont font = PDType1Font.HELVETICA_BOLD;

        for( int i=0; i<allPages.size(); i++ )
        {
            PDPage page = (PDPage)allPages.get( i );
            PDRectangle pageSize = page.findMediaBox();
            PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, true,true);
            PDFont font = PDType1Font.TIMES_ROMAN;
            float fontSize = 15.0f;
            contentStream.beginText();
            // set font and font size
            contentStream.setFont( font, fontSize);
            contentStream.moveTextPositionByAmount(700, 1150);
            contentStream.drawString( message);
            contentStream.endText();

            //contentStream.
            contentStream.close();}

        doc.save( outfile );
    }
    finally
    {
        if( doc != null )
        {
            doc.close();
        }
    }
}`

推荐答案

基本上,您正在运行当前版本1.8.2中的PDFBox错误.

Essentially you are running into a PDFBox bug in the current version 1.8.2.

在创建新内容流之后,在使用字体之前添加页面资源的getFonts调用:

Add a getFonts call of the page resources after creating the new content stream before using a font:

PDPage page = (PDPage)allPages.get( i );
PDRectangle pageSize = page.findMediaBox();
PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, true,true);
page.getResources().getFonts();  // <<<<<<<<
PDFont font = PDType1Font.TIMES_ROMAN;
float fontSize = 15.0f;
contentStream.beginText();

错误本身:

该错误位于方法PDResources.addFont中,该方法从PDPageContentStream.setFont中调用:

The bug itself:

The bug is in the method PDResources.addFont which is called from PDPageContentStream.setFont:

public String addFont(PDFont font) 
{
    return addFont(font, MapUtil.getNextUniqueKey( fonts, "F" ));
}

它使用fonts成员变量的当前内容来确定当前页面上新字体资源的唯一名称.不幸的是,此时该成员变量仍可以 (并且在您的情况下为 )未初始化.这导致MapUtil.getNextUniqueKey( fonts, "F" )调用始终返回 F0 .

It uses the current content of the fonts member variable to determine a unique name for the new font resource on the page at hand. Unfortunately this member variable still can be (and in your case is) uninitialized at this time. This results in the MapUtil.getNextUniqueKey( fonts, "F" ) call to always return F0.

然后在addFont(PDFont, String)调用期间隐式初始化font变量.

The font variable then is initialized implicitly during the addFont(PDFont, String) call later.

因此,不幸的是,如果该页面上已经存在名为 F0 的字体,则会将其替换为新字体.

Thus, if unfortunately there already existed a font named F0 on that page, it is replaced by the new font.

已经用您的PDF测试过了,这正是您所遇到的情况.由于现有字体 F0 使用某种自定义编码,而替换字体使用标准字体,因此原来使用 F0 编写的文本现在看起来像乱码.

Having tested with your PDF this is exactly what happens in your case. As the existing font F0 uses some custom encoding while your replacement font uses a standard one, the text originally written using F0 now looks like gibberish.

上面提到的解决方法隐式初始化该成员变量,因此可以防止字体替换.

The work-around mentioned above implicitly initializes that member variable and, thus, prevents the font replacement.

如果您打算在生产中使用PDFBox来完成此任务,则可能要报告该错误.

If you plan to use PDFBox in production for this task, you might want to report the bug.

PS:如上面的评论中所述,在继承资源的上下文中还有另一个错误需要观察.也应该引起PDFBox开发的注意.

PS: As mentioned in the comments above there is another bug to observe in context with inherited resources. It should be brought to the PDFBox development's attention, too.

PPS:同时,对于版本1.8.3和2.0.0的问题,PDFBox中已解决了此问题,请参见. PDFBOX-1753 .

PPS: The issue at hand meanwhile has been fixed in PDFBox for versions 1.8.3 and 2.0.0, cf. PDFBOX-1753.

这篇关于使用PDFBox将页眉添加到现有的PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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