Extract attachments from PDF documents

This example demonstrates how to extract embedded attachments — for example, images, documents, spreadsheets — from within a PDF and save them to a specified folder.

Prerequisites

  • Nutrient Document Converter Services (DCS) installed, licensed, and running.

  • Implemented OpenService() and CloseService() helpers.

Sample code

/// <summary>
        /// Extract attachments from a PDF file.
        /// </summary>
        /// <param name="ServiceURL">URL endpoint for the PDF Converter service.</param>
        /// <param name="sourceFileName">Source filename.</param>
        /// <param name="targetFolder">Target folder to receive the output file.</param>
        static void ExtractAttachmentsFromFile(string ServiceURL, string sourceFileName, string targetFolder)
        {
            Console.WriteLine($"Extracting attachments from {sourceFileName}");

            // Open a service client.
            DocumentConverterServiceClient client = null;

            // Create an `OpenOptions` instance with minimum properties.
            OpenOptions openOptions = new OpenOptions();
            openOptions.FileExtension = Path.GetExtension(sourceFileName);
            openOptions.OriginalFileName = Path.GetFileName(sourceFileName);
            try
            {

                // Read the source file into a byte array.
                byte[] sourceFile = File.ReadAllBytes(sourceFileName);

                // Open the service.
                client = OpenService(ServiceURL);


                // Perform the conversion.
                BatchResults results = client.ExtractEmbeddedFiles(sourceFile, openOptions);

                // Check if there are any results. If not, show a console message.
                if (results == null)
                {
                    Console.WriteLine($"No results returned");
                }
                else
                {
                    // If the target folder does not exist, create it.
                    if (!Directory.Exists(targetFolder))
                    {
                        Directory.CreateDirectory(targetFolder);
                    }
                    Console.WriteLine($"Output to: {targetFolder}");
                    // For each result returned.
                    foreach (BatchResult result in results.Results)
                    {
                        string filename = result.FileName;
                        Console.WriteLine(filename);
                        // Write the file contents to the new output file.
                        File.WriteAllBytes(Path.Combine(targetFolder, filename), result.File);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"{ex.Message}");
            }
            finally
            {
                // If the client has been opened, close it.
                if (client != null)
                {
                    CloseService(client);
                }
            }
        }
extract-attachments