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?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Generic Generics in Managed C++
- Why am I getting UnauthorizedAccessException on th
- Correctly parse PDF paragraphs with Python
So the solution:
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.
Thanks Misnyo Solution. But for me it works like this:
For MigraDoc (ver 1.30) I could save it with
I found simpler solution:
Source: http://usefulaspandcsharp.wordpress.com/2010/03/09/save-a-pdf-to-a-byte-array-using-pdf-sharpmigradoc/
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.
Edit: To get a byte[] containing a PDF file, you only have to call:
Early versions of PDFsharp do not reset the stream position.
So you have to call
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.
Edit 2: instead of
stream.ToArray()
it may be more efficient to usestream.GetBuffer()
, but this buffer is usually larger than the PDF file and you only have to usestream.Length
bytes from that buffer. Very useful for method that take abyte[]
along with a length parameter.