C# 如何使用ItextSharp将图像(字节)插入pdf(字节)
我在数据库表中存储了一个pdf(字节),在数据库表中也存储了图像(字节) 现在我想从数据库中读取pdf文件,将图像(字节)插入pdf文件,并将新的pdf(字节)保存到新表中。 它给出的错误为“文档没有页面” 我使用了以下代码:C# 如何使用ItextSharp将图像(字节)插入pdf(字节),c#,asp.net,itext,C#,Asp.net,Itext,我在数据库表中存储了一个pdf(字节),在数据库表中也存储了图像(字节) 现在我想从数据库中读取pdf文件,将图像(字节)插入pdf文件,并将新的pdf(字节)保存到新表中。 它给出的错误为“文档没有页面” 我使用了以下代码: string fileName = "~/AuthDoc/" + Convert.ToString(consentMain.AppointmentId) +".pdf"; string
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;
代码太多,缺少一行重要的代码。我获取了您的代码并删除了所有不必要的行(以及使您的文件损坏的行):
我还添加了缺少的行:
stamper.Close()代码>您的代码太多,缺少一行重要的代码。我获取了您的代码并删除了所有不必要的行(以及使您的文件损坏的行):
我还添加了缺少的行:stamper.Close()
可能无法解决您的问题,但我不会在writer.Close()之前调用fs.Close()
。如果您使用
块将一次性物品放入一个块中,您会自动强制自己使用正确的顺序。您的代码很旧,因为您没有使用最新版本的iText,而且使用文档
和PdfStamper
类也很尴尬。这些类在您使用的iText版本中是互斥的。为什么不升级?首先删除对文档
和PdfWriter
的所有引用(完全扔掉这些行),然后确保关闭PdfStamper
对象:stamper.close()
。或扔掉你所有的代码,用iText 7 for.NET重新开始。谢谢@BrunoLowagie,现在它可以正常工作了。我的评论是一个答案。可能不会解决你的问题,但我不会在writer.Close()
之前调用fs.Close()
。如果您使用
块将一次性物品放入一个块中,您会自动强制自己使用正确的顺序。您的代码很旧,因为您没有使用最新版本的iText,而且使用文档
和PdfStamper
类也很尴尬。这些类在您使用的iText版本中是互斥的。为什么不升级?首先删除对文档
和PdfWriter
的所有引用(完全扔掉这些行),然后确保关闭PdfStamper
对象:stamper.close()
。或扔掉你所有的代码,用iText 7 for.NET重新开始。谢谢@BrunoLowagie,现在它工作正常了。我回答了我的评论。谢谢@Bruno,code woks fineHi使用你的代码,我可以在第一页添加图像。如何在pdf的第二页添加图像?您是否看到stamper.GetOverContent(1)行代码>?1表示您希望在第一页添加内容。如果你想在第二页添加一些东西,你需要stamper.GetOverContent(2)
如果没有第二页,则需要使用InsertPage()
方法插入额外的页。请查看。我有一个问题,写在pdf文件的文本,如果pf有超过1页。提前谢谢。我不明白那个问题。写得不好。你应该重新措辞,以便10岁的孩子能理解。现在,它写得好像你期望有人能读懂你的心思。另外:您正在使用旧版本的iText。你为什么不开始使用iText 7呢?谢谢@Bruno,Code woks fineHi使用你的代码我可以在第一页添加图像。如何在pdf的第二页添加图像?您是否看到stamper.GetOverContent(1)行代码>?1表示您希望在第一页添加内容。如果你想在第二页添加一些东西,你需要stamper.GetOverContent(2)
如果没有第二页,则需要使用InsertPage()
方法插入额外的页。请查看。我有一个问题,写在pdf文件的文本,如果pf有超过1页。提前谢谢。我不明白那个问题。写得不好。你应该重新措辞,以便10岁的孩子能理解。现在,它写得好像你期望有人能读懂你的心思。另外:您正在使用旧版本的iText。你为什么不改用iText 7呢?
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;