Skip to content

Commit

Permalink
921492 Resolved the given feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
sameerkhan001 committed Jan 30, 2025
1 parent b786fb3 commit 2e81b79
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,38 @@ Step 3: **Add required namespaces**: Include the following namespaces in your `P
Step 4: **Implement PDF compression**: Use the following code snippet in `Program.cs` to compress PDF files:

```csharp
// Open a file stream to read the input PDF file.
using (FileStream fileStream = new FileStream(Input.pdf", FileMode.Open, FileAccess.Read))
{
// Create a new PdfLoadedDocument object from the file stream.
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(fileStream))
{
// Create a new PdfCompressionOptions object.
PdfCompressionOptions options = new PdfCompressionOptions();

//Enable the compress image.
options.CompressImages = true;

//Set the image quality.
options.ImageQuality = 50;

// Compress the PDF document.
loadedDocument.Compress(options);

//Create file stream.
using (FileStream outputFileStream = new FileStream("Output/Output.pdf", FileMode.Create, FileAccess.ReadWrite))
{
//Save the PDF document to file stream.
loadedDocument.Save(outputFileStream);
}
//Close the document.
loadedDocument.Close(true);
}
}
// Open a file stream to read the input PDF file
using (FileStream fileStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read))
{
// Load the PDF document from the file stream
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(fileStream))
{
// Create a new PdfCompressionOptions object
PdfCompressionOptions options = new PdfCompressionOptions();

// Enable image compression and set the image quality
options.CompressImages = true;
options.ImageQuality = 50;

// Enable font optimization
options.OptimizeFont = true;

// Enable page content optimization
options.OptimizePageContents = true;

// Remove metadata from the PDF
options.RemoveMetadata = true;

// Compress the PDF document
loadedDocument.Compress(options);

// Save the document into a memory stream
using (MemoryStream outputStream = new MemoryStream())
{
loadedDocument.Save(outputStream);
}
}
}
```

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).
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 2e81b79

Please sign in to comment.