Sample C# code for using Apryse SDK to reduce PDF file size by removing redundant information and compressing data streams using the latest in image compression technology. Learn more about our UWP SDK.
1//
2// Copyright (c) 2001-2020 by PDFTron Systems Inc. All Rights Reserved.
3//
4
5using System;
6using System.Collections.Generic;
7using System.IO;
8using System.Threading.Tasks;
9using Windows.Foundation;
10
11using pdftron.PDF;
12using pdftron.SDF;
13
14using PDFNetUniversalSamples.ViewModels;
15
16namespace PDFNetSamples
17{
18 public sealed class OptimizerTest : Sample
19 {
20 public OptimizerTest() :
21 base("Optimizer", "The sample shows how to use 'pdftron.PDF.Optimizer' to reduce PDF file size by reducing the file size, removing redundant information, and compressing data streams using the latest in image compression technology. 'pdftron.PDF.Optimizer' is an optional Add-On to PDFNet Core SDK.")
22 {
23 }
24
25 public override IAsyncAction RunAsync()
26 {
27 return Task.Run(new System.Action(async () => {
28 WriteLine("--------------------------------");
29 WriteLine("Starting Optimizer Test...");
30 WriteLine("--------------------------------\n");
31 //--------------------------------------------------------------------------------
32 // Example 1) Simple optimization of a pdf with default settings.
33 //
34 try
35 {
36 using (PDFDoc doc = new PDFDoc(Path.Combine(InputPath, "newsletter.pdf")))
37 {
38 doc.InitSecurityHandler();
39 Optimizer.Optimize(doc);
40 String output_file_path = Path.Combine(OutputPath, "newsletter_opt1.pdf");
41 WriteLine("Saving " + output_file_path);
42 await doc.SaveAsync(output_file_path, SDFDocSaveOptions.e_linearized);
43 WriteLine("Done. Results saved in " + output_file_path);
44 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
45 }
46 }
47 catch (Exception e)
48 {
49 WriteLine(GetExceptionMessage(e));
50 }
51
52 //--------------------------------------------------------------------------------
53 // Example 2) Reduce image quality and use jpeg compression for
54 // non monochrome images.
55 try
56 {
57 using (PDFDoc doc = new PDFDoc(Path.Combine(InputPath, "newsletter.pdf")))
58 {
59 doc.InitSecurityHandler();
60
61 OptimizerImageSettings image_settings = new OptimizerImageSettings();
62
63 // low quality jpeg compression
64 image_settings.SetCompressionMode(OptimizerImageSettingsCompressionMode.e_jpeg);
65 image_settings.SetQuality(1);
66
67 // Set the output dpi to be standard screen resolution
68 image_settings.SetImageDPI(144, 96);
69
70 // this option will recompress images not compressed with
71 // jpeg compression and use the result if the new image
72 // is smaller.
73 image_settings.ForceRecompression(true);
74
75 // this option is not commonly used since it can
76 // potentially lead to larger files. It should be enabled
77 // only if the output compression specified should be applied
78 // to every image of a given type regardless of the output image size
79 //image_settings.ForceChanges(true);
80
81 // use the same settings for both color and grayscale images
82 OptimizerOptimizerSettings opt_settings = new OptimizerOptimizerSettings();
83 opt_settings.SetColorImageSettings(image_settings);
84 opt_settings.SetGrayscaleImageSettings(image_settings);
85
86
87 Optimizer.Optimize(doc, opt_settings);
88
89 String output_file_path = Path.Combine(OutputPath, "newsletter_opt2.pdf");
90 WriteLine("Saving " + output_file_path);
91 await doc.SaveAsync(output_file_path, SDFDocSaveOptions.e_linearized);
92 WriteLine("Done. Results saved in " + output_file_path);
93 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
94 }
95 }
96 catch (Exception e)
97 {
98 WriteLine(GetExceptionMessage(e));
99 }
100
101 //--------------------------------------------------------------------------------
102 // Example 3) Use monochrome image settings and default settings
103 // for color and grayscale images.
104 try
105 {
106 using (PDFDoc doc = new PDFDoc(Path.Combine(InputPath, "newsletter.pdf")))
107 {
108 doc.InitSecurityHandler();
109
110 OptimizerMonoImageSettings mono_image_settings = new OptimizerMonoImageSettings();
111
112 mono_image_settings.SetCompressionMode(OptimizerMonoImageSettingsCompressionMode.e_jbig2);
113 mono_image_settings.ForceRecompression(true);
114
115 OptimizerOptimizerSettings opt_settings = new OptimizerOptimizerSettings();
116 opt_settings.SetMonoImageSettings(mono_image_settings);
117
118 Optimizer.Optimize(doc, opt_settings);
119
120 String output_file_path = Path.Combine(OutputPath, "newsletter_opt3.pdf");
121 WriteLine("Saving " + output_file_path);
122 await doc.SaveAsync(output_file_path, SDFDocSaveOptions.e_linearized);
123 WriteLine("Done. Results saved in " + output_file_path);
124 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
125 }
126 }
127 catch (Exception e)
128 {
129 WriteLine(GetExceptionMessage(e));
130 }
131
132 WriteLine("\n--------------------------------");
133 WriteLine("Done Optimizer Test.");
134 WriteLine("--------------------------------\n");
135 })).AsAsyncAction();
136 }
137 }
138}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales