DynaPDF.Optimize
----------------

Optimizes PDF.

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [DynaPDF](component_DynaPDF.md) | [6.0](newinversion60.md) | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |

MBS( "DynaPDF.Optimize"; PDF { ; Flags; Min1BitRes; MinGrayRes; MinColorRes; Res1BitImages; ResGrayImages; ResColorImages; Filter1Bit; FilterGray; FilterColor; JPEGQuality; JP2KQuality; MinLineWidth } ) 

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

### 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 &amp; 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 &amp; 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.   
  
| Name | Value | Description |
|---|---|---|
| Default | 0 | Just rebuild the content streams. |
| InMemory | 1 | Optimize the file fully in memory. Only useful for small PDF files. |
| ConvertAllColors | 2 | If set, Separation, DeviceN, and NChannel color spaces will be converted to the device space. |
| IgnoreICCBased | 4 | If set, ICCBased color spaces will be left unchanged. |
| ScaleImages | 8 | Scale images as specified in the TOptimizeParams structure. |
| SkipMaskedImages | 16 | Meaningful only if ofScaleImages is set. If set, don't scale images with a color mask. |
| NewLinkNames | 32 | If set, rename all object links to short names like F1, F2 etc. |
| DeleteInvPaths | 64 | Delete invisible paths. An invisible path is a path that was finished with the no-op operator "n". |
| FlattenLayers | 128 | Flatten layers if any. |
| DeletePrivateData | 256 | Delete private data objects from pages, templates, and images. |
| DeleteThumbnails | 512 | Thumbnails can be deleted since PDF viewers can create thumbnails easily on demand. |
| DeleteAlternateImages | 1024 | If set, alternate images will be deleted. |
| NoImageSizeCheck | 2048 | Meaningful only if ofScaleImages is set. If set, do not check whether the scaled image is smaller as the original image. |
| IgnoreZeroLineWidth | 4096 | Meaningful 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). |
| AdjZeroLineWidthOnly | 8192 | Meaningful 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 -&gt; LineWidth == 0).  
| CompressWithJBIG2 | 16384 | If set, 1 bit images are compressed with JBIG2 if not already compressed with this filter. |
|---|--:|---|
| NoFilterCheck | 32768 | Meaningful 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.  
| ConvertGrayTo1Bit | 65536 | Useful for scanned faxes since many scanners create gray images for black &amp; white input. Special color spaces like Separation, DeviceN, and NChannel are ignored unless the flag ConvertAllColors is set too. |
|---|--:|---|
| ConvertToGray | 131072 | If set, images, text, and vector graphics are converted to DeviceGray. |
| ConvertToRGB | 262144 | If set, images, text, and vector graphics are converted to DeviceRGB. |
| ConvertToCMYK | 524288 | If set, images, text, and vector graphics are converted to DeviceCMYK. |
| ReplaceJP2KWithJPEG | 1048576 | Meaningful 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. |
| ConvTextToOutlines | 4194304 | If 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. |
| ConvNonEmbFontsOnly | 8388608 | Meaningful only, if the flag ofConvTextToOutlines is set. If set, text of non-embedded fonts are converted to outlines only. Embedded fonts remain embedded. |
| IgnoreDeviceN | 16777216 | If set, DeviceN color spaces are left unchanged. |
| IgnoreNChannel | 33554432 | If set, NChannel color spaces are left unchanged. |
| IgnoreSeparation | 67108864 | If set, Separation color spaces are left unchanged. |
| FailOnOverprinting | 134217728 | Meaningful 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. |
| SkipOverprintedObj | 268435456 | Meaningful 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.  
For all image operation, it is recommended to initialize color management with [DynaPDF.InitColorManagement](DynaPDFInitColorManagement.md) function.  
  
If you have an open page, we close it automatically for you before doing the import.  
See also [Optimize](https://www.monkeybreadsoftware.com/DynaPDF-Manual/Optimize.shtml) 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](DynaPDFIsInitialized.md)") ≠ 1 \]  
 Perform Script \[“InitDynaPDF”\]  
End If  
\#Clear current PDF document   
Set Variable \[$pdf ; Value:MBS ("[DynaPDF.New](DynaPDFNew.md)")\]  
\#For writing to file instead of container, pass path here:   
// Set Variable \[$r ; Value:MBS ("[DynaPDF.OpenOutputFile](DynaPDFOpenOutputFile.md)"; $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](DynaPDFOpenPDFFromFile.md)"; $pdf ; "/Users/cs/Desktop/input.pdf")\]  
 #or container  
 Set Variable \[$r ; Value:MBS ("[DynaPDF.OpenPDFFromContainer](DynaPDFOpenPDFFromContainer.md)"; $pdf ; Merge PDFs::InputPDF )\]  
 #Import all pages  
 Set Variable \[$r ; Value:MBS ("[DynaPDF.ImportPDFFile](DynaPDFImportPDFFile.md)"; $pdf ; $destpage )\]  
 If \[GetAsNumber($r ) &gt;0\]  
 Set Variable \[$destPage ; Value:$r +1\]  
 End If  
 Go to Record/Request/Page \[Next; Exit after last\]  
End Loop  
\#Repair &amp; Optimize PDF   
Set Variable \[$r ; Value:MBS ("DynaPDF.Optimize"; $pdf )\]  
\#Save to container   
Set Field \[Merge PDFs::FinalPDF ; MBS ("[DynaPDF.Save](DynaPDFSave.md)"; $pdf ; "Merged.pdf")\]  
Set Variable \[$r ; Value:MBS ("[DynaPDF.Release](DynaPDFRelease.md)"; $pdf )\]  
Custom function for easy optimization:

 Let (\[   
// Custom Function OptimizePDF(InputPDF)  
//  
// Optimizes PDF and returns either new one or old one in case of error.  
  
PDF = MBS ("[DynaPDF.New](DynaPDFNew.md)");  
OpenResult = MBS ("[DynaPDF.OpenPDFFromContainer](DynaPDFOpenPDFFromContainer.md)"; PDF; InputPDF);  
OpenError = MBS ("IsError");  
ImportResult = MBS ("[DynaPDF.ImportPDFFile](DynaPDFImportPDFFile.md)"; 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](DynaPDFSave.md)"; PDF; GetAsText(InputPDF));  
SaveError = MBS ("IsError");  
ReleaseError = MBS ("[DynaPDF.Release](DynaPDFRelease.md)"; PDF);  
  
Result = If(   
 // all okay?  
 OpenError = 0 and ImportError = 0 and OptimizeError = 0 and SaveError = 0 and MBS ( "[Container.GetSize](ContainerGetSize.md)"; OutputPDF; "PDF " ) &gt; 0;   
 // than return new PDF  
 OutputPDF;  
 // return old PDF  
 InputPDF)  
   
\];  
result )  
Optimizes with files input/output:

 # Initialize DynaPDF if needed   
If \[ MBS ("[DynaPDF.IsInitialized](DynaPDFIsInitialized.md)") ≠ 1 \]   
 Perform Script \[ “InitDynaPDF” \]  
End If  
\# Clear current PDF document   
Set Variable \[ $pdf ; Value: MBS ("[DynaPDF.New](DynaPDFNew.md)") \]   
\# Load PDF from container   
Set Variable \[ $r ; Value: MBS ("[DynaPDF.OpenPDFFromFile](DynaPDFOpenPDFFromFile.md)"; $pdf ; "C:\\Users\\Christian\\Desktop\\171206Y\_C24555\_Annie\_PDFs\\171206Y\_C24555\_Annie\_WOprice.pdf") \]   
Set Variable \[ $r ; Value: MBS ("[DynaPDF.OpenOutputFile](DynaPDFOpenOutputFile.md)"; $pdf ; "C:\\Users\\Christian\\Desktop\\171206Y\_C24555\_Annie\_PDFs\\171206Y\_C24555\_Annie\_WOprice.optimized.pdf") \]   
Set Variable \[ $r ; Value: MBS ("[DynaPDF.ImportPDFFile](DynaPDFImportPDFFile.md)"; $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](DynaPDFSave.md)"; $pdf ; "Merged.pdf") \]   
Set Variable \[ $r ; Value: MBS ("[DynaPDF.Release](DynaPDFRelease.md)"; $pdf ) \]  
Convert all colors to CMYK:

 Set Variable \[ $r ; Value: MBS ("[DynaPDF.InitColorManagement](DynaPDFInitColorManagement.md)"; $pdf ; "DeviceCMYK";  
3;  
"/System/Library/ColorSync/Profiles/Generic Gray Profile.icc";  
"/System/Library/ColorSync/Profiles/Generic RGB Profile.icc";  
"/System/Library/ColorSync/Profiles/Generic CMYK Profile.icc";  
"/System/Library/ColorSync/Profiles/Generic CMYK Profile.icc"  
)\]  
Set Variable \[ $r ; Value: MBS ("DynaPDF.Optimize"; $pdf ; "ConvertToCMYK") \]  
### See also

- [DynaPDF.ImportPDFFile](DynaPDFImportPDFFile.md)
- [DynaPDF.MarkTemplateAsWatermark](DynaPDFMarkTemplateAsWatermark.md)
- [DynaPDF.OpenOutputFile](DynaPDFOpenOutputFile.md)
- [DynaPDF.OpenPDFFromContainer](DynaPDFOpenPDFFromContainer.md)
- [DynaPDF.OpenPDFFromFile](DynaPDFOpenPDFFromFile.md)
- [DynaPDF.Parser.Create](DynaPDFParserCreate.md)
- [DynaPDF.Parser.WriteToPage](DynaPDFParserWriteToPage.md)
- [DynaPDF.Release](DynaPDFRelease.md)
- [DynaPDF.Save](DynaPDFSave.md)
- [IsError](IsError.md)

### Release notes

- **Version 16.3**
    - Added ConvGrayToSauvola to the compression filter flags for DynaPDF.Optimize function.
    - Added DeleteWatermarks, DeleteWatermarkAnnots and UseSauvolaFilter for DynaPDF.Optimize function.
- **Version 11.2**
    - Fixed an issue with [DynaPDF.Optimize](https://www.mbsplugins.eu/DynaPDFOptimize.shtml) complaining about no open output file.
- **Version 9.3**
    - Added flags for [DynaPDF.Optimize](http://www.mbsplugins.eu/DynaPDFOptimize.shtml): ConvertToGray, ConvertToRGB, ConvertToCMYK and ReplaceJP2KWithJPEG.
- **Version 9.0**
    - Changed [DynaPDF.Optimize](http://www.mbsplugins.eu/DynaPDFOptimize.shtml) to use in memory flag by default unless you change it.
- **Version 8.2**
    - Added checks to [DynaPDF.Optimize](http://www.mbsplugins.eu/DynaPDFOptimize.shtml), [DynaPDF.Print](http://www.mbsplugins.eu/DynaPDFPrint.shtml) and [DynaPDF.Save](http://www.mbsplugins.eu/DynaPDFSave.shtml) to complain if no pages are in document.

### Example Databases

- [DynaPDF/Optimize PDF](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/DynaPDF/Optimize%20PDF.shtml#2ScriptAnchor_)

### Blog Entries

- [MBS FileMaker Plugin, version 16.3pr3](https://www.mbsplugins.de/archive/2026-06-22/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)
- [Make PDF certificates in calculation with DynaPDF](https://www.mbsplugins.de/archive/2026-05-18/Make_PDF_certificates_in_calcu/monkeybreadsoftware_blog_filemaker)
- [Retract text from a PDF with DynaPDF](https://www.mbsplugins.de/archive/2025-11-01/Retract_text_from_a_PDF_with_D/monkeybreadsoftware_blog_filemaker)
- [Unlocking the Power of PDFs in FileMaker with DynaPDF and the MBS FileMaker Plugin](https://www.mbsplugins.de/archive/2024-09-16/Unlocking_the_Power_of_PDFs_in/monkeybreadsoftware_blog_filemaker)
- [Can FileMaker do that?](https://www.mbsplugins.de/archive/2024-02-03/Can_FileMaker_do_that/monkeybreadsoftware_blog_filemaker)
- [Things you can do with DynaPDF](https://www.mbsplugins.de/archive/2021-07-02/Things_you_can_do_with_DynaPDF/monkeybreadsoftware_blog_filemaker)
- [Combine MBS functions to automate processes in FileMaker](https://www.mbsplugins.de/archive/2019-04-11/Combine_MBS_functions_to_autom/monkeybreadsoftware_blog_filemaker)
- [Custom function to optimize PDF in FileMaker via DynaPDF](https://www.mbsplugins.de/archive/2016-12-07/Custom_function_to_optimize_PD/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin 6.0 for OS X/Windows](https://www.mbsplugins.de/archive/2016-01-28/MBS_FileMaker_Plugin_60_for_OS/monkeybreadsoftware_blog_filemaker)
- [Optimize PDF Script](https://www.mbsplugins.de/archive/2016-01-19/Optimize_PDF_Script/monkeybreadsoftware_blog_filemaker)

### FileMaker Magazin

- [Ausgabe 5/2021, Seite 28](https://filemaker-magazin.de/neuigkeit/4158-Appetithappen-FMM_202105)

This function checks for a license.

Created 18th January 2016 , last changed 25th February 2026

  
[DynaPDF.OpenTagEx](DynaPDFOpenTagEx.md) - [DynaPDF.PageLink](DynaPDFPageLink.md)

[HTML Version](DynaPDFOptimize.shtml)