←Select platform

GetEmbeddedFileCount(string,string) Method

Summary

Gets the total number of embedded files (attachments) in a PDF file.

Syntax
C#
Java
public static int GetEmbeddedFileCount( 
   string fileName, 
   string password 
) 
public static int getEmbeddedFileCount( 
   java.lang.String,  
   java.lang.String 
); 

Parameters

fileName

The name of the PDF file.

password

The password to use when loading this PDF file (if the file is encrypted); otherwise, use null.

Return Value

The total number of embedded files (attachments) in the PDF.

Example

This example will re-generate a PDF document by stitching the pages of original document, followed by all the attachments found.

C#
VB
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Controls; 
using Leadtools.Drawing; 
using Leadtools.ImageProcessing; 
using Leadtools.Pdf; 
using Leadtools.Svg; 
using Leadtools.WinForms; 
 
 
public static void ExtractAttachmentsAndStitch(string inputFileName, string outputFileName) 
{ 
   File.Delete(outputFileName); 
 
   // Get the number of embedded files (attachments) in the input file 
   int attachmentCount = PDFFile.GetEmbeddedFileCount(inputFileName, null); 
 
   // If the file does not have any attachments we are done 
   if (attachmentCount == 0) 
   { 
      File.Copy(inputFileName, outputFileName, true); 
      return; 
   } 
 
   // To convert attachment files that are not PDF 
   var rasterCodecs = new RasterCodecs(); 
   rasterCodecs.ThrowExceptionsOnInvalidImages = false; 
 
   var tempFiles = new List<string>(); 
 
   // Now, extract all the attachments 
   for (int attachmentNumber = 1; attachmentNumber <= attachmentCount; attachmentNumber++) 
   { 
      // Extract this attachment to a temporary file 
      string tempFile = Path.GetTempFileName(); 
      PDFFile.ExtractEmbeddedFile(inputFileName, null, attachmentNumber, tempFile); 
 
      // If attachment is not PDF, convert it 
      RasterImageFormat format; 
      int pageCount; 
 
      using (CodecsImageInfo info = rasterCodecs.GetInformation(tempFile, true)) 
      { 
         format = info.Format; 
         pageCount = info.TotalPages; 
      } 
 
      if (format == RasterImageFormat.Unknown) 
      { 
         // We do not know what to do with this attachment, log and ignore it 
         Console.WriteLine("Could not convert attachment file to PDF, ignoring"); 
         File.Delete(tempFile); 
      } 
      else if (format != RasterImageFormat.RasPdf) 
      { 
         // Simple conversion using RasterImage. 
         // TODO for the Example: A better way is to use the DocumentConverter to get true document conversion with all the 
         // options available 
         string tempPdfFile = Path.GetTempFileName(); 
 
         for (int pageNumber = 1; pageNumber <= pageCount; pageNumber++) 
         { 
            using (RasterImage image = rasterCodecs.Load(tempFile, pageNumber)) 
            { 
               rasterCodecs.Save(image, tempPdfFile, RasterImageFormat.RasPdfJpeg422, 0, 1, -1, -1, CodecsSavePageMode.Append); 
            } 
         } 
 
         File.Copy(tempPdfFile, tempFile, true); 
         File.Delete(tempPdfFile); 
         tempFiles.Add(tempFile); 
      } 
      else 
      { 
         // TODO for the Example: Check if this file is PDF with attachments and call the function recursively. 
         tempFiles.Add(tempFile); 
      } 
   } 
 
   // Time to generate the final document, first the pages from the original document 
 
   // Did we extract/convert any attachments? 
   if (tempFiles.Count > 0) 
   { 
      // Note that if the original document is PDF portfolio, then it does not contain real pages. Just a placeholder that should be ignored 
      if (!PDFFile.IsPortfolio(inputFileName, null)) 
      { 
         PDFFile file = new PDFFile(inputFileName); 
         file.MergeWith(tempFiles.ToArray(), outputFileName); 
      } 
      else 
      { 
         // This is portfolio, so we will ignore the original file and just merge the attachments 
         string firstFile = tempFiles[0]; 
         tempFiles.RemoveAt(0); 
         PDFFile file = new PDFFile(firstFile); 
         file.MergeWith(tempFiles.ToArray(), outputFileName); 
         File.Delete(firstFile); 
      } 
   } 
   else 
   { 
      // No, just copy the original file over 
      File.Copy(inputFileName, outputFileName, true); 
   } 
 
   // Clean up 
   foreach (string tempFile in tempFiles) 
      File.Delete(tempFile); 
   rasterCodecs.Dispose(); 
} 
Imports Leadtools 
Imports Leadtools.Codecs 
Imports Leadtools.Pdf 
Imports Leadtools.WinForms 
Imports Leadtools.Svg 
Imports Leadtools.ImageProcessing 
 
