Open PDF document programmatically
Contents
[
Hide
]
Open a PDF from a file
- Pass the input file path to the Document constructor.
- Read the number of pages with PageCollection::get_Count().
- 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
- Open a readable stream with System::IO::File::OpenRead().
- Pass the stream to the Document constructor.
- Read the number of pages with PageCollection::get_Count().
- 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
- Read the input PDF into a byte array.
- Create a System::IO::MemoryStream from the byte array.
- Pass the memory stream to the Document constructor.
- Read the number of pages with PageCollection::get_Count().
- 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
- Pass the input file path and password to the Document constructor.
- Read the number of pages with PageCollection::get_Count().
- Print the page count.
- 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();
}
}