PDFsharp & MigraDoc Forum

PDFsharp - A .NET library for processing PDF & MigraDoc - Creating documents on the fly
It is currently Wed Aug 13, 2025 10:47 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules

Also see our new Tailored Support & Services site.



Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Wed Apr 27, 2011 2:11 am 
Offline

Joined: Wed Apr 27, 2011 2:01 am
Posts: 4
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


Top
 Profile  
Reply with quote  
PostPosted: Wed Apr 27, 2011 6:44 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3134
Location: Cologne, Germany
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

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Apr 27, 2011 2:06 pm 
Offline

Joined: Wed Apr 27, 2011 2:01 am
Posts: 4
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;
}


Top
 Profile  
Reply with quote  
PostPosted: Thu Apr 28, 2011 7:16 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3134
Location: Cologne, Germany
With 0 images in "List<Image> images" you never call AddPage. Could this be the problem?

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Nov 19, 2012 11:57 am 
Offline

Joined: Mon Nov 19, 2012 11:52 am
Posts: 1
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.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 36 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group