【发布时间】: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可以很容易地以任何其他方式创建并包含任何类型的字节。因此,您应该删除标签pdf 和itextsharp,并将它们替换为与使用System.IO.File和Web 服务相关的标签。