Աշխատել .NET-ում մեծ PDF փաստաթղթերի հետ

Բլոգի կատեգորիա՝ PDF.NET

05.02.2021

PDF սպեցիֆիկացիան սահմանում է 1.0-1.4 տարբերակի PDF փաստաթղթի չափը, որը չպետք է գերազանցի 9,3 ԳԲ-ը (9.999.999.999 բայթ):
1.5 և ավելի բարձր տարբերակի PDF փաստաթուղթը ֆայլի չափի սահմանափակում չունի, երբ օգտագործվում է սեղմված խաչաձև հղումների աղյուսակ:

VintaSoft Imaging .NET SDK-ն թույլ է տալիս ստեղծել, բացել, փոփոխել և պահպանել 1.0-1.4 տարբերակի PDF փաստաթուղթ, երբ ֆայլի չափը չի գերազանցում 9,3 ԳԲ-ը (9.999.999.999 բայթ):
SDK-ն թույլ է տալիս ստեղծել, բացել, փոփոխել և պահպանել 1.5 և ավելի բարձր տարբերակի PDF փաստաթուղթ, որն օգտագործում է սեղմված խաչաձև հղումների աղյուսակ, երբ ֆայլի չափը չի գերազանցում 256 ՏԲ-ն (281.474.976.710.655 բայթ):

PDF սպեցիֆիկացիան չի սահմանափակում PDF փաստաթղթում պահվող կցորդի չափը:
VintaSoft Imaging .NET SDK-ն թույլ է տալիս PDF փաստաթղթին կցորդ ավելացնել: Ընդհանուր առմամբ, SDK-ն կարող է PDF փաստաթղթին կցորդ ավելացնել, երբ դրա չափը չի գերազանցում 2 ԳԲ-ն (2.147.483.647 բայթ): Բացի այդ, SDK-ն թույլ է տալիս PDF ֆայլի կցորդ ավելացնել, որը մեծ է 2 ԳԲ-ից, բայց չի գերազանցում 931 ԳԲ-ն (999.999.999.999 բայթ), երբ PDF փաստաթուղթը կոդավորված չէ և ռեսուրսը ավելացված է None կամ ZIP սեղմմամբ:
PDF փաստաթղթից ռեսուրս ստանալու համար կիրառեք նույն չափի սահմանափակումները, ինչ ավելացնելու դեպքում։


Ահա C# կոդը, որը ցույց է տալիս, թե ինչպես ավելացնել և վերականգնել կցորդ PDF փաստաթղթից/դրան, երբ կցորդի չափը գերազանցում է 2 ԳԲ-ն։
/// <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));
                }
            }
        }
    }
}


Ահա C# կոդը, որը ցույց է տալիս, թե ինչպես ավելացնել և վերականգնել կցորդ PDF փաստաթղթից/դրան, երբ կցորդի չափը չի գերազանցում 2 ԳԲ-ը։
/// <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));
                }
            }
        }
    }
}