Skip to content

Commit 2e81b79

Browse files
committed
921492 Resolved the given feedback.
1 parent b786fb3 commit 2e81b79

4 files changed

Lines changed: 32 additions & 28 deletions

File tree

  • Compression/Compress-the-existing-PDF-document/.NET/Compress-the-existing-PDF-document
  • Merge PDFs/Merge-multiple-documents-from-stream/.NET/Merge-multiple-documents-from-stream/Data

Compression/Compress-the-existing-PDF-document/.NET/Compress-the-existing-PDF-document/README.md

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,38 @@ Step 3: **Add required namespaces**: Include the following namespaces in your `P
2020
Step 4: **Implement PDF compression**: Use the following code snippet in `Program.cs` to compress PDF files:
2121

2222
```csharp
23-
// Open a file stream to read the input PDF file.
24-
using (FileStream fileStream = new FileStream(Input.pdf", FileMode.Open, FileAccess.Read))
25-
{
26-
// Create a new PdfLoadedDocument object from the file stream.
27-
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(fileStream))
28-
{
29-
// Create a new PdfCompressionOptions object.
30-
PdfCompressionOptions options = new PdfCompressionOptions();
31-
32-
//Enable the compress image.
33-
options.CompressImages = true;
34-
35-
//Set the image quality.
36-
options.ImageQuality = 50;
37-
38-
// Compress the PDF document.
39-
loadedDocument.Compress(options);
40-
41-
//Create file stream.
42-
using (FileStream outputFileStream = new FileStream("Output/Output.pdf", FileMode.Create, FileAccess.ReadWrite))
43-
{
44-
//Save the PDF document to file stream.
45-
loadedDocument.Save(outputFileStream);
46-
}
47-
//Close the document.
48-
loadedDocument.Close(true);
49-
}
50-
}
23+
// Open a file stream to read the input PDF file
24+
using (FileStream fileStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read))
25+
{
26+
// Load the PDF document from the file stream
27+
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(fileStream))
28+
{
29+
// Create a new PdfCompressionOptions object
30+
PdfCompressionOptions options = new PdfCompressionOptions();
31+
32+
// Enable image compression and set the image quality
33+
options.CompressImages = true;
34+
options.ImageQuality = 50;
35+
36+
// Enable font optimization
37+
options.OptimizeFont = true;
38+
39+
// Enable page content optimization
40+
options.OptimizePageContents = true;
41+
42+
// Remove metadata from the PDF
43+
options.RemoveMetadata = true;
44+
45+
// Compress the PDF document
46+
loadedDocument.Compress(options);
47+
48+
// Save the document into a memory stream
49+
using (MemoryStream outputStream = new MemoryStream())
50+
{
51+
loadedDocument.Save(outputStream);
52+
}
53+
}
54+
}
5155
```
5256

5357
You can download a complete working sample from the [GitHub repository](https://github.com/SyncfusionExamples/PDF-Examples/tree/master/Compression/Compress-the-existing-PDF-document).

0 commit comments

Comments
 (0)