PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 3:12 pm

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: Watermark troubles
PostPosted: Fri Dec 16, 2016 2:51 pm 
Offline

Joined: Fri Dec 16, 2016 2:35 pm
Posts: 1
I have migrated to PdfSharp-1.50b3, but my watermark is not work on this version...

Code:
XFont fontKM = new XFont("Times New Roman", 48, XFontStyle.Bold);
XFont fontStamp = new XFont("Times New Roman", 24, XFontStyle.Regular);
XPen penPart1 = new XPen(XColor.FromArgb(80, 103, 8, 123), 1);
XBrush brushPart1 = new XSolidBrush(XColor.FromArgb(70, 103, 8, 123));
XPen penPart2 = new XPen(XColor.FromArgb(80, 154, 207, 22), 1);
XBrush brushPart2 = new XSolidBrush(XColor.FromArgb(70, 154, 207, 22));
XPen penStamp = new XPen(XColor.FromArgb(80, 0, 0, 0), 1);
XBrush brushStamp = new XSolidBrush(XColor.FromArgb(75, 0, 0, 0));
String strStamp = String.Format(wmarkStamp, DateTime.Now.ToString(DateTimeFormat));

using (PdfDocument resDoc = new PdfDocument())
{
   foreach (VWcsDocsRecord item in ccDocuments)
   {
      using (MemoryStream ms = new MemoryStream(item.FILES))
      {
         try
         {
            using (PdfDocument cDoc = PdfReader.Open(ms, PdfDocumentOpenMode.Import))
            {               
               foreach (PdfPage page in cDoc.Pages)
               {
                  PdfPage newPage = resDoc.AddPage(page);

                  using (XGraphics cnv = XGraphics.FromPdfPage(newPage, XGraphicsPdfPageOptions.Append))
                  {
                     // Get the size (in point) of the text
                     XSize sizePart1 = cnv.MeasureString(wmarkKMPart1, fontKM);
                     XSize sizePart2 = cnv.MeasureString(wmarkKMPart2, fontKM);
                     XSize sizeStamp = cnv.MeasureString(strStamp, fontStamp);

                     // Create a graphical path
                     XGraphicsPath pathPart1 = new XGraphicsPath();
                     XGraphicsPath pathPart2 = new XGraphicsPath();
                     XGraphicsPath pathStamp = new XGraphicsPath();


                     // Add the text to the path
                     pathPart1.AddString(wmarkKMPart1, fontKM.FontFamily, fontKM.Style, fontKM.Size,
                       new XPoint((page.Width - sizePart1.Width - sizePart2.Width) / 2, (page.Height - sizePart1.Height) / 2), XStringFormats.Default);
                     pathPart2.AddString(wmarkKMPart2, fontKM.FontFamily, fontKM.Style, fontKM.Size,
                       new XPoint(((page.Width - sizePart1.Width - sizePart2.Width) / 2) + sizePart1.Width, (page.Height - sizePart1.Height) / 2), XStringFormats.Default);
                     pathStamp.AddString(strStamp, fontStamp.FontFamily, fontStamp.Style, fontStamp.Size,
                        new XPoint(page.Width / 2, ((page.Height - sizePart1.Height) / 2) + sizePart1.Height * 1.3), XStringFormats.TopCenter);

                     // Stroke the outline of the path
                     cnv.DrawPath(penPart1, brushPart1, pathPart1);
                     cnv.DrawPath(penPart2, brushPart2, pathPart2);
                     cnv.DrawPath(penStamp, brushStamp, pathStamp);
                  }
               }            
               realCount++;
            }
         }
         catch (Exception exc)
         {
         }
      }
   }
}


The result pdf-file has only original pages without my watermark on library 1.50b3...
Maybe I am doing something wrong...


Top
 Profile  
Reply with quote  
 Post subject: Re: Watermark troubles
PostPosted: Fri Dec 16, 2016 8:35 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
Hi!

Have you changed the XStringFormat parameter in the call to AddString?

The Watermark sample that comes with the PDFsharp source works like it always did, but required some changes. Check the source for that sample and try if it works with your PDF file.

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


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 143 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