Reputation: 269
I open an existing pdf. Checking for protection and ask for password if it is protected and open it with:
PdfReader pdfReader = null;
Stream outputStream = null;
PdfStamper pdfStamper = null;
try
{
pdfReader = GetPdfReaderObject();
outputStream = new FileStream(filePathDestination, FileMode.Create, FileAccess.Write, FileShare.None);
pdfStamper = new PdfStamper(pdfReader, outputStream);
PdfLayer layer = new PdfLayer("watermark", pdfStamper.Writer);
for (int pageIndex = 1; pageIndex <= pdfReader.NumberOfPages; pageIndex++) {
pdfStamper.FormFlattening = false;
iTextSharp.text.Rectangle pageRectangle = pdfReader.GetPageSizeWithRotation(pageIndex);
PdfContentByte pdfData = pdfStamper.GetOverContent(pageIndex);
pdfData.BeginLayer(layer);
PdfGState graphicsState = new PdfGState();
graphicsState.FillOpacity = 0.5F;
pdfData.SetGState(graphicsState);
pdfData.BeginText();
iTextSharp.text.Image watermarkImage = iTextSharp.text.Image.GetInstance(System.Drawing.Image.FromFile(watermarkImagePath), ImageFormat.Png);
float width = pageRectangle.Width;
float height = pageRectangle.Height;
watermarkImage.SetAbsolutePosition(width / 2 - watermarkImage.Width / 2, height / 2 - watermarkImage.Height / 2);
pdfData.AddImage(watermarkImage);
pdfData.EndText();
pdfData.EndLayer();
}
}
pdfStamper.Close();
outputStream.Close();
outputStream.Dispose();
pdfReader.Close();
pdfReader.Dispose();
} catch (Exception e) {
....
}
}
After my modifications I save it but the protection is destroyed.
Why the protection is destroyed? How can I save the protection from the origin document und add it to my modified one.
Regards
Upvotes: 2
Views: 8006
Reputation: 95888
You use a PdfStamper
to manipulate an existing PDF.
For any source PDF: If you want the result to be encrypted, you may use the SetEncryption
method appropriately.
Have a look at the EncryptionPdf.cs, especially its method EncryptPdf
:
PdfReader reader = ...;
using (MemoryStream ms = new MemoryStream())
{
using (PdfStamper stamper = new PdfStamper(reader, ms))
{
stamper.SetEncryption(
USER, OWNER,
PdfWriter.ALLOW_PRINTING,
PdfWriter.ENCRYPTION_AES_128 | PdfWriter.DO_NOT_ENCRYPT_METADATA
);
}
return ms.ToArray();
}
Here USER
and OWNER
are the user and owner passwords of your choice. You might want to use a different set of permissions.
For already encrypted source PDFs you may alternatively choose to use the PdfStamper
in append mode (i.e. use a PdfStamper
constructor with a bool append
parameter set to true
). In that case the original encryption will also be applied to the updated PDF.
Upvotes: 3
Reputation: 77528
Which version of iText are you using?
When a password protected PDF was opened using the owner
password and manipulated by PdfStamper
, all original password protection was indeed removed. This changed in iText 5.3.5. See the changelog:
Important: we now keep the original owner password when stamping a document.
In other words: with all iText versions prior to 5.3.5, the owner password is lost. Starting with 5.3.5, it should be kept.
Upvotes: 1