【问题标题】:How to Insert Image (byte) into pdf (byte) using ItextSharp如何使用 ItextSharp 将图像(字节)插入 pdf(字节)
【发布时间】:2018-01-31 10:00:16
【问题描述】:

我有一个 pdf(byte) 存储在数据库表中,我还在数据库表中存储了图像(byte)。

现在我想从数据库中读取 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;

【问题讨论】:

  • 可能无法解决您的问题,但我不会在writer.Close() 之前致电fs.Close()。如果您将一次性用品放在using 块中,您会自动强制自己使用正确的顺序。
  • 您的代码很旧,因为您没有使用最新版本的 iText,而且在您使用 Document and 的意义上很尴尬PdfStamper 类。这些类在您使用的 iText 版本中是互斥的。为什么不升级?
  • 首先删除对DocumentPdfWriter 的所有引用(完全丢弃这些行),然后确保关闭PdfStamper 对象:stamper.Close()。或者...扔掉你所有的代码,用 iText 7 for .NET 重新开始。
  • 感谢@BrunoLowagie,现在可以正常使用了。
  • 我的评论是一个答案。

标签: c# asp.net itext


【解决方案1】:

您的代码太多,缺少一条重要的行。我拿走了你的代码并删除了所有不必要的行(这会使你的文件损坏):

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();

【讨论】:

  • 感谢@Bruno,代码很好
  • 嗨使用您的代码,我可以在第一页添加图像。如何在pdf第二页添加图片?
  • 你看到stamper.GetOverContent(1);这行了吗? 1 表示您要在第一页上添加一些内容。如果要在第二页添加东西,则需要stamper.GetOverContent(2); 如果没有第二页,则需要InsertPage() 方法插入额外的页面。
  • 你能看看stackoverflow.com/questions/48560523/…。如果 pf 超过 1 页,我在 extingin pdf 文件上写文本时遇到问题。提前致谢。
  • 我不明白这个问题。写的不太好。您应该改写它,以便 10 岁的孩子可以理解它。现在,它写得好像你希望有人能读懂你的想法。 另外:您使用的是旧版本的 iText。你为什么不开始使用 iText 7?
猜你喜欢
  • 1970-01-01
  • 2019-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-01
  • 1970-01-01
  • 2012-10-18
相关资源
最近更新 更多