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

error on rotated/croped pages
https://forum.pdfsharp.net/viewtopic.php?f=3&t=3310
Page 1 of 1

Author:  iloslois [ Mon Mar 07, 2016 9:22 am ]
Post subject:  error on rotated/croped pages

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:
Code:
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.RotateTransform(-page.Rotate);
}

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:
Code:
PdfDocument doc = PdfReader.Open(filepath, PdfSharp.Pdf.IO.PdfDocumentOpenMode.Modify);
doc.Save(filepath);


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

Author:  Thomas Hoevel [ Mon Mar 07, 2016 10:17 am ]
Post subject:  Re: error on rotated/croped pages

Probably a duplicate entry:
viewtopic.php?f=3&t=3247

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