How to copy one page of a PDFDocument , to another PDFDocument

Nexussim Lements :

I have a doubt about PDFDocuments. How can I copy a specific page of a PdfDocument object , to another PdfDocument object.

I've tried with the moveTo method , but It didn't worked , also Ive tried with copyPagesTo method , but I throws an Requested page is out of bounds (when I try to copy for example only one page , from 1 to 1).

Any hint?

List<PdfDocument> pdfDocuments = new ArrayList<>();   
PdfDocument pdfWithMultiplePages = here I have a PDF with 3 pages.
for (int i = 0 ; i<pdfWithMultiplePages.getNumberOfPages() ; i++){
    final ByteArrayOutputStream byteArrayOutputStream = new 
    ByteArrayOutputStream();
    final PdfWriter pdfWriter = new PdfWriter(byteArrayOutputStream);    
    PdfDocument pdfDocument = new PdfDocument(pdfWriter);
    pdfDocument.copyPagesTo(i+1, i+1,pdfWithMultiplePages);
    pdfWriter.close();
    byteArrayOutputStream.close();
    shippingLabels.add(shippingLabelSplitted);
}

I've tried also with this :

List<PdfDocument> pdfDocuments = new ArrayList<>();   
PdfDocument pdfWithMultiplePages = here I have a PDF with 3 pages.
for (int i = 0 ; i<pdfWithMultiplePages.getNumberOfPages() ; i++){
    final ByteArrayOutputStream byteArrayOutputStream = new 
    ByteArrayOutputStream();
    final PdfWriter pdfWriter = new PdfWriter(byteArrayOutputStream);    
    PdfDocument pdfDocument = new PdfDocument(pdfWriter);
    pdfDocument.addPage(1,pdfWithMultiplePages.getPage(i+1));
    pdfWriter.close();
    byteArrayOutputStream.close();
    shippingLabels.add(shippingLabelSplitted);
}

But it throws :

com.itextpdf.kernel.PdfException: Page [email protected] cannot be added to document [email protected], because it belongs to document [email protected]

mkl :

A page in a PDF has many relations to other objects in a PDF.

If you could add a page located in one document to another one, the page would reside in both documents. Thus, the page suddenly would have to have all those relations to objects in both documents. This obviously does not work, thus iText prevents this.

Instead you have to create a copy of the page(s) in question for which the relations are switched to documents in the target document.

For this task there are multiple method overloads of PdfDocument.copyPagesTo. Thus, these methods indeed are the ones to use.

Unfortunately you mistake source and target of the operation:

PdfDocument pdfWithMultiplePages = here I have a PDF with 3 pages.
....
PdfDocument pdfDocument = new PdfDocument(pdfWriter);
pdfDocument.copyPagesTo(i+1, i+1,pdfWithMultiplePages);

This tries to copy page i+1 from pdfDocument to pdfWithMultiplePages. But you just created pdfDocument from scratch, so it does not have any pages yet. What you most probably want is:

pdfWithMultiplePages.copyPagesTo(i+1, i+1, pdfDocument);

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

PdfMerger iText7を使用してPdfDocumentをbyte []に

分類Dev

List <PdfDocument>を単一のPdfDocumentにマージする方法

分類Dev

PDFDocumentの1ページを別のPDFDocumentにコピーする方法

分類Dev

How to navigate one Content page to another Content page from client project (IOS/Android) in xamarin forms?

分類Dev

How to navigate one Content page to another Content page from client project (IOS/Android) in xamarin forms?

分類Dev

How to copy an object from one type to another in Java?

分類Dev

how to send form data from one page to another using django

分類Dev

Android-PdfDocument-LinearLayoutを複数のPDFページに分割

分類Dev

Android-PdfDocument-LinearLayoutを複数のPDFページに分割

分類Dev

How to copy references of elements of one vector to another vector?

分類Dev

PDFOutlineを使用してSwift / CocoaのPDFDocumentに目次を追加する

分類Dev

2つのPdfDocumentオブジェクトをマージする

分類Dev

How to duplicate/copy an item from one list onto another list

分類Dev

自分のキャンバスをPdfDocument.Pageに設定する方法

分類Dev

How to get the value of input field in one page and print it in another page?

分類Dev

既存のPdfDocumentをファイルに保存します

分類Dev

How to copy file attributes from one file to another in Java?

分類Dev

How to I copy one google doc into another

分類Dev

How to pass form fields from one page to another page in ReactJS?

分類Dev

How to submit a number from one page to another

分類Dev

PdfDocument2ページに印刷

分類Dev

How to copy the content from one cell to another

分類Dev

How can one copy a directory tree from one drive to another, replicating any junction points, under Windows?

分類Dev

How to copy one column data into another column in oracle

分類Dev

How to copy one directory to another with window command prompt?

分類Dev

How to copy file from one use to another?

分類Dev

How to take javascript from one page and use it in php on another page

分類Dev

Swift 3: PDFDocument がクラッシュする

分類Dev

PDFBox:印刷直後に「PDFDocumentを閉じませんでした」

Related 関連記事

  1. 1

    PdfMerger iText7を使用してPdfDocumentをbyte []に

  2. 2

    List <PdfDocument>を単一のPdfDocumentにマージする方法

  3. 3

    PDFDocumentの1ページを別のPDFDocumentにコピーする方法

  4. 4

    How to navigate one Content page to another Content page from client project (IOS/Android) in xamarin forms?

  5. 5

    How to navigate one Content page to another Content page from client project (IOS/Android) in xamarin forms?

  6. 6

    How to copy an object from one type to another in Java?

  7. 7

    how to send form data from one page to another using django

  8. 8

    Android-PdfDocument-LinearLayoutを複数のPDFページに分割

  9. 9

    Android-PdfDocument-LinearLayoutを複数のPDFページに分割

  10. 10

    How to copy references of elements of one vector to another vector?

  11. 11

    PDFOutlineを使用してSwift / CocoaのPDFDocumentに目次を追加する

  12. 12

    2つのPdfDocumentオブジェクトをマージする

  13. 13

    How to duplicate/copy an item from one list onto another list

  14. 14

    自分のキャンバスをPdfDocument.Pageに設定する方法

  15. 15

    How to get the value of input field in one page and print it in another page?

  16. 16

    既存のPdfDocumentをファイルに保存します

  17. 17

    How to copy file attributes from one file to another in Java?

  18. 18

    How to I copy one google doc into another

  19. 19

    How to pass form fields from one page to another page in ReactJS?

  20. 20

    How to submit a number from one page to another

  21. 21

    PdfDocument2ページに印刷

  22. 22

    How to copy the content from one cell to another

  23. 23

    How can one copy a directory tree from one drive to another, replicating any junction points, under Windows?

  24. 24

    How to copy one column data into another column in oracle

  25. 25

    How to copy one directory to another with window command prompt?

  26. 26

    How to copy file from one use to another?

  27. 27

    How to take javascript from one page and use it in php on another page

  28. 28

    Swift 3: PDFDocument がクラッシュする

  29. 29

    PDFBox:印刷直後に「PDFDocumentを閉じませんでした」

ホットタグ

アーカイブ