使用 C++ 在 PDF 中查找和替換文本

PDF 是一種流行的格式,廣泛用於在組織和個人之間共享文檔。在某些情況下,您必須在共享之前查找並替換 PDF 文檔中的某些文本。您可以手動執行此操作,但這會花費更多時間並且效率較低。更好更快的選擇是以編程方式執行此操作。在本文中,您將學習如何使用 C++ 查找和替換 PDF 文件中的文本。

用於查找和替換 PDF 文件中的文本的 C++ API

Aspose.PDF for C++ 是一個用於處理 PDF 文件的 C++ 庫。它提供了一系列功能,可幫助您自動化 PDF 工作流程的各個方面。其中一項功能是查找和替換 PDF 文件中的文本。您可以通過 NuGet 安裝 API 或直接從 下載 部分下載。

PM> Install-Package Aspose.PDF.Cpp

使用 C++ 在 PDF 中查找和替換文本

Aspose.PDF for C++ 提供了 TextFragmentAbsorber 類用於在 PDF 文檔中搜索文本。您使用要查找的文本初始化此類,並使用它來檢索所有匹配的文本片段。一旦所有片段都可用,您就可以遍歷它們並替換文本。以下是使用 C++ 在 PDF 文件中查找和替換文本的步驟。

以下是使用 C++ 在整個 PDF 文件中查找和替換文本的示例代碼。

// 加載PDF文件
auto pdfDocument = MakeObject<Document>(u"SourceDirectory\\Sample 1.pdf");

// 創建 TextAbsorber 對像以查找輸入搜索短語的所有實例
auto textFragmentAbsorber = MakeObject<TextFragmentAbsorber>(u"Document");

// 接受所有頁面的吸收器
pdfDocument->get_Pages()->Accept(textFragmentAbsorber);

// 獲取提取的文本片段
auto textFragmentCollection = textFragmentAbsorber->get_TextFragments();

// 遍歷片段
for (auto textFragment : textFragmentCollection)
{
	// 更新文本和其他屬性
	textFragment->set_Text(u"UPDATED TEXT");
	textFragment->get_TextState()->set_Font(FontRepository::FindFont(u"TimesNewRoman"));
	textFragment->get_TextState()->set_FontSize(22);
}

// 保存生成的 PDF 文檔。
pdfDocument->Save(u"OutputDirectory\\UpdatedDocument.pdf");

C++ 查找和替換特定 PDF 頁面中的文本

在某些情況下,您可能只想查找和替換特定頁面上的文本,而不是整個文檔。為此,接受要替換文本的頁面的 TextFragmentAbsorber 對象。以下是在 PDF 文檔的特定頁面上查找和替換文本的步驟。

以下是使用 C++ 查找和替換特定 PDF 頁面上的文本的示例代碼。

// 加載PDF文件
auto pdfDocument = MakeObject<Document>(u"SourceDirectory\\Sample 1.pdf");

// 創建 TextAbsorber 對像以查找輸入搜索短語的所有實例
auto textFragmentAbsorber = MakeObject<TextFragmentAbsorber>(u"Document");

// 接受文件第二頁的吸收器
pdfDocument->get_Pages()->idx_get(2)->Accept(textFragmentAbsorber);

// 獲取提取的文本片段
auto textFragmentCollection = textFragmentAbsorber->get_TextFragments();

// 遍歷片段
for (auto textFragment : textFragmentCollection)
{
	// 更新文本和其他屬性
	textFragment->set_Text(u"UPDATED TEXT");
	textFragment->get_TextState()->set_Font(FontRepository::FindFont(u"TimesNewRoman"));
	textFragment->get_TextState()->set_FontSize(22);
}

// 保存生成的 PDF 文檔。
pdfDocument->Save(u"OutputDirectory\\UpdatedDocument2.pdf");

使用 C++ 替換 PDF 頁面區域中的文本

您可以指定要替換文本的頁面區域,而不是搜索整個頁面。為此,API 提供了 Rectangle 類。以下是在 PDF 頁面的特定部分查找和替換文本的步驟。

以下是在特定 PDF 頁面區域中查找和替換文本的示例代碼。

// 加載PDF文件
auto pdfDocument = MakeObject<Document>(u"SourceDirectory\\Sample 1.pdf");

// 創建 TextAbsorber 對像以查找輸入搜索短語的所有實例
auto textFragmentAbsorber = MakeObject<TextFragmentAbsorber>(u"Document");

// 在頁面範圍內搜索文本
textFragmentAbsorber->get_TextSearchOptions()->set_LimitToPageBounds(true);

// 為 TextSearchOptions 指定頁面區域
textFragmentAbsorber->get_TextSearchOptions()->set_Rectangle(MakeObject<Rectangle>(100, 100, 800, 700));

// 接受文檔第一頁的吸收器
pdfDocument->get_Pages()->idx_get(1)->Accept(textFragmentAbsorber);

// 獲取提取的文本片段
auto textFragmentCollection = textFragmentAbsorber->get_TextFragments();

// 遍歷片段
for (auto textFragment : textFragmentCollection)
{
	// 更新文本和其他屬性
	textFragment->set_Text(u"UPDATED TEXT");
	textFragment->get_TextState()->set_Font(FontRepository::FindFont(u"TimesNewRoman"));
	textFragment->get_TextState()->set_FontSize(22);
}

// 保存生成的 PDF 文檔。
pdfDocument->Save(u"OutputDirectory\\UpdatedDocument.pdf");

使用正則表達式查找和替換 PDF 文件中的文本

Aspose.PDF for C++ 還提供了使用正則表達式搜索文本的能力。使用正則表達式,您可以找到電子郵件地址或電話號碼等文本。為此,您必須指定正則表達式而不是搜索字符串,並使用 TextSearchOptions 類來指示您正在使用正則表達式用於搜索的表達式。以下是使用正則表達式查找和替換 PDF 文件中的文本的步驟。

以下是使用正則表達式查找和替換 PDF 文件中文本的示例代碼。

// 加載PDF文件
auto pdfDocument = MakeObject<Document>(u"SourceDirectory\\Sample 2.pdf");

// 創建 TextAbsorber 對像以查找輸入搜索短語的所有實例
auto textFragmentAbsorber = MakeObject<TextFragmentAbsorber>(u"\\d{4} - \\d{4}"); // Like 1999-2000

// 設置文本搜索選項以啟用正則表達式使用
auto textSearchOptions = MakeObject<TextSearchOptions>(true);
textFragmentAbsorber->set_TextSearchOptions(textSearchOptions);

// 接受文檔所有頁面的吸收器
pdfDocument->get_Pages()->Accept(textFragmentAbsorber);

// 獲取提取的文本片段
auto textFragmentCollection = textFragmentAbsorber->get_TextFragments();

// 遍歷片段
for (auto textFragment : textFragmentCollection)
{
	// 更新文本和其他屬性
	textFragment->set_Text(u"UPDATED TEXT");
	textFragment->get_TextState()->set_Font(FontRepository::FindFont(u"TimesNewRoman"));
	textFragment->get_TextState()->set_FontSize(22);
}

// 保存生成的 PDF 文檔。
pdfDocument->Save(u"OutputDirectory\\UpdatedDocument.pdf");

獲得免費許可證

您可以通過申請 免費的臨時許可證 來試用沒有評估限制的 API。

結論

在本文中,您學習瞭如何使用 C++ 查找和替換 PDF 文件中的文本。您已經了解瞭如何替換整個 PDF 文檔、特定 PDF 頁面或頁面的特定區域中的文本。此外,您還學習瞭如何使用正則表達式搜索和替換文本。 Aspose.PDF for C++ 是一個強大的 API,具有許多附加功能,使處理 PDF 文檔變得輕而易舉。您可以使用 官方文檔 詳細探索 API。如果您有任何疑問,請隨時在論壇 上與我們聯繫。

也可以看看