Generating a password protected PDF with hyperlinks with the Aspose.Pdf library

I’m hoping someone can help me with a problem I’m having with generating a password protected PDF with hyperlinks with the Aspose.Pdf library. The problem is that the password protection seems to screw the hyperlink up …

The following code works fine (without any password protection):

// create new PDF doc with hyperlink in
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
Aspose.Pdf.Page page = doc.Pages.Add();
Aspose.Pdf.Text.TextFragment textFragment = new Aspose.Pdf.Text.TextFragment("hyperlink test");
textFragment.Hyperlink = new Aspose.Pdf.WebHyperlink("https://www.google.co.uk/");
page.Paragraphs.Add(textFragment);

// password protect the PDF
// Aspose.Pdf.Permissions permission = (Aspose.Pdf.Permissions.PrintingQuality | Aspose.Pdf.Permissions.PrintDocument | Aspose.Pdf.Permissions.ModifyTextAnnotations | Aspose.Pdf.Permissions.FillForm | Aspose.Pdf.Permissions.ModifyContent | Aspose.Pdf.Permissions.ExtractContent | Aspose.Pdf.Permissions.AssembleDocument);
// doc.Encrypt(“pass1”, “pass2”, permission, Aspose.Pdf.CryptoAlgorithm.RC4x128);

// save the doc
doc
.Save(“C:\temp\PDFHyperlink\test.pdf”);

<p style=“margin: 0px 0px 1em; padding: 0px; border: 0px; font-size: 15px; clear: both; color: rgb(36, 39, 41); font-family: Arial, “Helvetica Neue”, Helvetica, sans-serif; line-height: 19.5px; background-color: rgb(255, 255, 255);”>The PDF that is generated is fine with the hyperlink successfully opening a browser and navigating to google

<p style=“margin: 0px 0px 1em; padding: 0px; border: 0px; font-size: 15px; clear: both; color: rgb(36, 39, 41); font-family: Arial, “Helvetica Neue”, Helvetica, sans-serif; line-height: 19.5px; background-color: rgb(255, 255, 255);”>enter image description here

<p style=“margin: 0px 0px 1em; padding: 0px; border: 0px; font-size: 15px; clear: both; color: rgb(36, 39, 41); font-family: Arial, “Helvetica Neue”, Helvetica, sans-serif; line-height: 19.5px; background-color: rgb(255, 255, 255);”>However, if you uncomment the lines that password protect the PDF, the hyperlinks are screwed up

<pre class=“lang-cs prettyprint prettyprinted” style=“margin-top: 0px; margin-bottom: 1em; padding: 5px; border: 0px; font-size: 13px; width: auto; max-height: 600px; overflow: auto; font-family: Consolas, Menlo, Monaco, “Lucida Console”, “Liberation Mono”, “DejaVu Sans Mono”, “Bitstream Vera Sans Mono”, “Courier New”, monospace, sans-serif; color: rgb(57, 51, 24); word-wrap: normal; background-color: rgb(239, 240, 241);”><code style=“margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, “Lucida Console”, “Liberation Mono”, “DejaVu Sans Mono”, “Bitstream Vera Sans Mono”, “Courier New”, monospace, sans-serif; white-space: inherit;”>// create new PDF doc with hyperlink in
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
Aspose.Pdf.Page page = doc.Pages.Add();
Aspose.Pdf.Text.TextFragment textFragment = new Aspose.Pdf.Text.TextFragment(“hyperlink test”);
textFragment
.Hyperlink = new Aspose.Pdf.WebHyperlink(https://www.google.co.uk/);
page
.Paragraphs.Add(textFragment);

// password protect the PDF
Aspose.Pdf.Permissions permission = (Aspose.Pdf.Permissions.PrintingQuality | Aspose.Pdf.Permissions.PrintDocument | Aspose.Pdf.Permissions.ModifyTextAnnotations | Aspose.Pdf.Permissions.FillForm | Aspose.Pdf.Permissions.ModifyContent | Aspose.Pdf.Permissions.ExtractContent | Aspose.Pdf.Permissions.AssembleDocument);
doc
.Encrypt(“pass1”, “pass2”, permission, Aspose.Pdf.CryptoAlgorithm.RC4x128);

// save the doc
doc
.Save(“C:\temp\PDFHyperlink\test.pdf”);<p style=“margin: 0px 0px 1em; padding: 0px; border: 0px; font-size: 15px; clear: both; color: rgb(36, 39, 41); font-family: Arial, “Helvetica Neue”, Helvetica, sans-serif; line-height: 19.5px; background-color: rgb(255, 255, 255);”>enter image description here

<p style=“margin: 0px 0px 1em; padding: 0px; border: 0px; font-size: 15px; clear: both; color: rgb(36, 39, 41); font-family: Arial, “Helvetica Neue”, Helvetica, sans-serif; line-height: 19.5px; background-color: rgb(255, 255, 255);”>… and you get the following message when the hyperlink is clicked:

<p style=“margin: 0px 0px 1em; padding: 0px; border: 0px; font-size: 15px; clear: both; color: rgb(36, 39, 41); font-family: Arial, “Helvetica Neue”, Helvetica, sans-serif; line-height: 19.5px; background-color: rgb(255, 255, 255);”>enter image description here

<p style=“margin: 0px 0px 1em; padding: 0px; border: 0px; font-size: 15px; clear: both; color: rgb(36, 39, 41); font-family: Arial, “Helvetica Neue”, Helvetica, sans-serif; line-height: 19.5px; background-color: rgb(255, 255, 255);”>… and if you click Allow, nothing happens!

<p style=“margin: 0px 0px 1em; padding: 0px; border: 0px; font-size: 15px; clear: both; color: rgb(36, 39, 41); font-family: Arial, “Helvetica Neue”, Helvetica, sans-serif; line-height: 19.5px; background-color: rgb(255, 255, 255);”>Is there a problem with the way I’m password protecting the PDF? Any help would be appreciated.

Hi Carl,

Thanks for your inquiry. I have tested your scenario and noticed the hyperlinks are updated in enrypted PDF document, so I have logged an issue in our issue tracking system as PDFNET-41153 and also linked your request to it. We will keep you updated via this thread regarding the issue status.

We are sorry for the inconvenience caused.

I am running into the same issue. Has this been fixed? I am using v18.9 .

@petarian

Thanks for your inquiry.

I am afraid that earlier logged issue is not yet resolved due to large number of pending issues in the queue. We will definitely let you know once significant progress is made against resolution of the issue. Please spare us little time.

We are sorry for the inconvenience.

The issues you have found earlier (filed as PDFNET-41153) have been fixed in Aspose.PDF for .NET 24.12.