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}
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.*
15import com.pdftron.sdf.ObjSet
16import com.pdftron.sdf.SDFDoc
17import java.util.*
18
19class AddImageTest : PDFNetSample() {
20 init {
21 setTitle(R.string.sample_addimage_title)
22 setDescription(R.string.sample_addimage_description)
23 }
24
25 override fun run(outputListener: OutputListener?) {
26 super.run(outputListener)
27 mOutputListener = outputListener
28 mFileList.clear()
29 printHeader(outputListener!!)
30
31 try {
32
33 PDFDoc().use { doc ->
34 val f = ElementBuilder() // Used to build new Element objects
35 val writer = ElementWriter() // Used to write Elements to the page
36
37 var page = doc.pageCreate() // Start a new page
38 writer.begin(page) // Begin writing to this page
39
40 // ----------------------------------------------------------
41 // Add JPEG image to the output file
42 var img = Image.create(doc.sdfDoc, Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "peppers.jpg")!!.absolutePath)
43 var element = f.createImage(img, 50.0, 500.0, (img.imageWidth / 2).toDouble(), (img.imageHeight / 2).toDouble())
44 writer.writePlacedElement(element)
45
46 // ----------------------------------------------------------
47 // Add a PNG image to the output file
48 img = Image.create(doc.sdfDoc, Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "butterfly.png")!!.absolutePath)
49 element = f.createImage(img, Matrix2D(100.0, 0.0, 0.0, 100.0, 300.0, 500.0))
50 writer.writePlacedElement(element)
51
52 // ----------------------------------------------------------
53 // Add a GIF image to the output file
54 img = Image.create(doc.sdfDoc, Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "pdfnet.gif")!!.absolutePath)
55 element = f.createImage(img, Matrix2D(img.imageWidth.toDouble(), 0.0, 0.0, img.imageHeight.toDouble(), 50.0, 350.0))
56 writer.writePlacedElement(element)
57
58 // ----------------------------------------------------------
59 // Add a TIFF image to the output file
60 img = Image.create(doc.sdfDoc, Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "grayscale.tif")!!.absolutePath)
61 element = f.createImage(img, Matrix2D(img.imageWidth.toDouble(), 0.0, 0.0, img.imageHeight.toDouble(), 10.0, 50.0))
62 writer.writePlacedElement(element)
63
64 writer.end() // Save the page
65 doc.pagePushBack(page) // Add the page to the document page sequence
66
67 // ----------------------------------------------------------
68 // Embed a monochrome TIFF. Compress the image using lossy JBIG2 filter.
69
70 page = doc.pageCreate(Rect(0.0, 0.0, 612.0, 794.0))
71 writer.begin(page) // begin writing to this page
72
73 // Note: encoder hints can be used to select between different compression methods.
74 // For example to instruct PDFNet to compress a monochrome image using JBIG2 compression.
75 val hint_set = ObjSet()
76 val enc = hint_set.createArray() // Initilaize encoder 'hint' parameter
77 enc.pushBackName("JBIG2")
78 enc.pushBackName("Lossy")
79
80 img = Image.create(doc.sdfDoc, Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "multipage.tif")!!.absolutePath)
81 element = f.createImage(img, Matrix2D(612.0, 0.0, 0.0, 794.0, 0.0, 0.0))
82 writer.writePlacedElement(element)
83
84 writer.end() // Save the page
85 doc.pagePushBack(page) // Add the page to the document page sequence
86
87 // ----------------------------------------------------------
88 // Add a JPEG2000 (JP2) image to the output file
89
90 // Create a new page
91 page = doc.pageCreate()
92 writer.begin(page) // Begin writing to the page
93
94 // Embed the image.
95 img = Image.create(doc.sdfDoc, Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "palm.jp2")!!.absolutePath)
96
97 // Position the image on the page.
98 element = f.createImage(img, Matrix2D(img.imageWidth.toDouble(), 0.0, 0.0, img.imageHeight.toDouble(), 96.0, 80.0))
99 writer.writePlacedElement(element)
100
101 // Write 'JPEG2000 Sample' text string under the image.
102 writer.writeElement(f.createTextBegin(Font.create(doc.sdfDoc, Font.e_times_roman), 32.0))
103 element = f.createTextRun("JPEG2000 Sample")
104 element.setTextMatrix(1.0, 0.0, 0.0, 1.0, 190.0, 30.0)
105 writer.writeElement(element)
106 writer.writeElement(f.createTextEnd())
107
108 writer.end() // Finish writing to the page
109 doc.pagePushBack(page)
110
111 // ----------------------------------------------------------
112 // doc.Save((Utils.createExternalFile("addimage.pdf", mFileList).getAbsolutePath()).c_str(), Doc.e_remove_unused, 0);
113 doc.save(Utils.createExternalFile("addimage.pdf", mFileList).absolutePath, SDFDoc.SaveMode.LINEARIZED, null)
114 mOutputListener!!.println("Done. Result saved in addimage.pdf...")
115 }
116 } catch (e: PDFNetException) {
117 mOutputListener!!.printError(e.stackTrace)
118 mOutputListener!!.printError(e.stackTrace)
119 }
120
121 for (file in mFileList) {
122 addToFileList(file)
123 }
124 printFooter(outputListener)
125 }
126
127 companion object {
128
129 private var mOutputListener: OutputListener? = null
130
131 private val mFileList = ArrayList<String>()
132 }
133
134}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales