Code samples for PDF Optimizer
Product update
The PDF Optimizer has been replaced by the Pdftools SDK.
See the Migrate from the PDF Optimizer to the Pdftools SDK documentation.
PDF Optimizer lets you compress and optimize PDFs for the web, printing, and long-term archiving. Here are some examples of how to integrate the code in your development.
Optimizing
Optimize PDF document for archiving
1// Create the optimizer
2pOptimizer = PdfOptimizeCreateObject();
3
4// Choose the optimization profile for archiving
5PdfOptimizeSetProfile(pOptimizer, eOptimizationProfileArchive);
6
7// Open input file
8if (!PdfOptimizeOpen(pOptimizer, szInputPath, _T("")))
9{
10 _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath,
11 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
12 iReturnValue = 1;
13 goto cleanup;
14}
15
16// Save output file
17if (!PdfOptimizeSaveAs(pOptimizer, szOutputPath, _T(""), _T(""), ePermNoEncryption))
18{
19 _tprintf(_T("Output file %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szOutputPath,
20 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
21 iReturnValue = 1;
22 goto cleanup;
23}
24
25// Close input amd output files
26if (!PdfOptimizeClose(pOptimizer))
27{
28 _tprintf(_T("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szInputPath,
29 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
30 iReturnValue = 1;
31 goto cleanup;
32}
33
1// Create the optimizer
2using (Optimizer optimizer = new Optimizer())
3{
4 // Choose the optimization profile for archiving
5 optimizer.Profile = PDFOptimizationProfile.eOptimizationProfileArchive;
6
7 // Open input file
8 if (!optimizer.Open(inputPath, ""))
9 throw new Exception(String.Format("Input file {0} cannot be opened. " +
10 "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
11
12 // Save output file
13 if (!optimizer.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption))
14 throw new Exception(String.Format("Output file {0} cannot be created. " +
15 "{1} (ErrorCode: 0x{2:x}).", outputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
16
17 // Close input file
18 if (!optimizer.Close())
19 throw new Exception(String.Format("Input file {0} cannot be closed. " +
20 "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
21}
22
1// Create the optimizer
2optimizer = new PdfOptimize();
3
4// Choose the optimization profile for archiving
5optimizer.setProfile(PdfOptimize.OPTIMIZATIONPROFILE.eOptimizationProfileArchive);
6
7// Open input file
8if (!optimizer.open(inputPath, ""))
9 throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
10 inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
11
12// Save output file
13if (!optimizer.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption))
14 throw new IOException(String.format("Output file %s cannot be created. %s (ErrorCode: 0x%08x).",
15 outputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
16
17// Close input file
18if (!optimizer.close())
19 throw new IOException(String.format("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).",
20 inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
21
Optimize PDF document with Mixed Raster Content (MRC)
1// Create the optimizer
2pOptimizer = PdfOptimizeCreateObject();
3
4// Choose the optimization profile for MRC
5PdfOptimizeSetProfile(pOptimizer, eOptimizationProfileMRC);
6
7// Set MRC foreground and background layer resolution
8PdfOptimizeSetMrcLayerResolutionDPI(pOptimizer, fResolution);
9
10// Open input file
11if (!PdfOptimizeOpen(pOptimizer, szInputPath, _T("")))
12{
13 _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath,
14 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
15 iReturnValue = 1;
16 goto cleanup;
17}
18
19// Save output file
20if (!PdfOptimizeSaveAs(pOptimizer, szOutputPath, _T(""), _T(""), ePermNoEncryption))
21{
22 _tprintf(_T("Output file %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szOutputPath,
23 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
24 iReturnValue = 1;
25 goto cleanup;
26}
27
28// Close input file
29if (!PdfOptimizeClose(pOptimizer))
30{
31 _tprintf(_T("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szInputPath,
32 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
33 iReturnValue = 1;
34 goto cleanup;
35}
36
1// Create the optimizer
2using (Optimizer optimizer = new Optimizer())
3{
4 // Choose the optimization profile for MRC
5 optimizer.Profile = PDFOptimizationProfile.eOptimizationProfileMRC;
6
7 // Set MRC foreground and background layer resolution
8 optimizer.MrcLayerResolutionDPI = resolution;
9
10 // Open input file
11 if (!optimizer.Open(inputPath, ""))
12 throw new Exception(String.Format("Input file {0} cannot be opened. " +
13 "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
14
15 // Save output file
16 if (!optimizer.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption))
17 throw new Exception(String.Format("Output file {0} cannot be created. " +
18 "{1} (ErrorCode: 0x{2:x}).", outputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
19
20 // Close input file
21 if (!optimizer.Close())
22 throw new Exception(String.Format("Input file {0} cannot be closed. " +
23 "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
24}
25
1// Create the optimizer
2optimizer = new PdfOptimize();
3
4// Choose the optimization profile for MRC
5optimizer.setProfile(PdfOptimize.OPTIMIZATIONPROFILE.eOptimizationProfileMRC);
6
7// Set MRC foreground and background layer resolution
8optimizer.setMrcLayerResolutionDPI(resolution);
9
10// Open input file
11if (!optimizer.open(inputPath, ""))
12 throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
13 inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
14
15// Save output file
16if (!optimizer.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption))
17 throw new IOException(String.format("Output file %s cannot be created. %s (ErrorCode: 0x%08x).",
18 outputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
19
20// Close input file
21if (!optimizer.close())
22 throw new IOException(String.format("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).",
23 inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
24
Merge and subset fonts from PDF document
1// Create the optimizer
2pOptimizer = PdfOptimizeCreateObject();
3
4// Choose subset and merge embedded fonts
5PdfOptimizeSetSubsetFonts(pOptimizer, 1);
6PdfOptimizeSetMergeEmbeddedFonts(pOptimizer, 1);
7
8// Convert Type1 font programs to CFF (compact font format)
9PdfOptimizeSetConvertToCFF(pOptimizer, 1);
10
11// Open input file
12if (!PdfOptimizeOpen(pOptimizer, szInputPath, _T("")))
13{
14 _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath,
15 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
16 iReturnValue = 1;
17 goto cleanup;
18}
19
20// Save output file
21if (!PdfOptimizeSaveAs(pOptimizer, szOutputPath, _T(""), _T(""), ePermNoEncryption))
22{
23 _tprintf(_T("Output file %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szOutputPath,
24 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
25 iReturnValue = 1;
26 goto cleanup;
27}
28
29// Close input file
30if (!PdfOptimizeClose(pOptimizer))
31{
32 _tprintf(_T("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szInputPath,
33 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
34 iReturnValue = 1;
35 goto cleanup;
36}
37
1// Create the optimizer
2using (Optimizer optimizer = new Optimizer())
3{
4 // Choose subset and merge embedded fonts
5 optimizer.SubsetFonts = true;
6 optimizer.MergeEmbeddedFonts = true;
7
8 // Convert Type1 font programs to CFF (compact font format)
9 optimizer.ConvertToCFF = true;
10
11 // Open input file
12 if (!optimizer.Open(inputPath, ""))
13 throw new Exception(String.Format("Input file {0} cannot be opened. " +
14 "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
15
16 // Save output file
17 if (!optimizer.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption))
18 throw new Exception(String.Format("Output file {0} cannot be created. " +
19 "{1} (ErrorCode: 0x{2:x}).", outputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
20
21 // Close input file
22 if (!optimizer.Close())
23 throw new Exception(String.Format("Input file {0} cannot be closed. " +
24 "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
25}
26
1// Create the optimizer
2optimizer = new PdfOptimize();
3
4// Choose subset and merge embedded fonts
5optimizer.setSubsetFonts(true);
6optimizer.setMergeEmbeddedFonts(true);
7
8// Convert Type1 font programs to CFF (compact font format)
9optimizer.setConvertToCFF(true);
10
11// Open input file
12if (!optimizer.open(inputPath, ""))
13 throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
14 inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
15
16// Save output file
17if (!optimizer.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption))
18 throw new IOException(String.format("Output file %s cannot be created. %s (ErrorCode: 0x%08x).",
19 outputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
20
21// Close input file
22if (!optimizer.close())
23 throw new IOException(String.format("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).",
24 inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
25
Optimize images of PDF document
1// Create the optimizer
2pOptimizer = PdfOptimizeCreateObject();
3
4// Set candidate compression type
5PdfOptimizeSetBitonalCompressions(pOptimizer, eComprAttemptGroup4 | eComprAttemptJBIG2 |
6 eComprAttemptSource);
7PdfOptimizeSetContinuousCompressions(pOptimizer, eComprAttemptJPEG | eComprAttemptJPEG2000 |
8 eComprAttemptSource);
9PdfOptimizeSetIndexedCompressions(pOptimizer, eComprAttemptFlate | eComprAttemptLZW |
10 eComprAttemptSource);
11
12// Set target resolution for down-sampling
13PdfOptimizeSetResolutionDPI(pOptimizer, fResolution);
14
15// Down-sample images if their resolution exceeds 130% of the target resolution
16PdfOptimizeSetThresholdDPI(pOptimizer, 1.3f * PdfOptimizeGetResolutionDPI(pOptimizer));
17
18// Apply a 20% higher resolution to bi-tonal images
19PdfOptimizeSetBitonalResolutionDPI(pOptimizer, 1.2f * fResolution);
20PdfOptimizeSetBitonalThresholdDPI(pOptimizer,
21 1.3f * PdfOptimizeGetBitonalResolutionDPI(pOptimizer));
22
23// Set image quality (for lossy compression types)
24PdfOptimizeSetImageQuality(pOptimizer, iQuality);
25
26// Clip invisible parts of images
27PdfOptimizeSetClipImages(pOptimizer, 1);
28
29// Convert images' color spaces to the least complex possible
30PdfOptimizeSetReduceColorComplexity(pOptimizer, 1);
31
32// Open input file
33if (!PdfOptimizeOpen(pOptimizer, szInputPath, _T("")))
34{
35 _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath,
36 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
37 iReturnValue = 1;
38 goto cleanup;
39}
40
41// Save output file
42if (!PdfOptimizeSaveAs(pOptimizer, szOutputPath, _T(""), _T(""), ePermNoEncryption))
43{
44 _tprintf(_T("Output file %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szOutputPath,
45 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
46 iReturnValue = 1;
47 goto cleanup;
48}
49
50// Close input file
51if (!PdfOptimizeClose(pOptimizer))
52{
53 _tprintf(_T("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szInputPath,
54 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
55 iReturnValue = 1;
56 goto cleanup;
57}
58
1// Create the optimizer
2using (Optimizer optimizer = new Optimizer())
3{
4 // Set candidate compression types
5 optimizer.BitonalCompressions = PDFComprAttempt.eComprAttemptGroup4 |
6 PDFComprAttempt.eComprAttemptJBIG2 | PDFComprAttempt.eComprAttemptSource;
7 optimizer.ContinuousCompressions = PDFComprAttempt.eComprAttemptJPEG |
8 PDFComprAttempt.eComprAttemptJPEG2000 | PDFComprAttempt.eComprAttemptSource;
9 optimizer.IndexedCompressions = PDFComprAttempt.eComprAttemptFlate |
10 PDFComprAttempt.eComprAttemptLZW | PDFComprAttempt.eComprAttemptSource;
11
12 // Set target resolution for down-sampling
13 optimizer.ResolutionDPI = resolution;
14
15 // Down-sample images if their resolution is greater than 130% of the target resolution
16 optimizer.ThresholdDPI = (float)1.3 * optimizer.ResolutionDPI;
17
18 // Apply a higher resolution to bi-tonal images
19 optimizer.BitonalResolutionDPI = (float)1.2 * resolution;
20 optimizer.BitonalThresholdDPI = (float)1.3 * optimizer.BitonalResolutionDPI;
21
22 // Set image quality (for lossy compression types)
23 optimizer.ImageQuality = quality;
24
25 // Clip invisible parts of images
26 optimizer.ClipImages = true;
27
28 // Convert images' color spaces to the least complex possible
29 optimizer.ReduceColorComplexity = true;
30
31 // Open input file
32 if (!optimizer.Open(inputPath, ""))
33 throw new Exception(String.Format("Input file {0} cannot be opened. " +
34 "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
35
36 // Save output file
37 if (!optimizer.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption))
38 throw new Exception(String.Format("Output file {0} cannot be created. " +
39 "{1} (ErrorCode: 0x{2:x}).", outputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
40
41 // Close input file
42 if (!optimizer.Close())
43 throw new Exception(String.Format("Input file {0} cannot be closed. " +
44 "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
45}
46
1// Create the optimizer
2optimizer = new PdfOptimize();
3
4// Set candidate compression types
5optimizer.setBitonalCompressions(NativeLibrary.COMPRESSIONATTEMPT.eComprAttemptGroup4 |
6 NativeLibrary.COMPRESSIONATTEMPT.eComprAttemptJBIG2 |
7 NativeLibrary.COMPRESSIONATTEMPT.eComprAttemptSource);
8optimizer.setContinuousCompressions(NativeLibrary.COMPRESSIONATTEMPT.eComprAttemptJPEG |
9 NativeLibrary.COMPRESSIONATTEMPT.eComprAttemptJPEG2000 |
10 NativeLibrary.COMPRESSIONATTEMPT.eComprAttemptSource);
11optimizer.setIndexedCompressions(NativeLibrary.COMPRESSIONATTEMPT.eComprAttemptFlate |
12 NativeLibrary.COMPRESSIONATTEMPT.eComprAttemptLZW |
13 NativeLibrary.COMPRESSIONATTEMPT.eComprAttemptSource);
14
15// Set target resolution for down-sampling
16optimizer.setResolutionDPI(resolution);
17
18// Down-sample images if their resolution is greater than 130% of the target resolution
19optimizer.setThresholdDPI((float)1.3 * resolution);
20
21// Apply a higher resolution to bitonal images
22optimizer.setBitonalResolutionDPI((float)1.2 * resolution);
23optimizer.setBitonalThresholdDPI((float)1.3 * optimizer.getBitonalResolutionDPI());
24
25// Set image quality (for lossy compression type)
26optimizer.setImageQuality(quality);
27
28// Clip invisible parts of images
29optimizer.setClipImages(true);
30
31// Convert images' color spaces to the least complex possible
32optimizer.setReduceColorComplexity(true);
33
34// Open input file
35if (!optimizer.open(inputPath, ""))
36 throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
37 inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
38
39// Save output file
40if (!optimizer.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption))
41 throw new IOException(String.format("Output file %s cannot be created. %s (ErrorCode: 0x%08x).",
42 outputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
43
44// Close input file
45if (!optimizer.close())
46 throw new IOException(String.format("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).",
47 inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
48
Optimize PDF document for printing
1// Create the optimizer
2pOptimizer = PdfOptimizeCreateObject();
3
4// Choose the optimization profile for printing
5PdfOptimizeSetProfile(pOptimizer, eOptimizationProfilePrint);
6
7// Open input file
8if (!PdfOptimizeOpen(pOptimizer, szInputPath, _T("")))
9{
10 _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath,
11 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
12 iReturnValue = 1;
13 goto cleanup;
14}
15
16// Save output file
17if (!PdfOptimizeSaveAs(pOptimizer, szOutputPath, _T(""), _T(""), ePermNoEncryption))
18{
19 _tprintf(_T("Output file %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szOutputPath,
20 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
21 iReturnValue = 1;
22 goto cleanup;
23}
24
25// Close input file
26if (!PdfOptimizeClose(pOptimizer))
27{
28 _tprintf(_T("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szInputPath,
29 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
30 iReturnValue = 1;
31 goto cleanup;
32}
33
1// Create the optimizer
2using (Optimizer optimizer = new Optimizer())
3{
4 // Choose the optimization profile for printing
5 optimizer.Profile = PDFOptimizationProfile.eOptimizationProfilePrint;
6
7 // Open input file
8 if (!optimizer.Open(inputPath, ""))
9 throw new Exception(String.Format("Input file {0} cannot be opened. " +
10 "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
11
12 // Save output file
13 if (!optimizer.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption))
14 throw new Exception(String.Format("Output file {0} cannot be created. {1} " +
15 "(ErrorCode: 0x{2:x}).", outputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
16
17 // Close input file
18 if (!optimizer.Close())
19 throw new Exception(String.Format("Input file {0} cannot be closed. {1} " +
20 "(ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
21}
22
1// Create the optimizer
2optimizer = new PdfOptimize();
3
4// Choose the optimization profile for printing
5optimizer.setProfile(PdfOptimize.OPTIMIZATIONPROFILE.eOptimizationProfilePrint);
6
7// Open input file
8if (!optimizer.open(inputPath, ""))
9 throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
10 inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
11
12// Save output file
13if (!optimizer.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption))
14 throw new IOException(String.format("Output file %s cannot be created. %s (ErrorCode: 0x%08x).",
15 outputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
16
17// Close input file
18if (!optimizer.close())
19 throw new IOException(String.format("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).",
20 inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
21
Optimize PDF document for web
1// Create the optimizer
2pOptimizer = PdfOptimizeCreateObject();
3
4// Choose the optimization profile for web/e-mail
5PdfOptimizeSetProfile(pOptimizer, eOptimizationProfileWeb);
6
7// Disable auto-linearization
8PdfOptimizeSetAutoLinearize(pOptimizer, 0);
9
10// Set linearization
11PdfOptimizeSetLinearize(pOptimizer, bLinearize);
12
13// Open input file
14if (!PdfOptimizeOpen(pOptimizer, szInputPath, _T("")))
15{
16 _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath,
17 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
18 iReturnValue = 1;
19 goto cleanup;
20}
21
22// Save output file
23if (!PdfOptimizeSaveAs(pOptimizer, szOutputPath, _T(""), _T(""), ePermNoEncryption))
24{
25 _tprintf(_T("Output file %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szOutputPath,
26 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
27 iReturnValue = 1;
28 goto cleanup;
29}
30
31// Close input file
32if (!PdfOptimizeClose(pOptimizer))
33{
34 _tprintf(_T("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szInputPath,
35 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
36 iReturnValue = 1;
37 goto cleanup;
38}
39
1// Create the optimizer
2using (Optimizer optimizer = new Optimizer())
3{
4 // Choose the optimization profile for web/e-mail
5 optimizer.Profile = PDFOptimizationProfile.eOptimizationProfileWeb;
6
7 // Disable auto-linearization
8 optimizer.AutoLinearize = false;
9
10 // Set linearization
11 optimizer.Linearize = linearize;
12
13 // Open input file
14 if (!optimizer.Open(inputPath, ""))
15 throw new Exception(String.Format("Input file {0} cannot be opened. " +
16 "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
17
18 // Save output file
19 if (!optimizer.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption))
20 throw new Exception(String.Format("Output file {0} cannot be created. " +
21 "{1} (ErrorCode: 0x{2:x}).", outputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
22
23 // Close input file
24 if (!optimizer.Close())
25 throw new Exception(String.Format("Input file {0} cannot be closed. " +
26 "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
27}
28
1// Create the optimizer
2optimizer = new PdfOptimize();
3
4// Choose the optimization profile for web/e-mail
5optimizer.setProfile(PdfOptimize.OPTIMIZATIONPROFILE.eOptimizationProfileWeb);
6
7// Disable auto-linearization
8optimizer.setAutoLinearize(false);
9
10// Set linearization
11optimizer.setLinearize(linearize);
12
13// Open input file
14if (!optimizer.open(inputPath, ""))
15 throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
16 inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
17
18// Save output file
19if (!optimizer.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption))
20 throw new IOException(String.format("Output file %s cannot be created. %s (ErrorCode: 0x%08x).",
21 outputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
22
23// Close input file
24if (!optimizer.close())
25 throw new IOException(String.format("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).",
26 inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
27
Removing
Flatten annotations of PDF document
1// Create the optimizer
2pOptimizer = PdfOptimizeCreateObject();
3
4// Flatten and remove annotations and form fields
5PdfOptimizeSetStrip(pOptimizer, eStripAnnots | eStripFormsAnnots);
6
7// Open input file
8if (!PdfOptimizeOpen(pOptimizer, szInputPath, _T("")))
9{
10 _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath,
11 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
12 iReturnValue = 1;
13 goto cleanup;
14}
15
16// Save output file
17if (!PdfOptimizeSaveAs(pOptimizer, szOutputPath, _T(""), _T(""), ePermNoEncryption))
18{
19 _tprintf(_T("Output file %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szOutputPath,
20 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
21 iReturnValue = 1;
22 goto cleanup;
23}
24
25// Close input file
26if (!PdfOptimizeClose(pOptimizer))
27{
28 _tprintf(_T("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szInputPath,
29 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
30 iReturnValue = 1;
31 goto cleanup;
32}
33
1// Create the optimizer
2using (Optimizer optimizer = new Optimizer())
3{
4 // Flatten and remove annotations and form fields
5 optimizer.Strip = PDFStripType.eStripAnnots | PDFStripType.eStripFormsAnnots;
6
7 // Open input file
8 if (!optimizer.Open(inputPath, ""))
9 throw new Exception(String.Format("Input file {0} cannot be opened. " +
10 "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
11
12 // Save output file
13 if (!optimizer.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption))
14 throw new Exception(String.Format("Output file {0} cannot be created. " +
15 "{1} (ErrorCode: 0x{2:x}).", outputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
16
17 // Close input file
18 if (!optimizer.Close())
19 throw new Exception(String.Format("Input file {0} cannot be closed. " +
20 "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
21}
22
1// Create the optimizer
2optimizer = new PdfOptimize();
3
4// Flatten and remove annotations and form fields
5optimizer.setStrip(NativeLibrary.STRIPTYPE.eStripAnnots |
6 NativeLibrary.STRIPTYPE.eStripFormsAnnots);
7
8// Open input file
9if (!optimizer.open(inputPath, ""))
10 throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
11 inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
12
13// Save output file
14if (!optimizer.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption))
15 throw new IOException(String.format("Output file %s cannot be created. %s (ErrorCode: 0x%08x).",
16 outputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
17
18// Close input file
19if (!optimizer.close())
20 throw new IOException(String.format("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).",
21 inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
22
Remove additional information of PDF
1// Create the optimizer
2pOptimizer = PdfOptimizeCreateObject();
3
4// Remove additional information at one's option
5PdfOptimizeSetStrip(pOptimizer, eStripAlternates | eStripMetadata | eStripOutputIntents |
6 eStripStructTree | eStripThumb);
7
8// Open input file
9if (!PdfOptimizeOpen(pOptimizer, szInputPath, _T("")))
10{
11 _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath,
12 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
13 iReturnValue = 1;
14 goto cleanup;
15}
16
17// Save output file
18if (!PdfOptimizeSaveAs(pOptimizer, szOutputPath, _T(""), _T(""), ePermNoEncryption))
19{
20 _tprintf(_T("Output file %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szOutputPath,
21 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
22 iReturnValue = 1;
23 goto cleanup;
24}
25
26// Close input file
27if (!PdfOptimizeClose(pOptimizer))
28{
29 _tprintf(_T("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szInputPath,
30 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
31 iReturnValue = 1;
32 goto cleanup;
33}
34
1// Create the optimizer
2using (Optimizer optimizer = new Optimizer())
3{
4 // Configure which additional information to remove
5 optimizer.Strip = PDFStripType.eStripAlternates | PDFStripType.eStripMetadata |
6 PDFStripType.eStripOutputIntents | PDFStripType.eStripStructTree | PDFStripType.eStripThumb;
7
8 // Open input file
9 if (!optimizer.Open(inputPath, ""))
10 throw new Exception(String.Format("Input file {0} cannot be opened. " +
11 "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
12
13 // Save output file
14 if (!optimizer.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption))
15 throw new Exception(String.Format("Output file {0} cannot be created. " +
16 "{1} (ErrorCode: 0x{2:x}).", outputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
17
18 // Close input file
19 if (!optimizer.Close())
20 throw new Exception(String.Format("Input file {0} cannot be closed. " +
21 "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
22}
23
1// Create the optimizer
2optimizer = new PdfOptimize();
3
4// Configure which additional information to remove
5optimizer.setStrip(NativeLibrary.STRIPTYPE.eStripAlternates |
6 NativeLibrary.STRIPTYPE.eStripMetadata | NativeLibrary.STRIPTYPE.eStripOutputIntents |
7 NativeLibrary.STRIPTYPE.eStripStructTree | NativeLibrary.STRIPTYPE.eStripThumb);
8
9// Open input file
10if (!optimizer.open(inputPath, ""))
11 throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
12 inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
13
14// Save output file
15if (!optimizer.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption))
16 throw new IOException(String.format("Output file %s cannot be created. %s (ErrorCode: 0x%08x).",
17 outputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
18
19// Close input file
20if (!optimizer.close())
21 throw new IOException(String.format("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).",
22 inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
23
Features and Configuration
Encrypt document with user or owner password
1// Create the optimizer
2pOptimizer = PdfOptimizeCreateObject();
3
4// Open input file
5if (!PdfOptimizeOpen(pOptimizer, szInputPath, _T("")))
6{
7 _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath,
8 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
9 iReturnValue = 1;
10 goto cleanup;
11}
12
13// Save output file, provide user and owner password
14if (!PdfOptimizeSaveAs(pOptimizer, szOutputPath, _T(szUserPassword), _T(szOwnerPassword),
15 ePermAnnotate))
16{
17 _tprintf(_T("Output file %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szOutputPath,
18 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
19 iReturnValue = 1;
20 goto cleanup;
21}
22
23// Close input file
24if (!PdfOptimizeClose(pOptimizer))
25{
26 _tprintf(_T("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szInputPath,
27 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
28 iReturnValue = 1;
29 goto cleanup;
30}
31
1// Create the optimizer
2using (Optimizer optimizer = new Optimizer())
3{
4 // Open input file
5 if (!optimizer.Open(inputPath, ""))
6 throw new Exception(String.Format("Input file {0} cannot be opened. " +
7 "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
8
9 // Save output file, provide user and owner password
10 if (!optimizer.SaveAs(outputPath, userPassword, ownerPassword, PDFPermission.ePermAnnotate))
11 throw new Exception(String.Format("Output file {0} cannot be created. " +
12 "{1} (ErrorCode: 0x{2:x}).", outputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
13
14 // Close input file
15 if (!optimizer.Close())
16 throw new Exception(String.Format("Input file {0} cannot be closed. " +
17 "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
18}
19
1// Create the optimizer
2optimizer = new PdfOptimize();
3
4// Open input file
5if (!optimizer.open(inputPath, ""))
6 throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
7 inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
8
9// Save output file, provide user and owner password
10if (!optimizer.saveAs(outputPath, userPassword, ownerPassword,
11 NativeLibrary.PERMISSION.ePermAnnotate)) throw new IOException(String.format("Output file %s " +
12 "cannot be created. %s (ErrorCode: 0x%08x).", outputPath, optimizer.getErrorMessage(),
13 optimizer.getErrorCode()));
14
15// Close input file
16if (!optimizer.close())
17 throw new IOException(String.format("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).",
18 inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
19
Extract images and font files from PDF
1// Create the optimizer
2pOptimizer = PdfOptimizeCreateObject();
3
4// Set extract fonts and images
5PdfOptimizeSetExtractFonts(pOptimizer, 1);
6PdfOptimizeSetExtractImages(pOptimizer, 1);
7
8// Open input file
9if (!PdfOptimizeOpen(pOptimizer, szInputPath, _T("")))
10{
11 _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath,
12 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
13 iReturnValue = 1;
14 goto cleanup;
15}
16
17// Extract fonts and images
18if (!PdfOptimizeSaveAs(pOptimizer, szOutputPath, _T(""), _T(""), ePermNoEncryption))
19{
20 _tprintf(_T("Fonts and images cannot be extracted. %s (ErrorCode: 0x%08x).\n"),
21 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
22 iReturnValue = 1;
23 goto cleanup;
24}
25
26// Close input file
27if (!PdfOptimizeClose(pOptimizer))
28{
29 _tprintf(_T("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szInputPath,
30 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
31 iReturnValue = 1;
32 goto cleanup;
33}
34
1// Create the optimizer
2using (Optimizer optimizer = new Optimizer())
3{
4 // Set extract fonts and images
5 optimizer.ExtractFonts = true;
6 optimizer.ExtractImages = true;
7
8 // Open input file
9 if (!optimizer.Open(inputPath, ""))
10 throw new Exception(String.Format("Input file {0} cannot be opened. " +
11 "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
12
13 // Extract fonts and images
14 if (!optimizer.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption))
15 throw new Exception(String.Format("Fonts and images could not be extracted. " +
16 "{0} (ErrorCode: 0x{1:x}).", optimizer.ErrorMessage, optimizer.ErrorCode));
17
18 // Close input file
19 if (!optimizer.Close())
20 throw new Exception(String.Format("Input file {0} cannot be closed. " +
21 "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
22}
23
1// Create the optimizer
2optimizer = new PdfOptimize();
3
4// Set extract fonts and images
5optimizer.setExtractFonts(true);
6optimizer.setExtractImages(true);
7
8// Open input file
9if (!optimizer.open(inputPath, ""))
10 throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
11 inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
12
13// Extract fonts and images
14if (!optimizer.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption))
15 throw new IOException(String.format("Fonts and images could not be extracted. %s " +
16 "(ErrorCode: 0x%08x).", outputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
17
18// Close input file
19if (!optimizer.close())
20 throw new IOException(String.format("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).",
21 inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
22
Set document info of a PDF document
1// Create the optimizer
2pOptimizer = PdfOptimizeCreateObject();
3
4// Set author, title, subject, creator and producer of PDF
5PdfOptimizeSetInfoEntry(pOptimizer, _T("Author"), _T(szAuthor));
6PdfOptimizeSetInfoEntry(pOptimizer, _T("Title"), _T(szTitle));
7PdfOptimizeSetInfoEntry(pOptimizer, _T("Subject"), _T(szSubject));
8PdfOptimizeSetInfoEntry(pOptimizer, _T("Creator"), _T(szCreator));
9PdfOptimizeSetInfoEntry(pOptimizer, _T("Producer"), _T(szProducer));
10
11// Open input file
12if (!PdfOptimizeOpen(pOptimizer, szInputPath, _T("")))
13{
14 _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath,
15 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
16 iReturnValue = 1;
17 goto cleanup;
18}
19
20// Save output file
21if (!PdfOptimizeSaveAs(pOptimizer, szOutputPath, _T(""), _T(""), ePermNoEncryption))
22{
23 printf(_T("Output file %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szOutputPath,
24 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
25 iReturnValue = 1;
26 goto cleanup;
27}
28
29// Close input file
30if (!PdfOptimizeClose(pOptimizer))
31{
32 _tprintf(_T("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szInputPath,
33 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
34 iReturnValue = 1;
35 goto cleanup;
36}
37
1// Create the optimizer
2using (Optimizer optimizer = new Optimizer())
3{
4 // Set author, title, subject, creator and producer of PDF
5 optimizer.SetInfoEntry("Author", author);
6 optimizer.SetInfoEntry("Title", title);
7 optimizer.SetInfoEntry("Subject", subject);
8 optimizer.SetInfoEntry("Creator", creator);
9 optimizer.SetInfoEntry("Producer", producer);
10
11 // Open input file
12 if (!optimizer.Open(inputPath, ""))
13 throw new Exception(String.Format("Input file {0} cannot be opened. " +
14 "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
15
16 // Save output file
17 if (!optimizer.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption))
18 throw new Exception(String.Format("Output file {0} cannot be created. " +
19 "{1} (ErrorCode: 0x{2:x}).", outputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
20
21 // Close input file
22 if (!optimizer.Close())
23 throw new Exception(String.Format("Input file {0} cannot be closed. " +
24 "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
25}
26
1// Create the optimizer
2optimizer = new PdfOptimize();
3
4// Set author, title, creator and producer of PDF
5optimizer.setInfoEntry("Author", author);
6optimizer.setInfoEntry("Title", title);
7optimizer.setInfoEntry("Subject", subject);
8optimizer.setInfoEntry("Creator", creator);
9optimizer.setInfoEntry("Producer", producer);
10
11// Open input file
12if (!optimizer.open(inputPath, ""))
13 throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
14 inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
15
16// Save output file
17if (!optimizer.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption))
18 throw new IOException(String.format("Output file %s cannot be created. %s (ErrorCode: 0x%08x).",
19 outputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
20
21// Close input file
22if (!optimizer.close())
23 throw new IOException(String.format("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).",
24 inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
25
Upgrade the PDF version
1// Create the optimizer
2pOptimizer = PdfOptimizeCreateObject();
3
4// Set minimum version
5if (!PdfOptimizeSetVersion(pOptimizer, szVersion))
6{
7 _tprintf(_T("The version %s of the output file %s cannot be set. %s (ErrorCode: 0x%08x).\n"),
8 szVersion, szOutputPath, PdfOptimizeGetErrorMessage(pOptimizer),
9 PdfOptimizeGetErrorCode(pOptimizer));
10 iReturnValue = 1;
11 goto cleanup;
12}
13
14// Open input file
15if (!PdfOptimizeOpen(pOptimizer, szInputPath, _T("")))
16{
17 _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath,
18 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
19 iReturnValue = 1;
20 goto cleanup;
21}
22
23// Save output file
24if (!PdfOptimizeSaveAs(pOptimizer, szOutputPath, _T(""), _T(""), ePermNoEncryption))
25{
26 _tprintf(_T("Output file %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szOutputPath,
27 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
28 iReturnValue = 1;
29 goto cleanup;
30}
31
32// Close input file
33if (!PdfOptimizeClose(pOptimizer))
34{
35 _tprintf(_T("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szInputPath,
36 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
37 iReturnValue = 1;
38 goto cleanup;
39}
40
1// Create the optimizer
2using (Optimizer optimizer = new Optimizer())
3{
4 // Set minimum version of output file
5 if (!optimizer.SetVersion(version))
6 throw new Exception(String.Format("The version {0} of the output file {1} cannot be set. " +
7 "{2} (ErrorCode: 0x{3:x}).", version, outputPath, optimizer.ErrorMessage,
8 optimizer.ErrorCode));
9
10 // Open input file
11 if (!optimizer.Open(inputPath, ""))
12 throw new Exception(String.Format("Input file {0} cannot be opened. " +
13 "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
14
15 // Save output file
16 if (!optimizer.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption))
17 throw new Exception(String.Format("Output file {0} cannot be created. " +
18 "{1} (ErrorCode: 0x{2:x}).", outputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
19
20 // Close input file
21 if (!optimizer.Close())
22 throw new Exception(String.Format("Input file {0} cannot be closed. " +
23 "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
24}
25
1// Create the optimizer
2optimizer = new PdfOptimize();
3
4// Set minimum version of output file
5if (!optimizer.setVersion(version))
6 throw new IOException(String.format("The version %s of the output file %s cannot be set. " +
7 "%s (ErrorCode: 0x%08x).", version, outputPath, optimizer.getErrorMessage(),
8 optimizer.getErrorCode()));
9
10// Open input file
11if (!optimizer.open(inputPath, ""))
12 throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
13 inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
14
15// Save output file
16if (!optimizer.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption))
17 throw new IOException(String.format("Output file %s cannot be created. %s (ErrorCode: 0x%08x).",
18 outputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
19
20// Close input file
21if (!optimizer.close())
22 throw new IOException(String.format("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).",
23 inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
24
In Memory
Optimize PDF in-memory
1// Create the optimizer
2pOptimizer = PdfOptimizeCreateObject();
3
4// Open input PDF from memory buffer
5if (!PdfOptimizeOpenMem(pOptimizer, pInputBuffer, nLength, _T("")))
6{
7 _tprintf(_T("Input buffer cannot be opened in-memory. %s (ErrorCode: 0x%08x).\n"),
8 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
9 iReturnValue = 1;
10 goto cleanup;
11}
12
13// Save output file in-memory
14if (!PdfOptimizeSaveInMemory(pOptimizer))
15{
16 _tprintf(_T("Output document cannot be saved in-memory. %s (ErrorCode: 0x%08x).\n"),
17 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
18 iReturnValue = 1;
19 goto cleanup;
20}
21
22// Close input file
23if (!PdfOptimizeClose(pOptimizer))
24{
25 _tprintf(_T("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szInputPath,
26 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
27 iReturnValue = 1;
28 goto cleanup;
29}
30
31// Create output file
32if ((pOutputFile = _tfopen(szOutputPath, _T("wb"))) == NULL)
33{
34 _tprintf(_T("Output file %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szOutputPath,
35 PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
36 iReturnValue = 1;
37 goto cleanup;
38}
39
40// Get the PDF as a memory buffer
41pOutputBuffer = PdfOptimizeGetPdf(pOptimizer);
42// Write bytes to output file
43if (fwrite(pOutputBuffer->m_pData, 1, pOutputBuffer->m_nLength, pOutputFile) != pOutputBuffer->m_nLength)
44{
45 _tprintf(_T("Failed to write data to output file %s.\n"), szOutputPath);
46 iReturnValue = 1;
47 goto cleanup;
48}
49fclose(pOutputFile);
50pOutputFile = NULL;
51
1// Create the optimizer
2using (Optimizer optimizer = new Optimizer())
3{
4 // Open input buffer in-memory
5 if (!optimizer.OpenMem(inputBuffer, ""))
6 throw new Exception(String.Format("Input buffer cannot be opened in-memory. " +
7 "{0} (ErrorCode: 0x{1:x}).", optimizer.ErrorMessage, optimizer.ErrorCode));
8
9 // Save output file in-memory
10 if (!optimizer.SaveInMemory())
11 throw new Exception(String.Format("Output document cannot be saved in-memory. " +
12 "{0} (ErrorCode: 0x{1:x}).", optimizer.ErrorMessage, optimizer.ErrorCode));
13
14 // Close input file
15 if (!optimizer.Close())
16 throw new Exception(String.Format("Input file {0} cannot be closed. " +
17 "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
18
19 // Get PDF as buffer
20 byte[] outputBuffer = optimizer.GetPdf();
21 if (outputBuffer == null)
22 throw new Exception(String.Format("Output file {0} cannot be created. " +
23 "{1} (ErrorCode: 0x{2:x}).", outputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
24
25 // Write bytes to output file.
26 File.WriteAllBytes(outputPath, outputBuffer);
27}
28
1// Create the optimizer
2optimizer = new PdfOptimize();
3
4// Open input buffer in-memory
5if (!optimizer.openMem(inputBuffer, ""))
6 throw new IOException(String.format("Input buffer cannot be opened in-memory. " +
7 "%s (ErrorCode: 0x%08x).", optimizer.getErrorMessage(), optimizer.getErrorCode()));
8
9// Save output file in-memory
10if (!optimizer.saveInMemory())
11 throw new IOException(String.format("Output document cannot be saved in-memory. " +
12 "%s (ErrorCode: 0x%08x).", optimizer.getErrorMessage(), optimizer.getErrorCode()));
13
14// Close input file
15if (!optimizer.close())
16 throw new IOException(String.format("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).",
17 inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
18
19// Get PDF as buffer
20byte[] outputBuffer = optimizer.getPdf();
21if (outputBuffer == null)
22 throw new IOException(String.format("Output file %s cannot be created. %s (ErrorCode: 0x%08x).",
23 outputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
24
25// Write bytes to output file
26Files.write(Paths.get(outputPath), outputBuffer, StandardOpenOption.CREATE_NEW);
27