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

Document Rendering Problem
https://forum.pdfsharp.net/viewtopic.php?f=2&t=1709
Page 1 of 1

Author:  nareshreddy [ Thu Jun 30, 2011 10:59 am ]
Post subject:  Document Rendering Problem

Hi
I created pdf file using MigraDoc.
Initially i used the PdfDocumentRenderer class for rendering.. it's work's fine.
later i added the watermark for this and i changed rendering Document using MigraDoc.Rendering.DocumentRenderer class, based on the one of the thread in Pdfsharp forums.

Here water mark is displaying nicely.
1)Image is not displaying
2)Entire page is rotating

what is the mistake in my code?
here is my code...

Code:
  Document document = new Document();
            document.UseCmykColor = true;
            document.DefaultPageSetup.PageFormat = PageFormat.A4;
            document.DefaultPageSetup.LeftMargin = 30;
            document.DefaultPageSetup.RightMargin = 30;         
    Section section1 = document.AddSection();
            string headerImage = Path.GetFullPath(".") + "\\Resources\\rccPdfLogo.png";           
            MigraDoc.DocumentObjectModel.Shapes.Image image = section1.AddImage(headerImage);
            image.LockAspectRatio = true;
            image.RelativeVertical = RelativeVertical.Line;
            image.RelativeHorizontal = RelativeHorizontal.Margin;
            image.Top = ShapePosition.Top;
            image.Left = ShapePosition.Center;
            image.Width = document.DefaultPageSetup.PageWidth - (document.DefaultPageSetup.RightMargin +document.DefaultPageSetup. LeftMargin);
            image.WrapFormat.Style = WrapStyle.TopBottom;
            Paragraph pgDocTitle = section1.AddParagraph();
            pgDocTitle.Format.Alignment = ParagraphAlignment.Left;
            pgDocTitle.Elements.AddLineBreak();           
            pgDocTitle.Format.Font.ApplyFont(fontTitles);
            pgDocTitle.AddFormattedText("Your patient matches the below patient profile which the panel of experts have assessed", TextFormat.Bold | TextFormat.Underline);
         
      MigraDoc.Rendering.DocumentRenderer docR = new DocumentRenderer(document);
            docR.PrepareDocument();

            PdfDocument document1 = new PdfDocument();
            if (docR.FormattedDocument.PageCount > 0)
            {
                int pageCount = docR.FormattedDocument.PageCount;
                XFont waterfont = new XFont("Arial", 30);
                string watermark = "For academic use only";
                for (int i = 1; i < pageCount + 1; i++)
                {
                    PdfPage newpage = document1.AddPage();
                    newpage.Height = XUnit.FromCentimeter(29.7).Point;
                    newpage.Width = XUnit.FromCentimeter(21.5).Point;                   
                    XGraphics gfx = XGraphics.FromPdfPage(newpage, XGraphicsPdfPageOptions.Append, XPageDirection.Downwards);                 
                    XSize size = gfx.MeasureString(watermark, waterfont);
                    gfx.TranslateTransform(newpage.Width / 2, newpage.Height / 2);
                    gfx.RotateTransform(-Math.Atan(newpage.Height / newpage.Width) * 90 / Math.PI);
                    gfx.TranslateTransform(-newpage.Width / 2, -newpage.Height / 2);
                    XGraphicsPath path = new XGraphicsPath();
                    path.AddString(watermark, waterfont.FontFamily, XFontStyle.BoldItalic, 30,
                      new XPoint((newpage.Width - size.Width) / 2, (newpage.Height - size.Height) / 2),
                      XStringFormats.Default);
                    XPen pen = new XPen(XColor.FromArgb(50, 139, 131, 120), 1);
                    XBrush brush = new XSolidBrush(XColor.FromArgb(50, 250, 250, 250));
                    gfx.DrawPath(pen, brush, path);
                    docR.RenderPage(gfx, i);
                }
            }     
            string filename = Path.GetFullPath(".") + "\\PDFImages\\RccToolKit.pdf";
            document1.Save(filename);

Author:  Thomas Hoevel [ Thu Jun 30, 2011 11:22 am ]
Post subject:  Re: Document Rendering Problem

I'd try gfx.Save() before and gfx.Restore() after the watermark code and before the call to RenderPage().
Otherwise I presume your transformations will also apply to RenderPage.

See here:
http://www.pdfsharp.net/wiki/Clock-sample.ashx

Author:  nareshreddy [ Fri Jul 01, 2011 1:00 pm ]
Post subject:  Re: Document Rendering Problem

Thanks Thomas.
It's worked.

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