【问题标题】:How to create a document with unequal page sizes in iText 7如何在 iText 7 中创建页面大小不等的文档
【发布时间】:2017-10-12 10:44:09
【问题描述】:

如何在 iText 7 中创建页面大小不等的文档?

在 iText7 中可以吗?

在 iText5 中,我使用了 document.setPageSize()document.newPage()

【问题讨论】:

  • 是的。通过在Document 级别更改页面大小存在几种方法。采取的最佳方法取决于您的用例。如果您在添加某些内容后需要新页面,您可以设置页面大小并添加分页符以强制新页面,如果您需要每 X 页横向页面,页面事件是您的最佳选择等。
  • 是的,我想在添加某些内容后添加一个新页面(不同的页面类型)。如何设置页面大小并添加分页符以强制打开新页面?我需要帮助。感谢您的评论。

标签: java itext itext7


【解决方案1】:

如果您通过高级 API(Document.add() 及其同类)添加内容,并且所需的页面大小直接与特定内容相关联,则通过 PdfDocument.setDefaultPageSize 修改默认页面大小可能是最干净和最简单的方法,像这样:

public void createPdf(String dest) throws IOException, FileNotFoundException{
    PdfWriter writer = new PdfWriter(dest);
    PdfDocument pdfDoc = new PdfDocument(writer);
    Document doc = new Document(pdfDoc);
    pdfDoc.setDefaultPageSize(PageSize.A5);//All pages will be added using this page size
    String paragraphOneText = "I have seen the face of sorrow\n" +
            "She looks away in the distance\n" +
            "Across all these bridges\n" +
            "From whence I came\n" +
            "And those spans, trussed and arched\n" +
            "Hold up our lives as we go back again\n" +
            "To how we thought then\n" +
            "To how we thought we thought then";
    String paragraphTwoText = "I have seen sorrow's face,\n" +
            "But she is ever turned away\n" +
            "And her words leave me blind\n" +
            "Her eyes make me mute\n" +
            "I do not understand what she says to me\n" +
            "I do not know if to obey\n" +
            "Or attempt a flood of tears";
    String paragraphThreeText  = "I have seen her face\n" +
            "She does not speak\n" +
            "She does not weep\n" +
            "She does not know me\n" +
            "For I am but a stone fitted in place\n" +
            "On the bridge where she walks";
    String attribution = "--Toc the Younger";

    Paragraph p = new Paragraph(paragraphOneText);
    //Current default pagesize is A5, so any new pages will be created as A5
    doc.add(p);
    //Changing default pagesize will affect any new pages that are created
    pdfDoc.setDefaultPageSize(PageSize.A5.rotate());
    //Adding an areabreak of type NEXT_PAGE will force the creation of a new page
    doc.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
    p = new Paragraph(paragraphTwoText);
    doc.add(p);
    pdfDoc.setDefaultPageSize(PageSize.A5);
    doc.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
    p = new Paragraph(paragraphThreeText);
    doc.add(p);
    p= new Paragraph(attribution);
    doc.add(p);
    doc.close();
}

【讨论】:

    【解决方案2】:

    也许它会起作用

    Rectangle one = new Rectangle(70,140);
    document.setPageSize(one);
    

    【讨论】:

    • 感谢您的回答,但 iText7 中的 Document 类没有 setPageSize() api。
    猜你喜欢
    • 2014-05-31
    • 2020-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多