フォーラムで彼らからの返答や助けが得られなかったので、これはPDFシャープの制限だったと思います。そこでコードを開き、エラーを修正するために次の変更を加えました。まず、SecurityHandler.csクラスに新しいプロパティを追加しました
public string OwnerPassword
{
set { SecurityHandler.OwnerPassword = value; }
}
/// <summary>
/// TODO: JOSH
/// </summary>
public bool MaintainOwnerAndUserPassword
{
get { return SecurityHandler.MaintainOwnerAndUserPassword; }
set { SecurityHandler.MaintainOwnerAndUserPassword = value; }
}
次に、PdfDocument.csクラスのdoSaveメソッドを次のように変更しました。
void DoSave(PdfWriter writer)
{
if (this.pages == null || this.pages.Count == 0)
throw new InvalidOperationException("Cannot save a PDF document with no pages.");
try
{
bool encrypt = this.securitySettings.DocumentSecurityLevel != PdfDocumentSecurityLevel.None;
if (encrypt)
{
PdfStandardSecurityHandler securityHandler = this.securitySettings.SecurityHandler;
if (securityHandler.Reference == null)
this.irefTable.Add(securityHandler);
else
Debug.Assert(this.irefTable.Contains(securityHandler.ObjectID));
this.trailer.Elements[PdfTrailer.Keys.Encrypt] = this.securitySettings.SecurityHandler.Reference;
}
else
this.trailer.Elements.Remove(PdfTrailer.Keys.Encrypt);
PrepareForSave();
if (encrypt && !securitySettings.SecurityHandler.MaintainOwnerAndUserPassword)
this.securitySettings.SecurityHandler.PrepareEncryption();
...
最後に、PDFSecuritySettings.csのCanSaveメソッドを次のように変更しました。
internal bool CanSave(ref string message)
{
if (this.documentSecurityLevel != PdfDocumentSecurityLevel.None)
{
if ((SecurityHandler.userPassword == null || SecurityHandler.userPassword.Length == 0) &&
(SecurityHandler.ownerPassword == null || SecurityHandler.ownerPassword.Length == 0) &&
!SecurityHandler.MaintainOwnerAndUserPassword)
{
message = PSSR.UserOrOwnerPasswordRequired;
return false;
}
}
return true;
}
これにより、MaintainOwnerAndUserPassword設定を設定できるようになり、ハッシュ化されたユーザー名とパスワードが既にあると仮定すると、正常に機能し、ダンディになります。
何度も何度も。