Skip to content

Commit

Permalink
Added Compress PDF document in .NET sample code
Browse files Browse the repository at this point in the history
  • Loading branch information
Bobrovsky committed Dec 16, 2017
1 parent 7346180 commit bf6a0da
Show file tree
Hide file tree
Showing 7 changed files with 357 additions and 1 deletion.
105 changes: 105 additions & 0 deletions Samples/Compression/CompressAllTechniques/C#/CompressAllTechniques.cs
@@ -0,0 +1,105 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using BitMiracle.Docotic.Pdf;

namespace BitMiracle.Docotic.Pdf.Samples
{
public static class CompressAllTechniques
{
public static void Main()
{
// NOTE:
// When used in trial mode, the library imposes some restrictions.
// Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
// for more information.

const string originalFile = @"Sample Data\jpeg.pdf";
const string compressedFile = "CompressAllTechniques.pdf";

using (PdfDocument pdf = new PdfDocument(originalFile))
{
// 1. Recompress images
foreach (PdfPage page in pdf.Pages)
{
foreach (PdfPaintedImage painted in page.GetPaintedImages())
{
PdfImage image = painted.Image;

// image that is used as mask or image with attached mask are
// not good candidates for recompression
if (image.IsMask || image.Mask != null || image.Width < 8 || image.Height < 8)
continue;

// get size of the painted image
int width = Math.Max(1, (int)painted.Bounds.Width);
int height = Math.Max(1, (int)painted.Bounds.Height);

if (image.ComponentCount == 1 && image.BitsPerComponent == 1 &&
image.Compression != PdfImageCompression.Group4Fax)
{
image.RecompressWithGroup4Fax();
}
else if (width < image.Width || height < image.Height)
{
// NOTE: PdfImage.ResizeTo() method is not supported in version for .NET Standard
if (image.ComponentCount >= 3)
image.ResizeTo(width, height, PdfImageCompression.Jpeg, 90);
else
image.ResizeTo(width, height, PdfImageCompression.Flate, 9);
}
}
}

// 2 Setup save options
pdf.SaveOptions.Compression = PdfCompression.Flate;
pdf.SaveOptions.UseObjectStreams = true;
pdf.SaveOptions.RemoveUnusedObjects = true;
pdf.SaveOptions.OptimizeIndirectObjects = true;
pdf.SaveOptions.WriteWithoutFormatting = true;

// 3. Remove structure information
pdf.RemoveStructureInformation();

// 4. Flatten form fields
// Controls become uneditable after that
pdf.FlattenControls();

// 5. Clear metadata
pdf.Metadata.Basic.Clear();
pdf.Metadata.DublinCore.Clear();
pdf.Metadata.MediaManagement.Clear();
pdf.Metadata.Pdf.Clear();
pdf.Metadata.RightsManagement.Clear();

pdf.Metadata.Custom.Properties.Clear();

foreach (XmpSchema schema in pdf.Metadata.Schemas)
schema.Properties.Clear();

pdf.Info.Clear(false);

// 6. Unembed fonts
foreach (PdfFont font in pdf.GetFonts())
{
// Only unembed popular fonts installed in the typical OS. You can extend
// the list of such fonts in the "if" statement below.
if (font.Name == "Arial" || font.Name == "Verdana")
font.Unembed();
}

pdf.Save(compressedFile);
}

string message = string.Format(
"Original file size: {0} bytes;\r\nCompressed file size: {1} bytes",
new FileInfo(originalFile).Length,
new FileInfo(compressedFile).Length
);
MessageBox.Show(message);

Process.Start(compressedFile);
}
}
}
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{80675702-2670-4AA4-B474-EADBD35320B8}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>BitMiracle.Docotic.Pdf.Samples</RootNamespace>
<AssemblyName>CompressAllTechniques</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="BitMiracle.Docotic.Pdf" />
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="CompressAllTechniques.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="..\..\..\Sample Data\jpeg.pdf">
<Link>Sample Data\jpeg.pdf</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
13 changes: 13 additions & 0 deletions Samples/Compression/CompressAllTechniques/README.md
@@ -0,0 +1,13 @@
# Compress PDF document in .NET
This sample shows how to optimize size of an existing PDF document using Docotic.Pdf.

NOTE: To get proper results please use this sample code with a license key. You can get a free license key here:
http://bitmiracle.com/pdf-library/download-pdf-library.aspx

Docotic.Pdf supports different compression methods. You can use all of them to get the best compression ratio:
1. recompress images (if any)
2. setup save options
3. remove structure information
4. flatten form fields
5. remove metadata
6. unembed fonts
@@ -0,0 +1,101 @@
Imports System
Imports System.Diagnostics
Imports System.IO
Imports System.Windows.Forms

Imports Microsoft.VisualBasic

Imports BitMiracle.Docotic.Pdf

Namespace BitMiracle.Docotic.Pdf.Samples
Public NotInheritable Class CompressAllTechniques
Public Shared Sub Main()
' NOTE:
' When used in trial mode, the library imposes some restrictions.
' Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
' for more information.

Const originalFile As String = "Sample Data\jpeg.pdf"
Const compressedFile As String = "CompressAllTechniques.pdf"

Using pdf As New PdfDocument(originalFile)
' 1. Recompress images
For Each page As PdfPage In pdf.Pages
For Each painted As PdfPaintedImage In page.GetPaintedImages()
Dim image As PdfImage = painted.Image

