2017-10-12 8 views
1

iText 7でページサイズが異なるドキュメントを作成するにはどうすればよいですか?iText 7で不等なページサイズのドキュメントを作成する方法

iText7でも可能ですか?

iText5では、document.setPageSize()document.newPage()を使用しました。

+0

はい。 'Document'レベルでページサイズを変更することによっていくつかのアプローチが存在します。取る最良のアプローチはあなたのユースケースに依存します。特定のコンテンツが追加された後で新しいページが必要な場合は、ページサイズを設定して改ページ要素を追加して新しいページを強制することができます。等 –

+0

はい、特定のコンテンツが追加された後、新しいページ(異なるページの種類)を追加します。ページサイズを設定し、新しいページを強制するページ区切り要素を追加する方法は?私は助けが必要です。ご意見ありがとうございます。 – Franken

答えて

1

を働くかもしれませんページサイズを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

あなたは高レベルAPI(Document.add()およびその同類)を介してコンテンツを追加している、と目的のページ・サイズが直接特定のコンテンツに結び付けられ、デフォルトを変更した場合多分それは

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

答えていただきありがとうございますが、iText7のDocumentクラスにはsetPageSize()APIがありません。 – Franken

関連する問題