PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Sep 27, 2024 1:30 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Landscape Issues
PostPosted: Wed Nov 17, 2010 2:07 am 
Offline

Joined: Wed Nov 17, 2010 1:58 am
Posts: 1
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Landscape Issues
PostPosted: Wed Nov 17, 2010 10:33 am 
Offline
Supporter

Joined: Fri May 15, 2009 3:28 pm
Posts: 96
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


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 62 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