Add Image to PDF - Java Sample Code

Sample code to use Apryse SDK for programmatically inserting various raster image formats (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) into a PDF document. Sample code provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby and VB.

Learn more about our Server SDK and PDF Editing & Manipulation Library.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import java.io.File;
7import java.io.IOException;
8
9import javax.imageio.ImageIO;
10
11import com.pdftron.common.Matrix2D;
12import com.pdftron.common.PDFNetException;
13import com.pdftron.pdf.*;
14import com.pdftron.sdf.SDFDoc;
15import com.pdftron.sdf.ObjSet;
16import com.pdftron.sdf.Obj;
17
18//-----------------------------------------------------------------------------------
19// This sample illustrates how to embed various raster image formats
20// (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) in a PDF document.
21//
22// Note: On Windows platform this sample utilizes GDI+ and requires GDIPLUS.DLL to
23// be present in the system path.
24//-----------------------------------------------------------------------------------
25public class AddImageTest {
26
27 public static void main(String[] args)
28 {
29 PDFNet.initialize(PDFTronLicense.Key());
30
31 // Relative path to the folder containing test files.
32 String input_path = "../../TestFiles/";
33 String output_path = "../../TestFiles/Output/";
34
35 try (PDFDoc doc = new PDFDoc())
36 {
37 ElementBuilder f = new ElementBuilder(); // Used to build new Element objects
38 ElementWriter writer = new ElementWriter(); // Used to write Elements to the page
39
40 Page page = doc.pageCreate(); // Start a new page
41 writer.begin(page); // Begin writing to this page
42
43 // ----------------------------------------------------------
44 // Add JPEG image to the output file
45 Image img = Image.create(doc.getSDFDoc(), input_path + "peppers.jpg");
46 Element element = f.createImage(img, 50, 500, img.getImageWidth()/2, img.getImageHeight()/2);
47 writer.writePlacedElement(element);
48
49 // ----------------------------------------------------------
50 // Add a PNG image to the output file
51 img = Image.create(doc.getSDFDoc(), input_path + "butterfly.png");
52 element = f.createImage(img, new Matrix2D(100, 0, 0, 100, 300, 500));
53 writer.writePlacedElement(element);
54
55 // ----------------------------------------------------------
56 // Add a GIF image to the output file
57 img = Image.create(doc.getSDFDoc(), input_path + "pdfnet.gif");
58 element = f.createImage(img, new Matrix2D(img.getImageWidth(), 0, 0, img.getImageHeight(), 50, 350));
59 writer.writePlacedElement(element);
60
61 // ----------------------------------------------------------
62 // Add a TIFF image to the output file
63 img = Image.create(doc.getSDFDoc(), input_path + "grayscale.tif");
64 element = f.createImage(img, new Matrix2D(img.getImageWidth(), 0, 0, img.getImageHeight(), 10, 50));
65 writer.writePlacedElement(element);
66
67 writer.end(); // Save the page
68 doc.pagePushBack(page); // Add the page to the document page sequence
69
70 // ----------------------------------------------------------
71 // Embed a monochrome TIFF. Compress the image using lossy JBIG2 filter.
72
73 page = doc.pageCreate(new Rect(0, 0, 612, 794));
74 writer.begin(page); // begin writing to this page
75
76 // Note: encoder hints can be used to select between different compression methods.
77 // For example to instruct PDFNet to compress a monochrome image using JBIG2 compression.
78 ObjSet hint_set = new ObjSet();
79 Obj enc = hint_set.createArray(); // Initilaize encoder 'hint' parameter
80 enc.pushBackName("JBIG2");
81 enc.pushBackName("Lossy");
82
83 img = Image.create(doc.getSDFDoc(), input_path + "multipage.tif");
84 element = f.createImage(img, new Matrix2D(612, 0, 0, 794, 0, 0));
85 writer.writePlacedElement(element);
86
87 writer.end(); // Save the page
88 doc.pagePushBack(page); // Add the page to the document page sequence
89
90 // ----------------------------------------------------------
91 // Add a JPEG2000 (JP2) image to the output file
92
93 // Create a new page
94 page = doc.pageCreate();
95 writer.begin(page); // Begin writing to the page
96
97 // Embed the image.
98 img = Image.create(doc.getSDFDoc(), input_path + "palm.jp2");
99
100 // Position the image on the page.
101 element = f.createImage(img, new Matrix2D(img.getImageWidth(), 0, 0, img.getImageHeight(), 96, 80));
102 writer.writePlacedElement(element);
103
104 // Write 'JPEG2000 Sample' text string under the image.
105 writer.writeElement(f.createTextBegin(Font.create(doc.getSDFDoc(), Font.e_times_roman), 32));
106 element = f.createTextRun("JPEG2000 Sample");
107 element.setTextMatrix(1, 0, 0, 1, 190, 30);
108 writer.writeElement(element);
109 writer.writeElement(f.createTextEnd());
110
111 writer.end(); // Finish writing to the page
112 doc.pagePushBack(page);
113
114 // ----------------------------------------------------------
115 // doc.Save((output_path + "addimage.pdf").c_str(), Doc.e_remove_unused, 0);
116 doc.save((output_path + "addimage.pdf"), SDFDoc.SaveMode.LINEARIZED, null);
117 System.out.println("Done. Result saved in addimage.pdf...");
118 }
119 catch (PDFNetException e)
120 {
121 e.printStackTrace();
122 System.out.println(e);
123 }
124
125 PDFNet.terminate();
126 }
127}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales