Tutorial: Create Your First ImageGear for Java PDF Project
In this tutorial, you’ll learn how to configure a Java project for a console application. You’ll also learn how to open a PDF and save as a new file.
-
- Make sure that you have installed JDK and ImageGear for Java PDF properly. See System Requirements and Installation. You will need to copy a sample.pdf file inside the directory where you will be creating the tutorial sample.
- Create a new Java file, and name it (e.g., MyFirstIGJavaPDFProject.java). Insert the following code there:
import com.accusoft.imagegearpdf.*; public class MyFirstIGJavaPDFProject { private PDF pdf; private Document document; static { System.loadLibrary("IgPdf"); } // Application entry point. public static void main(String[] args) { boolean linearized = false; String inputPath = "sample.pdf"; String outputPath = "sample_output.pdf";; MyFirstIGJavaPDFProject app = new MyFirstIGJavaPDFProject(); app.loadAndSave(inputPath, outputPath, linearized); } // Load and save the PDF file. private void loadAndSave(String inputPath, String outputPath, boolean linearized) { try { this.initializePdf(); this.openPdf(inputPath); this.savePdf(outputPath, linearized); } catch (Throwable ex) { System.err.println("Exception: " + ex.toString()); } finally { this.terminatePdf(); } } // Initialize the PDF session. private void initializePdf() { this.pdf = new PDF(); this.pdf.initialize(); } // Open input PDF document. private void openPdf(String inputPath) { this.document = new Document(); this.document.openDocument(inputPath); } // Save PDF document to the output path. private void savePdf(String outputPath, boolean linearized) { SaveOptions saveOptions = new SaveOptions(); // Set LINEARIZED attribute as provided by the user. saveOptions.setLinearized(linearized); this.document.saveDocument(outputPath, saveOptions); } // Close the PDF document and terminate the PDF session. private void terminatePdf() { if (this.document != null) { this.document.closeDocument(); this.document = null; } if (this.pdf != null) { this.pdf.terminate(); this.pdf = null; } } }
- Now, let’s go over some of the important areas in the sample code with more detail. The com.accusoft.imagegearpdf namespace:
- Allows you to load and save native PDF documents
- Allows rasterization of PDF pages by converting them to bitmaps and adding raster pages to a PDF document
- Provides multi-page read and write support for the entire document
To enable the com.accusoft.imagegearpdf namespace in your project, specify the following directive:
import com.accusoft.imagegearpdf.*;
To initialize and support processing of PDF files we need:
// Initialize the PDF session. private void initializePdf() { this.pdf = new PDF(); this.pdf.initialize(); }
- There is one main object that is used in this sample code: The Document that holds the entire loaded document.
private Document document; … this.document = new Document(); this.document.openDocument(inputPath);
- You can save the loaded document using:
SaveOptions saveOptions = new SaveOptions(); // Set LINEARIZED attribute as provided by the user. saveOptions.setLinearized(linearized); this.document.saveDocument(outputPath, saveOptions);
See About Linearized PDF Files for more information.
- Now, you can build and run your sample. Please make sure that you have a PDF file named sample.pdf in the same directory where your sample source resided, or change the inputPath in the sample code so that it points to any existing PDF file.
- Now, open the terminal in the directory containing your source file and run the following commands:
- Compile
javac -classpath $HOME/Accusoft/ImageGearJavaPDF1-64/java/IgPdf.jar MyFirstIGJavaPDFProject.java
After running this command, you should see a file named MyFirstIGJavaPDFProject.class in your current directory.
- Build
jar cfe MyFirstIGJavaPDFProject.jar MyFirstIGJavaPDFProject MyFirstIGJavaPDFProject.class
After running this command, you should see a file named MyFirstIGJavaPDFProject.jar in your current directory.
- Set the environment variable (you only have to do this one time)
export LD_PRELOAD=$HOME/Accusoft/ImageGearJavaPDF1-64/lib/libIGCORE18.so
- run
java -classpath $HOME/Accusoft/ImageGearJavaPDF1-64/java/IgPdf.jar:. MyFirstIGJavaPDFProject
- Compile
After running your sample, you should see a new PDF file, named sample_output.pdf, in your current directory.