PDFsharp & MigraDoc Forum https://forum.pdfsharp.net/ |
|
Saving PdfDocument to stream with pages https://forum.pdfsharp.net/viewtopic.php?f=2&t=1638 |
Page 1 of 1 |
Author: | sc_3 [ Wed Apr 27, 2011 2:11 am ] |
Post subject: | Saving PdfDocument to stream with pages |
Hi, I'm trying to save a PdfDocument to stream after adding a set of pages. However, when I open the stream elsewhere, the stream doesn't seem to have saved my pages. So my question is could someone let me know if I am adding the page correctly to the PdfDocument or why this is happening?? public byte[] Execute(List<Image> images) { MemoryStream stream= new System.IO.MemoryStream(); PdfDocument pdfDoc = new PdfSharp.Pdf.PdfDocument(); foreach (Image image in images) { PdfPage imagePage = pdfDoc.AddPage(); XGraphics xGraphics = PdfSharp.Drawing.XGraphics.FromPdfPage(imagePage); XImage imageToDraw= PdfSharp.Drawing.XImage.FromGdiPlusImage(image); xGraphics.DrawImage(imageToDraw, 0, 0); } pdfDoc.Close(); pdfDoc.Save(stream); return stream.ToArray(); } Just to clarify, when I am trying to create a pdf to save from this byte[] later, the PdfDocument is created but has no pages so I cannot save because I get an exception. I had tested saving the PdfDocument from the method directly to file and this works no problem, just not when I try to recreate the PdfDocument from the byte[] later. So in my unit test for example, I do the following Stream stream = new MemoryStream(builder.Execute(imageList)); PdfDocument pdf = new PdfDocument(stream); pdf.Save("C:\\test.pdf"); //Throws exception because the PdfDocument has no pages However I cannot create the pdf here. I'd appreciate any help or insight someone can provide on this. Thanks |
Author: | Thomas Hoevel [ Wed Apr 27, 2011 6:44 am ] |
Post subject: | Re: Saving PdfDocument to stream with pages |
Hi! The Web sample shows how to get a PDF file in a byte[]. Maybe you must specify "false" as second parameter with the Save() call and call Close() after ToArray(). See here: http://www.pdfsharp.net/wiki/Clock-sample.ashx |
Author: | sc_3 [ Wed Apr 27, 2011 2:06 pm ] |
Post subject: | Re: Saving PdfDocument to stream with pages |
Hi, I had actually followed that example previously to not closing the stream. I was originally passing the stream instead of the byte[] and wanted to see if there was any change when I passed the byte []. It has the same effect. When the execution flow returns back to the lines: Stream stream = new MemoryStream(builder.Execute(imageList)); PdfDocument pdf = new PdfDocument(stream); pdf.Save("C:\\test.pdf"); //Throws exception because the PdfDocument has no pages I still do not see any data being saved to the pdf doc. I also tried to add a tag when the pdf document is created but that doesn't save either when the stream arrives back to my unit test. My modified code to generate the pdf is below now using the approach advised: public byte[] Execute(List<Image> images) { byte [] pdfArray = null; MemoryStream stream= new System.IO.MemoryStream(); PdfDocument pdfDoc = new PdfSharp.Pdf.PdfDocument(); foreach (Image image in images) { PdfPage imagePage = pdfDoc.AddPage(); XGraphics xGraphics = PdfSharp.Drawing.XGraphics.FromPdfPage(imagePage); XImage imageToDraw= PdfSharp.Drawing.XImage.FromGdiPlusImage(image); xGraphics.DrawImage(imageToDraw, 0, 0); } pdfDoc.Close(); pdfDoc.Save(stream, false); pdfArray = stream.ToArray(); stream.Close(); return pdfArray; } |
Author: | Thomas Hoevel [ Thu Apr 28, 2011 7:16 am ] |
Post subject: | Re: Saving PdfDocument to stream with pages |
With 0 images in "List<Image> images" you never call AddPage. Could this be the problem? |
Author: | batas2 [ Mon Nov 19, 2012 11:57 am ] |
Post subject: | Re: Saving PdfDocument to stream with pages |
I had exactly the same problem as topic author. Even "Hello world" example seem to not work correctly. I solved problem using this: Code: PdfDocument d = PdfReader.Open(new MemoryStream(output, 0, output.Length)); Do not create PdfDocument with MemoryStream as constructor parameter, instead use PdfReader. |
Page 1 of 1 | All times are UTC |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |