我正在尝试使用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);
}
}
}
答案 0 :(得分:0)
也许您需要关闭“myPDFWriter”
答案 1 :(得分:0)
我找到了解决方案。 我的下一个方法发送附带pdf的电子邮件,但问题是pdf文件在附加时保持打开状态。
我补充说:
using (System.Net.Mail.Attachment data = new System.Net.Mail.Attachment(HttpContext.Current.Server.MapPath(bytes)))
{
mm.Attachments.Add(data);
...
}
现在它正在运作。