Open PDF document programmatically

The following code snippet also work with Aspose.PDF.Drawing library.

Open existing PDF document

There are several ways to open a document. The easiest is to specify a file name.

public static void OpenDocument()
{
    var fileName = @"C:\tmp\tourguidev2_gb_tags.pdf";
    using (var pdfDocument = new Aspose.Pdf.Document(fileName))
    {
        Console.WriteLine($"Pages {pdfDocument.Pages.Count}");
    }
}

Open existing PDF document from stream

public static void OpenDocumentStream()
{
    const string fileName = "SJPR0033_Folder_Utland_16sid_ENG_web3.pdf";
    var remoteUri = "https://www.sj.se/content/dam/SJ/pdf/Engelska/";
    // Create a new WebClient instance.
    var webClient = new WebClient();
    // Concatenate the domain with the Web resource filename.
    var strWebResource = remoteUri + fileName;
    Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, strWebResource);

    var stream = new MemoryStream();
    webClient.OpenRead(strWebResource)?.CopyTo(stream);

    using (var pdfDocument = new Aspose.Pdf.Document(stream))
    {
        Console.WriteLine($"Pages {pdfDocument.Pages.Count}");
    }
}

Open encrypted PDF document

public static void OpenDocumentWithPassword()
{
    const string fileName = @"C:\tmp\DocSite.pdf";
    const string password = "Aspose2020";
    try
    {
        using (var pdfDocument = new Aspose.Pdf.Document(fileName, password))
        {
            Console.WriteLine($"Pages {pdfDocument.Pages.Count}");
        }
    }
    catch (InvalidPasswordException e)
    {
        Console.WriteLine(e);
    }
}