When disable printing option is set to true in document then hyperlink is not redirected (treated as text) .net

Hi,

I am uploading document from UI and open it with Adobe Reader view in my web application in c# language. I have used Aspose.Pdf namespaces.

When I set option ‘Disable Print’ to true in document then document’s all hyperlink is not working and redirecting (treated as text only with underline) but when I set option ‘Disable Print’ to false then its working fine.

We want to customized only Enable print and disable print , that shouldn’t affect to hyperlink or any other permissions.

Can you please help me with this issue.

Regards.

@yespan,

Was the PDF created with Aspose PDF library?

Would you mind sharing the document for further testing?

What version of Aspose PDF are you currently using?

Version : Aspose.PDF 22.12.0
PDF - There is FileUpload control in UI, so any one can upload file from their local system. Any file upload by UI is converted to aspose pdf with customize things like add password protection, disable printing etc.
I am attaching two file TestPDF.pdf is the one which is initially uploaded by user and TestPdfConverted.pdf is after converting to aspose PDF with all settings applied.

TestPdfConverted.pdf - Hyperlink is not working when I set disable printing option to true.TestPdfConverted.pdf (38.3 KB)
TestPdf.pdf (37.4 KB)

@yespan,

I forgot to ask you one of the most important things.
Can you provide a code snippet of what you do with the PDF document before saving it.

@yespan,

Can you try updating your library to 23.1?

I executed this code and it worked fine.

private void Logic(Document doc)
{
    DocumentPrivilege documentPrivilege = DocumentPrivilege.AllowAll;

    documentPrivilege.AllowPrint = false;

    doc.Encrypt("user", "owner", documentPrivilege, CryptoAlgorithm.AESx128, false);
}

HyperlinksStopWorkingWhenDisablingPrintOption_input.pdf (37.4 KB)
HyperlinksStopWorkingWhenDisablingPrintOption_output.pdf (37.6 KB)

Okay , Let me try this one.
Second output pdf is not working that you shared. Show permission issue. Capture.PNG (124.2 KB)

I do not know what type of validations are you using.

I generated that document via Aspose using the source document that you provide us, then I executed only the lines I showed abode. Then I opened via Adobe Acrobat DC:

The links are working. as you can see in the picture.

If you do not want to open the files I attached, you can simply replicate everything I did including the code.
Keep in mind I am using Version 23.1

Got it, First I will try with my existing version 22.12.0.
Is that possible to cause of this issue ?

@yespan,

I just downgraded my system to check it, and it worked as well with:

Hi , @carlos.molina
I have below code still its not working. Can you please refer below code:

Public void EncryptDoc(Document doc)
{
var docSecurity = new PdfFileSecurity(doc);
DocumentPrivilege privs = null;
if (disablePrint)
{
privs = DocumentPrivilege.AllowAll;
privs.AllowPrint = false;
}
else
{
privs = DocumentPrivilege.Print;
}

if (!docSecurity.EncryptFile(userPassword, ownerPassword, privs, KeySize.x128, Algorithm.AES))
    throw new Exception("Error in Aspose.Pdf.PdfFileSecurity.EncryptFile method");
	
docSecurity.Document.IsLinearized = true;
docSecurity.Document.Flatten();


.................
.............

}

I am using 22.12.0 version.

@yespan,

I found the issue. It had nothing to do with the versión.

Please read the documentation of what Flatten does.

docSecurity.Document.Flatten(); 

If you remove or comment that line your code will work as intended.
Flatten removes the fields(hyperlinks).

Hi @carlos.molina,

Thanks for the response. But same code is working fine for hyperlink when DisablePrint value is false

                **if (disablePrint)**

** {**
** privs = DocumentPrivilege.AllowAll;**
** privs.AllowPrint = false;**
** }**
** else**
** {**
** privs = DocumentPrivilege.Print;**
** }**

When else part is executed its working fine but not working for If part. So how is this related to flatten(), becuase for both cases flatten() will call.

What’s your opinion for this ?

@yespan,

I just tested the code without the if and with the Flatten method, and it does what I said before.

Code example:

private void LogicClientTwo(Document doc)
{
    var docSecurity = new PdfFileSecurity(doc);
    DocumentPrivilege privs = null;            
    privs = DocumentPrivilege.Print;            

    if (!docSecurity.EncryptFile("user", "owner", privs, KeySize.x128, Algorithm.AES))
        throw new Exception("Error in Aspose.Pdf.PdfFileSecurity.EncryptFile method");

    docSecurity.Document.IsLinearized = true;
    docSecurity.Document.Flatten();
}

Input / output files
HyperlinksStopWorkingWhenDisablingPrintOption_inout.pdf (37.4 KB)
HyperlinksStopWorkingWhenDisablingPrintOption_output.pdf (36.7 KB)

I removed the if, let the code be executed always(as if the the boolean was false) and the hyperlinks dissapear.
The Flatten method is the one removing the hyperlinks.

It’s working :smiley:
Thank you so much @carlos.molina for your support and quick responses. :+1: