PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Wed Jul 10, 2024 10:26 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Thu Jun 30, 2011 10:59 am 
Offline

Joined: Wed Jun 22, 2011 7:32 am
Posts: 12
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);


Top
 Profile  
Reply with quote  
PostPosted: Thu Jun 30, 2011 11:22 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
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

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Fri Jul 01, 2011 1:00 pm 
Offline

Joined: Wed Jun 22, 2011 7:32 am
Posts: 12
Thanks Thomas.
It's worked.


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 25 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