Public Shared Sub ExtractAttachmentsAndStitch(inputFileName As String, outputFileName As String) 
   File.Delete(outputFileName) 
 
   ' Get the number of embedded files (attachments) in the input file 
   Dim attachmentCount As Integer = PDFFile.GetEmbeddedFileCount(inputFileName, Nothing) 
 
   ' If the file does Not have any attachments we are done 
   If attachmentCount = 0 Then 
      File.Copy(inputFileName, outputFileName, True) 
      Return 
   End If 
 
   ' To convert attachment files that are Not PDF 
   Dim rasterCodecs As New RasterCodecs() 
   rasterCodecs.ThrowExceptionsOnInvalidImages = False 
 
   Dim tempFiles As New List(Of String)() 
 
   ' Now, extract all the attachments 
   For attachmentNumber As Integer = 1 To attachmentCount 
      ' Extract this attachment to a temporary file 
      Dim tempFile As String = Path.GetTempFileName() 
      PDFFile.ExtractEmbeddedFile(inputFileName, Nothing, attachmentNumber, tempFile) 
 
      ' If attachment Is Not PDF, convert it 
      Dim format As RasterImageFormat 
      Dim pageCount As Integer 
 
      Using info As CodecsImageInfo = rasterCodecs.GetInformation(tempFile, True) 
         format = info.Format 
         pageCount = info.TotalPages 
      End Using 
 
      If format = RasterImageFormat.Unknown Then 
         ' We do Not know what to do with this attachment, log And ignore it 
         Console.WriteLine("Could not convert attachment file to PDF, ignoring") 
         File.Delete(tempFile) 
      ElseIf format <> RasterImageFormat.RasPdf Then 
         ' Simple conversion using RasterImage. 
         ' TODO for the Example: A better way Is To use the DocumentConverter To Get True document conversion With all the 
         ' options available 
         Dim tempPdfFile As String = Path.GetTempFileName() 
 
         For pageNumber As Integer = 1 To pageCount 
            Using image As RasterImage = rasterCodecs.Load(tempFile, pageNumber) 
               rasterCodecs.Save(image, tempPdfFile, RasterImageFormat.RasPdfJpeg422, 0, 1, -1, -1, CodecsSavePageMode.Append) 
            End Using 
         Next 
 
         File.Copy(tempPdfFile, tempFile, True) 
         File.Delete(tempPdfFile) 
         tempFiles.Add(tempFile) 
      Else 
         ' TODO for the Example: Check if this file Is PDF with attachments And call the function recursively. 
         tempFiles.Add(tempFile) 
      End If 
   Next 
 
   ' Time to generate the final document, first the pages from the original document 
 
   ' Did we extract/convert any attachments? 
   If tempFiles.Count > 0 Then 
      ' Note that if the original document Is PDF portfolio, then it does Not contain real pages. Just a placeholder that should be ignored 
      If Not PDFFile.IsPortfolio(inputFileName, Nothing) Then 
         Dim pdfFile As New PDFFile(inputFileName) 
         pdfFile.MergeWith(tempFiles.ToArray(), outputFileName) 
      Else 
         ' This Is portfolio, so we will ignore the original file And just merge the attachments 
         Dim firstFile As String = tempFiles(0) 
         tempFiles.RemoveAt(0) 
         Dim pdfFile As New PDFFile(firstFile) 
         pdfFile.MergeWith(tempFiles.ToArray(), outputFileName) 
         File.Delete(firstFile) 
      End If 
   Else 
      ' No, just copy the original file over 
      File.Copy(inputFileName, outputFileName, True) 
   End If 
 
   ' Clean up 
   For Each tempFile As String In tempFiles 
      File.Delete(tempFile) 
   Next 
   rasterCodecs.Dispose() 
End Sub 
Requirements
Target Platforms
Help Version 21.0.2021.7.6
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Pdf Assembly

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.