PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Apr 27, 2024 9:06 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Wed Jan 27, 2010 1:59 pm 
Offline

Joined: Wed Jan 27, 2010 1:36 pm
Posts: 1
I can successfully create an on-the-fly pdf using

PdfSharp.Pdf.PdfDocument document = new PdfSharp.Pdf.PdfDocument();
PdfPage page = document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
--- a series of gfx.DrawStrings to build page
System.IO.MemoryStream stream = new System.IO.MemoryStream();
document.Save(stream, false);
stream.Position = 0;
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", stream.Length.ToString());
Response.BinaryWrite(stream.ToArray());
Response.Flush();
stream.Close();
Response.End();

In most cases, I display a preview of a request (as a pdfsharp pdf) and display it in an I-frame. My client approves the request by clicking a button, and I e-mail a link to all appropriate contacts. These contacts can then click the link in the e-mail to regenerate the pdfsharp pdf in a browser window.

My client base has extended beyond my Intranet and I don't want to open my application to external access. I would like to simply modify my application to replace the link in the generated e-mail with an attachment. I used the following code:

1) On the initial preview generation, I save the pdfsharp document to Session
System.Web.HttpContext.Current.Session["pdf"] = document;
2) In the code section that generates the e-mails, I tried using
System.IO.MemoryStream PDFstream = new System.IO.MemoryStream();
((PdfSharp.Pdf.PdfDocument)Session["pdf"]).Save(PDFstream, false);
PDFstream.Position = 0;
Attachment PDFFile = new Attachment(PDFstream, new System.Net.Mime.ContentType("application/pdf"));
PDFFile.ContentDisposition.FileName = "myAttachedFile.pdf";
message.Attachments.Add(PDFFile);

The attachment is created and added to the e-mail, but is blank when clicked. If I open the file in a text editor, it appears to be a pdfsharp generated pdf file (the text is very similar to a pdfsharp pdf file that is originally built in the browser...it's just a bit larger). It seems like I'm missing something simple, like a format or something. Can you assist ?

PS - I'm trying to avoid creating an actual file on a disk that I would need to delete later.

Thanks


Top
 Profile  
Reply with quote  
PostPosted: Thu Jan 28, 2010 9:13 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
Hi!

You can create a new MemoryStream from the byte[] returned by "stream.ToArray());"
If this works then there is a problem with the stream returned by PDF (please let us know so we can investigate this).
If this still doesn't work, the problem is somewhere else (and you can still create a temporary file).

"PDFstream.Position = 0;" is no longer required with current versions of PDFsharp.

_________________
Regards
Thomas Hoevel
PDFsharp Team


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 371 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