Scheduled Maintenance
-
February 10, 2026, from 8:00 AM to 9:00 AM PST. Account-related operations might be temporarily unavailable.

AddImage

Sample Java 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. Learn more about our Android SDK and PDF Editing & Manipulation Library.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6package com.pdftron.android.pdfnetsdksamples.samples;
7
8import com.pdftron.android.pdfnetsdksamples.OutputListener;
9import com.pdftron.android.pdfnetsdksamples.PDFNetSample;
10import com.pdftron.android.pdfnetsdksamples.R;
11import com.pdftron.android.pdfnetsdksamples.util.Utils;
12import com.pdftron.common.Matrix2D;
13import com.pdftron.common.PDFNetException;
14import com.pdftron.pdf.Element;
15import com.pdftron.pdf.ElementBuilder;
16import com.pdftron.pdf.ElementWriter;
17import com.pdftron.pdf.Font;
18import com.pdftron.pdf.Image;
19import com.pdftron.pdf.PDFDoc;
20import com.pdftron.pdf.Page;
21import com.pdftron.pdf.Rect;
22import com.pdftron.sdf.Obj;
23import com.pdftron.sdf.ObjSet;
24import com.pdftron.sdf.SDFDoc;
25
26import java.util.ArrayList;
27
28public class AddImageTest extends PDFNetSample {
29
30 private static OutputListener mOutputListener;
31
32 private static ArrayList<String> mFileList = new ArrayList<>();
33
34 public AddImageTest() {
35 setTitle(R.string.sample_addimage_title);
36 setDescription(R.string.sample_addimage_description);
37 }
38
39 @Override
40 public void run(OutputListener outputListener) {
41 super.run(outputListener);
42 mOutputListener = outputListener;
43 mFileList.clear();
44 printHeader(outputListener);
45
46 try (PDFDoc doc = new PDFDoc())
47 {
48 ElementBuilder f = new ElementBuilder(); // Used to build new Element objects
49 ElementWriter writer = new ElementWriter(); // Used to write Elements to the page
50
51 Page page = doc.pageCreate(); // Start a new page
52 writer.begin(page); // Begin writing to this page
53
54 // ----------------------------------------------------------
55 // Add JPEG image to the output file
56 Image img = Image.create(doc.getSDFDoc(), Utils.getAssetTempFile(INPUT_PATH + "peppers.jpg").getAbsolutePath());
57 Element element = f.createImage(img, 50, 500, img.getImageWidth()/2, img.getImageHeight()/2);
58 writer.writePlacedElement(element);
59
60 // ----------------------------------------------------------
61 // Add a PNG image to the output file
62 img = Image.create(doc.getSDFDoc(), Utils.getAssetTempFile(INPUT_PATH + "butterfly.png").getAbsolutePath());
63 element = f.createImage(img, new Matrix2D(100, 0, 0, 100, 300, 500));
64 writer.writePlacedElement(element);
65
66 // ----------------------------------------------------------
67 // Add a GIF image to the output file
68 img = Image.create(doc.getSDFDoc(), Utils.getAssetTempFile(INPUT_PATH + "pdfnet.gif").getAbsolutePath());
69 element = f.createImage(img, new Matrix2D(img.getImageWidth(), 0, 0, img.getImageHeight(), 50, 350));
70 writer.writePlacedElement(element);
71
72 // ----------------------------------------------------------
73 // Add a TIFF image to the output file
74 img = Image.create(doc.getSDFDoc(), Utils.getAssetTempFile(INPUT_PATH + "grayscale.tif").getAbsolutePath());
75 element = f.createImage(img, new Matrix2D(img.getImageWidth(), 0, 0, img.getImageHeight(), 10, 50));
76 writer.writePlacedElement(element);
77
78 writer.end(); // Save the page
79 doc.pagePushBack(page); // Add the page to the document page sequence
80
81 // ----------------------------------------------------------
82 // Embed a monochrome TIFF. Compress the image using lossy JBIG2 filter.
83
84 page = doc.pageCreate(new Rect(0, 0, 612, 794));
85 writer.begin(page); // begin writing to this page
86
87 // Note: encoder hints can be used to select between different compression methods.
88 // For example to instruct PDFNet to compress a monochrome image using JBIG2 compression.
89 ObjSet hint_set = new ObjSet();
90 Obj enc = hint_set.createArray(); // Initilaize encoder 'hint' parameter
91 enc.pushBackName("JBIG2");
92 enc.pushBackName("Lossy");
93
94 img = Image.create(doc.getSDFDoc(), Utils.getAssetTempFile(INPUT_PATH + "multipage.tif").getAbsolutePath());
95 element = f.createImage(img, new Matrix2D(612, 0, 0, 794, 0, 0));
96 writer.writePlacedElement(element);
97
98 writer.end(); // Save the page
99 doc.pagePushBack(page); // Add the page to the document page sequence
100
101 // ----------------------------------------------------------
102 // Add a JPEG2000 (JP2) image to the output file
103
104 // Create a new page
105 page = doc.pageCreate();
106 writer.begin(page); // Begin writing to the page
107
108 // Embed the image.
109 img = Image.create(doc.getSDFDoc(), Utils.getAssetTempFile(INPUT_PATH + "palm.jp2").getAbsolutePath());
110
111 // Position the image on the page.
112 element = f.createImage(img, new Matrix2D(img.getImageWidth(), 0, 0, img.getImageHeight(), 96, 80));
113 writer.writePlacedElement(element);
114
115 // Write 'JPEG2000 Sample' text string under the image.
116 writer.writeElement(f.createTextBegin(Font.create(doc.getSDFDoc(), Font.e_times_roman), 32));
117 element = f.createTextRun("JPEG2000 Sample");
118 element.setTextMatrix(1, 0, 0, 1, 190, 30);
119 writer.writeElement(element);
120 writer.writeElement(f.createTextEnd());
121
122 writer.end(); // Finish writing to the page
123 doc.pagePushBack(page);
124
125 // ----------------------------------------------------------
126 // doc.Save((Utils.createExternalFile("addimage.pdf", mFileList).getAbsolutePath()).c_str(), Doc.e_remove_unused, 0);
127 doc.save((Utils.createExternalFile("addimage.pdf", mFileList).getAbsolutePath()), SDFDoc.SaveMode.LINEARIZED, null);
128 mOutputListener.println("Done. Result saved in addimage.pdf...");
129 }
130 catch (PDFNetException e)
131 {
132 mOutputListener.printError(e.getStackTrace());
133 mOutputListener.printError(e.getStackTrace());
134 }
135
136 for (String file : mFileList) {
137 addToFileList(file);
138 }
139 printFooter(outputListener);
140 }
141
142}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales