PdfFileStamp and output file size problem

Hi,

I have problem with file size becoming too large after using PdfFileStamp

I have some input pdf document (with size about 2 MB and 138 pages, file is produced from merged pdf’s done with Aspose.Words) which I need to be stamp,ed. Each page is stamped with another pdf document (each page with different stamp produced by Aspose.Words).
Stamp file has about 17kB size.

After stamping, output doc has about 9.5 MB size. After opening this file with Adobe Acrobat Professional and re-saving it, size drops to 1.8 MB

We are planing to use this scenario with Pdf document > 100MB and over 10000 pages.

My test code to reproduce scenario is

private Document GetMainDocument(int pagesCount)
{
Document doc = new Document();
for (int i = 0; i < pagesCount; i++)
{
doc.Pages.Add();
}
return doc;
}

[Test]
public void Stamp_Pdf_With_Pdf()
{
int pagesCount = 100;

Document doc = GetMainDocument(pagesCount);

PdfFileStamp fileStamp = new PdfFileStamp(doc);
fileStamp.OutputFile = “Doc.Stamped.pdf”;
for(int i = 0; i < pagesCount; i++)
{
using (Stream ms =
new FileStream(@“TestData\StampOverlays\stamp.pdf”,
FileMode.Open))
{
Stamp stamp = new Stamp();
stamp.PageNumber = i;
stamp.BindPdf(ms, 1);
fileStamp.AddStamp(stamp);
}
}

fileStamp.Close();
}
With this code GetMainDocument produces 50kB file and after stamping it grows to 3.5 MB
Is there any other way to stamp and have lower file size?
Is there any way to reduce size of already existing file (by re-saving it somehow)?

The test was done with Aspose Pdf 6.5 and Aspose Words 10.4.

Hi Jacek,

Thank you for the details and sample code.

Please share your template PDF document and the PDF document you use as a stamp with us. This will help us identify the cause of the issue you are facing by testing at our end.

Sorry for the inconvenience.

Hi nausherwan.aslam,

I have attached two stamp pdf’s, smaller and larger. Can’t attach document I’m stamping because it’s confidential but it can be reproduced with document generated by GetMainDocument function.

Regards
Jacek Bator

Hi Jacek,

Thank you for sharing the PDF documents.

We have found your mentioned issue after an initial test. Your issue has been registered in our issue tracking system with issue id: PDFNEWNET-33110. We will update you via this forum thread regarding any updates against your issue.

Sorry for the inconvenience,

Hello Jacek,


Thanks for your patience.

We have further investigated this problem and have observed that when executing the code snippet that you have shared, the resultant file with following sizes are generated (v. 6.9):

For first stamp: 1.5M
For second stamp: 46M

Please note that in your code, you are adding every stamp separately, that’s why every new stamp is added as differnet XForm.

This explains file sizes:

First stamp is 17K,
Second stamp is 477K
Resultant file has 100 pages and stamp is added for each of them, thus:
for 1st stamp, 17K * 100 ~= 1700K (1.5M in real)
for 2nd stamp, 477K * 100 ~= 47700K (46M in real)

So in order to decrease the resultant file size and execution time, the customer may consider using Stamp.Pages property which allows to set numbers of pages which will be stamped (instead of Stamp.Page) and stamps all pages at once. Since v. 6.9, stamp is added as shared resources (i.e. one XForm for all pages).

[C#]
int pagesCount = 100;
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
for (int i = 0; i < pagesCount; i++)
{ doc.Pages.Add(); }

PdfFileStamp fileStamp = new PdfFileStamp(doc);
fileStamp.OutputFile = @“D:\pdftest\Doc.Stamped.pdf”;
int[] pages = new int[pagesCount];
//create array of pages which will be stamped
for (int i = 0; i < pagesCount; i++)
{ pages[i] = i + 1; }

using (Stream ms =
new FileStream(@“D:\pdftest\stamp.large.pdf”, FileMode.Open))
{ Aspose.Pdf.Facades.Stamp stamp = new Aspose.Pdf.Facades.Stamp(); stamp.Pages = pages; stamp.BindPdf(ms, 1); fileStamp.AddStamp(stamp); }
fileStamp.Close();

  • for 1st stamp, resultant file size is 552KB.
  • for 2nd stamp, resultant file size is 505KB.

For your reference, I have also attached the PDF document generated for 2nd stamp scenario and I have used our upcoming release version of Aspose.Pdf for .NET 7.0.0. In case you still encounter any issue or you have any further query, please feel free to contact. We are sorry for your inconvenience.

for 1st stamp, 17K * 100 ~= 1700K (1.5M in real)

Thank you for the info.The problem was that for this data output file was 3.5MB and in real scenario the difference was even bigger. I guess during that time Aspose.pdf got better so thank you for good job.

Our real scenario is little bit more complicated because stamps are dynamically generated and each page stamp has distinct content so cannot be used for more that one page.

JacekBator:
>for 1st stamp, 17K * 100 ~= 1700K (1.5M in real)

Thank you for the info.The problem was that for this data output file was 3.5MB and in real scenario the difference was even bigger. I guess during that time Aspose.pdf got better so thank you for good job.

Hello Jacek,

Please wait for the new release and try using the approach specified earlier and in case you still feel that the performance is not optimized, please feel free to contact.

JacekBator:
Our real scenario is little bit more complicated because stamps are dynamically generated and each page stamp has distinct content so cannot be used for more that one page.

In case you notice that the performance is not much optimized, can you please help us in replicating this scenario so that we can optimize the performance in this context. We are sorry for your inconvenience.

The issues you have found earlier (filed as PDFNEWNET-33110) have been fixed in Aspose.Pdf for .NET 7.0.0.


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