PDF 转 Office
Foxit PDF SDK 在 Windows 和 Linux 平台上提供了 API,可将 PDF 文件转换为 MS office 套件格式,同时保持原始文档的布局和格式。
系统要求
平台: Windows, Linux
开发语言: C, C++, Java, Python, C#
License Key: license key 中包含 'PDF2Office' 模块的权限
SDK 版本: Foxit PDF SDK for Windows (C, C++, Java, Python, C#) 9.0 或更高版本; Foxit PDF SDK for Linux (C, C++, Java, Python, C#) 9.1 或更高版本
PDF 转 Office 资源文件
请联系 Foxit 支持团队或者销售团队以获取 PDF 转 Office 资源文件包 (Foxit PDF Conversion SDK (C++))。
备注:
- 从版本 9.2 开始,要求使用 Foxit PDF Conversion SDK 1.5 或更高版本。
- 对于版本 9.0 / 9.1,需要使用 Foxit PDF Conversion SDK 1.4 或更低版本。
当获取到 Foxit PDF Conversion SDK 包后,将其解压到所需目录 (比如,Windows 解压到"/foxitpdfconversionsdk_*_win/",Linux x86/x64 解压到"/foxitpdfconversionsdk_*_Linux/",Linux armv7/armv8 解压到"/foxitpdfconversionsdk_*_Linux_arm/")。解压后用于 PDF 转 Office 的资源文件如下所示:
Windows:
Linux x86/x64:
Linux armv7/armv8:
如何运行 pdf2office demo
Foxit PDF SDK 提供了一个 pdf2office demo 用来展示如何使用 Foxit PDF SDK 将 PDF 文件转换为 MS office 套件格式。该 demo 位于 "\examples\simple_demo\pdf2office" 文件夹下。
准备一个 PDF2Office 资源目录
在运行 pdf2office demo 之前,您需要先将 PDF2Office 资源文件 (Foxit PDF Conversion SDK) 解压到一个指定的目录(比如,Windows 解压到:"C:/foxitpdfconversionsdk_*_win/"),然后将 "lib" 文件夹下的引擎文件传递给 API PDF2Office.initialize,以初始化 PDF2Office 引擎。
配置 demo
对于 pdf2office demo,您可以在"\examples\simple_demo\html2pdf\pdf2office.java"文件中配置 demo。
指定 pdf2office 引擎目录
在 "pdf2office.java" 文件中,如下所示,添加 "pdf2office" 引擎文件的路径,用以将 PDF 文件转换为 office 文件。
// Please ensure the path is valid.
PDF2Office.initialize("C:/foxitpdfconversionsdk\_\*\_win/lib/fpdfconversionsdk\_win32.dll");
指定 pdf2office metrics 数据文件
PDF2OfficeSettingData setting\_data = new PDF2OfficeSettingData();
// Please ensure the path is valid.
setting\_data.setMetrics\_data\_folder\_path("C:/foxitpdfconversionsdk\_\*\_win/res/metrics\_data");
(可选) 指定是否启用基于机器学习的识别功能
setting\_data.enable\_ml\_recognition(false);
(可选) 指定要转换的页面范围
setting\_data.setPage\_range(new Range());
(可选) 指定是否转换 PDF 文档中的注释
setting\_data.setInclude\_pdf\_comments(true);
(可选) 对于 PDF 转 Word,指定是否保留其页面布局
setting\_data.getWord\_setting\_data().setEnable\_retain\_page\_layout(false);
运行 demo
成功运行 demo 后,控制台将默认打印以下内容:
输出文档位于\examples\simple_demo\output\_files\pdf2office
文件夹下。
如何使用 PDF2office API
import com.foxit.sdk.PDFException;
import com.foxit.sdk.common.Library;
import com.foxit.sdk.common.Progressive;
import com.foxit.sdk.common.fxcrt.FileReaderCallback;
import com.foxit.sdk.addon.conversion.pdf2office.PDF2Office;
import com.foxit.sdk.addon.conversion.pdf2office.ConvertCallback;
import com.foxit.sdk.addon.conversion.pdf2office.PDF2OfficeSettingData;
import java.io.\*;
class CustomConvertCallback extends ConvertCallback {
CustomConvertCallback() {}
@Override
public boolean needToPause() {
return true;
}
@Override
public void progressNotify(int converted\_count, int total\_count) {
}
}
CustomConvertCallback callback = new CustomConvertCallback();
Progressive progressive = PDF2Office.startConvertToWord(input\_path + "word.pdf", null, output\_path + "pdf2word\_result.docx", setting\_data, callback);
if (progressive.getRateOfProgress() != 100)
{
int state = Progressive.e\_ToBeContinued;
while (Progressive.e\_ToBeContinued == state)
{
state = progressive.resume();
}
}
System.out.println("Convert PDF file to Word format file.");
progressive = PDF2Office.startConvertToExcel(input\_path + "excel.pdf", null, output\_path + "pdf2excel\_result.xlsx", setting\_data, callback);
if (progressive.getRateOfProgress() != 100)
{
int state = Progressive.e\_ToBeContinued;
while (Progressive.e\_ToBeContinued == state)
{
state = progressive.resume();
}
}
System.out.println("Convert PDF file to Excel format file.");
progressive = PDF2Office.startConvertToPowerPoint(input\_path + "powerpoint.pdf", null, output\_path + "pdf2powerpoint\_result.pptx", setting\_data, callback);
if (progressive.getRateOfProgress() != 100)
{
int state = Progressive.e\_ToBeContinued;
while (Progressive.e\_ToBeContinued == state)
{
state = progressive.resume();
}
}
System.out.println("Convert PDF file to PowerPoint format file.");