Foxit PDF SDK for Windows

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

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

using foxit.pdf;
...
PDFDoc doc = new PDFDoc();

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

How to load an existing PDF document from file path

using foxit.pdf;
using foxit.common;
...
PDFDoc doc = new PDFDoc("Sample.pdf");
error_code = doc.Load("");
if (error_code != ErrorCode.e_ErrSuccess) return;
...

How to load an existing PDF document from a memory buffer

using foxit.pdf;
using foxit.common;
...
byte[] byte_buffer = File.ReadAllBytes(input_file);
IntPtr buffer = System.Runtime.InteropServices.Marshal.AllocHGlobal(byte_buffer.Length);
try {
 System.Runtime.InteropServices.Marshal.Copy(byte_buffer, 0, buffer, byte_buffer.Length);
}
Finally {
 System.Runtime.InteropServices.Marshal.FreeHGlobal(buffer);
}
PDFDoc doc = new PDFDoc(buffer, (uint)byte_buffer.Length);
error_code = doc.Load("");
if (error_code != ErrorCode.e_ErrSuccess) return;
...

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

using foxit.pdf;
using foxit.common;
...
class FileReader : FileReaderCallback
 {
 private FileStream file_ = null;
 private long offset_ = 0;
 public FileReader(long offset)
 {
 this.offset_ = offset;
 }
 public Boolean LoadFile(String file_path)
 {
 file_ = new FileStream(file_path, FileMode.OpenOrCreate);
 return true;
 }
 public override long GetSize()
 {
 return this.offset_;
 }
 public override bool ReadBlock(IntPtr buffer, long offset, uint size)
 {
 int read_size = 0;
 file_.Seek(offset, SeekOrigin.Begin);
 byte[] array = new byte[size + 1];
 read_size = file_.Read(array, 0, (int)size);
 Marshal.Copy(array, 0, buffer, (int)size);
 return read_size == size ? true : false;
 }
 public override void Release()
 {
 this.file_.Close();
 }
 }
...
FileReader file_reader = new FileReader(offset);
 file_reader.LoadFile(file_name);
PDFDoc doc_real = new PDFDoc(file_reader, false)
error_code = doc.Load("");
if (error_code != ErrorCode.e_ErrSuccess) return;
...

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

using foxit.pdf;
using foxit.common;
...
PDFDoc doc = new PDFDoc("Sample.pdf");
error_code = doc.Load("");
if (error_code != ErrorCode.e_ErrSuccess) return;
...
// Get the first page of the document.
PDFPage page = doc.GetPage(0);
// Parse page.
page.StartParse((int)foxit.pdf.PDFPage.ParseFlags.e_ParsePageNormal, null, false);
...

How to save a PDF to a file

using foxit.pdf;
using foxit.common;
...
PDFDoc doc = new PDFDoc("Sample.pdf");
error_code = doc.Load("");
if (error_code != ErrorCode.e_ErrSuccess) return;
...
// Do operations for the PDF document.
...
// Save the changes for the PDF document.
string newPdf = "the output path for the saved PDF";
doc.SaveAs(newPdf, (int)PDFDoc.SaveFlags.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: