2017-10-12 60 views
1

如何在iText 7中創建頁面大小不相同的文檔?如何在iText 7中創建頁面大小不相等的文檔

iText7有可能嗎?

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

+0

是的。通過在'文檔'級別更改頁面大小,存在幾種方法。最好的方法取決於你的用例。如果在添加特定內容後需要新頁面,則可以設置頁面大小並添加分頁元素以強制新頁面,如果每個X頁面都需要類似橫向頁面的頁面,則頁面事件是最佳選擇 –

+0

是的,我想在添加特定內容後添加一個新頁面(不同的頁面類型)。如何設置頁面大小並添加分頁元素來強制新頁面?我需要幫助。謝謝你的評論。 – Franken

回答

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(); 
} 
0

也許它可能工作

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

感謝您的回答,但iText7中的Document類沒有setPageSize()api。 – Franken

相關問題