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

Free Support Forum - aspose.com

Unable to merge and save PDF with security

Hi,

I am trying to merge two PDF documents (both got attachments) into one and save with security. But its throwing error in Save() method. error is
"Object reference not set to an instance of an object."

Version used : version 10.8.0.0

Code Used
--------------------------

Aspose.Pdf.Document pdfDocument1 = new Aspose.Pdf.Document("PDF_Merge_3.pdf");
Aspose.Pdf.Document pdfDocument2 = new Aspose.Pdf.Document("PDF_Merge_4.pdf");

Aspose.Pdf.Document pdfDocumentNew = new Aspose.Pdf.Document();

pdfDocumentNew.Pages.Add(pdfDocument1.Pages);
pdfDocumentNew.Pages.Add(pdfDocument2.Pages);

foreach (FileSpecification fileSpecification in pdfDocument1.EmbeddedFiles)
{
pdfDocumentNew.EmbeddedFiles.Add(fileSpecification);
}

foreach (FileSpecification fileSpecification in pdfDocument2.EmbeddedFiles)
{
pdfDocumentNew.EmbeddedFiles.Add(fileSpecification);
}

DocumentPrivilege documentPrivilege = DocumentPrivilege.ForbidAll;
PdfFileSecurity security = new PdfFileSecurity(pdfDocumentNew);
security.SetPrivilege(documentPrivilege);
pdfDocumentNew.Save("test.pdf");


Input files are attached.

Hi Shakkeeb,


Thanks for your inquiry. Please note PDF document is created dynamically by API and in order to get real PDF document it needs to render its programming/dynamic model. So we can render PDF document model by calling save() method. You can save your document to stream and reload from that stream for further processing as following. It will help you to accomplish the task.

Aspose.Pdf.Document
pdfDocument1 = new Aspose.Pdf.Document(“PDF_Merge_3.pdf”);<o:p></o:p>

Aspose.Pdf.Document pdfDocument2 = new Aspose.Pdf.Document("PDF_Merge_4.pdf");

Aspose.Pdf.Document pdfDocumentNew = new Aspose.Pdf.Document();

pdfDocumentNew.Pages.Add(pdfDocument1.Pages);

pdfDocumentNew.Pages.Add(pdfDocument2.Pages);

foreach (FileSpecification fileSpecification in pdfDocument1.EmbeddedFiles)

{

pdfDocumentNew.EmbeddedFiles.Add(fileSpecification);

}

foreach (FileSpecification fileSpecification in pdfDocument2.EmbeddedFiles)

{

pdfDocumentNew.EmbeddedFiles.Add(fileSpecification);

}

MemoryStream ms = new MemoryStream();

pdfDocumentNew.Save(ms);

pdfDocumentNew = new Document(ms);

DocumentPrivilege documentPrivilege = DocumentPrivilege.ForbidAll;

PdfFileSecurity security = new PdfFileSecurity(pdfDocumentNew);

security.SetPrivilege(documentPrivilege);

pdfDocumentNew.Save("test.pdf");

Please feel free to contact us for any further assistance.

Best Regards,