Optimzeresource is increasing size drastically

Hi I had 2 issues 1) fonts were not getting embedded as subset due to which file size was increasing

So I added
Pdf.fontutilities.subsetfonts(subsetallfont);
Pdf.embeddedstandfonts = true.

It worked fine

In issue 2) I have to handle file size which was getting increased from 300 to 15000 kb so I applied Image compression above the 2 lines mentioned in point 1 above , but by doing so, few of file containing msmincho font, gets destructive.

So I tried applying

Pdf.fontutilities.subsetfonts(subsetallfont);
Pdf.embeddedstandfonts = true.

Above and below image compression optimisation techniques, but then I am getting cannot process closed stream and I am.stuck completly.

I cannot share actual code as per company policy
IMG_20231123_155344.jpg (277.5 KB)

@GayatriB

Can you please share the sample file that is causing the issue. We will test the scenario in our environment and address it accordingly.

Hi sorry but can’t share any of company documents .

My main concern is why I get error " cannot read closed stream" when I am calling pdf.fontutilitis.subsetallfonts for second time ie,after applying optimisation resources function.

We have some production issue for which I need to submit the fix asap.

NOTE : This issue occurs only if I add ResizeImage and CompressImages in OptimizationOption. Else it will not come. So kindly confirm, if you dispose obj while this 2 flags are set? Do we need to seek stream to 0 location

Note we are also using licence copy of aspose so let me know if we get special support In this case

Hi Team any Updates on my Reply above?? @asad.ali

Hi Asad can you please look in to my issue as i have to provide fix for PRoduction. for File size increading from 300KB to 15000KB after converting to PDFA

@GayatriNaik @GayatriB

Please note that it is necessary for us to have a sample PDF document in order to replicate the issue in our environment. We are afraid that we cannot guess or determine the real issue without investigating it. Therefore, we requested you to share at least one sample PDF document. We assure you that we do not disclose your files with anyone. They are only used by Aspose Staff and are destroyed once issues are resolved.

But atleast can you confirm which object gets disposed when we set compress image = true??

Coz when I call subset all font second time will not cause closed stream issue if I don’t set CompressImages and resize image flags

@GayatriB

Are you saving the document as well before calling the method second time? About the flag of setting image compression, we are checking the details and will get back to you shortly.

No hhas you can see in the attachment I am calling

  1. first part to subset and embed font
  2. second part to optimizeresource
  3. third part same repeating part 1 mentioned above.

Then I am calling pdf.convert.
Then saving documents.

The only reaswon I was force to add first part was pdf file with msminchbo font gets corrupted . I mean file gets open but fonts are displayed as a garbage value so I thought that optimizeresource is not able to work with it. But after adding first part now, ie all part 1 ,2, and 3 now is causing cannot process closed stream.

@asad.ali Requesting you to please provide me the solution since it’s most praising issue for us from customer that file size is increasing from 30 kn to direct 15000 kb …

Also the reason why I want to add subset and embed font in code is because of the font is not embedded in pdf, it picks up from system and add it as “Embedded” and not “EmbeddedSubset” (not sure if it’s a big or working as per design) so this was increasing size of few other filed where fonts were not embedded. So to reduce other pdf file sizes, I need to add this part somewhere in code.

@GayatriB

Since we do not have enough information to replicate the issue in our environment, we can only suggest you save and re-initialize the document after performing every operation. In the meanwhile, we are try to replicate the issue with our sample files and will let you once we have some results to share.

Can you share code snippet of what you mentioned in line one ? Save n reinitialize?? Also note I am saving to memorystream and not to any location.

Also I am creating first and saving in last line so still I try ur provided solution?

Can u provide exact what n where u want us to change? I will do n reply u @asad.ali if that work

@GayatriB

// Save document
MemoryStream ms = new MemoryStream();
doc.Save(ms);
ms.Seek(0, SeekOrigin.Begin);
doc = new Document(ms);

Also, please share if this issue is happening with every PDF document or with a specific one?

I tried above code mentioned by you but it didn’t work coz when I call pdf.save, it throws cannot process closed stream.

I tried another combination

  1. part a - embed and substitute fonts
  2. part b- optimisationresource

Then call pdf.convet.
Then SaveDocument.

Showing details in screenshots but still it throws exception for one document,

but it solves my other 2 issues a) add embedded subset so file size reduces b) resolved issue where file size was increasing from 300kb to 1500000kb .

