Travailler avec des documents PDF volumineux en .NET

Catégorie du blog: PDF.NET

05.02.2021

La spécification PDF définit la taille des documents PDF versions 1.0 à 1.4 à 9,3 Go (9 999 999 999 octets).
Les documents PDF de version 1.5 et supérieure ne sont pas limités en taille de fichier lorsqu'ils utilisent un tableau de références croisées compressé.

VintaSoft Imaging .NET SDK permet de créer, d'ouvrir, de modifier et d'enregistrer des documents PDF de version 1.0-1.4 lorsque la taille du fichier ne dépasse pas 9,3 Go (9.999.999.999 octets).
Le SDK permet de créer, d'ouvrir, de modifier et d'enregistrer un document PDF de version 1.5 et supérieure, utilisant une table de références croisées compressée, lorsque la taille du fichier ne dépasse pas 256 To (281.474.976.710.655 octets).

La spécification PDF ne limite pas la taille des pièces jointes stockées dans un document PDF.
VintaSoft Imaging .NET SDK permet d'ajouter une pièce jointe à un document PDF. Généralement, le SDK peut ajouter une pièce jointe dont la taille ne dépasse pas 2 Go (2.147.483.647 octets). Il permet également d'ajouter une pièce jointe PDF plus volumineuse que 2 Go, mais n'excédant pas 931 Go (999.999.999.999 octets), à condition que le document PDF ne soit pas chiffré et que la ressource soit ajoutée sans compression ou avec une compression ZIP.
Pour récupérer des ressources à partir d'un document PDF, appliquez les mêmes restrictions de taille que pour l'ajout.


Voici un code C# qui montre comment ajouter et récupérer une pièce jointe à un document PDF lorsque sa taille dépasse 2 Go:
/// <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));
                }
            }
        }
    }
}


Voici un code C# qui montre comment ajouter et récupérer une pièce jointe depuis/vers un document PDF lorsque la taille de la pièce jointe ne dépasse pas 2 Go:
/// <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));
                }
            }
        }
    }
}