PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Jul 05, 2024 9:21 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Wed Dec 08, 2010 11:19 am 
Offline

Joined: Wed Dec 08, 2010 10:39 am
Posts: 2
hi

this is what I have done, as a test :
Code:
 protected PdfDocument genererPDF()
        {
            PdfSharp.Pdf.PdfDocument doc = new PdfSharp.Pdf.PdfDocument();
            string fontName = "Times"; ;
            XFont fontEntete = new XFont(fontName, 35);
            PdfPage page = doc.AddPage();
            page.Width = XUnit.FromMillimeter(210);
            page.Height = XUnit.FromMillimeter(297);
            XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsUnit.Millimeter);

            gfx.DrawString("TEST", fontEntete, XBrushes.Aqua, 40, 40);
            return doc;
        }

protected void Page_Load(object sender, EventArgs e)
        {
            PdfDocument pdf = genererPDF();
           
            // saving to disk
            string path=@"d:/bid/test.pdf";
            pdf.Save(path);

            // Envoi du pdf au navigateur
            MemoryStream sortie = new MemoryStream();
            pdf.Save(sortie, false);

            Response.Clear();

            Response.ContentType = "application/pdf";
            Response.AddHeader("content-length", sortie.Length.ToString());
            Response.BinaryWrite(sortie.ToArray());
            Response.Flush();
            Response.End();
           }


if i don't save to disk prior to send to browser, it's ok. But if I do, pdf displayed is a blank page, or i get an error popup from acrobat reader. How can I fix this ?

(ps : sorry for my english : i'm french)


Top
 Profile  
Reply with quote  
PostPosted: Wed Dec 08, 2010 12:34 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
roro06 wrote:
How can I fix this?

Save to MemoryStream only.
Write that MemoryStream to disk (using BinaryWriter.Write(byte[]) Method) and send it to the browser.
Better performance and a workaround for a limitation of PDFsharp.

Or call "genererPDF();" for each Save(). Simple change, but worse performance.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Dec 09, 2010 11:03 am 
Offline

Joined: Wed Dec 08, 2010 10:39 am
Posts: 2
thanks for your reply.

it works, in this context.

But my problem is a bit more complicated : the page displaying PDFs is a generic one :

pgPdfVisu.aspx :
Code:
 protected void Page_Load(object sender, EventArgs e)
        {
            if Session["pdf"] == null)
            {
                throw new HttpException  (404, "Document inexistant");
            }
            else
            {
                PdfDocument pdf = (PdfDocument)Session["pdf"];

                // Envoi du pdf au navigateur
                MemoryStream sortie = new MemoryStream();
               pdf.Save(sortie, false);
         
                Response.Clear();
     
              Response.ContentType = "application/pdf";
                Response.AddHeader("content-length", sortie.Length.ToString());
                Response.BinaryWrite(sortie.ToArray());
                Response.Flush();
                sortie.Close();
                Response.End();
              }


and pdf are generated (and saved) in several other aspx pages.
eg :
Code:
...
                            PdfDocument pdf = ed.devis_generePDF(true);
                            Session["pdf"] = pdf;

                           // I try to save to disk here

                             ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "editLoad", "var pdf_p=window.open('pgPdfVisu.aspx', 'pdf'); setTimeout(function(){ if(!pdf_p){ window.alert('Un anti-pop-up a empéché d ouvrir le devis');}}, 500);", true);
                       



Top
 Profile  
Reply with quote  
PostPosted: Tue Dec 14, 2010 4:49 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
roro06 wrote:
But my problem is a bit more complicated

Do you always save the file or only sometimes?

Either case: when you save to file, set Session["pdf"] to null and instead set Session["pdfbytes"] to the array. This requires a slight modification of the generic page.
If you save always, then you always pass the byte[] instead of the PdfDocument.

_________________
Regards
Thomas Hoevel
PDFsharp Team


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 73 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:  
cron
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group