0

I have a pdf(byte) stored in database table and I have also stored image(byte) in database table.

Now I want to read pdf file from database and insert image (byte) into pdf file and want to save new pdf (byte) into new table. It gives error as "The Document has no page." I have used below code:

 string fileName = "~/AuthDoc/" + Convert.ToString(consentMain.AppointmentId) +".pdf";                              
            string newFile = System.Web.Hosting.HostingEnvironment.MapPath(fileName);
            PdfReader reader = new PdfReader(consentDoc); // get pdf byte from datbase
            Rectangle size = reader.GetPageSizeWithRotation(1);
            Document document = new Document(size);
            // open the writer
            FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);
            PdfWriter writer = PdfWriter.GetInstance(document, fs);
            document.Open();



            // PdfReader reader = new PdfReader(consentDoc);
          //   FileStream fs = new FileStream(newFile, FileMode.Create);
            var stamper = new PdfStamper(reader, fs);
            var pdfContentByte = stamper.GetOverContent(1);
            iTextSharp.text.Image PatientSign = iTextSharp.text.Image.GetInstance(consentMain.PatientSign); // image from database
            PatientSign.SetAbsolutePosition(100, 100);
            pdfContentByte.AddImage(PatientSign);

            document.Close();
            fs.Close();
            writer.Close();
            reader.Close();
            byte[] bytes = System.IO.File.ReadAllBytes(newFile);
            return bytes;
5
  • Probably not going to fix your problem, but I wouldn't call fs.Close() before writer.Close(). If you put disposables in a using block you'd automatically force yourself to use the right order. Commented Jan 31, 2018 at 10:08
  • Your code is old, in the sense that you're not using the most recent version of iText, and it's awkward in the sense that you use the Document and the PdfStamper class. Those classes were mutually exclusive in the version of iText you're using. Why don't you upgrade? Commented Jan 31, 2018 at 10:09
  • Start by removing all references to Document and PdfWriter (throw away those lines completely), then make sure you close the PdfStamper object: stamper.Close(). Or... throw away all of your code and start anew with iText 7 for .NET. Commented Jan 31, 2018 at 10:10
  • Thanks @BrunoLowagie , Now it works fine. Commented Jan 31, 2018 at 11:28
  • I made my comment an answer. Commented Jan 31, 2018 at 11:32

1 Answer 1

2

You have too much code, and one important line is missing. I took your code and removed all the lines that aren't necessary (and that make your file corrupt):

string fileName = "~/AuthDoc/" + Convert.ToString(consentMain.AppointmentId) +".pdf";                              
string newFile = System.Web.Hosting.HostingEnvironment.MapPath(fileName);
PdfReader reader = new PdfReader(consentDoc); // get pdf byte from datbase
FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);
var stamper = new PdfStamper(reader, fs);
var pdfContentByte = stamper.GetOverContent(1);
iTextSharp.text.Image PatientSign = iTextSharp.text.Image.GetInstance(consentMain.PatientSign); // image from database
PatientSign.SetAbsolutePosition(100, 100);
pdfContentByte.AddImage(PatientSign);
stamper.Close();
reader.Close();
byte[] bytes = System.IO.File.ReadAllBytes(newFile);
return bytes;

I also added the missing line: stamper.Close();

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks @Bruno, Code woks fine
Hi Using your code I can add images on first page. How to add image on second page of pdf?
Do you see the line stamper.GetOverContent(1);? The 1 indicates that you want to add something on the first page. If you want to add something on the second page, you need stamper.GetOverContent(2); If there is no second page, you need the InsertPage() method to insert an extra page.
Can you please look at stackoverflow.com/questions/48560523/…. I have an issue to write text on extingin pdf file if pf has more then 1 page. Thanks in Advance.
I don't understand that question. It's not well-written. You should rephrase it so that a 10-year old can understand it. Right now, it's written as if you expect that someone can read your mind. Also: you are using an old version of iText. Why didn't you start using iText 7 instead?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.