简体   繁体   English

使用iTextSharp创建PDF而不保存文件

[英]Create PDF with iTextSharp without saving the file

I am using Visual Studio 2013, c# Windows Application, iTextSharp: 我正在使用Visual Studio 2013,c#Windows应用程序,iTextSharp:

I can easily create/save a pdf file and write to it but is there a way to just use a pdf file and write to it without first saving it? 我可以轻松创建/保存pdf文件并将其写入,但是有没有一种方法可以仅使用pdf文件并写入而无需先保存它? I don't want to be creating a temporary pdf file every-time someone runs a report. 我不想每次有人运行报告时都创建一个临时pdf文件。 Thanks in Advance !! 提前致谢 !!

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();

You can use a MemoryMappedFile and write to it and it is not disk based. 您可以使用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;            
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM