# Examples

_Available starting from PD4ML v4.1.1_

All examples are part of the public [`pd4ml-examples`](https://github.com/zxfr/pd4ml-examples) repository on GitHub, under [`src/main/java/com/pd4ml/pdf/optimizer/example/usecases/`](https://github.com/zxfr/pd4ml-examples/tree/master/src/main/java/com/pd4ml/pdf/optimizer/example/usecases), and are ordinary runnable classes with a `main(String[] args)`. Clone the repository; easiest from an IDE: right-click the class, Run. From the command line:

```bash
git clone https://github.com/zxfr/pd4ml-examples.git
cd pd4ml-examples
mvn compile dependency:build-classpath -Dmdep.outputFile=cp.txt
java -cp "target/classes;$(cat cp.txt)" \
     com.pd4ml.pdf.optimizer.example.usecases.<ClassName> <args...>
```

Every example builds its own small sample PDF in memory (no external fixture files needed) and was actually run end-to-end -- these aren't untested snippets.

| #  | Class                                 | Demonstrates                                                                                                                               |
| -- | -------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
| 01 | [`Ex01_BasicCleanup`](https://github.com/zxfr/pd4ml-examples/blob/master/src/main/java/com/pd4ml/pdf/optimizer/example/usecases/Ex01_BasicCleanup.java)                   | The minimal call: clean up a PDF with real orphaned objects from an incremental-update edit. Start here.                                   |
| 02 | [`Ex02_AlreadyCleanIsANoop`](https://github.com/zxfr/pd4ml-examples/blob/master/src/main/java/com/pd4ml/pdf/optimizer/example/usecases/Ex02_AlreadyCleanIsANoop.java)            | Optimizing an already-clean document reports zero removed objects -- a report, not an assumption.                                          |
| 03 | [`Ex03_SharedResourceIsNotDuplicated`](https://github.com/zxfr/pd4ml-examples/blob/master/src/main/java/com/pd4ml/pdf/optimizer/example/usecases/Ex03_SharedResourceIsNotDuplicated.java)  | Two pages sharing one font object still share it after optimizing.                                                                         |
| 04 | [`Ex04_EncryptedInputRequiresPassword`](https://github.com/zxfr/pd4ml-examples/blob/master/src/main/java/com/pd4ml/pdf/optimizer/example/usecases/Ex04_EncryptedInputRequiresPassword.java) | Optimizing an encrypted input needs its password; the output is plain by default.                                                          |
| 05 | [`Ex05_EncryptOutput`](https://github.com/zxfr/pd4ml-examples/blob/master/src/main/java/com/pd4ml/pdf/optimizer/example/usecases/Ex05_EncryptOutput.java)                  | Cleaning a PDF and re-encrypting the output in the same call.                                                                              |
| 06 | [`Ex06_ErrorHandlingPatterns`](https://github.com/zxfr/pd4ml-examples/blob/master/src/main/java/com/pd4ml/pdf/optimizer/example/usecases/Ex06_ErrorHandlingPatterns.java)          | Every [`PdfOptimizeException`](https://pd4ml.com/javadoc/com/pd4ml/pdf/optimizer/PdfOptimizeException.html) failure mode and how to recognize it.                                                                         |
| 07 | [`Ex07_DuplicateContentDeduplication`](https://github.com/zxfr/pd4ml-examples/blob/master/src/main/java/com/pd4ml/pdf/optimizer/example/usecases/Ex07_DuplicateContentDeduplication.java)  | Two originally-distinct but byte-identical font objects are merged into one by default; turning that off with [`deduplicateContent(false)`](https://pd4ml.com/javadoc/com/pd4ml/pdf/optimizer/PdfOptimizer.Options.html#deduplicateContent(boolean)). |
| 08 | [`Ex08_LinearizeForFastWebView`](https://github.com/zxfr/pd4ml-examples/blob/master/src/main/java/com/pd4ml/pdf/optimizer/example/usecases/Ex08_LinearizeForFastWebView.java) | Linearized ("Fast Web View") output with `linearize(true)`: the first page loads before the rest of the file has arrived. PD4ML v4.2.0 and newer. |

For a ready-made command-line tool covering the same ground without writing any code, see the [pd4optimizecli Reference](pd4optimizecli-reference) and run [`com.pd4ml.pdf.optimizer.cli.Pd4OptimizeCli`](https://pd4ml.com/javadoc/com/pd4ml/pdf/optimizer/cli/Pd4OptimizeCli.html).

## See also

* [PD4ML PDF Optimizer Manual](pd4ml-optimize-manual) -- the programmer's manual behind these examples.
* [pd4optimizecli Reference](pd4optimizecli-reference) -- command-line reference for `com.pd4ml.pdf.optimizer.cli.Pd4OptimizeCli`.
* [PD4ML COS API Manual](../pd4ml-pdf-cos/pd4ml-cos-api-manual) / [PD4ML COS Examples](../pd4ml-pdf-cos/pd4ml-cos-examples) -- standalone [`com.pd4ml.pdf.cos`](https://pd4ml.com/javadoc/com/pd4ml/pdf/cos/package-summary.html) foundation this package is built on.
* [PD4ML PDF Merge Manual](../pd4ml-pdf-merge/pd4ml-merge-manual) / [PD4ML Merge Examples](../pd4ml-pdf-merge/pd4ml-merge-examples) -- combining page ranges from multiple PDFs, a natural companion pass.
* [PD4ML XFDF Manual](../pd4ml-pdf-xfdf/pd4ml-xfdf-manual) / [PD4ML XFDF Examples](../pd4ml-pdf-xfdf/pd4ml-xfdf-examples) -- importing annotations/form field values, which offers its own `--optimize` post-import cleanup delegating to this same package.
