Compress PDF

Can you point me to documentation describing how to open, compress, and save an existing PDF?


Also when I link source code on the forum, how do I enable the syntax highlighting like you guys do when you reply?
1 Like

robert.kerr:
Can you point me to documentation describing how to open, compress, and save an existing PDF?
Hi Robert,

Thanks for contacting support.

Please visit the following link for required information on Optimize PDF File Size

robert.kerr:
Also when I link source code on the forum, how do I enable the syntax highlighting like you guys do when you reply?
In order to preserve the syntax highlighting and formatting, you may either use the Insert code Snippet button from toolbar above reply section or you can first paste the code in MS word and then copy paste it to message text area.

1 Like

Thanks. What size PDF can optimize handle? I’m sending PDFs of only 16 MB at it and it throws out-of-memory exceptions.

VS2015 update 3
Aspose.Pdf .NET 11.9

private static void Shrink(string sourceFilename, string targetFilename)
{
try
{
var pdfDocument = new Document(sourceFilename);

pdfDocument.OptimizeResources(new Document.OptimizationOptions()
{
LinkDuplcateStreams = true,
RemoveUnusedObjects = true,
RemoveUnusedStreams = true,
CompressImages = false,
ImageQuality = 10,
UnembedFonts = false,
});

pdfDocument.Save(targetFilename);
}
catch (Exception ex)
{
Console.WriteLine($" FAIL: {ex.Message}");
}
}

If I remove the Optimization section, and just Open then Save, the resulting PDFs are larger than the originals.

var pdfDocument = new Document(sourceFilename);
pdfDocument.Save(targetFilename);

I cannot share the PDFs because of HIPAA.

EDIT: I found the culprit! If I omit “RemoveUnusedStreams = true” option, it does not throw the out-of-memory exception, and appropriately optimizes the documents otherwise.

var optimizationOptions = new Document.OptimizationOptions()
{
LinkDuplcateStreams = true,
RemoveUnusedObjects = true,
//RemoveUnusedStreams = true,
CompressImages = false,
UnembedFonts = false,
};

p.s. I don’t see an “insert code snippet” button on the toolbar above the editor.
1 Like

It’s the RemoveUnusedStreams option. If set to false, no out-of-memory exception.

If set to true, no matter what other options (including everything else to false), it throws exception.

LinkDuplcateStreams = false,
RemoveUnusedObjects = false,
RemoveUnusedStreams = true,
CompressImages = false,
UnembedFonts = false,


robert.kerr:
Thanks. What size PDF can optimize handle? I’m sending PDFs of only 16 MB at it and it throws out-of-memory exceptions.
VS2015 update 3
Aspose.Pdf .NET 11.9

private static void Shrink(string sourceFilename, string targetFilename)
{
try
{
var pdfDocument = new Document(sourceFilename);

pdfDocument.OptimizeResources(new Document.OptimizationOptions()
{
LinkDuplcateStreams = true,
RemoveUnusedObjects = true,
RemoveUnusedStreams = true,
CompressImages = false,
ImageQuality = 10,
UnembedFonts = false,
});

pdfDocument.Save(targetFilename);
}
catch (Exception ex)
{
Console.WriteLine($" FAIL: {ex.Message}");
}
}

If I remove the Optimization section, and just Open then Save, the resulting PDFs are larger than the originals.

var pdfDocument = new Document(sourceFilename);
pdfDocument.Save(targetFilename);
Hi Robert,

Thanks for the acknowledgement.

There is no limitation in terms of PDF size and the reasons you are facing this problem, might be related to the structure and complexity of input document.
robert.kerr:
I cannot share the PDFs because of HIPAA.
We have been using all parameters for Optimization and did not notice any issue. However in order to resolve this problem, we need your input document, so that we can further look into this matter.

For your convenience, I have marked this forum thread as private, so that the contents shared in this thread are only accessible to Aspose staff. Please note that the documents share by our customers are only used for testing purposes and once a particular issue is resolved, they are removed from our system.

robert.kerr:

p.s. I don’t see an “insert code snippet” button on the toolbar above the editor.
Please take a look over following image.

When I click “Quick Reply”, I don’t get the last two buttons, or the My Messages dropdown.


Also, I tried to attach a screenshot, but when I click the “picture” button, for Content Selector, then click on either “Files” or “Pictures”, chrome throws an “error loading callback”.

Try creating a normal forum user and maybe you can see what I’m seeing.

As for the PDF, I’ll need to find a tool that can anonymize it, removing HIPAA text. Suggestions? Something to replace string A with string B would be fine, if it didn’t otherwise alter the document.

robert.kerr:
When I click “Quick Reply”, I don’t get the last two buttons, or the My Messages dropdown.
Hi Robert,

Thanks for your feedback and sorry for the delayed response. Your concerns have been shared with web development team and they are further look into this matter. Now concerning to My Messages, you may not have privileges to define predefined messages.

robert.kerr:
Also, I tried to attach a screenshot, but when I click the “picture” button, for Content Selector, then click on either “Files” or “Pictures”, chrome throws an “error loading callback”.
In order to share the snapshot/image, please try using Add/Update button under File attachment label.


robert.kerr:
As for the PDF, I’ll need to find a tool that can anonymize it, removing HIPAA text. Suggestions? Something to replace string A with string B would be fine, if it didn’t otherwise alter the document.
You may consider using either of the two options to accomplish this requirement.

Hi,

In order to replace text inside PDF file, please follow the instructions specified over Replace Text in a PDF Document