Hi,
I use MigraDoc's DocumentRenderer for rendering to PDF. Also, I create a PdfDocument object and when I add a new page to my MigraDoc Document, I add a new page to that too, thought im sure you could do it all at "render time", something like this:
Code:
DocumentRenderer _pdfRenderer = new DocumentRenderer(myExistingMigraDocDocument);
PdfDocument _pdfDoc = new PdfDocument();
for(int i = 0; i < myExistingMigraDocDocument.Sections.Count; i++){
if(myExistingMigraDocDocument.Sections[i].PageSetup.Orientation == MigraDoc.DocumentObjectModel.Orientation.Landscape){
_pdfDoc.AddPage(PdfSharp.PageOrientation.Landscape)
}
else{
_pdfDoc.AddPage(PdfSharp.PageOrientation.Portrait)
}
}
then render the pages like this:
Code:
for(int i = 0; i < _pdfDoc.PageCount; i++){
_pdfRenderer.RenderPage(XGraphics.FromPdfPage(_pdfDoc.Pages[i]), i + 1);
}
For your reference, here is the function I have created (and spent many hours perfecting):
Code:
public void RenderPgs()
{
try
{
Renderer = new DocumentRenderer(CommonData.Instance.GetByID(__GUID).Document);
Renderer.PrivateFonts = PrivateFonts;
Renderer.PrepareDocument();
int _ovflCnt = 0;
try
{
int _cnt = CommonData.Instance.GetByID(__GUID).Document.Sections.Count;
int i = 0;
while (i < _cnt)
{
Renderer.RenderPage(CommonData.Instance.GetByID(__GUID).GraphicsObjs[i], i + 1);
Document _tDoc = new Document();
_tDoc.Add(CommonData.Instance.GetByID(__GUID).Document.Sections[i - _ovflCnt].Clone());
DocumentRenderer _tRend = new DocumentRenderer(_tDoc);
_tRend.PrivateFonts = PrivateFonts;
_tRend.PrepareDocument();
if (_tRend.FormattedDocument.PageCount > 1)
{
PdfPage _sPage = CommonData.Instance.GetByID(__GUID).PdfDocument.Pages[i];
int _pgCnt = _tRend.FormattedDocument.PageCount;
for (int w = 1; w < _pgCnt; w++)
{
PdfPage _Page = CommonData.Instance.GetByID(__GUID).PdfDocument.InsertPage(i + w);
_Page.Size = _sPage.Size;
_Page.Orientation = _sPage.Orientation;
XGraphics _gfx = CreateXGfx(i + w, _Page);
_tRend.RenderPage(_gfx, w + 1);
_ovflCnt++;
}
i += _ovflCnt;
_cnt += _ovflCnt;
}
i++;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
This function is setup to work with my way of producing a pdf, I add the pages to the PdfDocument as I add sections to MigraDoc Document, the "if (_tRend.FormattedDocument.PageCount > 1)" condition, and subsequent for loop are for handling page overflows (i.e. a Section may take up more than one page...) and gettign the correct page orientation on the overflow pages in the PdfDocument.
Hope this helps,
Mike