问题
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
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");
回答1:
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.
来源:https://stackoverflow.com/questions/48064902/itext-7-pdfreader-is-not-opened-with-owner-password-error