Foxit PDF Conversion SDK

How to convert office 2 pdf via Conversion SDK

System requirements

Platform: Windows
Programming Language:  C,C++, C#, Java, Python, Nodejs
License Key requirement: ‘Office2PDF’ module permission in the license key

Conversion interface

Office2PDF provides three conversion interfaces: ConvertFromWord, ConvertFromExcel, ConvertFromPowerPoint.In Foxit PDF Conversion SDK, these interfaces are defined in the header file ”fs_office2pdf.h”.Take “ConvertFromWord” as an example to introduce interface parameters in Windows.(The parameters of the two interfaces “ConvertFromExcel” and “ConvertFromPowerPoint” are similar to “ConvertFromWord”.)

The parameters for the interface come in two varieties: file path and stream.

The file path parameters are as follows:

Parameter Windows
src_word_file_pathPath of a Word format file. This should not be an empty string.
src_file_password(Reserved,encrypted files are unsupported yet)Password for the input Word format file. If no password is needed for the file,please pass an empty string.
saved_pdf_pathPath of the saved PDF file as conversion result. This should not be an empty string.

The stream parameters are as follows:

Parameter Windows
src_word_readerA ReaderCallback object which is implemented by user to load a word document. It should not be NULL.
src_file_password(Reserved,encrypted files are unsupported yet)Password for the input Word format file. If no password is needed for the file, please pass an empty string.
saved_pdf_file_streamA foxit::common::file::StreamCallback object which is implemented by user to read the contents of the converted PDF file. It should not be NULL.

How to work with office2pdf API

C++

#if defined(_WIN32) || defined(_WIN64)
#include <direct.h>
#else
#include <sys/stat.h>
#endif
#include <iostream>
#include <string>
 
#include "../../../include/common/fs_common.h"
#include "../../../include/conversion/fs_office2pdf.h"
 
using namespace std;
using namespace foxit;
using namespace foxit;
using namespace foxit::common;
using namespace foxit::file;
using foxit::common::Library;
using namespace conversion::office2pdf;
 
// Convert Word file to PDF file with file path.
WString word_file_path = L"word.docx";
WString output_path = L"word2pdf_result.pdf";
Office2PDF::ConvertFromWord(word_file_path, NULL, output_path);
     
// Convert Word file to PDF file with stream.
FileReader* file_reader_word = new FileReader();
FileStream* file_writer_word = new FileStream();
file_reader_word->LoadFile(String::FromUnicode(L"word.docx"));
file_writer_word->LoadFile(String::FromUnicode(L"word2pdf_stream_result.pdf"));
Office2PDF::ConvertFromWord(file_reader_word, NULL, file_writer_word);
 
// Convert Excel file to PDF file with file path.
WString excel_file_path =L"excel.xlsx";
output_path =L"excel2pdf_result.pdf";
Office2PDF::ConvertFromExcel(excel_file_path, NULL, output_path);
 
// Convert Excel file to PDF file with stream.
FileReader* file_reader_excel = new FileReader();
FileStream* file_writer_excel = new FileStream();
file_reader_excel->LoadFile(String::FromUnicode(input_path + L"excel.xlsx"));
file_writer_excel->LoadFile(String::FromUnicode(L"excel2pdf_stream_result.pdf"));
Office2PDF::ConvertFromExcel(file_reader_excel, NULL, file_writer_excel);
 
 
// Convert PowerPoint file to PDF file with file path.
WString ppt_file_path =L"powerpoint.pptx";
output_path =L"ppt2pdf_result.pdf";
Office2PDF::ConvertFromPowerPoint(ppt_file_path, NULL, output_path);
 
// Convert PowerPoint file to PDF file with stream.
FileReader* file_reader_ppt = new FileReader();
FileStream* file_writer_ppt = new FileStream();
file_reader_ppt->LoadFile(String::FromUnicode(input_path + L"powerpoint.pptx"));
file_writer_ppt->LoadFile(String::FromUnicode(L"ppt2pdf_stream_result.pdf"));
Office2PDF::ConvertFromPowerPoint(file_reader_ppt, NULL, file_writer_ppt);

C

#include <stdio.h>
#include <stdlib.h>
 
#include "../../../include/fs_basictypes_c.h"
#include "../../../include/fs_common_c.h"
#include "../../../include/fx_stream_c.h"
#include "../../../include/fs_office2pdf_c.h"
 
 
wchar_t output_file[MAX_FILE_PATH];
wchar_t word_file_path[MAX_FILE_PATH];
wchar_t excel_file_path[MAX_FILE_PATH];
wchar_t ppt_file_path[MAX_FILE_PATH];
wchar_t word_file_path_stream[MAX_FILE_PATH];
wchar_t excel_file_path_stream[MAX_FILE_PATH];
wchar_t ppt_file_path_stream[MAX_FILE_PATH];
 
 
FSReaderCallback* callback_filereader_word = NULL;
FSStreamCallback* callback_filestream_word = NULL;
FSReaderCallback* callback_filereader_excel = NULL;
FSStreamCallback* callback_filestream_excel = NULL;
FSReaderCallback* callback_filereader_ppt = NULL;
FSStreamCallback* callback_filestream_ppt = NULL;
 
FS_BOOL is_susccees;
// Convert Word file to PDF file with file path.
swprintf_s(word_file_path, MAX_FILE_PATH, L"word.docx");
swprintf_s(output_file, MAX_FILE_PATH, L"word2pdf_result.pdf");
FSDK_Office2PDF_ConvertFromWord(word_file_path, L"", output_file, &is_susccees);  
 
//Convert Word file to PDF file with stream.
swprintf(word_file_path_stream, MAX_FILE_PATH - 1, L"word2pdf_stream_result.pdf");
callback_filereader_word = (FSReaderCallback*)malloc(sizeof(FSReaderCallback));
gOpenFile(&file, word_file_path, "rb");
callback_filereader_word->user_data = callback_filereader_word;
callback_filereader_word->ReadBlock = gReadBlock;
callback_filereader_word->Release = gRelease;
callback_filereader_word->GetSize = gGetSize;
 
callback_filestream_word = (FSStreamCallback*)malloc(sizeof(FSStreamCallback));
gOpenFile(&filestream, word_file_path_stream, "wb");
callback_filestream_word->Flush = gFlushFileStream;
callback_filestream_word->GetPosition = gGetPositionFileStream;
callback_filestream_word->GetSize = gGetSizeFileStream;
callback_filestream_word->IsEOF = gIsEOFFileStream;
callback_filestream_word->WriteBlock = gWriteBlockFileStream;
callback_filestream_word->ReadBlock = gReadBlockFileStream;
callback_filestream_word->ReadBlock0 = gReadBlock0FileStream;
callback_filestream_word->Release = gReleaseFileStream;
callback_filestream_word->Retain = gRetainFileStream;
callback_filestream_word->user_data = callback_filestream_word;
ref = 0;
cur_pos = 0;
FSDK_Office2PDF_ConvertFromWord0(callback_filereader_word, L"", callback_filestream_word, &is_susccees);
 
// Convert Excel file to PDF file with file path.
swprintf_s(excel_file_path, MAX_FILE_PATH, L"excel.xlsx");
swprintf_s(output_file, MAX_FILE_PATH, L"%excel2pdf_result.pdf");
FSDK_Office2PDF_ConvertFromExcel(excel_file_path, L"", output_file, &is_susccees);
 
// Convert Excel file to PDF file with stream.
swprintf(excel_file_path_stream, MAX_FILE_PATH - 1, L"excel2pdf_stream_result.pdf");
callback_filereader_excel = (FSReaderCallback*)malloc(sizeof(FSReaderCallback));
gOpenFile(&file, excel_file_path, "rb");
callback_filereader_excel->user_data = callback_filereader_excel;
callback_filereader_excel->ReadBlock = gReadBlock;
callback_filereader_excel->Release = gRelease;
callback_filereader_excel->GetSize = gGetSize;
 
callback_filestream_excel = (FSStreamCallback*)malloc(sizeof(FSStreamCallback));
gOpenFile(&filestream, excel_file_path_stream, "wb");
callback_filestream_excel->Flush = gFlushFileStream;
callback_filestream_excel->GetPosition = gGetPositionFileStream;
callback_filestream_excel->GetSize = gGetSizeFileStream;
callback_filestream_excel->IsEOF = gIsEOFFileStream;
callback_filestream_excel->ReadBlock = gReadBlockFileStream;
callback_filestream_excel->ReadBlock0 = gReadBlock0FileStream;
callback_filestream_excel->WriteBlock = gWriteBlockFileStream;
callback_filestream_excel->Release = gReleaseFileStream;
callback_filestream_excel->Retain = gRetainFileStream;
callback_filestream_excel->user_data = callback_filestream_excel;
ref = 0;
cur_pos = 0;
error_code = FSDK_Office2PDF_ConvertFromExcel0(callback_filereader_excel, L"", callback_filestream_excel, &is_susccees);
 
// Convert PowerPoint file to PDF file with file path.
swprintf_s(ppt_file_path, MAX_FILE_PATH, L"%powerpoint.pptx");
swprintf_s(output_file, MAX_FILE_PATH, L"%ppt2pdf_result.pdf");
FSDK_Office2PDF_ConvertFromPowerPoint(ppt_file_path, L"", output_file, &is_susccees);
 
// Convert PowerPoint file to PDF file with stream.
swprintf(ppt_file_path_stream, MAX_FILE_PATH - 1, L"powerpoint2pdf_stream_result.pdf");
callback_filereader_ppt = (FSReaderCallback*)malloc(sizeof(FSReaderCallback));
gOpenFile(&file, ppt_file_path, "rb");
callback_filereader_ppt->user_data = callback_filereader_ppt;
callback_filereader_ppt->ReadBlock = gReadBlock;
callback_filereader_ppt->Release = gRelease;
callback_filereader_ppt->GetSize = gGetSize;
 
callback_filestream_ppt = (FSStreamCallback*)malloc(sizeof(FSStreamCallback));
gOpenFile(&pptfilestream, ppt_file_path_stream, "wb");
callback_filestream_ppt->Flush = gFlushFileStream;
callback_filestream_ppt->GetPosition = gGetPositionFileStream;
callback_filestream_ppt->GetSize = gGetSizeFileStream;
callback_filestream_ppt->IsEOF = gIsEOFFileStream;
callback_filestream_ppt->ReadBlock = gReadBlockFileStream;
callback_filestream_ppt->ReadBlock0 = gReadBlock0FileStream;
callback_filestream_ppt->WriteBlock = gWriteBlockFileStream;
callback_filestream_ppt->Release = gReleaseFileStream;
callback_filestream_ppt->Retain = gRetainFileStream;
callback_filestream_ppt->user_data = callback_filestream_ppt;
FSDK_Office2PDF_ConvertFromPowerPoint0(callback_filereader_ppt, L"", callback_filestream_ppt, &is_susccees);

C#

using System;
using System.IO;
using System.Runtime.InteropServices;
using foxit;
using foxit.common;
using foxit.common.fxcrt;
using foxit.conversion.office2pdf;
 
// Convert Word file to PDF file with file path.
string word_file_path ="word.docx";
string saved_pdf_path ="word2pdf_result.pdf";
foxit.conversion.office2pdf.Office2PDF.ConvertFromWord(word_file_path, "", saved_pdf_path);
 
// Convert Word file to PDF file with stream.
using (FileReader filereadercallback = new FileReader())
using (FileStreamCallback fileStreamcallback = new FileStreamCallback())
{
  filereadercallback.LoadFile(word_file_path);
  fileStreamcallback.LoadFile(output_path + "word2pdf_stream_result.pdf");
  Office2PDF.ConvertFromWord(filereadercallback, null, fileStreamcallback);
}
 
// Convert Excel file to PDF file with file path.
string excel_file_path ="excel.xlsx";
saved_pdf_path ="excel2pdf_result.pdf";
foxit.conversion.office2pdf.Office2PDF.ConvertFromExcel(excel_file_path, "", saved_pdf_path);
 
// Convert Excel file to PDF file with stream.
using (FileReader filereadercallback = new FileReader())
using (FileStreamCallback fileStreamcallback = new FileStreamCallback())
{
  filereadercallback.LoadFile(excel_file_path);
  fileStreamcallback.LoadFile(output_path + "excel2pdf_stream_result.pdf");
  Office2PDF.ConvertFromExcel(filereadercallback, null, fileStreamcallback);
}
 
// Convert PowerPoint file to PDF file with file path.
string ppt_file_path ="powerpoint.pptx";
saved_pdf_path ="ppt2pdf_result.pdf";
foxit.conversion.office2pdf.Office2PDF.ConvertFromPowerPoint(ppt_file_path, "", saved_pdf_path);
 
