Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7896947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:01:31+00:00 2026-06-03T08:01:31+00:00

I am using PDFSharp in 2 different applications for 2 different reasons. The first

  • 0

I am using PDFSharp in 2 different applications for 2 different reasons. The first is password protect a document and the second is to watermark the document. These processes both work individually in there own applications/workflows. The problem is that application 1 only knows about the password and application 2 only knows about the watermark, application 1 uses a default owner password and a dynamic user password, application 2 opens the document with the owner password to apply the watermark. The problem is that the password is not persisted, it seems PDFSharp while saving the document ignores the previous PDF password?!

Is there a way of keeping the security settings when applying the watermark, without explicitly defining the passwords again?

I posted this on the PDFSharp forum but they are ignoring it which is not a good sign?!
http://forum.pdfsharp.net/viewtopic.php?f=2&t=2003&p=5737#p5737

Kind Regards,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T08:01:32+00:00Added an answer on June 3, 2026 at 8:01 am

    I think this was a limitation of PDF sharp, as I got no response or help from them on there forum. I opened up there code and made the following changes to correct the error. Firstly i added a new property on the SecurityHandler.cs class

    public string OwnerPassword
    {
      set { SecurityHandler.OwnerPassword = value; }
    }
    
    /// <summary>
    /// TODO: JOSH
    /// </summary>
    public bool MaintainOwnerAndUserPassword
    {
        get { return SecurityHandler.MaintainOwnerAndUserPassword; }
        set { SecurityHandler.MaintainOwnerAndUserPassword = value; }
    }
    

    Then I changed the doSave method on the PdfDocument.cs class to look like the following:

        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();
    
    ...
    

    Finally I changed the CanSave method on the PDFSecuritySettings.cs to this:

        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;
    }
    

    This should allow you to set the MaintainOwnerAndUserPassword setting and assuming you already have a hashed username and password it should work fine and dandy,

    Over and out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using PDFSharp in my c# project. In my previous work place I
I am using PDFsharp in an ASP.NET MVC application. I want to add an
I'm trying to use PDFSharp to add 2 text strings to a document -
I am using PDFsharp to create PDF files in my C# application. It's working
I have a program that is manually generating a PDF using PDFsharp in C#.
I am successfully able to extract images from a pdf using pdfsharp. The image
Using PyObjC , you can use Python to write Cocoa applications for OS X.
We use PDFSharp (the GDI+ build) in one of our web applications. In one
I am using PdfSharp dll to print a pdf. This is the code that
I am using an external DLL (pdfsharp) to open (then manipulate) lots of PDF

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.