Components All New MacOS Windows Linux iOS
Examples Mac & Win Server Client Guides Statistic FMM Blog Deprecated Old

DynaPDF.Optimize

Optimizes PDF.

Component Version macOS Windows Linux Server iOS SDK
DynaPDF 6.0 ✅ Yes ✅ Yes ✅ Yes ✅ Yes ✅ Yes
MBS( "DynaPDF.Optimize"; PDF { ; Flags; Min1BitRes; MinGrayRes; MinColorRes; Res1BitImages; ResGrayImages; ResColorImages; Filter1Bit; FilterGray; FilterColor; JPEGQuality; JP2KQuality; MinLineWidth } )   More

Parameters

Parameter Description Example Flags
PDF The PDF reference. $pdf
Flags The flags to use.
Can be a number with values from dynapdf documentation, see table on this page.
"ConvertAllColors,ScaleImages" Optional
Min1BitRes Minimum resolution before scaling for one bit images.
Default is 100.
Can be zero to disable.
200 Optional
MinGrayRes Minimum resolution before scaling for gray images.
Default is 100.
Can be zero to disable.
200 Optional
MinColorRes Minimum resolution before scaling for color images.
Default is 100.
Can be zero to disable.
200 Optional
Res1BitImages Resolution for 1 bit black & white images.
Default is 150.
150 Optional
ResGrayImages Resolution for gray images.
Default is 150.
150 Optional
ResColorImages Resolution for images with more than one color channel.
Default is 150.
150 Optional
Filter1Bit Filter for black & white images.
Can be Flate, JPEG, CCITT3, CCITT4, LZW, FlateBW or JP2K.
Default is JPEG.
"JPEG" Optional
FilterGray Filter for gray images.
Can be Flate, JPEG, CCITT3, CCITT4, LZW, FlateBW or JP2K.
Default is JPEG.
"JPEG" Optional
FilterColor Filter for multi-channel images.
Can be Flate, JPEG, CCITT3, CCITT4, LZW, FlateBW or JP2K.
Default is JPEG.
"JPEG" Optional
JPEGQuality JPEG quality.
Default is 80.
80 Optional
JP2KQuality JPEG 2000 quality
Default is 80.
80 Optional
MinLineWidth Minimum line width for lines.
Zero means no hair line removal.
0 Optional

Result

Returns OK or error.

Description

Optimizes PDF.
This repairs also a lot of errors in the PDF and can shrink the PDF size.

NameValueDescription
Default0Just rebuild the content streams.
InMemory1Optimize the file fully in memory. Only useful for small PDF files.
ConvertAllColors2If set, Separation, DeviceN, and NChannel color spaces will be converted to the device space.
IgnoreICCBased4If set, ICCBased color spaces will be left unchanged.
ScaleImages8Scale images as specified in the TOptimizeParams structure.
SkipMaskedImages16Meaningful only if ofScaleImages is set. If set, don't scale images with a color mask.
NewLinkNames32If set, rename all object links to short names like F1, F2 etc.
DeleteInvPaths64Delete invisible paths. An invisible path is a path that was finished with the no-op operator "n".
FlattenLayers128Flatten layers if any.
DeletePrivateData256Delete private data objects from pages, templates, and images.
DeleteThumbnails512Thumbnails can be deleted since PDF viewers can create thumbnails easily on demand.
DeleteAlternateImages1024If set, alternate images will be deleted.
NoImageSizeCheck2048Meaningful only if ofScaleImages is set. If set, do not check whether the scaled image is smaller as the original image.
IgnoreZeroLineWidth4096Meaningful only if the parameter MinLineWidth of the TOptimizeParams structure is greater zero. If set, ignore line width operators with a value of zero (zero means one device unit).
AdjZeroLineWidthOnly8192Meaningful only if the parameter MinLineWidth of the TOptimizeParams structure is greater zero.
// If set, change the line width of real hairlines only (a hairline is a one pixel width line -> LineWidth == 0).
CompressWithJBIG216384If set, 1 bit images are compressed with JBIG2 if not already compressed with this filter.
NoFilterCheck32768Meaningful only, if the flag ofCompressWithJBIG2 is set. If set, re-compress all 1 bit images, also if already compressed with JBIG2.
// This flag is mainly a debug flag to compare the compression ratio with other JBIG2 implementations.
ConvertGrayTo1Bit65536Useful for scanned faxes since many scanners create gray images for black & white input. Special color spaces like Separation, DeviceN, and NChannel are ignored unless the flag ConvertAllColors is set too.
ConvertToGray131072If set, images, text, and vector graphics are converted to DeviceGray.
ConvertToRGB262144If set, images, text, and vector graphics are converted to DeviceRGB.
ConvertToCMYK524288If set, images, text, and vector graphics are converted to DeviceCMYK.
ReplaceJP2KWithJPEG1048576Meaningful only, if the flag ofConvertToGray, ofConvertToRGB, or ofConvertToCMYK is set. If set, JPEG 2000 compressed images (which are converted to another color space) will be compressed with JPEG instead JPEG 2000. JPEG 2000 compression is very slow and requires much memory. JPEG compression is around 10 times faster and produces almost identical results.
ConvTextToOutlines4194304If set, texts of non-Type3 fonts are converted to outlines or vector graphics. The resulting file contains no fonts with exception of Type3 fonts, if any. Such files are large but avoid printing issues due to missing or damaged fonts.
ConvNonEmbFontsOnly8388608Meaningful only, if the flag ofConvTextToOutlines is set. If set, text of non-embedded fonts are converted to outlines only. Embedded fonts remain embedded.
IgnoreDeviceN16777216If set, DeviceN color spaces are left unchanged.
IgnoreNChannel33554432If set, NChannel color spaces are left unchanged.
IgnoreSeparation67108864If set, Separation color spaces are left unchanged.
FailOnOverprinting134217728Meaningful only if a color conversion flag is set. If set, the function returns with a fatal error if an object with enabled overprinting was found on the page. The appearance of overprinted objects would change and make the page maybe unusable. Overprinted objects can be left unchanged (set the flag SkipOverprintedObj in this case) or Optimize() can return with a fatal error to avoid the creation of an invalid document.
SkipOverprintedObj268435456Meaningful only if a color conversion flag is set. If set, objects that would be rendered with enabled overprinting are left unchanged to avoid color errors on the page.

Optimizing can fix bug in FileMaker 14 with empty portals resulting in broken PDF files.
Added optimization parameters for version 4 of DynaPDF.

After optimize, please close PDF. If you need to further edit it, please import it again in a new environment.

Requires a Pro license of DynaPDF.

See also Optimize function in DynaPDF manual.

Examples

Optimize by scaling images:

MBS( "DynaPDF.Optimize"; $pdf; "ScaleImages,IgnoreICCBased" )

Optimize with defaults:

MBS( "DynaPDF.Optimize"; $pdf)

Merge PDFs and optimize:

# Initialize DynaPDF if needed
If [ MBS("DynaPDF.IsInitialized") ≠ 1 ]
    Perform Script [“InitDynaPDF”]
End If
#Clear current PDF document
Set Variable [$pdf; Value:MBS("DynaPDF.New")]
#For writing to file instead of container, pass path here:
// Set Variable [$r; Value:MBS("DynaPDF.OpenOutputFile"; $pdf; "/Users/cs/Desktop/test.pdf")]
#Load PDF from container
Go to Record/Request/Page [First]
Set Variable [$destPage; Value:1]
Loop
    #Read from file
    // Set Variable [$r; Value:MBS("DynaPDF.OpenPDFFromFile"; $pdf; "/Users/cs/Desktop/input.pdf")]
    #or container
    Set Variable [$r; Value:MBS("DynaPDF.OpenPDFFromContainer"; $pdf; Merge PDFs::InputPDF)]
    #Import all pages
    Set Variable [$r; Value:MBS("DynaPDF.ImportPDFFile"; $pdf; $destpage)]
    If [GetAsNumber($r) >0]
        Set Variable [$destPage; Value:$r+1]
    End If
    Go to Record/Request/Page [Next; Exit after last]
End Loop
#Repair & Optimize PDF
Set Variable [$r; Value:MBS("DynaPDF.Optimize"; $pdf)]
#Save to container
Set Field [Merge PDFs::FinalPDF; MBS("DynaPDF.Save"; $pdf; "Merged.pdf")]
Set Variable [$r; Value:MBS("DynaPDF.Release"; $pdf)]

Custom function for easy optimization:

# Custom Function OptimizePDF(InputPDF)
#
# Optimizes PDF and returns either new one or old one in case of error.

Let ([
PDF = MBS("DynaPDF.New");
OpenResult = MBS("DynaPDF.OpenPDFFromContainer"; PDF; InputPDF);
OpenError = MBS("IsError");
ImportResult = MBS("DynaPDF.ImportPDFFile"; PDF);
ImportError = MBS("IsError");
Options = "FlattenLayers DeleteInvPaths ScaleImages NewLinkNames DeletePrivateData IgnoreZeroLineWidth DeleteAlternateImages DeleteThumbnails";
OptimizeResult = MBS("DynaPDF.Optimize"; PDF; Options; 50; 50; 50; 150; 150; 150; "JPEG"; "JPEG"; "JPEG");
OptimizeError = MBS("IsError");
OutputPDF = MBS("DynaPDF.Save"; PDF; GetAsText(InputPDF));
SaveError = MBS("IsError");
ReleaseError = MBS("DynaPDF.Release"; PDF);

Result = If(
    // all okay?
    OpenError = 0 and ImportError = 0 and OptimizeError = 0 and SaveError = 0 and MBS( "Container.GetSize"; OutputPDF; "PDF " ) > 0;
    // than return new PDF
    OutputPDF;
    // return old PDF
    InputPDF)
 
];
result )

Optimizes with files input/output:

# Initialize DynaPDF if needed
If [ MBS("DynaPDF.IsInitialized") ≠ 1 ]
    Perform Script [ “InitDynaPDF” ]
End If
# Clear current PDF document
Set Variable [ $pdf ; Value: MBS("DynaPDF.New") ]
# Load PDF from container
Set Variable [ $r ; Value: MBS("DynaPDF.OpenPDFFromFile"; $pdf; "C:\Users\Christian\Desktop\171206Y_C24555_Annie_PDFs\171206Y_C24555_Annie_WOprice.pdf") ]
Set Variable [ $r ; Value: MBS("DynaPDF.OpenOutputFile"; $pdf; "C:\Users\Christian\Desktop\171206Y_C24555_Annie_PDFs\171206Y_C24555_Annie_WOprice.optimized.pdf") ]
Set Variable [ $r ; Value: MBS("DynaPDF.ImportPDFFile"; $pdf; 1) ]
# Optimize
Set Variable [ $r ; Value: MBS("DynaPDF.Optimize"; $pdf; "FlattenLayers¶DeleteInvPaths¶ScaleImages¶NewLinkNames¶DeletePrivateData¶IgnoreZeroLineWidth¶DeleteAlternateImages¶DeleteThumbnails"; 50; 50; 50; 150; 150; 150; "JPEG"; "JPEG"; "JPEG") ]
# Save
Set Field [ Optimize PDF::Output PDF ; MBS("DynaPDF.Save"; $pdf; "Merged.pdf") ]
Set Variable [ $r ; Value: MBS("DynaPDF.Release"; $pdf) ]

See also

Release notes

Example Databases

Blog Entries

This function checks for a license.

Created 18th January 2016, last changed 23th November 2023


DynaPDF.OpenTagEx - DynaPDF.PageLink