Process PDFs with LowCode in C++
The Aspose::Pdf::LowCode namespace provides processors for common PDF tasks. Create a processor and its options, register input and output paths with FileDataSource, and call Process(). Text extraction returns its content through a ResultContainer.
Add the relevant source code to a project configured with Aspose.PDF for C++, apply your license, and call the example function with your file paths. Use different input and output paths, and create the output directory before running an example that writes a file.
Convert PDF to DOCX
Convert a PDF into a Word document using the DOCX output format.
- Create a DocConverter and PdfToDocOptions.
- Set the save format to
SaveFormat::DocX. - Add the input PDF path and the output
.docxpath, then callProcess().
#include <Aspose.PDF.Cpp/LowCode/DocConverter/DocConverter.h>
#include <Aspose.PDF.Cpp/LowCode/DocConverter/PdfToDocOptions.h>
#include <Aspose.PDF.Cpp/SaveFormat.h>
#include <Aspose.PDF.Cpp/LowCode/Implementations/FileDataSource.h>
#include <system/string.h>
namespace LowCodePdfProcessingExamples
{
using namespace System;
using namespace Aspose::Pdf::LowCode;
void ConvertPdfToDocx(String inputFile, String outputFile)
{
auto processor = MakeObject<DocConverter>();
auto options = MakeObject<PdfToDocOptions>();
options->set_SaveFormat(SaveFormat::DocX);
options->AddInput(MakeObject<FileDataSource>(inputFile));
options->AddOutput(MakeObject<FileDataSource>(outputFile));
processor->Process(options);
}
}
Extract Text from a PDF
Read text from a PDF and print each result to the console.
- Create a TextExtractor and TextExtractorOptions.
- Add the input PDF path and call
Process(). - Iterate over the result collection and print each result using
ToString().
#include <Aspose.PDF.Cpp/LowCode/TextExtractor/TextExtractor.h>
#include <Aspose.PDF.Cpp/LowCode/TextExtractor/TextExtractorOptions.h>
#include <Aspose.PDF.Cpp/LowCode/ResultContainer.h>
#include <Aspose.PDF.Cpp/LowCode/IOperationResult.h>
#include <Aspose.PDF.Cpp/LowCode/Implementations/FileDataSource.h>
#include <system/string.h>
#include <system/console.h>
#include <system/collections/list.h>
namespace LowCodePdfProcessingExamples
{
using namespace System;
using namespace Aspose::Pdf::LowCode;
void ExtractPdfText(String inputFile)
{
auto processor = MakeObject<TextExtractor>();
auto options = MakeObject<TextExtractorOptions>();
options->AddInput(MakeObject<FileDataSource>(inputFile));
// Text extraction returns text through the result collection.
auto results = processor->Process(options);
for (const auto& result : results->get_ResultCollection())
{
Console::WriteLine(result->ToString());
}
}
}
Flatten All PDF Form Fields
Flatten every field in a filled PDF form so its visible appearance becomes part of the page content.
- Create a FormFlattener and FormFlattenAllFieldsOptions.
- Add the filled PDF form and the output PDF path.
- Call
Process()to save the flattened form.
#include <Aspose.PDF.Cpp/LowCode/FormFlattener/FormFlattener.h>
#include <Aspose.PDF.Cpp/LowCode/FormFlattener/FormFlattenAllFieldsOptions.h>
#include <Aspose.PDF.Cpp/LowCode/Implementations/FileDataSource.h>
#include <system/string.h>
namespace LowCodePdfProcessingExamples
{
using namespace System;
using namespace Aspose::Pdf::LowCode;
void FlattenFormFields(String inputFile, String outputFile)
{
auto processor = MakeObject<FormFlattener>();
// Use a PDF containing filled form fields as input.
auto options = MakeObject<FormFlattenAllFieldsOptions>();
options->AddInput(MakeObject<FileDataSource>(inputFile));
options->AddOutput(MakeObject<FileDataSource>(outputFile));
processor->Process(options);
}
}
Linearize a PDF
Prepare a PDF for progressive loading by saving a linearized copy.
- Create an Optimizer and OptimizeOptions.
- Add the input and output PDF paths.
- Call
Process()to write the linearized PDF.
#include <Aspose.PDF.Cpp/LowCode/Optimizer/Optimizer.h>
#include <Aspose.PDF.Cpp/LowCode/Optimizer/OptimizeOptions.h>
#include <Aspose.PDF.Cpp/LowCode/Implementations/FileDataSource.h>
#include <system/string.h>
namespace LowCodePdfProcessingExamples
{
using namespace System;
using namespace Aspose::Pdf::LowCode;
void OptimizePdf(String inputFile, String outputFile)
{
auto processor = MakeObject<Optimizer>();
// OptimizeOptions requests document linearization.
auto options = MakeObject<OptimizeOptions>();
options->AddInput(MakeObject<FileDataSource>(inputFile));
options->AddOutput(MakeObject<FileDataSource>(outputFile));
processor->Process(options);
}
}
Encrypt a PDF with AES-256
Protect a PDF with owner and user passwords and restrict user permissions with the ForbidAll preset.
- Create a Security processor.
- Create EncryptionOptions with the owner and user passwords, the ForbidAll privilege preset, and the AES-256 algorithm.
- Add the input and output PDF paths, then call
Process(). - Read the first operation result and print the output path returned by
ToFile().
#include <Aspose.PDF.Cpp/LowCode/Security/Security.h>
#include <Aspose.PDF.Cpp/LowCode/Security/EncryptionOptions.h>
#include <Aspose.PDF.Cpp/LowCode/Implementations/FileDataSource.h>
#include <Aspose.PDF.Cpp/LowCode/ResultContainer.h>
#include <Aspose.PDF.Cpp/LowCode/IOperationResult.h>
#include <Aspose.PDF.Cpp/LowCode/Implementations/FileResult.h>
#include <system/string.h>
#include <system/console.h>
#include <system/collections/list.h>
#include <Aspose.PDF.Cpp/Facades/DocumentPrivilege.h>
namespace LowCodePdfProcessingExamples
{
using namespace System;
using namespace Aspose::Pdf::LowCode;
void EncryptPdf(String inputFile, String outputFile, String ownerPassword, String userPassword)
{
auto processor = MakeObject<Aspose::Pdf::LowCode::Security>();
auto options = MakeObject<EncryptionOptions>(ownerPassword, userPassword,
Aspose::Pdf::Facades::DocumentPrivilege::get_ForbidAll(),
Aspose::Pdf::CryptoAlgorithm::AESx256);
options->AddInput(MakeObject<FileDataSource>(inputFile));
options->AddOutput(MakeObject<FileDataSource>(outputFile));
auto result = processor->Process(options)->get_ResultCollection()->idx_get(0);
Console::WriteLine(u"Encrypted PDF saved to: " + result->ToFile());
}
}
Add a Visible Digital Signature to a PDF
Sign the first page of a PDF and place a visible signature in a specified rectangle. Use a PDF with at least one page and a PFX or P12 certificate that contains the private key.
- Create a Signature processor and initialize SignOptions with the certificate path and password.
- Set the page number to 1, enable signature visibility, and specify the rectangle and signing reason.
- Add the input and output PDF paths, then call
Process()to save the signed PDF.
#include <Aspose.PDF.Cpp/LowCode/Implementations/FileDataSource.h>
#include <Aspose.PDF.Cpp/LowCode/Signature/Signature.h>
#include <Aspose.PDF.Cpp/LowCode/Signature/SignOptions.h>
#include <Aspose.PDF.Cpp/LowCode/IDataSource.h>
#include <system/string.h>
#include <Aspose.PDF.Cpp/Rectangle.h>
namespace LowCodePdfProcessingExamples
{
using namespace System;
using namespace Aspose::Pdf::LowCode;
void SignPdf(String inputFile, String outputFile, String certificateFile, String certificatePassword)
{
auto processor = MakeObject<Signature>();
// Supply a PFX/P12 certificate containing the private key.
auto options = MakeObject<SignOptions>(certificateFile, certificatePassword);
options->set_PageNumber(1);
options->set_Visible(true);
options->set_Rectangle(MakeObject<Aspose::Pdf::Rectangle>(50, 50, 250, 120));
options->set_Reason(u"Document approval");
options->AddInput(MakeObject<FileDataSource>(inputFile));
options->AddOutput(MakeObject<FileDataSource>(outputFile));
processor->Process(options);
}
}
Add a Table to a PDF
Add a two-column table with one header row and one data row to an existing PDF.
- Create a TableGenerator and TableOptions. Add the input and output PDF paths.
- Call the AddTable method on the options, then use TableBuilder and TableRowBuilder to create two rows with two cells each.
- Use TableCellBuilder to add a text fragment to each cell, then call the Process method to save the PDF.
#include <Aspose.PDF.Cpp/LowCode/PdfGenerator/TableGenerator.h>
#include <Aspose.PDF.Cpp/LowCode/PdfGenerator/TableOptions.h>
#include <Aspose.PDF.Cpp/LowCode/PdfGenerator/Builder/TableRowBuilder.h>
#include <Aspose.PDF.Cpp/LowCode/PdfGenerator/Builder/TableCellBuilder.h>
#include <Aspose.PDF.Cpp/LowCode/Implementations/FileDataSource.h>
#include <system/string.h>
#include <Aspose.PDF.Cpp/Generator/BaseParagraph.h>
#include <Aspose.PDF.Cpp/Text/TextFragment.h>
#include <system/array.h>
namespace LowCodePdfProcessingExamples
{
using namespace System;
using namespace Aspose::Pdf::LowCode;
void AddTableToPdf(String inputFile, String outputFile)
{
auto processor = MakeObject<TableGenerator>();
auto options = MakeObject<TableOptions>();
options->AddInput(MakeObject<FileDataSource>(inputFile));
options->AddOutput(MakeObject<FileDataSource>(outputFile));
auto table = options->AddTable();
auto header = table->AddRow();
header->AddCell()->AddParagraph(MakeArray<SharedPtr<Aspose::Pdf::BaseParagraph>>({
MakeObject<Aspose::Pdf::Text::TextFragment>(u"Document") }));
header->AddCell()->AddParagraph(MakeArray<SharedPtr<Aspose::Pdf::BaseParagraph>>({
MakeObject<Aspose::Pdf::Text::TextFragment>(u"Status") }));
auto row = table->AddRow();
row->AddCell()->AddParagraph(MakeArray<SharedPtr<Aspose::Pdf::BaseParagraph>>({
MakeObject<Aspose::Pdf::Text::TextFragment>(u"Annual report") }));
row->AddCell()->AddParagraph(MakeArray<SharedPtr<Aspose::Pdf::BaseParagraph>>({
MakeObject<Aspose::Pdf::Text::TextFragment>(u"Approved") }));
processor->Process(options);
}
}
Merge Two PDF Documents
Combine two PDFs into one file. Pages from the first input appear before pages from the second.
- Create a Merger and MergeOptions.
- Add both input PDF paths in the order their pages should appear.
- Add the output PDF path and call
Process()to write the merged file.
#include <Aspose.PDF.Cpp/LowCode/Merger/Merger.h>
#include <Aspose.PDF.Cpp/LowCode/Merger/MergeOptions.h>
#include <Aspose.PDF.Cpp/LowCode/Implementations/FileDataSource.h>
#include <system/string.h>
namespace LowCodePdfProcessingExamples
{
using namespace System;
using namespace Aspose::Pdf::LowCode;
void MergePdfDocuments(String firstInputFile, String secondInputFile, String outputFile)
{
auto processor = MakeObject<Merger>();
auto options = MakeObject<MergeOptions>();
// Input order determines the page order in the merged document.
options->AddInput(MakeObject<FileDataSource>(firstInputFile));
options->AddInput(MakeObject<FileDataSource>(secondInputFile));
options->AddOutput(MakeObject<FileDataSource>(outputFile));
processor->Process(options);
}
}
See the Aspose.PDF for C++ API Reference for the full API.