Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Itext 7 - PdfReader is not opened with owner password Error

I am using This example for the latest Itext7 to fill in a document and I am getting this error: iText.Kernel.Crypto.BadPasswordException: PdfReader is not opened with owner password enter image description here So I looked around the net I found that some people found solution to this error using PdfReader.unethicalreading = true; but when I try to use this same code it says there is no definition in PDFReader named unethicalreading

Here is the Code I have:

 string src = @"C:\test1.pdf";
    string dest = @"C:\Test2.pdf";
    PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest));
    PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true);
    IDictionary<String, PdfFormField> fields = form.GetFormFields();
    PdfFormField toSet;
    fields.TryGetValue("Name", out toSet);
    toSet.SetValue("Some text");
like image 347
Ahmad Avatar asked Jan 02 '18 16:01

Ahmad


1 Answers

You need to change your code like this:

string src = @"C:\test1.pdf";
string dest = @"C:\Test2.pdf";
PdfReader reader = new PdfReader(src);
reader.setUnethicalReading(true);
PdfDocument pdfDoc = new PdfDocument(reader, new PdfWriter(dest));
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true);
IDictionary<String, PdfFormField> fields = form.GetFormFields();
PdfFormField toSet;
fields.TryGetValue("Name", out toSet);
toSet.SetValue("Some text");

This will allow you to go against the permissions that were defined by the original author of the document. This also proves that setting such permissions has become obsolete, because since PDF became an ISO standard, there is no longer a penalty for removing those permissions.

like image 171
Bruno Lowagie Avatar answered Sep 23 '22 13:09

Bruno Lowagie