PDF 转 Office (福昕自研转换引擎)
福昕 PDF SDK 提供高效的 PDF 转 Office 功能, 支持将 PDF 文档转换为 Office 文档 (docx, xlsx, pptx)。本节将介绍如何调用福昕自研转换引擎 pdf2office 实现此功能。 开发者可查阅 PDF 转 Office 示例配置与运行指南 ,了解配置步骤和运行示例,快速掌握该功能的使用方法。
若需了解福昕自研转换引擎,请参阅 探索福昕转换 SDK。
配置 PDF2Office 转换超时
自 v10.1 版本起,福昕自研 PDF2Office 插件模块引入了转换超时机制。该机制允许您设置转换过程的最大允许时间,超过该时间限制后,转换过程将自动终止。
代码示例:
Java
PDF2OfficeSettingData settingData = new PDF2OfficeSettingData();
settingData.timeout = 60; // 设置超时时间为 60 毫秒。
PDF2Office converter = new PDF2Office();
converter.StartConvertToWord(pdfFilePath, outputFilePath, settingData);
使用 PDF2office 模块
以下是示例代码,演示了如何使用福昕 PDF SDK 的 PDF2Ofice 模块,在不依赖第三方引擎的情况下,将不同类型的 Office 文档转换为 PDF 文件。
c++
#include "include/common/fs_common.h"
#include "include/addon/conversion/pdf2office/fs_pdf2office.h"
using namespace std;
using namespace foxit;
using namespace foxit::common;
using namespace foxit::common::file;
using foxit::common::Library;
using namespace addon::conversion::pdf2office;
class CustomConvertCallback : public ConvertCallback {
public:
CustomConvertCallback() {}
~CustomConvertCallback() {}
virtual bool NeedToPause() {
return true;
}
virtual void ProgressNotify(int converted_count, int total_count) {}
};
CustomConvertCallback callback;
Progressive progressive = PDF2Office::StartConvertToWord(input_path + L"word.pdf", NULL, output_directory + L"pdf2word_result.docx", setting_data, &callback);
if (progressive.GetRateOfProgress() != 100) {
Progressive::State state = Progressive::e_ToBeContinued;
while (Progressive::e_ToBeContinued == state) {
state = progressive.Continue();
}
}
cout << "Convert PDF file to Word format file with path." << endl;
// Convert PDF file to Excel format file.
progressive = PDF2Office::StartConvertToExcel(input_path + L"excel.pdf", NULL, output_directory + L"pdf2excel_result.xlsx", setting_data);
if (progressive.GetRateOfProgress() != 100) {
Progressive::State state = Progressive::e_ToBeContinued;
while (Progressive::e_ToBeContinued == state) {
state = progressive.Continue();
}
}
cout << "Convert PDF file to Excel format file with path." << endl;
// Convert PDF file to PowerPoint format file.
progressive = PDF2Office::StartConvertToPowerPoint(input_path + L"powerpoint.pdf", NULL, output_directory + L"pdf2powerpoint_result.pptx", setting_data);
if (progressive.GetRateOfProgress() != 100) {
Progressive::State state = Progressive::e_ToBeContinued;
while (Progressive::e_ToBeContinued == state) {
state = progressive.Continue();
}
}
cout << "Convert PDF file to PowerPoint format file with path." << endl;
C
#include "include/fs_basictypes_c.h"
#include "include/fs_common_c.h"
#include "include/fx_stream_c.h"
#include "include/fs_pdf2office_c.h"
static FS_BOOL gNeedToPause(void* user_data) {
return TRUE;
}
static void gProgressNotify(void* user_data, int converted_count, int total_count) {
}
FS_BOOL is_large_file = FALSE;
FILE* file = NULL;
FILE* filestream = NULL;
int ref = 0;
FS_INT64 cur_pos = 0;
FS_INT64 gGetSize(void* user_data) {
if (!user_data) return 0;
if (is_large_file) {
long long size_long;
_fseeki64(file, 0, SEEK_END);
size_long = _ftelli64(file);
return size_long;
} else {
fseek(file, 0, SEEK_END);
return (FS_INT64)ftell(file);
}
return 0;
}
FSConvertCallback * callback_word = (FSConvertCallback*)malloc(sizeof(FSConvertCallback));
callback ->user_data = callback;
callback ->NeedToPause = gNeedToPause;
callback ->ProgressNotify = gProgressNotify;
FS_PROGRESSIVE_HANDLE progressive_handle;
FSErrorCode error_code = FSDK_PDF2Office_StartConvertToWord(src_pdf_path, NULL, saved_word_file_path, setting_data, callback_word, &progressive_handle);
if (error_code != e_FSErrSuccess) {
switch (error_code) {
case e_FSErrNoPDF2OfficeModuleRight:
printf("[Failed] Conversion module is not contained in current Foxit PDF Conversion SDK keys.\n");
break;
default:
printf("Exception: %d\n",error_code);
break;
}
return 1;
}
int rate;
FSDK_Progressive_GetRateOfProgress(progressive_handle, &rate);
if (rate != 100) {
FSState state = e_FSToBeContinued;
while (e_FSToBeContinued == state) {
FSDK_Progressive_Continue(progressive_handle, &state);
}
}
printf("Convert PDF file to Word format file with path.\n");
FSConvertCallback * callback_excel = (FSConvertCallback*)malloc(sizeof(FSConvertCallback));
callback ->user_data = callback;
callback ->NeedToPause = gNeedToPause;
callback ->ProgressNotify = gProgressNotify;
error_code = FSDK_PDF2Office_StartConvertToExcel(src_pdf_path, NULL, saved_excel_file_path, setting_data, callback_excel, &progressive_handle);
if (error_code != e_FSErrSuccess) {
switch (error_code) {
case e_FSErrNoPDF2OfficeModuleRight:
printf("[Failed] Conversion module is not contained in current Foxit PDF Conversion SDK keys.\n");
break;
default:
printf("Exception: \n");
break;
}
return 1;
}
FSDK_Progressive_GetRateOfProgress(progressive_handle, &rate);
if (rate != 100) {
FSState state = e_FSToBeContinued;
while (e_FSToBeContinued == state) {
FSDK_Progressive_Continue(progressive_handle, &state);
}
}
printf("Convert PDF file to Excel format file with path.\n");
FSConvertCallback * callback_powepoint = (FSConvertCallback*)malloc(sizeof(FSConvertCallback));
callback ->user_data = callback;
callback ->NeedToPause = gNeedToPause;
callback ->ProgressNotify = gProgressNotify;
error_code = FSDK_PDF2Office_StartConvertToPowerPoint(src_pdf_path, NULL, saved_ppt_file_path, setting_data, powepoint, &progressive_handle);
if (error_code != e_FSErrSuccess) {
switch (error_code) {
case e_FSErrNoPDF2OfficeModuleRight:
printf("[Failed] Conversion module is not contained in current Foxit PDF Conversion SDK keys.\n");
break;
default:
printf("Exception: %d\n",error_code);
break;
}
return 1;
}
FSDK_Progressive_GetRateOfProgress(progressive_handle, &rate);
if (rate != 100) {
FSState state = e_FSToBeContinued;
while (e_FSToBeContinued == state) {
FSDK_Progressive_Continue(progressive_handle, &state);
}
}
printf("Convert PDF file to PowerPoint format file with path.\n");
java
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.");
py
import os
import sys
import site
if sys.version_info.major == 2:
_PYTHON2_ = True
else:
_PYTHON2_ = False
if _PYTHON2_:
site.addsitedir('../../../')
from FoxitPDFSDKPython2 import *
else:
from FoxitPDFSDKPython3 import *
class CustomConvertCallback(ConvertCallback):
def __init__(self, *args):
if _PYTHON2_:
super(CustomConvertCallback, self).__init__(*args)
else:
super().__init__(*args)
def __del__(self):
self.__disown__()
def Release(self, *args):
pass
def NeedToPause(self, *args):
return True
def ProgressNotify(self, *args):
converted_count = args[0]
total_count = args[1]
callback = CustomConvertCallback()
progressive = PDF2Office.StartConvertToWord(input_path + "word.pdf", "", output_directory + "pdf2word_result.docx", setting_data, callback)
if progressive.GetRateOfProgress() != 100:
state = Progressive.e_ToBeContinued
while (Progressive.e_ToBeContinued == state):
state = progressive.Continue()
print("Convert PDF file to Word format file with path.")
progressive = PDF2Office.StartConvertToExcel(input_path + "excel.pdf", "", output_directory + "pdf2excel_result.xlsx", setting_data, callback)
if progressive.GetRateOfProgress() != 100:
state = Progressive.e_ToBeContinued
while (Progressive.e_ToBeContinued == state):
state = progressive.Continue()
print("Convert PDF file to Excel format file with path.")
progressive = PDF2Office.StartConvertToPowerPoint(input_path + "powerpoint.pdf", "", output_directory + "pdf2powerpoint_result.pptx", setting_data, callback)
if progressive.GetRateOfProgress() != 100:
state = Progressive.e_ToBeContinued
while Progressive.e_ToBeContinued == state:
state = progressive.Continue()
print("Convert PDF file to PowerPoint format file with path.")
js
const FSDK = require("@foxitsoftware/foxit-pdf-sdk-node");
class CustomConvertCallback {
constructor() {
}
NeedToPause() {
return true;
}
ProgressNotify(converted_count, total_count) {
}
};
let custom_callback = new CustomConvertCallback();
let convert_callback = new FSDK.ConvertCallback(custom_callback);
let progressive = FSDK.PDF2Office.StartConvertToWord(input_path + "word.pdf", "", output_directory + "pdf2word_result.docx", setting_data, convert_callback);
if (progressive.GetRateOfProgress() != 100) {
let state = FSDK.Progressive.e_ToBeContinued;
while (FSDK.Progressive.e_ToBeContinued == state) {
state = progressive.Continue();
}
}
progressive = FSDK.PDF2Office.StartConvertToWord(reader_callback_word, "", stream_callback_word, setting_data, convert_callback_word_stream);
if (progressive.GetRateOfProgress() != 100) {
let state = FSDK.Progressive.e_ToBeContinued;
while (FSDK.Progressive.e_ToBeContinued == state) {
state = progressive.Continue();
}
}
progressive = FSDK.PDF2Office.StartConvertToExcel(reader_callback_excel, "", stream_callback_excel, setting_data, convert_callback_excel_stream);
if (progressive.GetRateOfProgress() != 100) {
let state = FSDK.Progressive.e_ToBeContinued;
while (FSDK.Progressive.e_ToBeContinued == state) {
state = progressive.Continue();
}
}
csharp
using System;
using System.IO;
using System.Runtime.InteropServices;
using foxit;
using foxit.common;
using foxit.common.fxcrt;
using foxit.addon.conversion.pdf2office;
public class CustomConvertCallback : ConvertCallback
{
public CustomConvertCallback() { }
public override bool NeedToPause()
{
return true;
}
public override void ProgressNotify(int converted_count, int total_count)
{
}
}
using (CustomConvertCallback callback = new CustomConvertCallback())
{
using (Progressive progressive = PDF2Office.StartConvertToWord(input_path + "word.pdf", null, output_path + "pdf2word_result.docx", setting_data, callback))
{
if (progressive.GetRateOfProgress() != 100)
{
Progressive.State state = Progressive.State.e_ToBeContinued;
while (Progressive.State.e_ToBeContinued == state)
{
state = progressive.Continue();
}
}
Console.WriteLine("Convert PDF file to Word format file with path.");
}
}
using (CustomConvertCallback callback = new CustomConvertCallback())
{
using (Progressive progressive = PDF2Office.StartConvertToExcel(input_path + "excel.pdf", null, output_path + "pdf2excel_result.xlsx", setting_data, callback))
{
if (progressive.GetRateOfProgress() != 100)
{
Progressive.State state = Progressive.State.e_ToBeContinued;
while (Progressive.State.e_ToBeContinued == state)
{
state = progressive.Continue();
}
}
Console.WriteLine("Convert PDF file to Excel format file with path.");
}
}
using (CustomConvertCallback callback = new CustomConvertCallback())
{
using (Progressive progressive = PDF2Office.StartConvertToPowerPoint(input_path + "powerpoint.pdf", null, output_path + "pdf2powerpoint_result.pptx", setting_data, callback))
{
if (progressive.GetRateOfProgress() != 100)
{
Progressive.State state = Progressive.State.e_ToBeContinued;
while (Progressive.State.e_ToBeContinued == state)
{
state = progressive.Continue();
}
}
Console.WriteLine("Convert PDF file to PowerPoint format file with path.");
}
}