Skip to content

Commit f374e8b

Browse files
committed
959881 Modified Proper code sample.
1 parent ad9d3ef commit f374e8b

File tree

3 files changed

+40
-31
lines changed

3 files changed

+40
-31
lines changed

Compression/Linux-Based-PDF-Compression-Using-Docker/.NET/Linux-Based-PDF-Compression-Using-Docker/Output/gitkeep.txt

Whitespace-only changes.

Compression/Linux-Based-PDF-Compression-Using-Docker/.NET/Linux-Based-PDF-Compression-Using-Docker/Program.cs

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,46 @@ public static void Main(string[] args)
1515

1616
public static void CompressPdf()
1717
{
18-
// Open the file specified by the path for reading using a FileStream
19-
using (FileStream inputDocument = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read))
18+
string path = Path.GetFullPath(@"Input.pdf");
19+
Console.WriteLine($"'{path}' pdf compression started.");
20+
21+
Stopwatch stopwatch = Stopwatch.StartNew();
22+
FileStream inputDocument = new FileStream(path, FileMode.Open, FileAccess.Read);
23+
Console.WriteLine($"Original file size: {inputDocument.Length:N0}");
24+
25+
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputDocument);
26+
27+
// Create a new compression option.
28+
PdfCompressionOptions options = new PdfCompressionOptions
2029
{
21-
// Load the PDF document from the input file stream
22-
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputDocument))
23-
{
24-
// Create a new instance of PdfCompressionOptions to specify compression settings
25-
PdfCompressionOptions options = new PdfCompressionOptions
26-
{
27-
// Enable image compression in the PDF document
28-
CompressImages = true,
29-
// Set the image quality to 30 (on a scale where lower values reduce quality)
30-
ImageQuality = 30,
31-
// Optimize the font usage in the PDF document
32-
OptimizeFont = true,
33-
// Optimize the contents of each page in the PDF document
34-
OptimizePageContents = true,
35-
// Remove metadata from the PDF document to reduce its size
36-
RemoveMetadata = true
37-
};
38-
39-
// Apply the compression options to the loaded PDF document
40-
loadedDocument.Compress(options);
41-
42-
// Create a file stream to store the compressed PDF document
43-
using (FileStream outputDocument = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
44-
{
45-
// Save the compressed PDF document to the output stream
46-
loadedDocument.Save(outputDocument);
47-
}
48-
}
49-
}
30+
// Enable image compression.
31+
CompressImages = true,
32+
// Set the image quality.
33+
ImageQuality = 30,
34+
// Optimize fonts in the PDF document.
35+
OptimizeFont = true,
36+
// Optimize page contents.
37+
OptimizePageContents = true,
38+
// Remove metadata from the PDF document.
39+
RemoveMetadata = true
40+
};
41+
42+
// Compress the PDF document.
43+
loadedDocument.Compress(options);
44+
45+
// Save the PDF document.
46+
MemoryStream outputDocument = new MemoryStream();
47+
48+
// Save the PDF document to memory stream.
49+
loadedDocument.Save(outputDocument);
50+
51+
stopwatch.Stop();
52+
Console.WriteLine($"Compressed file size: {outputDocument.Length:N0}");
53+
Console.WriteLine($"Time elapsed: {stopwatch.Elapsed}");
54+
55+
// Clean up resources.
56+
outputDocument.Close();
57+
loadedDocument.Close(true);
58+
inputDocument.Close();
5059
}
5160
}

0 commit comments

Comments
 (0)