// Convert PowerPoint file to PDF file with stream.
using (FileReader filereadercallback = new FileReader())
using (FileStreamCallback fileStreamcallback = new FileStreamCallback())
{
  filereadercallback.LoadFile(ppt_file_path);
  fileStreamcallback.LoadFile(output_path + "ppt2pdf_stream_result.pdf");
  Office2PDF.ConvertFromPowerPoint(filereadercallback, null, fileStreamcallback);
}

Python

import os
import sys
import site
 
if sys.version_info.major == 2:
    _PYTHON2_ = True
else:
    _PYTHON2_ = False
 
if _PYTHON2_:
    site.addsitedir('../../../')
    from FoxitPDFConversionSDKPython2 import *
else:
    from FoxitPDFConversionSDKPython3 import *
 
# Conver Word file to PDF file with file path.
word_file_path ="word.docx"
output_path ="word2pdf_result.pdf"
Office2PDF.ConvertFromWord(word_file_path, "", output_path)
 
# Convert Word file to PDF file with stream.
file_reader_word = FileReader()
file_reader_word.LoadFile(word_file_path)
file_stream_word = FileStream()
file_stream_word.LoadFile("word2pdf_stream_result.docx")       
Office2PDF.ConvertFromWord(file_reader_word, "", file_stream_word)
 
 
# Convert Excel file to PDF file with file path.
excel_file_path ="excel.xlsx"
output_path ="excel2pdf_result.pdf"
Convert.ConvertFromExcel(excel_file_path, "", output_path)
 
# Convert Excel file to PDF file with stream.
file_reader_excel = FileReader()
file_reader_excel.LoadFile(excel_file_path)
file_stream_excel = FileStream()
file_stream_excel.LoadFile("excel2pdf_stream_result.xlsx")     
Office2PDF.ConvertFromExcel(file_reader_excel, "", file_stream_excel)
 
# Convert PowerPoint file to PDF file with file path.
ppt_file_path ="powerpoint.pptx"
output_path ="ppt2pdf_result.pdf"
Convert.ConvertFromPowerPoint(ppt_file_path, "", output_path)
 
# Convert PowerPoint file to PDF file with stream.
file_reader_ppt = FileReader()
file_reader_ppt.LoadFile(ppt_file_path)
file_stream_ppt = FileStream()
file_stream_ppt.LoadFile("powerpoint2ppt_stream_result.pptx")
Convert.ConvertFromPowerPoint(file_reader_ppt, "", file_stream_ppt)

JAVA

import com.foxit.sdk.PDFException;
import com.foxit.sdk.common.Library;
import com.foxit.sdk.common.fxcrt.FileReaderCallback;
import com.foxit.sdk.common.fxcrt.StreamCallback;
import com.foxit.sdk.conversion.office2pdf.Office2PDF;
import java.io.*;
 
import static com.foxit.sdk.common.Constants.e_ErrSuccess;
import static com.foxit.sdk.common.Constants.e_ErrInvalidLicense;
import static com.foxit.sdk.common.Constants.e_ErrNoOffice2PDFModuleRight;
 
// Convert Word file to PDF file with file path.
String word_file_path ="word.docx";
String saved_pdf_path ="word2pdf_result.pdf";
Office2PDF.convertFromWord(word_file_path, "", saved_pdf_path);
 
// Convert Word file to PDF file with stream.
FileReader file_read_word = new FileReader();
file_read_word.LoadFile(word_file_path);
FileStream file_stream_word = new FileStream();
file_stream_word.LoadFile(output_path + "word2pdf_stream_result.pdf");
Office2PDF.convertFromWord(file_read_word, "", file_stream_word);
 
// Convert Excel file to PDF file with file path.
String excel_file_path ="excel.xlsx";
saved_pdf_path ="excel2pdf_result.pdf";
Office2PDF.convertFromExcel(excel_file_path, "", saved_pdf_path);
 
// Convert Excel file to PDF file with stream.
FileReader file_read_excel = new FileReader();
file_read_excel.LoadFile(word_file_path);
FileStream file_stream_excel = new FileStream();
file_stream_excel.LoadFile(output_path + "excel2pdf_stream_result.pdf");
Office2PDF.convertFromWord(file_read_excel, "", file_stream_excel);
 
 
// Convert PowerPoint file to PDF file with file path.
String ppt_file_path ="powerpoint.pptx";
saved_pdf_path ="ppt2pdf_result.pdf";
Office2PDF.convertFromPowerPoint(ppt_file_path, "", saved_pdf_path);
 
