我们有一个包含可填写字段的PDF表单库,可以在Adobe Reader中打开并由最终用户填写。请注意,我们不是生成PDF而只是使用现有的PDF。所有这些表单都受到保护,不会通过密码编辑Acrobat中的不可填写字段。
问题在于,当我们在代码中打开PDF以预填充某些字段时,密码保护会丢失,并且在Acrobat中可以编辑提供的表单。
所以:
以下是我们目前使用的代码的简化示例。我们已经尝试锁定输出流(代码当前不可用),但文件无法,然后Reader提示用户输入密码。
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
Byte[] password = encoding.GetBytes("secretPassword");
PdfReader reader = null;
MemoryStream outputStream = null;
PdfStamper stamper = null;
pdfStream.Seek(0, SeekOrigin.Begin);
reader = new PdfReader(pdfStream, password);
outputStream = new MemoryStream();
stamper = new PdfStamper(reader, outputStream);
AcroFields fields = stamper.AcroFields;
fields.SetField("prefillField1", "Some text");
stamper.Close();
outputStream.Close();
reader.Close();
// outputStream sent to user/browser. PDF opens in Reader, but the form can be opened in
// Acrobat without a password and edited.
答案 0 :(得分:2)
如果受密码保护,则无法编辑PDF。你希望你必须用密码打开它来编辑它。有了它,您似乎正在创建该文档的副本并呈现给用户。您可以使用编辑密码加密PDF。这是我发现我使用itextsharp的方法的副本。如果您在保留editPassword
null的同时设置openPassword
,那么您的用户应该能够查看pdf并与之交互,但无法根据您设置的权限对其进行编辑。希望这会有所帮助。
/// <summary>
/// Adds the given Open (User) and Edit (Owner) password to the PDF and optionally sets
/// the various Allow/Not Allowed restrictions
/// </summary>
/// <param name="openPassword">Open/User password</param>
/// <param name="editPassword">Edit/Owner password</param>
/// <param name="allowAssemply">Assembly means merging, extracting, etc the PDF pages</param>
/// <param name="allowCopy">Copy means right-click copying the content of the PDF to the system</param>
/// <param name="allowDegradedPrinting">Can print low quality version of the PDF</param>
/// <param name="allowFillIn">Can insert data into any AcroForms in the PDF</param>
/// <param name="allowModifyAnnotations">Modification of any annotations</param>
/// <param name="allowModifyContents">Modification of any content in the PDF</param>
/// <param name="allowPrinting">Allows printing at any quality level</param>
/// <param name="allowScreenReaders">Allows the content to be parsed/repurposed for screen readers</param>
public byte[] GetEncryptedPDF(byte[] pdf,
string openPassword, string editPassword,
bool allowAssemply, bool allowCopy,
bool allowDegradedPrinting, bool allowFillIn,
bool allowModifyAnnotations, bool allowModifyContents,
bool allowPrinting, bool allowScreenReaders)
{
int permissions = 0;
if (allowAssemply)
permissions = permissions | PdfWriter.ALLOW_ASSEMBLY;
if (allowCopy)
permissions = permissions | PdfWriter.ALLOW_COPY;
if (allowDegradedPrinting)
permissions = permissions | PdfWriter.ALLOW_DEGRADED_PRINTING;
if (allowFillIn)
permissions = permissions | PdfWriter.ALLOW_FILL_IN;
if (allowModifyAnnotations)
permissions = permissions | PdfWriter.ALLOW_MODIFY_ANNOTATIONS;
if (allowModifyContents)
permissions = permissions | PdfWriter.ALLOW_MODIFY_CONTENTS;
if (allowPrinting)
permissions = permissions | PdfWriter.ALLOW_PRINTING;
if (allowScreenReaders)
permissions = permissions | PdfWriter.ALLOW_SCREENREADERS;
PdfReader reader = new PdfReader(pdf);
using (MemoryStream memoryStream = new MemoryStream())
{
PdfEncryptor.Encrypt(reader, memoryStream, true, openPassword, editPassword, permissions);
reader.Close();
return memoryStream.ToArray();
}
}