Then only issue I face is for one file it’s getting crashed by throwing mentioned error " cannot process closed stream"
Sending 4 attached files for code and callstack error. @asad.ali please confirm

IMG_20231127_124519.jpg (222.8 KB)

IMG_20231127_124725.jpg (223.8 KB)

IMG_20231127_124741.jpg (338.1 KB)

IMG_20231127_124805.jpg (371.3 KB)

@GayatriB

We have checked all the information that you shared. It is clear that the issue is happening due to specific type of PDF document. Sadly, at this point and with this information we can only tell the possible reason behind such exception.

When a PDF document is initialized, API stores all resources in the system memory and flushes out those resources when you save document using Save() method or call ProcessParagraphs() method. Furthermore, if an object is initialized to be used in the document, it should not be disposed of before document is saved.

We are afraid that we cannot tell much by looking at the stack trace as it is encrypted/ We do need your sample PDF to properly investigate and address this issue in our environment. Please note that we do not disclose your files with anyone and no one can download them from this thread except you and Aspose Staff. We destroy the files from our system once the issue is fixed.

Ok as of now we are not disposing object at our end before save() .

Also I observed that this is happening with only some type of pdf document when I am setting few of the parameters eg. Subsetfont = trueresizeimage and CompressImages true in OptimizationOption. Else it will not crash for that pdf.

We are using licence copy of aspose.pdf. so is there any specific official email where I can share the pdf document via our company channel? Your official email id may help me to share it official.

Waiting for your email id.

In continuation with above reply, sending you sample file for testing Error in “Cannot read closed stream”

Please see attachment “Sample PDF with Gothic.pdf”. also let me know if this attachment is not available for you to download.

Please note that this is just one file, Actually i am facing issue with Multiple files and they all are failing with same reasons. but as of now i cannnot share other pdf files. This was just a Sample fil so i am allowded to share @asad.ali waiting for ur positive response now :slight_smile:

Sample PDF with MS Gothic.pdf (19.6 KB)

@GayatriNaik

We used below code snippet from your screenshot with the PDF document that you shared and could not notice any issue or exception in our environment. We used the latest version of the API. For your kind reference, an output PDF is also attached.

Document pdf = new Document(dataDir + "Sample PDF with MS Gothic.pdf");
MemoryStream outputStream = new MemoryStream();
var pdfsecurity = new PdfFileSecurity();
pdfsecurity.BindPdf(pdf);
pdfsecurity.DecryptFile(""); 
pdf.EmbedStandardFonts = true;
pdf.FontUtilities.SubsetFonts(FontSubsetStrategy.SubsetAllFonts);
var optimizationOptions = new Aspose.Pdf.Optimization.OptimizationOptions();
optimizationOptions.SubsetFonts = true;
optimizationOptions.ImageCompressionOptions.Version = ImageCompressionVersion.Mixed;
optimizationOptions.RemoveUnusedObjects = true;
optimizationOptions.ImageEncoding = ImageEncoding.Flate;
optimizationOptions.ImageCompressionOptions.ResizeImages = true;
optimizationOptions.ImageCompressionOptions.CompressImages = true;
optimizationOptions.ImageCompressionOptions.ImageQuality = 75;
optimizationOptions.MaxResoultion = 1000;
pdf.OptimizeResources(optimizationOptions);
var IsFileConverted = pdf.Convert(outputStream, PdfFormat.PDF_A_1A, ConvertErrorAction.Delete);
pdf.Save(dataDir + "output.pdf");

output.pdf (19.5 KB)

@asad.ali I am creating pdf from documentmetadat.sourcestream and not from specific location.

And this issue is related to that stream only I guess.

Also note we are using aspose.pdf 22.1.0 version. @asad.ali please confirm your version too Please.

Please confirm:

  1. You are not getting error when you are using stream for var pdf = new Document (…

Coz I tried var pdf = new Document( my machine location for the pdf file) and still it crashed.

  1. You are not getting error with Aspose.pdf version of 22.1.0.

  2. which version of aspose.pdf you are using?

@GayatriB

We tested using FileStream as well. We loaded the document from a FileStream and did not face any issue.

We tested using 23.11 version of the API.

Please note that we provide support on the basis of the latest available version of the API because it has maximum fixes and improvements. Also, issues in the older versions of the API are resolved in the higher versions. Therefore, we request you please test with the latest version and see if error still persists at your end. You can obtain a free 30-days temporary license to evaluate the latest version of the API and let us know about your feedback. We will further proceed to assist you accordingly.