C#、VB.NETを使用してPDFドキュメントを圧縮

ブログ カテゴリ: PDF.NET

2024/07/12

VintaSoft PDF .NET Plug-11inは、PDFドキュメントの圧縮と最適化に使用できます。また、VintaSoft PDF .NET Plug-inは、PDFドキュメントからメタデータを削除することもできます。PDFファイルのサイズを縮小すると、PDFファイルの転送時のネットワークトラフィックを削減し、PDFファイルが占有するストレージ容量を削減できます。これは、アーカイブ、メール送信、WebアプリケーションでのPDFドキュメントの使用などの分野で特に役立ちます。

PDF ドキュメントを最適化するために、VintaSoft PDF .NET Plug-in は次のアクションを実行できます。

PDF ドキュメントをパックします

PDFドキュメントには未使用のリソースが含まれている場合があります。VintaSoft PDF .NET Plug-inを使用すると、PDFドキュメント内の未使用のリソースを特定し、削除することができます。
また、PDF文書には変更履歴が保存されている場合があります。VintaSoft PDF .NET Plug-inを使用すると、PDF文書から変更履歴を削除できます。
また、PDFドキュメントのリソースは、最適な圧縮アルゴリズムで圧縮されない場合もあります。VintaSoft PDF .NET Plug-inは、より最適な圧縮アルゴリズムを使用してリソースを圧縮できます。
また、PDFファイルがPDF形式1.4以前を使用している場合、PDF文書には圧縮されていない相互参照表が含まれます。VintaSoft PDF .NET Plug-inは、PDF文書をPDF形式1.5以降で保存し、圧縮された相互参照表を使用できます。

既存の PDF ドキュメントを読み込み、PDF ドキュメントから使用されていないリソースを削除し、使用されている PDF リソースを最適な圧縮アルゴリズムで圧縮し、最適な PDF 形式で PDF ドキュメントを保存する方法を示す C# コードは次のとおりです:
/// <summary>
/// Packs the PDF document.
/// </summary>
/// <param name="inPdfFilename">The input PDF filename.</param>
/// <param name="outPdfFilename">The output PDF filename.</param>
public static void PackDocument(string inPdfFilename, string outPdfFilename)
{
    // create compressor with empty compression settings
    Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand compressor =
        Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand.CreateEmptyCompressor();

    // specify that compressor must use maximum Flate compression level (best compression)
    compressor.FlateCompressionLevel = 9;
    // specify that compressor must recompress all resource that uses None, LZW, Flate compression using Flate compression
    compressor.RecompressFlateCompression = true;
    compressor.UseFlateInsteadLzwCompression = true;
    compressor.UseFlateInsteadNoneCompression = true;

    // specify that compressor must remove incremental update info and unused objects
    compressor.PackDocument = true;

    // if version of PDF document is lower than 1.7
    if (GetPdfDocumentVersion(inPdfFilename) < 17)
    {
        // set output format to PDF 1.7
        compressor.DocumentPackFormat = Vintasoft.Imaging.Pdf.PdfFormat.Pdf_17;
    }

    // compress PDF document
    compressor.Compress(inPdfFilename, outPdfFilename);
}

/// <summary>
/// Returns the PDF document version.
/// </summary>
/// <param name="pdfFilename">The PDF filename.</param>
/// <returns>The version number in dual-digit format (10,11,12,13,14,15,16,17,20,...).</returns>
private static int GetPdfDocumentVersion(string pdfFilename)
{
    using (Vintasoft.Imaging.Pdf.PdfDocument document = new Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename))
        return document.Format.VersionNumber;
}


PDF ドキュメントのフォントを最適化します

一部のフォントグリフは、PDFドキュメント内のテキストのレンダリングに使用されません。VintaSoft PDF .NET Plug-inを使用すると、PDFドキュメント内のフォントから使用されていないグリフを削除できます。

以下は、Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand クラスを使用して PDF ドキュメント内のフォントを最適化する方法を示す C# コードです。
/// <summary>
/// Subsets fonts in PDF document.
/// </summary>
/// <param name="inPdfFilename">The input PDF filename.</param>
/// <param name="outPdfFilename">The output PDF filename.</param>
public static void SubsetFonts(string inPdfFilename, string outPdfFilename)
{
    // create compressor with empty compression settings
    Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand compressor =
       Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand.CreateEmptyCompressor();

    // specify that compressor must subset fonts in PDF document
    compressor.SubsetFonts = true;

    // compress PDF document
    compressor.Compress(inPdfFilename, outPdfFilename);
}


PDF ドキュメント内の画像を圧縮します。

多くのPDF文書には画像が含まれています。VintaSoft PDF .NET Plug-inを使用すると、PDF文書内の画像の解像度と色深度を下げることで、PDFファイルのサイズを縮小できます。

Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommandクラスを使用して、PDFドキュメント内のリソースの解像度と色深度を下げる方法を示すC#コードを以下に示します。
/// <summary>
/// Detects "read color depth" of PDF image resources and compress PDF document with intent to view in 150DPI.
/// </summary>
/// <param name="inPdfFilename">The input PDF filename.</param>
/// <param name="outPdfFilename">The output PDF filename.</param>
public static void CompressToViewIn150DPI(string inPdfFilename, string outPdfFilename)
{
    // create compressor that will compress PDF document using lossy compression algorithms
    Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand compressor =
       Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand.CreateLossyCompressor(150, false, false, false);

    // specify that compressor must use JPEG compression for color images
    compressor.ColorImagesCompression = Vintasoft.Imaging.Pdf.PdfCompression.Jpeg;
    // specify that compressor must set JPEG quality to 70
    compressor.ColorImagesCompressionSettings.JpegQuality = 70;

    // specify that compressor must detect if image is bitonal image and use optimal compression for bitonal image
    compressor.DetectBitonalImageResources = true;
    // specify that compressor must detect if image is black-white image and use optimal compression for black-white image
    compressor.DetectBlackWhiteImageResources = true;
    // specify that compressor must detect if image is grayscale image and use optimal compression for grayscale image
    compressor.DetectGrayscaleImageResources = true;

    // compress PDF document
    compressor.Compress(inPdfFilename, outPdfFilename);
}


