用C++打开PDF文档
Contents
[
Hide
]
打开现有的PDF文档
有几种方法可以打开文档。最简单的是指定一个文件名。
void OpenDocument()
{
String fileName("C:\\Samples\\sample.pdf");
auto document = MakeObject<Document>(fileName);
int countPages = document->get_Pages()->get_Count();
std::cout << "Pages " << countPages << std::endl;
}
从流中打开现有的PDF文档
void OpenDocumentStream()
{
String fileName("C:\\Samples\\sample.pdf");
System::SharedPtr<System::IO::MemoryStream> stream;
auto document = MakeObject<Document>(System::IO::File::OpenRead(fileName));
int countPages = document->get_Pages()->get_Count();
std::cout << "Pages " << countPages << std::endl;
}
打开加密的PDF文档
void BasicOperations::OpenDocumentWithPassword()
{
String fileName("C:\\Samples\\sample-pswd.pdf");
String password("Aspose2020");
try
{
auto document = MakeObject<Document>(fileName, password);
int countPages = document->get_Pages()->get_Count();
std::cout << "页数 " << countPages << std::endl;
}
catch (InvalidPasswordException e)
{
std::cerr << e.what();
}
}
从内存缓冲区打开PDF文档
#include <system/io/memory_stream.h>
#include <system/buffer.h>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
// ...
void OpenDocumentMemory(const uint8_t *buffer, int size) {
auto arr = MakeArray<uint8_t>(size);
Buffer::BlockCopy(buffer, 0, arr->data_ptr(), 0, size);
auto document = MakeObject<Document>(MakeObject<System::IO::MemoryStream>(arr));
int countPages = document->get_Pages()->get_Count();
std::cout << "页数 " << countPages << std::endl;
}