C# PDFsharp 保存到 MemoryStream

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1073277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:35:06  来源:igfitidea点击:

PDFsharp save to MemoryStream

c#.netpdfstreampdfsharp

提问by misnyo

I want to save a PdfSharp.Pdf.PdfDocument by its Save method to a Stream, but it doesn't attach the PDF header settings to it. So when I read back the Stream and return it to the user, he see that the PDF file is invalid. Is there a solution to attach the PDF header settings when PDFsharp saves to memory?

我想通过它的 Save 方法将 PdfSharp.Pdf.PdfDocument 保存到一个流,但它没有将 PDF 标题设置附加到它。所以当我读回 Stream 并将其返回给用户时,他看到 PDF 文件无效。当 PDFsharp 保存到内存时,是否有附加 PDF 标题设置的解决方案?

采纳答案by misnyo

So the solution:

所以解决办法:

MigraDoc.DocumentObjectModel.Document doc = new MigraDoc.DocumentObjectModel.Document();
MigraDoc.Rendering.DocumentRenderer renderer = new DocumentRenderer(doc);
MigraDoc.Rendering.PdfDocumentRenderer pdfRenderer = new MigraDoc.Rendering.PdfDocumentRenderer();
pdfRenderer.PdfDocument = pDoc;
pdfRenderer.DocumentRenderer = renderer;
using (MemoryStream ms = new MemoryStream())
{
  pdfRenderer.Save(ms, false);
  byte[] buffer = new byte[ms.Length];
  ms.Seek(0, SeekOrigin.Begin);
  ms.Flush();
  ms.Read(buffer, 0, (int)ms.Length);
}

There is this MigraDoc stuff which comes with PdfSharp, but i hardly found any proper doc/faq for it. After hours of googling i've found a snippet which was something like this. Now it works.

PdfSharp 附带了 MigraDoc 的东西,但我几乎找不到任何合适的文档/常见问题解答。经过数小时的谷歌搜索,我发现了一个类似这样的片段。现在它起作用了。

回答by I liked the old Stack Overflow

If you think there is an issue with PdfDocument.Save, then please report this on the PDFsharp forum (but please be more specific with your error description). Your "solution" looks like a hack to me. "pdfRenderer.Save" calls "PdfDocument.Save" internally. Whatever the problem is - your "solution" still calls the same Save routine.

如果您认为 PdfDocument.Save 存在问题,请在 PDFsharp 论坛上报告(但请更具体地说明您的错误)。你的“解决方案”对我来说就像一个黑客。“pdfRenderer.Save”在内部调用“PdfDocument.Save”。无论问题是什么 - 您的“解决方案”仍然调用相同的 Save 例程。

Edit: To get a byte[] containing a PDF file, you only have to call:

编辑:要获取包含 PDF 文件的字节 [],您只需调用:

MemoryStream stream = new MemoryStream();
document.Save(stream, false);
byte[] bytes = stream.ToArray();

Early versions of PDFsharp do not reset the stream position.

早期版本的 PDFsharp 不会重置流位置。

So you have to call

所以你必须打电话

ms.Seek(0, SeekOrigin.Begin); 

to reset the stream position before reading from the stream; this is no longer required for current versions.

在从流中读取之前重置流位置;当前版本不再需要这样做。

Using ToArray can often be used instead of reading from the stream.

通常可以使用 ToArray 代替从流中读取。

Edit 2: instead of stream.ToArray()it may be more efficient to use stream.GetBuffer(), but this buffer is usually larger than the PDF file and you only have to use stream.Lengthbytes from that buffer. Very useful for method that take a byte[]along with a length parameter.

编辑 2: 而不是使用stream.ToArray()它可能更有效stream.GetBuffer(),但此缓冲区通常比 PDF 文件大,您只需使用stream.Length该缓冲区中的字节。对于byte[]带有长度参数的方法非常有用。

回答by eCorke

I found simpler solution:

我找到了更简单的解决方案:

byte[] fileContents = null; 
using(MemoryStream stream = new MemoryStream()) 
{ 
    pdfDoc.Save(stream, true); 
    fileContents = stream.ToArray(); 
}

Source: http://usefulaspandcsharp.wordpress.com/2010/03/09/save-a-pdf-to-a-byte-array-using-pdf-sharpmigradoc/

资料来源:http: //usefulaspandcsharp.wordpress.com/2010/03/09/save-a-pdf-to-a-byte-array-using-pdf-sharpmigradoc/

回答by Alex G

For MigraDoc (ver 1.30) I could save it with

对于 MigraDoc (ver 1.30) 我可以用

PdfDocumentRenderer renderer = new PdfDocumentRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.Always);
renderer.Document = report.m_Document;

renderer.RenderDocument();


using (MemoryStream stream = new MemoryStream())
{
    renderer.PdfDocument.Save(stream, false);
    ... your code in here

}

回答by Ali Tabandeh

Thanks Misnyo Solution. But for me it works like this:

感谢 Misnyo 解决方案。但对我来说,它是这样工作的:

        Document document = new Document();
        PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer();   

        //Add to document here.......

        //render the document with pdf renderer
        pdfRenderer.Document = document;
        pdfRenderer.RenderDocument();

        //Save renderer result into stream   
        using(MemoryStream ms = new MemoryStream())
        {
            pdfRenderer.PdfDocument.Save(ms, false);
            byte[] buffer = new byte[ms.Length];
            ms.Seek(0, SeekOrigin.Begin);
            ms.Flush();
            ms.Read(buffer, 0, (int)ms.Length);
            ms.Position = 0;
        }