PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Mon Mar 30, 2020 8:56 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: Tue Jan 19, 2016 2:46 am 
Offline

Joined: Thu Jan 07, 2016 6:51 am
Posts: 3
I have a MigraDoc document that I'm using the MigraDoc.Rendering.PdfDcumentRenderer to render.

On the first "page" of the document is a table (it's the first table), that I want to draw a fancy background for (Two rounded rectangles, one with a brush, and one with a pen to give a bordered panel effect).

After calling RenderDocument() on the PdfDocumentRenderer, I can (and have) extracted an XGraphics instance via

Code:
var page = pdfDocumentRenderer.PdfDocument.Pages[0];
var gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend);


Currently, I'm doing a best guess on the dimensions of the rounded rectangle, however, what I would like to do is use the information in the rendered PDF to detect the start of the table, and the size of the table, and then use the points from these dimensions to calculate the dimensions of the rounded rectangle.

Is there a way to map a MigraDoc table to it's rendered position, or even detect a rendered table in the PdfDocument? Or is that information only generated and relevant while the MigraDoc document is being rendered, and after the fact it's a one way transform that is difficult to map back to anything, or recognise MigraDoc elements like tables?

Regards
Reuben Helms


Top
 Profile  
Reply with quote  
PostPosted: Tue Jan 19, 2016 7:22 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 532
Location: CCAA
When items have been rendered for PDF, you can query their positions.
You can add dummy rows at the top and the bottom of the table (zero height or with the appropriate height of your border) and later determine their positions. Ar add paragraphs before and after the table - it's easier to find paragraphs that are not in tables.
I tried that for demo purposes, but the demo is incomplete and not yet published (and won't be published soon).

Tip: all document objects have a Tag property for your use. Assign objects of your choice to mark paragraphs.

Also see this thread:
viewtopic.php?p=1904#p1904
Note: with PDFsharp 1.50 you no longer need the Internalsvisibleto trick, all you need is public.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Wed Jan 20, 2016 11:38 pm 
Offline

Joined: Thu Jan 07, 2016 6:51 am
Posts: 3
Cheers, Thomas.

I think I'll try an update to 1.50, and I'll post sample code back here to assist anyone else you might have the same issue.


Top
 Profile  
Reply with quote  
PostPosted: Thu Jan 21, 2016 12:25 am 
Offline

Joined: Thu Jan 07, 2016 6:51 am
Posts: 3
Glorious! That works a treat.

For my particular case, after calling RenderDocument() on my MigraDoc.Rendering.PdfDocumentRenderer instance, I call a custom PostRenderPdf() method that passes the PdfDocumentRenderer instance, for the purpose of applying any PDFSharp operations to the rendered MigraDoc document.

Poor error handling not withstanding, here's what I ended up doing.

Code:
        public static void PostRenderPdf(PdfDocumentRenderer pdfDocumentRenderer)
        {
            double startX = 0;
            double startY = 0;
            double width = 0;
            double height = 0;
            double panelBorderWidth = 0.28;

            // page numbering starts from 1
            var renderInfos = pdfDocumentRenderer.DocumentRenderer.GetRenderInfoFromPage(1);

            if (renderInfos != null && renderInfos.Length > 0)
            {
                for (int g = 0; g < renderInfos.Length; g++)
                {
                    switch (renderInfos[g].DocumentObject.Tag as string)
                    {
                        case "JobMatchPanel":
                            startX = renderInfos[g].LayoutInfo.ContentArea.X - panelBorderWidth;
                            startY = renderInfos[g].LayoutInfo.ContentArea.Y - panelBorderWidth;
                            width = renderInfos[g].LayoutInfo.ContentArea.Width + (panelBorderWidth * 2);
                            height = renderInfos[g].LayoutInfo.ContentArea.Height + (panelBorderWidth * 2);
                            break;
                    }
                }
            }

            var page = pdfDocumentRenderer.PdfDocument.Pages[0];
            var gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend);

            var rect = new XRect(startX, startY, width, height);
            var ellipseSize = new Size(9, 9);
            var color = new XSolidBrush(XColor.FromArgb(240, 240, 240));
            gfx.DrawRoundedRectangle(color, rect, ellipseSize);

            var pen = new XPen(XColor.FromArgb(128, 128, 128), 1);
            gfx.DrawRoundedRectangle(pen, rect, ellipseSize);
        }


The intention is to find the table that has been given a tag "JobMatchPanel" on the first page of my document, and apply a bordered panel effect that is 0.28 points around the original table.

There are things that probably could be done better (like build the anticipated border into the table itself, so I don't have to adjust the rectangle dimensions, and leave the loop early, once I have located the panel, since it's the only one in the document).

However, it's a good start, and DocumentObject Tags are now my friend.

Thank you for your help, Thomas.


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: Google [Bot] and 4 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