Foxit PDF SDK for Linux Foxit PDF SDK for Mac Foxit PDF SDK for Windows

How to load & save documents using Foxit PDF SDK (Java)

Foxit PDF SDK provides a series of options on how to handle documents. A PDF document object can be constructed with an existing PDF file from a file path, memory buffer, a custom implemented ReaderCallback object and an input file stream. The methods PDFDoc.Load or PDFDoc.StartLoad are used to load document content. Document objects inside a PDF are used for document level operations such as opening and closing files, getting pages, annotation, metadata and more. This article will describe how to perform these and other operations using Foxit PDF SDK.

Example:

How to create a PDF document from scratch

import static com.foxit.sdk.pdf.PDFDoc.*;
...
PDFDoc doc = new PDFDoc();

Note: It creates a new PDF document without any pages.

How to load an existing PDF document from file path

import static com.foxit.sdk.pdf.PDFDoc.*;
import static com.foxit.sdk.common.Constants.e_ErrSuccess;
...
PDFDoc doc = new PDFDoc("sample.pdf");
int error_code = doc.load(null);
if (error_code != e_ErrSuccess)
return;

How to load an existing PDF document from a memory buffer

import static com.foxit.sdk.pdf.PDFDoc.*;
import static com.foxit.sdk.common.Constants.e_ErrSuccess;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
...
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("sample.pdf"));
byte[] b = new byte[bis.available()];
bis.read(b);
PDFDoc doc = new PDFDoc(b);
error_code = doc.load(null);
if (error_code != e_ErrSuccess)
return;

How to load an existing PDF document from a file read callback object

import static com.foxit.sdk.pdf.PDFDoc.*;
import static com.foxit.sdk.common.Constants.e_ErrSuccess;
import java.io.IOException;
import com.foxit.sdk.common.fxcrt.FileReaderCallback;
import java.io.RandomAccessFile;
...
class FileReader extends FileReaderCallback {
private RandomAccessFile file_ = null;
FileReader() {
}
boolean LoadFile(String file_path) throws FileNotFoundException {
file_ = new RandomAccessFile(file_path, "r");
return true;
}
@Override
public long getSize() {
try {
return this.file_.length();
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}
@Override
public boolean readBlock(byte[] buffer, long offset, long size) {
try {
file_.seek(offset);
int read = file_.read(buffer, 0, (int) size);
return read == size ? true : false;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public void release() {
try {
this.file_.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
FileReader callback = new FileReader();
callback.LoadFile("sample.pdf");
PDFDoc doc = new PDFDoc(callback, false);
int error_code = doc.load(null);
if (error_code != e_ErrSuccess)
return;

How to load PDF document and get the first page of the PDF document

import static com.foxit.sdk.pdf.PDFDoc.*;
import static com.foxit.sdk.pdf.PDFPage.*;
import static com.foxit.sdk.common.Constants.e_ErrSuccess;
import static com.foxit.sdk.pdf.PDFPage.e_ParsePageNormal;
...
PDFDoc doc = new PDFDoc("sample.pdf");
int error_code = doc.load(null);
if (error_code != e_ErrSuccess)
return;
// Get the first page of the document.
PDFPage page = doc.getPage(0);
// Parse page.
page.startParse(e_ParsePageNormal, null, false);

How to save a PDF to a file

import static com.foxit.sdk.pdf.PDFDoc.*;
import static com.foxit.sdk.pdf.PDFPage.*;
import static com.foxit.sdk.common.Constants.e_ErrSuccess;
import static com.foxit.sdk.pdf.PDFDoc.e_SaveFlagNoOriginal;
...
PDFDoc doc = new PDFDoc("sample.pdf");
int error_code = doc.load(null);
if (error_code != e_ErrSuccess)
return;
doc.saveAs("new_Sample.pdf", e_SaveFlagNoOriginal);

Updated on April 1, 2019

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