public class PdfOptimizationOptions extends Object
Use this class to specify PDF optimization options to apply to the output PDF file.
These optimization options are supported for any input file formats which are supported for export to PDF. For details and code samples, see this page and its children.
Example usage:
try (Viewer viewer = new Viewer("sample.docx")) {
PdfViewOptions viewOptions = new PdfViewOptions();
viewOptions.setPdfOptimizationOptions(new PdfOptimizationOptions());
viewOptions.getPdfOptimizationOptions().setLinearize(true);
viewOptions.getPdfOptimizationOptions().setRemoveAnnotations(true);
viewOptions.getPdfOptimizationOptions().setRemoveFormFields(true);
viewOptions.getPdfOptimizationOptions().setConvertToGrayScale(true);
viewOptions.getPdfOptimizationOptions().setSubsetFonts(true);
viewOptions.getPdfOptimizationOptions().setCompressImages(true);
viewOptions.getPdfOptimizationOptions().setImageQuality(50);
viewOptions.getPdfOptimizationOptions().setResizeImages(true);
viewOptions.getPdfOptimizationOptions().setMaxResolution(50);
viewOptions.getPdfOptimizationOptions().setRemovePrivateInfo(true);
viewOptions.getPdfOptimizationOptions().setUnembedFonts(true);
viewOptions.getPdfOptimizationOptions().setLinkDuplicateStreams(true);
viewOptions.getPdfOptimizationOptions().setAllowReusePageContent(true);
viewer.view(viewOptions);
}
Viewer,
PdfViewOptions| Constructor and Description |
|---|
PdfOptimizationOptions()
Sets up default values of MaxResolution option to 300 and ImageQuality option to 100.
|
| Modifier and Type | Method and Description |
|---|---|
int |
getImageQuality()
If the source PDF file contains images, consider compressing the images and setting their quality.
|
int |
getMaxResolution()
Can be used with CompressImages option to resize the images with a lower resolution.
|
boolean |
isAllowReusePageContent()
If this property is set to true, the page content will be reused when optimizing the document for identical pages.
|
boolean |
isCompressImages()
If the source PDF file contains images, consider compressing the images and setting their quality.
|
boolean |
isConvertToGrayScale()
PDF file is composed of Text, Image, Attachment, Annotations, Graphs and other objects.
|
boolean |
isLinearize()
Enables optimization the output PDF file for viewing online with a web browser.
|
boolean |
isLinkDuplicateStreams()
Sometimes a document contains several identical resource streams (for example images).
|
boolean |
isOptimizeSpreadsheets()
Optimize Excel spreadsheets border lines and fonts for smaller output file size.
|
boolean |
isRemoveAnnotations()
Annotations can be deleted when they are unnecessary.
|
boolean |
isRemoveFormFields()
If the PDF document contains AcroForms, we can try to reduce the file size by flattening form fields.
|
boolean |
isRemovePrivateInfo()
Remove private information (page piece info) if set to true.
|
boolean |
isRemoveUnusedObjects()
A PDF document sometimes contains the PDF objects that are not referenced
from any other object in the document.
|
boolean |
isRemoveUnusedStreams()
Sometimes a document contains unused resource streams.
|
boolean |
isResizeImages()
Can be used with CompressImages option to resize the images with a lower resolution.
|
boolean |
isSubsetFonts()
Every font used to display text on the page contains set of glyphs for font characters.
|
boolean |
isUnembedFonts()
If the document uses embedded fonts it means that all font data is placed in the document.
|
void |
setAllowReusePageContent(boolean allowReusePageContent)
Sets whether to allow reusing page content in the PDF.
|
void |
setCompressImages(boolean compressImages)
Sets whether to compress the images in the PDF.
|
void |
setConvertToGrayScale(boolean convertToGrayScale)
Sets whether to convert the PDF to grayscale.
|
void |
setImageQuality(int imageQuality)
Sets the image quality for compressing the images in the PDF.
|
void |
setLinearize(boolean linearize)
Sets whether to optimize output PDF for online browsing with a web browser.
|
void |
setLinkDuplicateStreams(boolean linkDuplicateStreams)
Sets whether to link duplicate streams in the PDF.
|
void |
setMaxResolution(int maxResolution)
Sets the maximum resolution for resizing the images in the PDF.
|
void |
setOptimizeSpreadsheets(boolean optimizeSpreadsheets)
Sets whether to optimize the spreadsheets in the PDF.
|
void |
setRemoveAnnotations(boolean removeAnnotations)
Sets whether to remove unnecessary annotations from the PDF.
|
void |
setRemoveFormFields(boolean removeFormFields)
Sets whether to remove form fields from the PDF.
|
void |
setRemovePrivateInfo(boolean removePrivateInfo)
Sets whether to remove private information from the PDF.
|
void |
setRemoveUnusedObjects(boolean removeUnusedObjects)
Sets whether to remove unused objects from the PDF.
|
void |
setRemoveUnusedStreams(boolean removeUnusedStreams)
Sets whether to remove unused streams from the PDF.
|
void |
setResizeImages(boolean resizeImages)
Sets whether to resize the images in the PDF.
|
void |
setSubsetFonts(boolean subsetFonts)
Sets whether to subset the fonts in the PDF.
|
void |
setUnembedFonts(boolean unembedFonts)
Sets whether to unembed the embedded fonts in the PDF.
|
public PdfOptimizationOptions()
Sets up default values of MaxResolution option to 300 and ImageQuality option to 100.
For details and code samples, see this page and its children.
public int getImageQuality()
Note: The default value is 100.
To change the image quality, first set the setCompressImages(boolean) property to
public int getMaxResolution()
Can be used with CompressImages option to resize the images with a lower resolution. In this case, we should set CompressImage to true, ResizeImages to true and MaxResolution to the appropriate value.
Note: The default value is 300.
To allow this option, set the setCompressImages(boolean) and setMaxResolution(int) properties to
public boolean isAllowReusePageContent()
If this property is set to true, the page content will be reused when optimizing the document for identical pages.
Note: The default value is false.
true if page content is reused, false otherwise.public boolean isCompressImages()
If the source PDF file contains images, consider compressing the images and setting their quality. In order to enable image compression, set this option to true. All the images in a document will be re-compressed. The compression is defined by the ImageQuality property, which is the value of the quality in percent. 100% is unchanged quality. To decrease image quality, set the ImageQuality property less than 100.
Note: The default value is false.
Use this option to allow other compressing options: setImageQuality(int) and
true if images are compressed, false otherwise.public boolean isConvertToGrayScale()
PDF file is composed of Text, Image, Attachment, Annotations, Graphs and other objects. You may come across a requirement to convert a PDF from RGB colorspace to grayscale so that it would be faster while printing those PDF files. Also when the file is converted to grayscale, the size of the document is also reduced but with this change, the quality of the document may drop. Currently, this feature is supported by the Pre-Flight feature of Adobe Acrobat.
Note: The default value is false.
For code example, see this documentation.
true if the PDF is converted to grayscale, false otherwise.public boolean isLinearize()
Enables optimization the output PDF file for viewing online with a web browser.
For code example, see this documentation.
true if the output PDF is optimized for online browsing, false otherwise.public boolean isLinkDuplicateStreams()
Sometimes a document contains several identical resource streams (for example images). This may happen for example when a document is concatenated with itself. The output document contains two independent copies of the same resource stream. We analyze all resource streams and compare them. If streams are duplicated they are merged, that is, only one copy is left, references are changed appropriately and copies of the object are removed. Sometimes this decreases the document size.
Note: The default value is false.
true if duplicate streams are linked, false otherwise.public boolean isOptimizeSpreadsheets()
Optimize Excel spreadsheets border lines and fonts for smaller output file size.
The output PDF will not include embedded Arial and Times New Roman fonts with characters 32-127.
Note: The default value is false.
For code example, see this documentation.
true if spreadsheets are optimized, false otherwise.public boolean isRemoveAnnotations()
Annotations can be deleted when they are unnecessary. When they are needed but do not require additional editing, they can be flattened. It will reduce the file size.
Note: The default value is false.
For code example, see this documentation.
true if annotations are removed, false otherwise.public boolean isRemoveFormFields()
If the PDF document contains AcroForms, we can try to reduce the file size by flattening form fields.
Note: The default value is false.
For code example, see this documentation.
true if form fields are removed, false otherwise.public boolean isRemovePrivateInfo()
Remove private information (page piece info) if set to true.
Note: The default value is false.
true if private information is removed, false otherwise.public boolean isRemoveUnusedObjects()
A PDF document sometimes contains the PDF objects that are not referenced from any other object in the document. This may happen, for example, when a page is removed from the document page tree but the page object itself isn’t removed. Removing these objects doesn’t make the document invalid but rather shrinks it.
Note: The default value is false.
true if unused objects are removed, false otherwise.public boolean isRemoveUnusedStreams()
Sometimes a document contains unused resource streams. These streams are not “unused objects” because they are referenced from a page’s resource dictionary. This may happen in cases where an image has been removed from the page but not from the page resources. Also, this situation often occurs when pages are extracted from the document and document pages have “common” resources, that is, the same Resources object. Page contents are analyzed in order to determine if a resource stream is used or not. Unused streams are removed. Sometimes this decreases the document size.
Note: The default value is false.
true if unused streams are removed, false otherwise.public boolean isResizeImages()
Can be used with CompressImages option to resize the images with a lower resolution. In this case, we should set CompressImage to true, ResizeImages to true and MaxResolution to the appropriate value.
Note: The default value is false.
To allow this option, set the
true if images are resized, false otherwise.public boolean isSubsetFonts()
Every font used to display text on the page contains set of glyphs for font characters. PDF specification supports "font subset" i.e. font with only those glyphs which are used. This may cause issues when text should be updated (since probably required glyphs are absent in the font), but for the document which is not planned to change this allows to decrease size.
Note: The default value is false.
If the file uses embedded fonts, it contains all font data. GroupDocs.Viewer can subset embedded fonts to reduce the file size. For code example, see this documentation.
true if fonts are subsetted, false otherwise.public boolean isUnembedFonts()
If the document uses embedded fonts it means that all font data is placed in the document. The advantage is that the document is viewable regardless of whether the font is installed on the user’s machine or not. But embedding fonts makes the document larger. The unembed fonts method removes all embedded fonts. This decreases the document size but the document may become unreadable if the correct font is not installed.
Note: The default value is false.
true if embedded fonts are unembedded, false otherwise.public void setAllowReusePageContent(boolean allowReusePageContent)
Sets whether to allow reusing page content in the PDF.
Note: The default value is false.
allowReusePageContent - true to allow reusing page content, false otherwise.public void setCompressImages(boolean compressImages)
Sets whether to compress the images in the PDF.
Note: The default value is false.
Use this option to allow other compressing options: setImageQuality(int) and
compressImages - true to compress the images, false otherwise.public void setConvertToGrayScale(boolean convertToGrayScale)
Sets whether to convert the PDF to grayscale.
Note: The default value is false.
For code example, see this documentation.
convertToGrayScale - true to convert the PDF to grayscale, false otherwise.public void setImageQuality(int imageQuality)
Sets the image quality for compressing the images in the PDF.
Note: The default value is 100.
To change the image quality, first set the setCompressImages(boolean) property to
imageQuality - the image quality (value between 1 and 100).public void setLinearize(boolean linearize)
Sets whether to optimize output PDF for online browsing with a web browser.
For code example, see this documentation.
linearize - true to optimize the output PDF for online browsing, false otherwise.public void setLinkDuplicateStreams(boolean linkDuplicateStreams)
Sets whether to link duplicate streams in the PDF.
Note: The default value is false.
linkDuplicateStreams - true to link duplicate streams, false otherwise.public void setMaxResolution(int maxResolution)
Sets the maximum resolution for resizing the images in the PDF.
Note: The default value is 300.
To allow this option, set the setCompressImages(boolean) and setMaxResolution(int) properties to
maxResolution - the maximum resolution for the images.public void setOptimizeSpreadsheets(boolean optimizeSpreadsheets)
Sets whether to optimize the spreadsheets in the PDF.
For code example, see this documentation.
optimizeSpreadsheets - true to optimize the spreadsheets, false otherwise.public void setRemoveAnnotations(boolean removeAnnotations)
Sets whether to remove unnecessary annotations from the PDF.
Note: The default value is false.
For code example, see this documentation.
removeAnnotations - true to remove unnecessary annotations, false otherwise.public void setRemoveFormFields(boolean removeFormFields)
Sets whether to remove form fields from the PDF.
Note: The default value is false.
For code example, see this documentation.
removeFormFields - true to remove form fields, false otherwise.public void setRemovePrivateInfo(boolean removePrivateInfo)
Sets whether to remove private information from the PDF.
Note: The default value is false.
removePrivateInfo - true to remove private information, false otherwise.public void setRemoveUnusedObjects(boolean removeUnusedObjects)
Sets whether to remove unused objects from the PDF.
Note: The default value is false.
removeUnusedObjects - true to remove unused objects, false otherwise.public void setRemoveUnusedStreams(boolean removeUnusedStreams)
Sets whether to remove unused streams from the PDF.
removeUnusedStreams - true to remove unused streams, false otherwise.public void setResizeImages(boolean resizeImages)
Sets whether to resize the images in the PDF.
Note: The default value is false.
To allow this option, set the
resizeImages - true to resize the images, false otherwise.public void setSubsetFonts(boolean subsetFonts)
Sets whether to subset the fonts in the PDF.
Note: The default value is false.
If the file uses embedded fonts, it contains all font data. GroupDocs.Viewer can subset embedded fonts to reduce the file size. For code example, see this documentation.
subsetFonts - true to subset the fonts, false otherwise.public void setUnembedFonts(boolean unembedFonts)
Sets whether to unembed the embedded fonts in the PDF.
Note: The default value is false.
unembedFonts - true to unembed the embedded fonts, false otherwise.Copyright © 2025. All rights reserved.