简体   繁体   中英

iTextSharp how to Add and Extract image to existing PDF

I'm trying to use iTextSharp for mobile application.
So i'm creating pdf files and append this pdf with image,where absolution position is Height/Width of image!
So now,i need to add images(for each image i should use new page)into my existing pdf file,also i would like to know how to extract this images from my PDF file!

At current moment i tried to add image via stamper :

 using (System.IO.Stream inputPdfStream = new FileStream(PathOfPdf, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                using (System.IO.Stream inputImageStream = new FileStream(PathOfImage, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    using (System.IO.Stream outputPdfStream = new FileStream(PathOfPdf.Substring(0, PathOfPdf.Length - 4) + "T.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        var reader = new PdfReader(inputPdfStream);
                        var stamper = new PdfStamper(reader, outputPdfStream);
                        var pdfContentByte = stamper.GetOverContent(1);

                        Image image = Image.GetInstance(inputImageStream);
                        image.SetAbsolutePosition(image.Width, image.Height);
                        pdfContentByte.AddImage(image);
                        stamper.Close();
                    }
                }
            }  

And the problem is that new image just overlaying first page of pdf and save it!
Why that happens ? Also,how i can extract this images from pdf file!?
Thanks!!

Normally, I would mark this question as a duplicate, because your question has been answered before, but you aren't asking a single question, but using your post to ask two different questions, I have to provide an answer with links to the answers to those two questions. My apologies in advance to the SO users who consider answering questions that have been answered before as not-done on SO.

Question #1:

The problem is that your image covers existing content instead of appearing on a new page.

This is caused by the fact that you deliberately "stamp" the image on page 1. You get a canvas that represents the content of page 1 using stamper.GetOverContent(1); and then you add content over that existing content.

In reality, you want to insert a new page as is explained in my "approach 2" in answer to this question: How to add a cover/PDF in a existing iText document

You want something like this:

Image image = Image.GetInstance(inputImageStream);
stamper.insertPage(1, image);
PdfContentByte page1 = stamper.GetOverContent(1);
image.SetAbsolutePosition(0, 0);
page1.AddImage(image);

In the first line, we create the image instance. In the second line, we insert a page to the existing document. We choose it to be page 1 and we want it to have the same size as the image (class Image extends Rectangle ). In the third line we grab the content of the new (blank) page 1. You wrote "where absolution position is Height/Width of image!" I assume that is an error in your question, because that doesn't make much sense. We want to add the image at position 0, 0. If we used the width and the height of the image, the image wouldn't be visible: it would be added outside the visible area of the page.

Question #2:

How to extract images from an existing PDF document?

Please read the answers to:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM