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;
fs.Close()beforewriter.Close(). If you put disposables in ausingblock you'd automatically force yourself to use the right order.Documentand thePdfStamperclass. Those classes were mutually exclusive in the version of iText you're using. Why don't you upgrade?DocumentandPdfWriter(throw away those lines completely), then make sure you close thePdfStamperobject:stamper.Close(). Or... throw away all of your code and start anew with iText 7 for .NET.