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

Extracting measurements from MigraDoc for use in PDFSharp
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3275
Page 1 of 1

Author:  reubenhelms [ Tue Jan 19, 2016 2:46 am ]
Post subject:  Extracting measurements from MigraDoc for use in PDFSharp

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

Author:  TH-Soft [ Tue Jan 19, 2016 7:22 am ]
Post subject:  Re: Extracting measurements from MigraDoc for use in PDFShar

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.

Author:  reubenhelms [ Wed Jan 20, 2016 11:38 pm ]
Post subject:  Re: Extracting measurements from MigraDoc for use in PDFShar

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.

Author:  reubenhelms [ Thu Jan 21, 2016 12:25 am ]
Post subject:  Re: Extracting measurements from MigraDoc for use in PDFShar

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.

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