Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
pdf过温c#的例外情况(iTextSharp)_C#_.net_Pdf_Itext - Fatal编程技术网

pdf过温c#的例外情况(iTextSharp)

pdf过温c#的例外情况(iTextSharp),c#,.net,pdf,itext,C#,.net,Pdf,Itext,我正在尝试使用iTextSharp创建pdf文件。第一次创建pdf,但当我以编程方式删除或过度渲染时,我得到以下异常 我不知道为什么文件一直开着。。。这是我的示例代码 private void CreatePdf(string first, string last, string value) { using (MemoryStream myMemoryStream = new MemoryStream()) { Doc

我正在尝试使用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);
            }
        }

    }
也许你需要关闭“myPDFWriter”

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

我补充说:

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

现在它开始工作了。

我添加了myPDFWriter.Close();在document.Open()之前;但我得到了相同的异常。只需将其添加到document.Close()之后即可
document.Close()
特别调用底层
PdfWriter
Close
方法。此外
PdfWriter
只写入
MemoryStream
,而不写入文件系统。因此,任何文件系统锁显然都与此无关。您没有在acrobat或任何其他pdf阅读器中打开它吗?@SamuelHuylebroeck不,我不知道首先,您所展示的代码中使用的iTextSharp与问题完全无关,因为您只使用它在内存中创建pdf;甚至PDF格式本身也不重要;
byte[]content
可以以任何其他方式轻松创建,并包含任何类型的字节。因此,您应该删除标记,并用与
System.IO.File
和web服务的使用相关的标记替换它们。