' image that is used as mask or image with attached mask are
' not good candidates for recompression
If image.IsMask OrElse image.Mask IsNot Nothing OrElse image.Width < 8 OrElse image.Height < 8 Then
Continue For
End If

' get size of the painted image
Dim width As Integer = Math.Max(1, CInt(painted.Bounds.Width))
Dim height As Integer = Math.Max(1, CInt(painted.Bounds.Height))

If image.ComponentCount = 1 AndAlso image.BitsPerComponent = 1 AndAlso image.Compression <> PdfImageCompression.Group4Fax Then
image.RecompressWithGroup4Fax()
ElseIf width < image.Width OrElse height < image.Height Then
' NOTE: PdfImage.ResizeTo() method is not supported in version for .NET Standard
If image.ComponentCount = 3 Then
image.ResizeTo(width, height, PdfImageCompression.Jpeg, 90)
Else
image.ResizeTo(width, height, PdfImageCompression.Flate, 9)
End If
End If
Next
Next

' 2 Setup save options
pdf.SaveOptions.Compression = PdfCompression.Flate
pdf.SaveOptions.UseObjectStreams = True
pdf.SaveOptions.RemoveUnusedObjects = True
pdf.SaveOptions.OptimizeIndirectObjects = True
pdf.SaveOptions.WriteWithoutFormatting = True

' 3. Remove structure information
pdf.RemoveStructureInformation()

' 4. Flatten form fields
' Controls become uneditable after that
pdf.FlattenControls()

' 5. Clear metadata
pdf.Metadata.Basic.Clear()
pdf.Metadata.DublinCore.Clear()
pdf.Metadata.MediaManagement.Clear()
pdf.Metadata.Pdf.Clear()
pdf.Metadata.RightsManagement.Clear()

pdf.Metadata.[Custom].Properties.Clear()

For Each schema As XmpSchema In pdf.Metadata.Schemas
schema.Properties.Clear()
Next

pdf.Info.Clear(False)

' 6. Unembed fonts
For Each font As PdfFont In pdf.GetFonts()
' Only unembed popular fonts installed in the typical OS. You can extend
' the list of such fonts in the "if" statement below.
If font.Name = "Arial" OrElse font.Name = "Verdana" Then
font.Unembed()
End If
Next

pdf.Save(compressedFile)
End Using

Dim message As String = String.Format(
"Original file size: {0} bytes;" & vbCr & vbLf & "Compressed file size: {1} bytes",
New FileInfo(originalFile).Length,
New FileInfo(compressedFile).Length
)
MessageBox.Show(message)

Process.Start(compressedFile)
End Sub
End Class
End Namespace
@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.21022</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{4F4AEF23-064A-4A40-8136-3750FAE28A31}</ProjectGuid>
<OutputType>Exe</OutputType>
<StartupObject>BitMiracle.Docotic.Pdf.Samples.BitMiracle.Docotic.Pdf.Samples.CompressAllTechniques</StartupObject>
<RootNamespace>BitMiracle.Docotic.Pdf.Samples</RootNamespace>
<AssemblyName>CompressAllTechniques</AssemblyName>
<FileAlignment>512</FileAlignment>
<MyType>Console</MyType>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<OptionExplicit>On</OptionExplicit>
<OptionCompare>Binary</OptionCompare>
<OptionStrict>Off</OptionStrict>
<OptionInfer>On</OptionInfer>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<DefineDebug>true</DefineDebug>
<DefineTrace>true</DefineTrace>
<OutputPath>bin\Debug\</OutputPath>
<NoWarn>42016,41999,42017,42018,42019,42032,42036,42020,42021,42022</NoWarn>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<DefineDebug>false</DefineDebug>
<DefineTrace>true</DefineTrace>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<NoWarn>42016,41999,42017,42018,42019,42032,42036,42020,42021,42022</NoWarn>
</PropertyGroup>
<ItemGroup>
<Reference Include="BitMiracle.Docotic.Pdf">
<HintPath>..\..\..\..\BitMiracle.Docotic.Pdf.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Import Include="Microsoft.VisualBasic" />
<Import Include="System" />
<Import Include="System.Collections" />
<Import Include="System.Collections.Generic" />
<Import Include="System.Data" />
<Import Include="System.Diagnostics" />
</ItemGroup>
<ItemGroup>
<Compile Include="CompressAllTechniques.vb" />
</ItemGroup>
<ItemGroup>
<Content Include="..\..\..\Sample Data\jpeg.pdf">
<Link>Sample Data\jpeg.pdf</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.VisualBasic.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
6 changes: 5 additions & 1 deletion Samples/Compression/README.md
@@ -1,2 +1,6 @@
# Compression
This group contains samples showing how to optimize PDF documents. You can use multiple optimization techniques to reduce size of output PDF files.
This group contains samples showing how to optimize PDF documents. You can use multiple optimization techniques to reduce size of output PDF files.

[Compress PDF document in .NET](/Samples/Compression/CompressAllTechniques)

This sample shows how to optimize size of an existing PDF document using Docotic.Pdf.
2 changes: 2 additions & 0 deletions Samples/README.md
Expand Up @@ -17,6 +17,8 @@
[Save (extract) attachment](/Samples/Attachments/SaveAttachment)

# Compression
[Compress PDF document in .NET](/Samples/Compression/CompressAllTechniques)

# Display options
# Draw and print PDF
# Forms and Annotations
Expand Down

0 comments on commit bf6a0da

Please sign in to comment.