We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Setting password & permission on generated PDF

Hi

I’m doing some tests on Aspose.Pdf. As a first test, I’ve tried to create a new pdf file joining existing pdf files. The code is as simple as


using Aspose.Pdf;


var doc = new Document();
var currFile = 0;

var files = System.IO.Directory.GetFiles(@"\path\to\pdf\dir");
foreach (var f in files)
using (var doc1 = new Document(f))
{
currFile++ ;
Console.WriteLine("{1}, appending {0}…", f, currFile);
doc.Pages.Add(doc1.Pages);
}

doc.Save(@"\path\to\result\file.pdf");
doc.Dispose();

I can actually see the generated file, with the contents of the source files.
Next step was to set permission on the generated file. I’ve added these lines, just before the call to doc.Save(…)


doc.Encrypt("", “masterpassword”,

Permissions.PrintDocument | Permissions.PrintingQuality,

CryptoAlgorithm.RC4x128);


Anyway, the resulting pdf has no permissions at all! Different settings for cryptoAlgorithm have given the same result.
So, whats wrong with the code above?

Another potential issue: the generated files is always version 1.7: is there a way to set a different pdf format version? This post seems to suggest there should be such an option, but I cannot find it.


Hi Riccardo,

Thanks for using our products and sharing the sample source code.

rioka68:

Anyway, the resulting pdf has no permissions at all! Different settings for cryptoAlgorithm have given the same result.

I tested the scenario with your sample source code using Aspose.Pdf for .NET v6.9 and unable to notice any issue in resultant PDF. Resultant PDF document is attached for your reference. You can see in the security properties window, only printing is set to "Allowed". Kindly use the latest version of Aspose.Pdf for .NET v6.9 and check if it works fine for you. If you still face any issue kindly create a sample application along with template documents and post here to show the issue. This will help us to regenerate the exact issue and get the cause of the issue soon.

rioka68:

Another potential issue: the generated files is always version 1.7: is there a way to set a different pdf format version? This post seems to suggest there should be such an option, but I cannot find it.

This issue was resolved by the development team but it was reopened at the time of testing. Our development team is looking into this feature and hopefully it will be available in near future.

We apologize for your inconvenience.

Thanks & Regards,

rashid.ali:

I tested the scenario with your sample source code using Aspose.Pdf for .NET v6.9 and unable to notice any issue in resulting PDF.


I've done a little bit more testing, and I think I've found the origin of the problem: the pdf files to be concatenated already have their own permissions set, and this seems to prevent Encrypt to successfully set a new one.

So I tried with a single, simple, unencrypted file, generated with Aspose.Words (v11.3.0.0). The very same code results in an exception as I call .Save method. Call stack is largely obfuscated:

System.IO.__Error.StreamIsClosed()
...(calls to obfuscated code)...
Aspose.Pdf.Document.Save(Stream output)
Aspose.Pdf.Document.Save(String outputFileName)

I've attached the pdf file to be merged, so you can easily reproduce the error.

This error seems not to be related with permissions, as it occurs even when I remove the call to Encrypt(...).

As a note, I'm using Aspose.Pdf version 6.9.0.0

TIA

Hi Riccardo,

Thanks for sharing the details and template document with us.

I tested the scenario using your source code and noticed the same issue. However, you can achieve the same functionality in different way using Aspose.Pdf for .NET. Kindly visit the following documentation links for more details and code snippets as per your requirement.

Please feel free to contact support in case you need any further assistance.

Thanks & Regards,

rashid.ali:

Hi Riccardo,

Thanks for sharing the details and template document with us.

I tested the scenario using your source code and noticed the same issue. However, you can achieve the same functionality in different way using Aspose.Pdf for .NET....

OK, but this clearly seems a bug to me. Will it be fixed?

Hi Riccardo,

Thank you for reporting this issue to us. I managed to reproduce this issue on my side. For the sake of correction, I have logged this issue as PDFNEWNET-33740 in our bug tracking system. Your request has also been linked to the appropriate issue and you will be notified as soon as it is resolved.

We apologize for your inconvenience.

Thanks & Regards,

Hi Riccardo,

Thanks for your patience.

We have further investigated this problem and have concluded that when documents are concatenated, source data is not loaded into memory to
decrease required memory size. Thus, data of source documents must be
accessible during all concatenation process until the resultant document is
not saved.

When save is called, PDF streams are copied from source document(s) to destination document.

That’s why source documents must not be closed while resultant document is not saved and closed.

Your code fails because Document.Dispose() is called on exit from using() {} block. In order to fix this problem, please update your code in the following manner:

[C#]

<!–[if gte mso 9]>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-US</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:EnableOpenTypeKerning/>
<w:DontFlipMirrorIndents/>
<w:OverrideTableStyleHps/>
</w:Compatibility>
<m:mathPr>
<m:mathFont m:val=“Cambria Math”/>
<m:brkBin m:val=“before”/>
<m:brkBinSub m:val="–"/>
<m:smallFrac m:val=“off”/>
<m:dispDef/>
<m:lMargin m:val=“0”/>
<m:rMargin m:val=“0”/>
<m:defJc m:val=“centerGroup”/>
<m:wrapIndent m:val=“1440”/>
<m:intLim m:val=“subSup”/>
<m:naryLim m:val=“undOvr”/>
</m:mathPr></w:WordDocument>
<![endif]–><!–[if gte mso 10]>

/* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin-top:0in; mso-para-margin-right:0in; mso-para-margin-bottom:10.0pt; mso-para-margin-left:0in; line-height:115%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi;}

<![endif]–>

var doc = new Document();

var currFile = 0;

var files = System.IO.Directory.GetFiles(@“D:\pdftest”);

foreach (var f in files)

{


var
doc1 = new Document(f);

currFile++;


Console
.WriteLine("{1}, appending {0}…", f, currFile);

doc.Pages.Add(doc1.Pages);

}

doc.Save(@“D:\pdftest\Mergd_file.pdf”);

doc.Dispose();


In case you still face the same problem or you have any further query, please feel free to contact. We are sorry for the delay and inconvenience.

The issues you have found earlier (filed as PDFNEWNET-33740) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.