// Convert PowerPoint file to PDF file with stream.
FileReader file_ppt_excel = new FileReader();
file_ppt_excel.LoadFile(word_file_path);
FileStream file_stream_ppt = new FileStream();
file_stream_ppt.LoadFile(output_path + "ppt2pdf_stream_result.pdf");
Office2PDF.convertFromPowerPoint(file_ppt_excel, "", file_stream_ppt);

Nodejs

var {
  ErrorCode,
  Library,
  Office2PDF, 
  ReaderCallback,
  StreamCallback } = require("@foxitsoftware/foxit-pdf-conversion-sdk-node");
var fs = require('fs');
 
 
var src_word_path ="word.docx";
var saved_pdf_file_path ="word2pdf_withpath_result.pdf";
   
try {
  // Convert Word file to PDF file with path.
  Office2PDF.ConvertFromWordWithPath(src_word_path, "", saved_pdf_file_path);
  console.log("Convert Word format file to PDF file with path.");
} catch (e) {
  console.log(e);
}
 
try {
  // Convert Word file to PDF file with stream.
  var custom_readercallback_word = new CustomReaderCallback();
  var custom_streamcallback_word = new CustomStreamCallback();
  var src_pdf_path ="word.docx";
  var saved_pdf_file_path ="word2pdf_withstream_result.pdf";
  custom_readercallback_word.LoadFile(src_pdf_path);
  custom_streamcallback_word.LoadFile(saved_pdf_file_path);
  Office2PDF.ConvertFromWordWithStream(custom_readercallback_word, "", custom_streamcallback_word);
 
  delete custom_readercallback_word;
  delete custom_streamcallback_word;
} catch (e) {
  console.log(e);
}
 
try {
  // Convert Excel file to PDF file with path.
  var src_excel_path ="excel.xlsx";
  var saved_pdf_file_path ="excel2pdf_withpath_result.pdf";
  Office2PDF.ConvertFromExcelWithPath(src_excel_path, "", saved_pdf_file_path);
 
} catch (e) {
  console.log(e);
}
 
try {
  // Convert Excel file to PDF file with stream.
  var custom_readercallback_excel = new CustomReaderCallback();
  var custom_streamcallback_excel = new CustomStreamCallback();
  var src_excel_path ="excel.xlsx";
  var saved_pdf_file_path ="excel2pdf_withstream_result.pdf";
  custom_readercallback_excel.LoadFile(src_excel_path);
  custom_streamcallback_excel.LoadFile(saved_pdf_file_path);
  Office2PDF.ConvertFromExcelWithStream(custom_readercallback_excel, "", custom_streamcallback_excel);
 
  delete custom_readercallback_excel;
  delete custom_streamcallback_excel;
} catch (e) {
  console.log(e);
}
 
try {
  // Convert PowerPoint file to PDF file with path.
  var src_powerpoint_path ="powerpoint.pptx";
  var saved_pdf_file_path ="powerpoint2pdf_withpath_result.pdf";
  Office2PDF.ConvertFromPowerPointWithPath(src_powerpoint_path, "", saved_pdf_file_path);
} catch (e) {
  console.error(e);
}
 
try {
  // Convert PowerPoint file to PDF file with stream.
  var custom_readercallback_ppt = new CustomReaderCallback();
  var custom_streamcallback_ppt = new CustomStreamCallback();
  var src_powerpoint_path ="powerpoint.pptx";
  var saved_pdf_file_path ="powerpoint2pdf_withstream_result.pdf";
  custom_readercallback_ppt.LoadFile(src_powerpoint_path);
  custom_streamcallback_ppt.LoadFile(saved_pdf_file_path);
  Office2PDF.ConvertFromPowerPointWithStream(custom_readercallback_ppt, "", custom_streamcallback_ppt);
  delete custom_readercallback_ppt;
  delete custom_streamcallback_ppt;
} catch (e) {
  console.log(e);
}

Updated on October 13, 2023

Was this article helpful?
Thanks for your feedback. If you have a comment on how to improve the article, you can write it here: