搜索和替换 (Search and Replace)
搜索和替换功能允许您在 PDF 文档中查找特定的文本内容,并用新的内容替换它。
系统需求
平台: Windows, Linux, Mac
开发语言: C, C++, Java, C#, Python, Objective-C, Node.js
授权许可: 包含 AdvEdit
模块的授权码
SDK 版本: Foxit PDF SDK 9.0 或更高版本
使用搜索和替换功能
c++
#include "include/common/fs_common.h"
#include "include/pdf/fs_pdfdoc.h"
#include "include/addon/pageeditor/fs_searchreplace.h"
using namespace foxit;
using namespace foxit::common;
using foxit::common::Library;
using namespace pdf;
using namespace foxit::addon::pageeditor;
PDFDoc doc(input_file);
ErrorCode error_code = doc.Load();
// Instantiate a TextSearchReplace object.
TextSearchReplace text_searchreplace(doc);
// Configure search options, match whole words only, whether to set match only whole words and match case.
FindOption find_option(true, true);
ReplaceCallbackImpl* replace_callback = new ReplaceCallbackImpl();
// Set replacing callback function.
text_searchreplace.SetReplaceCallback(replace_callback);
// Set keywords and page index to do searching and replacing.
text_searchreplace.SetPattern(L"PDF", 0, find_option);
// Replace with new text.
while (text_searchreplace.ReplaceNext(L"PDC")) {}
C
#include "include/fs_basictypes_c.h"
#include "include/fs_common_c.h"
#include "include/fs_pdfdoc_c.h"
#include "include/fs_searchreplace_c.h"
FSErrorCode error_code = e_FSErrSuccess;
FS_TEXTSEARCHREPLACE_HANDLE searchreplace = NULL;
FSFindOption find_option;
FSReplaceCallback* replacecallback = NULL;
error_code = FSDK_PDFDoc_Load(doc, NULL);
// Instantiate a TextSearchReplace object.
FSDK_TextSearchReplace_Create(doc, &searchreplace);
// Set replacing callback function.
FSDK_TextSearchReplace_SetReplaceCallback(searchreplace, &replacecallback);
// Set keywords and page index to do searching and replacing.
FSDK_TextSearchReplace_SetPattern(searchreplace, L"PDF", 0, find_option);
// Replace with new text.
while (result) FSDK_TextSearchReplace_ReplaceNext(searchreplace, L"PDC", &result);
java
import com.foxit.sdk.common.fxcrt.RectF;
import com.foxit.sdk.common.fxcrt.RectFArray;
import com.foxit.sdk.pdf.PDFDoc;
import com.foxit.sdk.addon.pageeditor.*;
import static com.foxit.sdk.common.Constants.e_ErrSuccess;
PDFDoc doc = new PDFDoc(input_file);
error_code = doc.load(null);
// Instantiate a TextSearchReplace object.
TextSearchReplace searchreplace = new TextSearchReplace(doc);
// Configure search options, match whole words only, whether to set match only whole words and match case.
FindOption find_option = new FindOption();
ReplaceCallbackImpl replace_callback = new ReplaceCallbackImpl();
// Set replacing callback function.
searchreplace.setReplaceCallback(replace_callback);
// Set keywords and page index to do searching and replacing.
searchreplace.setPattern("PDF", 0, find_option);
// Replace with new text.
while(searchreplace.replaceNext("PDC")) {}
py
import os
import sys
import site
doc = PDFDoc(input_file)
error_code = doc.Load("")
# Instantiate a TextSearchReplace object.
searchreplace = TextSearchReplace(doc)
# Configure search options, match whole words only, whether to set match only whole words and match case.
find_option = FindOption(True, True)
# Set replacing callback function.
searchreplace.SetReplaceCallback(ReplaceCallbackImpl())
# Set keywords and page index to do searching and replacing.
searchreplace.SetPattern(pattern, 0, find_option)
# Replace with new text.
while searchreplace.ReplaceNext("PDC") == True:
objc
#include "FSPDFObjC.h"
FSPDFDoc *doc = [[FSPDFDoc alloc] initWithPath:input_file];
FSErrorCode code = [doc load:nil];
// Instantiate a TextSearchReplace object.
FSTextSearchReplace *searchreplace = [[FSTextSearchReplace alloc] initWithDoc:doc];
// Configure search options, match whole words only, whether to set match only whole words and match case.
FSFindOption* find_option = [[FSFindOption alloc] initWithIs_whole_word:true is_case_sensitive:true];
ReplaceCallback* replace_call_back = [[ReplaceCallback alloc] init];
// Set replacing callback function.
[searchreplace setReplaceCallback:replace_call_back];
// Set keywords and page index to do searching and replacing.
[searchreplace setPattern:pattern page_index:0 find_option:find_option];
// Replace with new text.
while ([searchreplace replaceNext:@"PDC"]) {}
js
const FSDK = require("@foxitsoftware/foxit-pdf-sdk-node");
let doc = new FSDK.PDFDoc(input_file);
let error_code = doc.Load("");
// Instantiate a TextSearchReplace object.
let text_searchreplace = new FSDK.TextSearchReplace(doc);
// Configure search options, match whole words only, whether to set match only whole words and match case.
let find_option = new FSDK.FindOption(true, true);
// Set replacing callback function.
let replace_callback_impl = new ReplaceCallbackImpl();
let replace_callback = new FSDK.ReplaceCallback(replace_callback_impl);
text_searchreplace.SetReplaceCallback(replace_callback);
// Set keywords and page index to do searching and replacing.
text_searchreplace.SetPattern("PDF", 0, find_option);
// Replace with new text.
while (text_searchreplace.ReplaceNext("PDC")) {}
csharp
using foxit.common;
using foxit.common.fxcrt;
using foxit.pdf;
using foxit.addon.pageeditor;
…
using (PDFDoc doc = new PDFDoc(input_file))
{
error_code = doc.Load(null);
// Instantiate a TextSearchReplace object.
using (TextSearchReplace searchreplace = new TextSearchReplace(doc))
// Configure search options, match whole words only, whether to set match only whole words and match case.
using (FindOption find_option = new FindOption(true, true))
using (ReplaceCallbackImpl replace_callback = new ReplaceCallbackImpl())
// Set replacing callback function.
searchreplace.SetReplaceCallback(replace_callback);
// Set keywords and page index to do searching and replacing.
searchreplace.SetPattern("PDF", 0, find_option);
// Replace with new text.
while (searchreplace.ReplaceNext("PDC") == true) {}
}