Itext 7 - PdfReader is not opened with owner password Error

家住魔仙堡 提交于 2020-01-24 12:10:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!