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 [ 142.04 KiB | Viewed 8119 times ]