Open PDF document programmatically

Open a PDF from a file

  1. Pass the input file path to the Document constructor.
  2. Read the number of pages with PageCollection::get_Count().
  3. Print the page count.
#include <iostream>

#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Text/TextFragment.h>
#include <Aspose.PDF.Cpp/Generator/Paragraphs.h>
#include <Aspose.PDF.Cpp/Exceptions/InvalidPasswordException.h>

using namespace System;
using namespace Aspose::Pdf;

void OpenDocument(String path_infile)
{
    auto document = MakeObject<Document>(path_infile);
    int countPages = document->get_Pages()->get_Count();
    std::cout << "Number of Pages :" << countPages << std::endl;
}

Open a PDF from a file stream

  1. Open a readable stream with System::IO::File::OpenRead().
  2. Pass the stream to the Document constructor.
  3. Read the number of pages with PageCollection::get_Count().
  4. Print the page count.
#include <iostream>

#include <system/io/file.h>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Text/TextFragment.h>
#include <Aspose.PDF.Cpp/Generator/Paragraphs.h>
#include <Aspose.PDF.Cpp/Exceptions/InvalidPasswordException.h>

using namespace System;
using namespace Aspose::Pdf;
void OpenDocumentStream(String path_infile)
{
    auto document = MakeObject<Document>(System::IO::File::OpenRead(path_infile));
    int countPages = document->get_Pages()->get_Count();
    std::cout << "Number of Pages :" << countPages << std::endl;
}

Open a PDF from memory

  1. Read the input PDF into a byte array.
  2. Create a System::IO::MemoryStream from the byte array.
  3. Pass the memory stream to the Document constructor.
  4. Read the number of pages with PageCollection::get_Count().
  5. Print the page count.
#include <iostream>

#include <system/io/file.h>
#include <system/io/memory_stream.h>

#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Generator/Paragraphs.h>

using namespace System;
using namespace Aspose::Pdf;

void OpenDocumentMemory(String path_infile)
{
    auto bytes = System::IO::File::ReadAllBytes(path_infile);
    auto document = MakeObject<Document>(MakeObject<System::IO::MemoryStream>(bytes));

    int countPages = document->get_Pages()->get_Count();
    std::cout << "Pages " << countPages << std::endl;
}

Open a password-protected PDF

  1. Pass the input file path and password to the Document constructor.
  2. Read the number of pages with PageCollection::get_Count().
  3. Print the page count.
  4. Catch InvalidPasswordException if the password is incorrect.
#include <iostream>

#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Generator/Paragraphs.h>
#include <Aspose.PDF.Cpp/Exceptions/InvalidPasswordException.h>

using namespace System;
using namespace Aspose::Pdf;
void OpenDocumentWithPassword(String path_infile, String password)
{
    try
    {
        auto document = MakeObject<Document>(path_infile, password);
        int countPages = document->get_Pages()->get_Count();

        std::cout << "Pages " << countPages << std::endl;
    }
    catch (Aspose::Pdf::InvalidPasswordException& e)
    {
        std::cerr << e.what();
    }
}