PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue May 07, 2024 8:36 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 Mar 24, 2010 1:27 pm 
Offline

Joined: Wed Mar 24, 2010 11:40 am
Posts: 2
Hello I had a look around the forum to see if anyone else had a situation similar to this but couldn't find anything.

Basically I'm new to MigraDoc and PDFSharp and have been using them to create a little information document which, when a user clicks on a product link on our website we want to create a dynamic PDF relating to it.

So I've got most of this working, except for one part, which is rendering more than one page of the document, so I'm looking for advice as to how to do this as I've tried various ways provided in the sample projects but have yet to find a way that works. The majority of my code relating to streaming and creating pages comes from the Mix MigraDoc and PDFSharp sample and I have included the important areas of my code below.

Just to add a little note as the document created is dynamic, some may only be one page long which is fine while others, may cover multiple pages.

So any help would be appreciated, thanks.

Code:
protected void Page_Load(object sender, EventArgs e)
    {
        // Create new PDF document
        PdfDocument document = new PdfDocument();
        document.Info.Title = "Course Information";
        document.Info.Author = "Middlesbrough College";
        document.Info.Subject = "Course Information";

        //Call to fill the document
        CourseInfo(document);

        // Send PDF to browser
        MemoryStream stream = new MemoryStream();
        document.Save(stream, false);
        Response.Clear();
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-length", stream.Length.ToString());
        Response.BinaryWrite(stream.ToArray());
        Response.Flush();
        stream.Close();
        Response.End();
    }

    public void CourseInfo(PdfDocument document)
    {
        //Required data retrieved via LINQ
        *LINQ performs queries to pull necessary data from the database here*

    // Create new page
        PdfPage page = document.AddPage();
        // Get an XGraphics object for drawing
        XGraphics gfx = XGraphics.FromPdfPage(page);

        XTextFormatter tf = new XTextFormatter(gfx);

        // Create a font
        XFont font = new XFont("Aerial", 20, XFontStyle.Bold);

        Document doc = new Document();

        //Creates the styles to be used in the document
        DefineStyles(doc);

        //Sets the Headers & Footers for the document
        DefineHeaders(doc);
       
        Section sec = doc.AddSection();
        //Page setup then data is inserted into the document
        *Paragraphs etc are put in place here*

               //Renders the document
        MigraDoc.Rendering.DocumentRenderer docRenderer = new DocumentRenderer(doc);
        docRenderer.PrepareDocument();

        // Render the page.
        docRenderer.RenderPage(gfx, 1);
    }


Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 24, 2010 2:17 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3097
Location: Cologne, Germany
Hello!
This bit of code will always render page 1 (and nothing else):
Code:
// Render the page.
docRenderer.RenderPage(gfx, 1);


After PrepareDocument, docRenderer will know how many pages you need. Call RenderPage in a loop.
viewtopic.php?p=1735#p1735

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 24, 2010 3:21 pm 
Offline

Joined: Wed Mar 24, 2010 11:40 am
Posts: 2
Hi and thank you for the quick response and link, that helped although now another issue has occured, which is that when using the for loop suggested 2 blank pages are shown at the start of the document. If I raise the for loops starting value to 2 this removes one of them, but if I change it to 3 it removes the first proper page of my document and keeps a plain page at the start.

I was wondering if you had any ideas as to how this can be resolved? If it helps as well as the Section I create in my courseinfo method, I also create another when I define the headers and footers which can be seen below:

Code:
 public void DefineHeaders(Document document)
    {
        Section sec = document.AddSection();
        sec.PageSetup.OddAndEvenPagesHeaderFooter = true;
        sec.PageSetup.StartingNumber = 1;

        sec.PageSetup.LeftMargin = 20;
        sec.PageSetup.RightMargin = 20;
        sec.PageSetup.FooterDistance = 60;
        sec.PageSetup.HeaderDistance = 30;

        Image header = sec.Headers.Primary.AddImage("e:/Sitefinity/WebSites/Home/UserControls/WebEnroll/Header.png");
        header.Height = "4cm";
        header.RelativeHorizontal = RelativeHorizontal.Margin;
        header.Left = ShapePosition.Center;
        header.Top = ShapePosition.Top;

        Image footer = sec.Footers.Primary.AddImage("e:/Sitefinity/WebSites/Home/UserControls/WebEnroll/footer.png");
        footer.Height = "2cm";
        footer.RelativeHorizontal = RelativeHorizontal.Margin;
        footer.Left = ShapePosition.Center;
        footer.Top = ShapePosition.Top;


Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 24, 2010 3:37 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3097
Location: Cologne, Germany
Gambo wrote:
If I raise the for loops starting value to 2 this removes one of them, but if I change it to 3 it removes the first proper page of my document and keeps a plain page at the start.

So it seems MigraDoc creates an empty page (maybe caused by AddPageBreak or AddSection or ...).
With start value 2 you remove this page.

Maybe you have an AddPage before the loop and one inside your loop. That's the page you cannot remove by changing the start value.

The answer lies in your code.

_________________
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: No registered users and 95 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