How to Convert PDF to Image Using Itextsharp

iTextSharp

iTextSharp provides a powerful API for working with PDF files. Developers can control various aspects of the document, including page size, orientation, margins, and headers/footers. It also supports the creation of bookmarks, links, and annotations within the PDF.

PDF Focus .NET

PDF Focus .NET is a powerful software development kit (SDK) that allows developers to work with PDF files in their .NET applications. It provides a wide range of capabilities for creating, editing, manipulating, converting, and extracting data from PDF documents.

Furthermore, PDF Focus .NET supports digital signatures, allowing developers to sign PDF documents with digital certificates for security and authentication purposes.

    To convert PDF to images using iTextSharp library in C#, you can follow these steps:
  1. Install iTextSharp library:
    • Open Visual Studio.
    • Go to "Tools" > "NuGet Package Manager" > "Manage NuGet Packages for Solution".
    • Search for "iTextSharp" and click "Install" for the iTextSharp package.
  2. Add reference and using statement:
    • Right-click on the "References" folder in your project in Visual Studio.
    • Select "Add Reference" and browse for the iTextSharp dll file (e.g., iTextSharp.dll) and click "OK".
    • Add the following using statement at the top of your code file:
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using System.IO;
    
  3. Write the conversion code:
    string pdfFilePath = @"path\to\your\pdf\file.pdf"; // Path to the PDF file
       string outputFolderPath = @"path\to\output\folder"; // Path to the output folder where the images will be saved
    
       using (var reader = new PdfReader(pdfFilePath))
       {
           for (int pageNumber = 1; pageNumber <= reader.NumberOfPages; pageNumber++)
           {
               string outputFilePath = Path.Combine(outputFolderPath, $"page_{pageNumber}.png");
               var page = reader.GetPageN(pageNumber);
               var size = page.GetPageSizeWithRotation();
    
               using (var document = new Document(size))
               {
                   using (var renderer = new PdfRenderer(document, outputFilePath))
                   {
                       PdfWriter.GetInstance(document, new FileStream(outputFilePath, FileMode.Create));
                       document.Open();
                       var cb = renderer.DirectContent;
                       document.NewPage();
                       renderer.RenderPage(cb, pageNumber);
                   }
               }
           }
       }
    
    Make sure to replace `"path\to\your\pdf\file.pdf"` with the actual path to your PDF file, and `"path\to\output\folder"` with the desired path for the output images folder.
  4. Run the code:
    • Build and run your C# program.
    • The PDF file will be converted to separate image files (PNG format) for each page, and saved in the specified output folder.

Please note that the iTextSharp library is no longer maintained by its original developers and there are other alternatives available for PDF to image conversion in C#.