PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue Mar 02, 2021 10:47 am

All times are UTC

Forum rules

Please read this before posting on this forum: Forum Rules

Post new topic This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
PostPosted: Mon Mar 07, 2016 9:22 am 

Joined: Mon Mar 07, 2016 8:42 am
Posts: 1
I'll create a watermark diagonal, centered and an other watermark horizontally on each page, but I had some problems on some files.
After some time of debugging I found the error...
I get problems with pages wehre was croped or rotated, so i changed my code as followed:
XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Append);
XPoint center = new XPoint(page.Width / 2, page.Height / 2);
if (page.Elements.ContainsKey("/CropBox"))
    //consider crop offset
    center = new XPoint(center.X - page.CropBox.X1, center.Y - page.CropBox.Y1);
if (page.Rotate != 0)
    //consider rotation of the page
    gfx.TranslateTransform(center.X, center.Y);

gfx.DrawString("horizontally watermark text", font, brush, bottomRight, XStringFormats.BottomRight);

if (page.Rotate == 0)
     gfx.TranslateTransform(center.X, center.Y);
gfx.RotateTransform(-Math.Atan(page.Rotate == 90 ? page.Width / page.Height : page.Height / page.Width) * 180 / Math.PI);
gfx.TranslateTransform(-center.X, -center.Y);

gfx.DrawString("diagonal watermark text", font, brush, center, XStringFormats.Center);

It works fine, but in one case I found a bug of the library.
If there is a file with one rectangular page, 90° rotated, it gets croped into square.
In the followed code i open a rectangular file (one 90° rotated page) with PDFSharp, save it and get a squared file:
PdfDocument doc = PdfReader.Open(filepath, PdfSharp.Pdf.IO.PdfDocumentOpenMode.Modify);

Maby there is a confusion between X and Y coordinates?
I found also a strange error in the watch of the PdfPage:
error.jpg [ 142.04 KiB | Viewed 3366 times ]

PostPosted: Mon Mar 07, 2016 10:17 am 
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3047
Location: Cologne, Germany
Probably a duplicate entry:

Thomas Hoevel
PDFsharp Team

Display posts from previous:  Sort by  
Post new topic This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC

Who is online

Users browsing this forum: No registered users and 3 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