PDFドキュメント内のコンテンツをクリアします。

PDFドキュメントには、リソース、ページ、フォント、画像、名前、コンテンツ演算子など、使用されていないオブジェクトが含まれている場合があります。また、画像やフォントのコピーなど、リソースの重複が含まれている場合もあります。VintaSoft PDF .NET Plug-inは、PDFドキュメント内の使用されていないオブジェクトとリソースの重複を検出し、削除することができます。

以下は、Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand クラスを使用して、PDF ドキュメントから使用されていないオブジェクトとリソースの重複を削除する方法を示す C# コードです。
/// <summary>
/// Removes unused and duplicated resources in the PDF document.
/// </summary>
/// <param name="inPdfFilename">The input PDF filename.</param>
/// <param name="outPdfFilename">The output PDF filename.</param>
public static void RemoveUnsusedResources(string inPdfFilename, string outPdfFilename)
{
    // create compressor with empty compression settings
    Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand compressor =
       Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand.CreateEmptyCompressor();

    // specify that compressor must remove duplicate resources from PDF document
    compressor.RemoveDuplicateResources = true;
    // specify that compressor must remove unused names resources from PDF document
    compressor.RemoveUnusedNamedResources = true;
    // specify that compressor must remove unused names from PDF document
    compressor.RemoveUnusedNames = true;
    // specify that compressor must remove unused pages from PDF document
    compressor.RemoveUnusedPages = true;
    // specify that compressor must remove invalid bookmarks from PDF document
    compressor.RemoveInvalidBookmarks = true;
    // specify that compressor must remove invalid links from PDF document
    compressor.RemoveInvalidLinks = true;

    // compress PDF document
    compressor.Compress(inPdfFilename, outPdfFilename);
}


PDF ドキュメントからメタデータとその他の要素を削除します。

PDFドキュメントには、PDFページの表示に影響を与えないオブジェクト(メタデータ、ブックマーク、埋め込みファイル、インタラクティブフォーム、ページサムネイル、構造ツリー、ドキュメント情報など)が含まれている場合があります。VintaSoft PDF .NET Plug-inを使用すると、ドキュメントに不要なオブジェクトをPDFドキュメントから削除できます。

Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommandクラスを使用してPDFドキュメントからオブジェクトを削除する方法を示すC#コードを以下に示します。
/// <summary>
/// Removes metadata, bookmarks, document information, embedded files, embedded thumbnails, interactive form and structure tree of the PDF document.
/// </summary>
/// <param name="inPdfFilename">The input PDF filename.</param>
/// <param name="outPdfFilename">The output PDF filename.</param>
public static void RemoveObjects(string inPdfFilename, string outPdfFilename)
{
    // create compressor with empty compression settings
    Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand compressor =
       Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand.CreateEmptyCompressor();

    // specify that compressor must remove metadata from PDF document
    compressor.RemoveMetadata = true;
    // specify that compressor must remove bookmarks from PDF document
    compressor.RemoveBookmarks = true;
    // specify that compressor must remove document information from PDF document
    compressor.RemoveDocumentInformation = true;
    // specify that compressor must remove embedded files from PDF document
    compressor.RemoveEmbeddedFiles = true;
    // specify that compressor must remove embedded thumbnails from PDF document
    compressor.RemoveEmbeddedThumbnails = true;
    // specify that compressor must remove interactive form from PDF document
    compressor.RemoveInteractiveForm = true;
    // specify that compressor must remove structure tree from PDF document
    compressor.RemoveStructureTree = true;

    // compress PDF document
    compressor.Compress(inPdfFilename, outPdfFilename);
}


PDF 文書から注釈を削除します

VintaSoft PDF .NET Plug-inを使用すると、PDFページに注釈が不要な場合、PDFページから注釈を削除できます。また、PDFページに注釈を表示する必要があるものの、ユーザーが注釈を操作できないようにする必要がある場合、VintaSoft PDF .NET Plug-inでは注釈をグラフィックに変換(フラット化)できます。

Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand クラスを使用して、注釈をグラフィックに変換 (注釈をフラット化) し、PDF ドキュメントからインタラクティブ フォームを削除する方法を示す C# コードを以下に示します:
/// <summary>
/// Flatten an annotations and remove intractive form of the PDF document.
/// </summary>
/// <param name="inPdfFilename">The input PDF filename.</param>
/// <param name="outPdfFilename">The output PDF filename.</param>
public static void FlattenAnnotations(string inPdfFilename, string outPdfFilename)
{
    // create compressor with empty compression settings
    Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand compressor =
       Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand.CreateEmptyCompressor();

    // specify that compressor must remove interactive form from PDF document
    compressor.RemoveInteractiveForm = true;
    // specify that compressor must flatten annotations in PDF document
    compressor.FlattenAnnotations = true;

    // compress PDF document
    compressor.Compress(inPdfFilename, outPdfFilename);
}