PDF-Dokument mit C# und VB.NET komprimieren

Blogkategorie: PDF ; .NET

12.07.2024

Das VintaSoft PDF .NET Plug-in kann zur Komprimierung und Optimierung von PDF-Dokumenten verwendet werden. Außerdem können mit dem VintaSoft PDF .NET Plug-in Metadaten aus PDF-Dokumenten entfernt werden. Durch die Reduzierung der PDF-Dateigröße kann der Netzwerkverkehr beim Übertragen von PDF-Dateien verringert und der von PDF-Dateien belegte Speicherplatz reduziert werden. Dies ist besonders nützlich in Bereichen wie Archivierung, E-Mail-Versand und der Verwendung von PDF-Dokumenten in Webanwendungen.

Zur Optimierung eines PDF-Dokuments kann das VintaSoft PDF .NET Plug-in die folgenden Aktionen ausführen:

PDF-Dokument packen

PDF-Dokumente können ungenutzte Ressourcen enthalten. Das VintaSoft PDF .NET Plug-in ermöglicht es, ungenutzte Ressourcen in PDF-Dokumenten zu ermitteln und zu entfernen.
PDF-Dokumente können auch einen Revisionsverlauf enthalten. Das VintaSoft PDF .NET Plug-in ermöglicht es, den Revisionsverlauf aus PDF-Dokumenten zu entfernen.
Ressourcen von PDF-Dokumenten können auch mit einem suboptimalen Komprimierungsalgorithmus komprimiert sein. Das VintaSoft PDF .NET Plug-in kann Ressourcen mithilfe optimalerer Komprimierungsalgorithmen komprimieren.
PDF-Dokumente können auch eine unkomprimierte Querverweistabelle enthalten, wenn die PDF-Datei das PDF-Format 1.4 oder älter verwendet. Das VintaSoft PDF .NET Plug-in kann PDF-Dokumente im PDF-Format 1.5 oder höher speichern und eine komprimierte Querverweistabelle verwenden.

Hier ist C#-Code, der zeigt, wie ein vorhandenes PDF-Dokument geladen, nicht verwendete Ressourcen entfernt, die verwendeten Ressourcen mit einem optimalen Komprimierungsalgorithmus komprimiert und das PDF-Dokument im optimalen Format gespeichert wird:
/// <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;
}


Schriftarten im PDF-Dokument optimieren

Einige Schriftzeichen werden für die Textdarstellung in einem PDF-Dokument nicht verwendet. Das VintaSoft PDF .NET Plug-in ermöglicht das Entfernen dieser Zeichen aus den Schriften im PDF-Dokument.

Hier ist C#-Code, der zeigt, wie man Schriftarten in einem PDF-Dokument mithilfe der Klasse Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand optimiert:
/// <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);
}


Bilder in einem PDF-Dokument komprimieren

Viele PDF-Dokumente enthalten Bilder. VintaSoft PDF .Das .NET Plug-in ermöglicht es, die Auflösung und Farbtiefe von Bildern in PDF-Dokumenten zu verringern, um die Dateigröße zu reduzieren.

Hier ist C#-Code, der zeigt, wie die Auflösung und Farbtiefe von Ressourcen in einem PDF-Dokument mithilfe der Klasse `Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand` verringert werden:
/// <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);
}


Inhalt in PDF-Dokumenten löschen

PDF-Dokumente können nicht verwendete Objekte enthalten, z. B. Ressourcen, Seiten, Schriftarten, Bilder, Namen und Inhaltsoperatoren. Außerdem können PDF-Dokumente doppelte Ressourcen enthalten, z. B. Bild- oder Schriftartenkopien. Das VintaSoft PDF .NET Plug-in kann nicht verwendete Objekte und doppelte Ressourcen in PDF-Dokumenten erkennen und entfernen.

Hier ist C#-Code, der zeigt, wie nicht verwendete Objekte und Ressourcenduplikate aus einem PDF-Dokument mithilfe der Klasse Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand entfernt werden:
/// <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);
}


Metadaten und andere Elemente aus einem PDF-Dokument entfernen

Ein PDF-Dokument kann Objekte enthalten, die die Anzeige der PDF-Seite nicht beeinträchtigen, z. B. Metadaten, Lesezeichen, eingebettete Dateien, interaktive Formulare, Seitenminiaturen, die Strukturstruktur und Dokumentinformationen. Das VintaSoft PDF .NET Plug-in ermöglicht das Entfernen von Objekten aus einem PDF-Dokument, wenn diese im Dokument nicht benötigt werden.

Hier ist C#-Code, der zeigt, wie Objekte mithilfe der Klasse Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand aus einem PDF-Dokument entfernt werden:
/// <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);
}


Anmerkungen aus PDF-Dokument entfernen

Das VintaSoft PDF .NET Plug-in ermöglicht das Entfernen von Anmerkungen von einer PDF-Seite, wenn diese nicht benötigt werden. Außerdem ermöglicht das VintaSoft PDF .NET Plug-in das Konvertieren von Anmerkungen in Grafiken (Reduzieren der Anmerkungen), wenn die Anmerkungen zwar auf der PDF-Seite angezeigt werden sollen, der Benutzer aber nicht mit ihnen interagieren können soll.

Hier ist C#-Code, der zeigt, wie man Anmerkungen in Grafiken umwandelt (Anmerkungen reduzieren) und interaktive Formulare aus einem PDF-Dokument mithilfe der Klasse Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand entfernt:
/// <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);
}