How to load a PDF document in C# and VB.Net

  1. Load from a file:
    
    DocumentCore dc = DocumentCore.Load(@"d:\Book.pdf");
    
    The dc object represents a document loaded into memory. The file format is detected automatically from the file extension: ".pdf".

    After loading you'll get the document presented as the Tree Of Objects, where the root node is DocumentCore class.

    To guarantee that a loadable content is really PDF and set some loading options, use PdfLoadOptions as 2nd parameter.

    
    DocumentCore dc = DocumentCore.Load(@"d:\Book.pdf", new PdfLoadOptions());
    
  2. Load from a Stream:
    
                // Assume that we already have a PDF document as bytes array.
                byte[] pdfBytes = null;
                // pdfBytes = ...
    
                DocumentCore dc = null;
                using (MemoryStream pdfStream = new MemoryStream(pdfBytes))
                {
                    dc = DocumentCore.Load(pdfStream, new PdfLoadOptions());
                }
                // Here we can do with our document 'dc' anything we need.
 

Complete code

using System;
using System.IO;
using SautinSoft.Document;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            LoadPDFFromFile();
            //LoadPDFFromStream();
        }
        // From a file
        static void LoadPDFFromFile()
        {
            string filePath = @"d:\Book.pdf";
            // The file format is detected automatically from the file extension: ".pdf".
            // But as shown in the example below, we can specify PdfLoadOptions as 2nd parameter
            // to explicitly set that a loadable document has PDF format.
            DocumentCore dc = DocumentCore.Load(filePath);
        }
        static void LoadPDFFromStream()
        {
            // Assume that we already have a PDF document as bytes array.
            byte[] fileBytes = File.ReadAllBytes(@"d:\Book.pdf");

            DocumentCore dc = null;
            // Create a MemoryStream
            using (MemoryStream ms = new MemoryStream(fileBytes))
            {
                // Specifying PdfLoadOptions we explicitly set that a loadable document is PDF.
                // Also we specified here to load only 1st page and 
                // switched off the 'OptimizeImage' to not merge adjacent images into a one.
                PdfLoadOptions pdfLO = new PdfLoadOptions()
                {
                    FromPage = 1,
                    ToPage = 1,
                    OptimizeImages = false
                };

                // Load a PDF document from the MemoryStream.
                dc = DocumentCore.Load(ms, new PdfLoadOptions());
            }
        }
    }
}
        
            Imports System
Imports System.IO
Imports SautinSoft.Document

Module ExampleVB

    Sub Main()
        LoadPDFFromFile()
        'LoadPDFFromStream();
    End Sub
    ' From a file
    Public Sub LoadPDFFromFile()
        Dim filePath As String = "d:\Book.pdf"
        ' The file format is detected automatically from the file extension: ".pdf".
        ' But as shown in the example below, we can specify PdfLoadOptions as 2nd parameter
        ' to explicitly set that a loadable document has PDF format.
        Dim dc As DocumentCore = DocumentCore.Load(filePath)
    End Sub
    Public Sub LoadPDFFromStream()
        ' Get document bytes.
        Dim fileBytes() As Byte = File.ReadAllBytes("d:\Book.pdf")

        Dim dc As DocumentCore = Nothing
        ' Create a MemoryStream
        Using ms As New MemoryStream(fileBytes)
            ' Specifying PdfLoadOptions we explicitly set that a loadable document is PDF.
            ' Also we specified here to load only 1st page and 
            ' switched off the 'OptimizeImage' to not merge adjacent images into a one.
            Dim pdfLO As New PdfLoadOptions() With {
                    .FromPage = 1,
                    .ToPage = 1,
                    .OptimizeImages = False
                }

            ' Load a PDF document from the MemoryStream.
            dc = DocumentCore.Load(ms, New PdfLoadOptions())
        End Using
    End Sub
End Module
© SautinSoft 2018