【问题标题】:Exception on pdf overwite c# (iTextSharp)pdf overwite c# (iTextSharp) 上的异常
【发布时间】:2016-09-20 07:48:27
【问题描述】:

我正在尝试使用 iTextSharp 创建一个 pdf 文件。第一次创建 pdf 但当我以编程方式删除或过度使用时,我得到以下异常。

我不知道为什么文档保持打开状态...这是我的示例代码

    private void CreatePdf(string first, string last, string value)
    {

        using (MemoryStream myMemoryStream = new MemoryStream())
        {
            Document document = new Document(PageSize.A4, 100, 100, 100, 100);
            PdfWriter myPDFWriter = PdfWriter.GetInstance(document, myMemoryStream);

            document.Open();

            BaseFont MyFont = iTextSharp.text.pdf.BaseFont.CreateFont("C:\\Windows\\Fonts\\Arial.ttf", iTextSharp.text.pdf.BaseFont.IDENTITY_H, iTextSharp.text.pdf.BaseFont.EMBEDDED);

            var titleFont = new iTextSharp.text.Font(MyFont, 16, iTextSharp.text.Font.BOLD);
            var subTitleFont = new iTextSharp.text.Font(MyFont, 22, iTextSharp.text.Font.NORMAL);
            var subTitleFont2 = new iTextSharp.text.Font(MyFont, 16, iTextSharp.text.Font.NORMAL);
            var boldTableFont = new iTextSharp.text.Font(MyFont, 12, iTextSharp.text.Font.NORMAL);
            var endingMessageFont = new iTextSharp.text.Font(MyFont, 10, iTextSharp.text.Font.ITALIC);
            var bodyFont = new iTextSharp.text.Font(MyFont, 12, iTextSharp.text.Font.NORMAL);

            var logo = iTextSharp.text.Image.GetInstance(Server.MapPath("~/images/" + currentEvent + ".jpg"));
            logo.ScaleToFit(395, 160);

            document.Add(logo);

            Helpers.GetEventInformation(currentEvent);


            Paragraph name = new Paragraph(first, subTitleFont);
            name.Alignment = 1;
            name.SpacingBefore = 20;
            document.Add(name);

            Paragraph surname = new Paragraph(last, subTitleFont);
            surname.Alignment = 1;
            document.Add(surname);


            Paragraph info1 = new Paragraph("Επιβεβαίωση Εγγραφής στο", subTitleFont2);
            info1.Alignment = 1;
            info1.SpacingBefore = 25;
            document.Add(info1);

            Paragraph info2 = new Paragraph(Helpers.confinfo, subTitleFont2);
            info2.Alignment = 1;
            document.Add(info2);

            Paragraph info3 = new Paragraph(Helpers.maildate, titleFont);
            info3.Alignment = 1;
            info3.SpacingBefore = 25;
            document.Add(info3);

            Paragraph info4 = new Paragraph(Helpers.mailtime, subTitleFont2);
            info4.Alignment = 1;
            document.Add(info4);


            document.Add(Chunk.NEWLINE);
            document.Add(Chunk.NEWLINE);

            var barcodeImage = iTextSharp.text.Image.GetInstance(Server.MapPath("~/barcodes/" + value + ".png"));
            barcodeImage.Alignment = iTextSharp.text.Image.ALIGN_CENTER;
            barcodeImage.SpacingBefore = 25;
            document.Add(barcodeImage);

            Paragraph barcodev = new Paragraph(value, endingMessageFont);
            barcodev.SpacingBefore = -5;
            barcodev.Alignment = 1;
            document.Add(barcodev);

            document.Add(Chunk.NEWLINE);
            document.Add(Chunk.NEWLINE);

            var logo2 = iTextSharp.text.Image.GetInstance(Server.MapPath("~/images/footer.jpg"));
            logo2.SpacingBefore = 30;
            logo2.ScaleToFit(395, 130);
            document.Add(logo2);


            document.Close();

            byte[] content = myMemoryStream.ToArray();

            pdfPath = Server.MapPath("~/barcodes/" + value + ".pdf");
            using (FileStream fs = System.IO.File.Create(pdfPath))
            {
                fs.Write(content, 0, (int)content.Length);
            }
        }

    }

【问题讨论】:

  • 你没有在 acrobat 或任何其他 pdf 阅读器中打开它吗?
  • @SamuelHuylebroeck 不,我没有
  • 首先,您提供的代码中使用的 iTextSharp 与问题完全无关,因为您仅使用它来创建在内存中的 PDF;即使是 PDF 格式本身也无所谓; byte[] content 可以很容易地以任何其他方式创建并包含任何类型的字节。因此,您应该删除标签pdfitextsharp,并将它们替换为与使用System.IO.File 和Web 服务相关的标签。

标签: c# .net pdf itext


【解决方案1】:

也许你需要关闭“myPDFWriter”

【讨论】:

  • 我添加了 myPDFWriter.Close();在 document.Open(); 之前但我得到了同样的例外。
  • 在document.Close()之后添加即可;
  • document.Close() 特别调用底层PdfWriterClose 方法。
  • 此外,PdfWriter 仅写入 MemoryStream,而不写入文件系统。因此,任何文件系统锁显然都与它无关。
【解决方案2】:

我找到了解决方案。 我的下一个方法发送附有 pdf 的电子邮件,但问题是 pdf 文件在附加时保持打开状态。

我添加了这个:

using (System.Net.Mail.Attachment data = new System.Net.Mail.Attachment(HttpContext.Current.Server.MapPath(bytes)))
            {
                mm.Attachments.Add(data);
                ...
            }

现在它正在工作。

【讨论】:

    猜你喜欢
    • 2023-03-03
    • 2012-05-24
    • 1970-01-01
    • 2015-06-07
    • 1970-01-01
    • 1970-01-01
    • 2011-05-11
    • 1970-01-01
    • 2015-05-29
    相关资源
    最近更新 更多