Arbeiten mit großen PDF-Dokumenten in .NET

Blogkategorie: PDF ; .NET

05.02.2021

Die PDF-Spezifikation definiert die Größe von PDF-Dokumenten der Versionen 1.0-1.4 auf maximal 9,3 GB (9.999.999.999 Byte).
PDF-Dokumente der Version 1.5 und höher unterliegen keiner Größenbeschränkung, wenn eine komprimierte Querverweistabelle verwendet wird.

Das VintaSoft Imaging .NET SDK ermöglicht das Erstellen, Öffnen, Bearbeiten und Speichern von PDF-Dokumenten der Versionen 1.0-1.4, sofern die Dateigröße 9,3 GB (9.999.999.999 Byte) nicht überschreitet.
Das SDK ermöglicht das Erstellen, Öffnen,Ändern und Speichern von PDF-Dokumenten der Version 1.5 und höher, die eine komprimierte Querverweistabelle verwenden, wenn die Dateigröße 256 TB (281.474.976.710.655 Bytes) nicht überschreitet.

Die PDF-Spezifikation beschränkt die Größe von Dateianhängen in PDF-Dokumenten nicht.
Das VintaSoft Imaging .NET SDK ermöglicht das Hinzufügen von Dateianhängen zu PDF-Dokumenten. Im Allgemeinen kann das SDK Dateianhänge hinzufügen, deren Größe 2 GB (2.147.483.647 Byte) nicht überschreitet. Das SDK ermöglicht auch das Hinzufügen von PDF-Dateianhängen, die größer als 2 GB, aber nicht größer als 931 GB (999.999.999.999 Byte) sind, sofern das PDF-Dokument nicht verschlüsselt ist und die Ressource mit der Komprimierungsmethode "Keine" oder "ZIP" hinzugefügt wird.
Für das Abrufen von Ressourcen aus PDF-Dokumenten gelten dieselben Größenbeschränkungen wie für das Hinzufügen.


Hier ist C#-Code, der zeigt, wie man einen Anhang zu einem PDF-Dokument hinzufügt und abruft, wenn die Anhangsgröße 2 GB überschreitet:
/// <summary>
/// Adds the large file attachment to PDF document.
/// </summary>
/// <param name="pdfFilename">The PDF filename.</param>
/// <param name="attachmentFilename">The attachment filename.</param>
public static void AddLargeAttachment(string pdfFilename, string attachmentFilename)
{
    // open PDF document
    using (Vintasoft.Imaging.Pdf.PdfDocument document = new Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename))
    {
        if (document.EmbeddedFiles == null)
            document.EmbeddedFiles = new Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFileSpecificationDictionary(document);

        // open attachment file
        using (System.IO.Stream attachmentStream = System.IO.File.OpenRead(attachmentFilename))
        {
            // set ZIP compression level to 2 (fast)
            Vintasoft.Imaging.Pdf.PdfCompressionSettings compressionSettings = new Vintasoft.Imaging.Pdf.PdfCompressionSettings();
            compressionSettings.ZipCompressionLevel = 2;

            // create PDF embedded file
            Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFile embeddedFile = new Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFile(
                document, attachmentStream, false, Vintasoft.Imaging.Pdf.PdfCompression.Zip, compressionSettings);
            
            // create PDF embedded file specification
            Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFileSpecification fileSpecification =
                 new Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFileSpecification(System.IO.Path.GetFileName(attachmentFilename), embeddedFile);

            // add PDF embedded file specification to PDF document
            document.EmbeddedFiles.Add(fileSpecification);

            // save changes in PDF document (file attachment will be encoded during saving of PDF document)
            document.SaveChanges();
        }
    }
}

/// <summary>
/// Extracts the file attachments of PDF document in specified folder.
/// </summary>
/// <param name="pdfFilename">The PDF filename.</param>
/// <param name="attachmentOutputDir">The attachment output directory.</param>
public static void ExtractFileAttachments(string pdfFilename, string attachmentOutputDir)
{
    // open PDF document
    using (Vintasoft.Imaging.Pdf.PdfDocument document = new Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename))
    {
        // if PDF document has embedded files
        if (document.EmbeddedFiles != null)
        {
            // for each file embedded in PDF document
            foreach (Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFileSpecification fileSpecification in document.EmbeddedFiles.Values)
            {
                if (fileSpecification.EmbeddedFile != null)
                {
                    // save embedded file resource to a file in output directory
                    string filename = System.IO.Path.GetFileName(fileSpecification.Filename);
                    fileSpecification.EmbeddedFile.Save(System.IO.Path.Combine(attachmentOutputDir, filename));
                }
            }
        }
    }
}


Hier ist C#-Code, der zeigt, wie man einen Anhang zu einem PDF-Dokument hinzufügt und abruft, wenn die Anhangsgröße 2 GB nicht überschreitet:Dies zeigt, wie man Anhänge zu/von PDF-Dokumenten hinzufügt und abruft, wenn die Anhangsgröße 2 GB nicht überschreitet:Dies zeigt, wie man Anhänge zu/von PDF-Dokumenten hinzufügt und abruft, wenn die Anhangsgröße 2 GB nicht überschreitet:
/// <summary>
/// Adds the file attachment to PDF document.
/// </summary>
/// <param name="pdfFilename">The PDF filename.</param>
/// <param name="attachmentFilename">The attachment filename.</param>
public static void AddAttachment(string pdfFilename, string attachmentFilename)
{
    // open PDF document
    using (Vintasoft.Imaging.Pdf.PdfDocument document = new Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename))
    {
        if (document.EmbeddedFiles == null)
            document.EmbeddedFiles = new Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFileSpecificationDictionary(document);

        // create PDF embedded file (file attachment will be encoded in constructor of PdfEmbeddedFile class)
        Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFile embeddedFile = new Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFile(
            document, attachmentFilename, Vintasoft.Imaging.Pdf.PdfCompression.Zip);

        // create PDF embedded file specification
        Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFileSpecification fileSpecification =
             new Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFileSpecification(System.IO.Path.GetFileName(attachmentFilename), embeddedFile);

        // add PDF embedded file specification to PDF document
        document.EmbeddedFiles.Add(fileSpecification);

        // save PDF document
        document.SaveChanges();
    }
}

/// <summary>
/// Extracts the file attachments of PDF document in specified folder.
/// </summary>
/// <param name="pdfFilename">The PDF filename.</param>
/// <param name="attachmentOutputDir">The attachment output directory.</param>
public static void ExtractFileAttachments(string pdfFilename, string attachmentOutputDir)
{
    // open PDF document
    using (Vintasoft.Imaging.Pdf.PdfDocument document = new Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename))
    {
        // if PDF document has embedded files
        if (document.EmbeddedFiles != null)
        {
            // for each file embedded in PDF document
            foreach (Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFileSpecification fileSpecification in document.EmbeddedFiles.Values)
            {
                if (fileSpecification.EmbeddedFile != null)
                {
                    // save embedded file resource to a file in output directory
                    string filename = System.IO.Path.GetFileName(fileSpecification.Filename);
                    fileSpecification.EmbeddedFile.Save(System.IO.Path.Combine(attachmentOutputDir, filename));
                }
            }
        }
    }
}