用PDFBox填充景观PDF [英] Filling landscape PDF with PDFBox

查看:310
本文介绍了用PDFBox填充景观PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试用PDFBox填充PDF表单,并且设法用纵向文档很好地做到了这一点.但是在横向模式下填充文档时出现问题.字段被填满,但是文本方向不好.它看起来像是垂直的,但仍然是纵向的,但旋转了90度.

I try to fill a PDF form with PDFBox and I managed to do it well with a portrait oriented document. But I have a problem when filling a document in landscape mode. The fields are filled up, but the text orientation is not good. It appear vertically like if it was still in portrait but in a rotation of 90 degrees.

这是我的简化代码:

PDDocument pdfDoc = PDDocument.load(MY_FILE);
PDDocumentCatalog docCatalog = pdfDoc.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();

acroForm.getField("aAddressLine1").setValue("ADDRESS1_HERE");
acroForm.getField("aAddressLine2").setValue("ADDRESS1_HERE");
acroForm.getField("country").setValue("COUNTRY_HERE");

pdfDoc.save(PATH_HERE);
pdfDoc.close();

您是否设法以横向模式填充PDF文档?

Did you manage to fill a PDF document in landscape mode?

感谢您的帮助.

推荐答案

简短答案

恐怕PDFBox尚未(从1.8.2版开始)允许您像您提供的那样填充横向PDF,因为它似乎没有查询和考虑有关表单字段所在页面的信息.上.

I'm afraid PDFBox does not yet (as of version 1.8.2) allow you to fill in landscape PDFs like the one you provided because it does not seem to query and factor in informations about the page the form field is located on.

长答案

有多种方法可以将页面定义为A4横向:

There are different ways you can define a page to be A4 landscape:

  1. 您可以通过媒体框定义将其直接定义为具有A4横向尺寸:

  1. You can define it to have the A4 landscape dimensions directly by means of a media box definition:

/MediaBox [0, 0, 842, 595]

在这种情况下,您的aAddressLine1的坐标为

In this case the coordinates of your aAddressLine1 would be

/Rect[23.1711 86.8914 292.121 100.132]

  • ,或者您可以将其定义为具有A4纵向尺寸并旋转90°(显然是270°):

  • or you can define it to have the A4 portrait dimensions and being rotated by 90° (or 270° obviously):

    /MediaBox [0, 0, 595, 842]
    /Rotate 90
    

    在这种情况下,aAddressLine1的坐标是

    In this case the coordinates of your aAddressLine1 are

    /Rect[86.8914 23.1711 100.132 292.121]
    

  • 您的示例文档使用后一种方法.

    Your example document uses the latter method.

    现在,PDFBox在为该字段创建外观流时,只会查看定义该字段的矩形,而忽略页面的属性.因此,PDFBox会看到一个非常狭窄且非常高的文本字段,并以此填充它.完全不知道结果将在PDF查看器中旋转.

    Now PDFBox, when creating an appearance stream for that field, only looks at the rectangle defining the field but ignores the properties of the page. Thus, PDFBox sees a very narrow and very high textfield and fills it in just like that. It is completely unaware that the result will be rotated in a PDF viewer.

    它应该做的是还查看该字段所在的页面.如果该页面上有一个/Rotate 条目,则应为该字段创建一个外观流,以显示以相反方向旋转的文本.

    What it should have done is to also look at the page the field is located on. If that page has a /Rotate entry, it should create an appearance stream for the field which displays the text rotated in the opposite direction.

    替代品

    在评论中您还问了

    如果PDFBox无法执行我想要的操作,您知道我可以使用的另一个库吗?

    Do you know another library I could use if PDFBox can't do what I want?

    我已经使用 iText 5.4.2测试了这一壮举:

    I have tested the feat with iText 5.4.2:

        PdfReader reader = new PdfReader(MY_FILE);
        OutputStream os = new FileOutputStream(PATH_HERE);
        PdfStamper stamper = new PdfStamper(reader, os);
    
        AcroFields acroFields = stamper.getAcroFields();
        acroFields.setField("aAddressLine1", "ADDRESS1_HERE");
        acroFields.setField("aAddressLine2", "ADDRESS1_HERE");
        stamper.close();
    

    (免费的iText版本是根据AGPL许可的;您必须确定这对您的项目是否可行.如果没有,也要获得商业许可.)

    (The free iText version is licensed under the AGPL; you have to decide whether that's ok for your project. There is a commercial license, too, if it's not ok.)

    我敢肯定其他PDF库也可以做到这一点,毕竟它并不是一个太奇特的功能...

    I'm sure other PDF libraries also can do that, it's not too exotic a feature after all...

    但是我还测试了 PDF小丑 0.1.3(树干版),也不起作用:

    But I also tested PDF Clown 0.1.3 (trunk version), which did not work either:

    File file = new File(MY_FILE);
    
    Document document = file.getDocument();
    Form form = document.getForm();
    form.getFields().get("aAddressLine1").setValue("ADDRESS1_HERE");
    form.getFields().get("aAddressLine2").setValue("ADDRESS1_HERE");
    
    file.save(new java.io.File(PATH_HERE), SerializationModeEnum.Incremental);
    file.close();
    

    这篇关于用PDFBox填充景观PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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