我正在使用Visual Studio 2013,c#Windows应用程序,iTextSharp:
我可以轻松地创建/保存pdf文件并写入它但是有没有办法只使用pdf文件并写入它而不先保存它?我不想在每次有人运行报告时创建临时pdf文件。在此先感谢!!
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10,10,42,35);
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("Test.pdf",
FileMode.Create));
doc.Open();
\\\\ Then I do a bunch of stuff, then do a close
doc.Close();
答案 0 :(得分:-2)
你可以使用MemoryMappedFile并写入它,它不是基于磁盘的。
using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew("INMEMORYPDF.pdf", 1000000))
{
PDFPageMargins margins = new PDFPageMargins(10, 10, 10, 10);
var document = new Document((_pageWidth > 540) ? PageSize.A4.Rotate() : PageSize.A4, margins.Left, margins.Right, margins.Top, margins.Bottom);
using (MemoryMappedViewStream stream = mmf.CreateViewStream())
{
PdfWriter.GetInstance(document, stream);
document.Open();
//MODIFY DOCUMENT
document.Close();
}
byte[] content;
using (MemoryMappedViewStream stream = mmf.CreateViewStream())
{
BinaryReader rdr = new BinaryReader(stream);
content = new byte[mmf.CreateViewStream().Length];
rdr.Read(content, 0, (int)mmf.CreateViewStream().Length);
}
return content;
}