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);
}