Hi Guys,
I am using PDFSharp in 2 different applications for 2 different reasons. The first is password protect a document and the second is to watermark the document. These processes both work individually in there own applications however the problem is that application 1 only knows about the password and application 2 only knows about the watermark, application 1 uses a default owner password and a dynamic user password, application 2 opens the document with the owner password to apply the watermark but can not reapply the passwords (ie the doc is saved without a password) is there a way of keeping the security settings when applying the watermark?
This is the watermarking code i am using
Code:
private void CreateWatermark(string file, string watermark)
{
log.Write(string.Format("Invoking CreateWatermark"), Category.General, Priority.Low, 2, TraceEventType.Verbose);
const int emSize = 150;
var font = new XFont("Times New Roman", emSize, XFontStyle.BoldItalic);
PdfDocument document = PdfReader.Open(file, "<password>");
if (document.Version < 14)
document.Version = 14;
for (int idx = 0; idx < document.Pages.Count; idx++)
{
PdfPage page = document.Pages[idx];
XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Append);
// Get the size (in point) of the text
XSize size = gfx.MeasureString(watermark, font);
// Define a rotation transformation at the canter of the page
gfx.TranslateTransform(page.Width/2, page.Height/2);
gfx.RotateTransform(-Math.Atan(page.Height/page.Width)*180/Math.PI);
gfx.TranslateTransform(-page.Width/2, -page.Height/2);
// Create a graphical path
var path = new XGraphicsPath();
// Add the text to the path
path.AddString(watermark, font.FontFamily, XFontStyle.BoldItalic, 150,
new XPoint((page.Width - size.Width)/2, (page.Height - size.Height)/2),
XStringFormats.Default);
// Create a dimmed red pen and brush
var pen = new XPen(XColor.FromArgb(50, 75, 0, 130), 3);
XBrush brush = new XSolidBrush(XColor.FromArgb(50, 106, 90, 205));
// Stroke the outline of the path
gfx.DrawPath(pen, brush, path);
}
log.Write(string.Format("Saving File with watermark '" + watermark + "'"), Category.General, Priority.Low);
document.Save(file);
}
Thanks for your help,
Josh