我有一个pdf(字节)存储在数据库表中,我还将图像(字节)存储在数据库表中。
现在我想从数据库中读取pdf文件并将图像(字节)插入到pdf文件中,并希望将新的pdf(字节)保存到新表中。 它会给出错误"文档没有页面。" 我使用下面的代码:
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;
答案 0 :(得分:2)
您的代码太多,缺少一条重要的代码。我拿了你的代码并删除了所有不必要的行(这会使你的文件损坏):
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;
我还添加了缺失的行:stamper.Close();