Irina_1001
Irina_1001

Reputation: 21

iTextSharp 5.3.3: replace the pages from the 1st document and insert the pages from the 2nd document instead of them

Forgive me for the bad english. I want replace the pages from the 1st document and insert the pages from the 2nd document instead of them. I use itextsharp 5.3.3. Pages of the Second document contain pictures. My code:

reader1:=New iTextSharp.text.pdf.PdfReader (file_name_1);

reader2:=New iTextSharp.text.pdf.PdfReader (file_name_2);

Document:= New iTextSharp.text.Document();

Document.Compress:=False;

For i:=4 To reader1.NumberOfPages Do

reader1.SetPageContent(i,reader2.GetPageContent(i));    

End For;

Stamper:=New iTextSharp.text.pdf.PdfStamper(reader1, New System.IO.FileStream(new_file_name, System.IO.FileMode.CreateNew));

stamper.Close();

As a result, the images in new document mixed up. What am I doing wrong? Thanks for any help!

Upvotes: 0

Views: 1074

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77606

Your code is wrong on many levels. You are copying content streams without copying any of the resources. I never want to see such code again, ever!

Please read http://www.manning.com/lowagie2/samplechapter6.pdf

The best way to achieve your assignment is to use PdfCopy. Create two PdfReader objects, and add 4 PdfImportedPage objects from the second reader, following by PdfImportedPage objects from the first reader starting at page 5.

Use the following code samples for inspiration:

http://itextpdf.com/examples/iia.php?id=123

http://kuujinbo.info/iTextInAction2Ed/index.aspx?ch=Chapter06&ex=Concatenate

If you've found a page advising to use your original approach, please let me know so that I can take action to have that page removed. If you've found this page on itextpdf.com, please DO NOT USE those examples WITHOUT READING THE DOCUMENTATION!

Upvotes: 2

Related Questions