PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Landscape Issues
https://forum.pdfsharp.net/viewtopic.php?f=2&t=1423
Page 1 of 1

Author:  ozczecho [ Wed Nov 17, 2010 2:07 am ]
Post subject:  Landscape Issues

I am using MigraDoc to produce a document. The document is A4 and Landscape. Using RtfDocumentRenderer it renders correctly as a RTF file. Using PdfDocumentRenderer nothing renders, the pages are blank. If I remove document.DefaultPageSetup.Orientation = Orientation.Landscape; then PdfDocumentRenderer renders document, but in portrait mode.

My Sample code:

Code:
         var document = new Document
                {
                    Info =
                        {
                            Title = "My Report",
                            Subject = "My Report",
                            Author = "Bob"
                        }
                };
            document.DefaultPageSetup.PageFormat = PageFormat.A4;
            document.DefaultPageSetup.Orientation = Orientation.Landscape;

            var section = document.AddSection();

            var headerText = section.Headers.Primary.AddParagraph();
            headerText.Format.Shading.Color = Color.Parse("LightGray");
            headerText.Format.LeftIndent = 1;
            headerText.Format.RightIndent = 1;
            headerText.AddText("Hello Hello Hello");

            var pdfRenderer = new PdfDocumentRenderer(false, PdfFontEmbedding.Always);
            pdfRenderer.Document = document;
            pdfRenderer.RenderDocument();


Can anyone tell my why that does not work ? If you replace the last 3 lines with:

Code:
            var renderer = new RtfDocumentRenderer();
            renderer.Render(document, string.Format(@"{0}{1}.rtf", Path, fileName), null);


...then a landscape rtf document is generated.

Thanks in advance.

Author:  mikesowerbutts [ Wed Nov 17, 2010 10:33 am ]
Post subject:  Re: Landscape Issues

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

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/