AddImage

Sample JavaScript 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 Web SDK and PDF Editing & Manipulation Library.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5//---------------------------------------------------------------------------------------
6// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
7// Consult legal.txt regarding legal and license information.
8//---------------------------------------------------------------------------------------
9
10(exports => {
11
12
13
14
15
16
17
18
19
20
21
22 exports.runAddImageTest = () => {
23 const PDFNet = exports.Core.PDFNet;
24
25 const main = async () => {
26 try {
27 // Relative path to the folder containing test files.
28 const inputURL = '../TestFiles/';
29
30 const doc = await PDFNet.PDFDoc.create();
31 doc.initSecurityHandler();
32 doc.lock();
33
34 const builder = await PDFNet.ElementBuilder.create(); // ElementBuilder, used to build new element Objects
35 // create a new page writer that allows us to add/change page elements
36 const writer = await PDFNet.ElementWriter.create(); // ElementWriter, used to write elements to the page
37 // define new page dimensions
38 const pageRect = await PDFNet.Rect.init(0, 0, 612, 794);
39 let page = await doc.pageCreate(pageRect);
40
41 writer.beginOnPage(page, PDFNet.ElementWriter.WriteMode.e_overlay);
42
43 // Adding a JPEG image to output file
44 let img = await PDFNet.Image.createFromURL(doc, inputURL + 'peppers.jpg');
45 let matrix = await PDFNet.Matrix2D.create(200, 0, 0, 250, 50, 500);
46 const matrix2 = await PDFNet.Matrix2D.createZeroMatrix();
47 await matrix2.set(200, 0, 0, 250, 50, 500);
48 let element = await builder.createImageFromMatrix(img, matrix2);
49 writer.writePlacedElement(element);
50
51 // Add a PNG to output file
52 img = await PDFNet.Image.createFromURL(doc, inputURL + 'butterfly.png');
53 matrix = await PDFNet.Matrix2D.create(await img.getImageWidth(), 0, 0, await img.getImageHeight(), 300, 500);
54 element = await builder.createImageFromMatrix(img, matrix);
55 writer.writePlacedElement(element);
56
57 // Add a GIF image to the output file
58 img = await PDFNet.Image.createFromURL(doc, inputURL + 'pdfnet.gif');
59 matrix = await PDFNet.Matrix2D.create(await img.getImageWidth(), 0, 0, await img.getImageHeight(), 50, 350);
60 element = await builder.createImageFromMatrix(img, matrix);
61 writer.writePlacedElement(element);
62
63 // Add a TIFF image to the output file
64 img = await PDFNet.Image.createFromURL(doc, inputURL + 'grayscale.tif');
65 matrix = await PDFNet.Matrix2D.create(await img.getImageWidth(), 0, 0, await img.getImageHeight(), 10, 50);
66 element = await builder.createImageFromMatrix(img, matrix);
67 writer.writePlacedElement(element);
68 writer.end();
69 doc.pagePushBack(page);
70
71 // Embed monochrome TIFF compressed using lossy JBIG2 filter
72 page = await doc.pageCreate(pageRect);
73 writer.beginOnPage(page, 1, true, true);
74
75 const hintSet = await PDFNet.ObjSet.create();
76 const enc = await hintSet.createArray();
77 await enc.pushBackName('JBIG2');
78 await enc.pushBackName('Lossy');
79
80 img = await PDFNet.Image.createFromURL(doc, inputURL + 'multipage.tif', enc);
81 matrix = await PDFNet.Matrix2D.create(612, 0, 0, 794, 0, 0);
82 element = await builder.createImageFromMatrix(img, matrix);
83 writer.writePlacedElement(element);
84 writer.end();
85 doc.pagePushBack(page);
86
87 // Add a JPEG200 to output file
88 page = await doc.pageCreate(pageRect);
89 writer.beginOnPage(page, 1, true, true);
90
91 img = await PDFNet.Image.createFromURL(doc, inputURL + 'palm.jp2');
92 matrix = await PDFNet.Matrix2D.create(await img.getImageWidth(), 0, 0, await img.getImageHeight(), 96, 80);
93 element = await builder.createImageFromMatrix(img, matrix);
94 writer.writePlacedElement(element);
95
96 // write 'JPEG2000 Sample' text under image
97 const timesFont = await PDFNet.Font.create(doc, PDFNet.Font.StandardType1Font.e_times_roman);
98 writer.writeElement(await builder.createTextBeginWithFont(timesFont, 32));
99 element = await builder.createTextRun('JPEG2000 Sample', timesFont, 32);
100 matrix = await PDFNet.Matrix2D.create(1, 0, 0, 1, 190, 30);
101 element.setTextMatrix(matrix); // await?
102 writer.writeElement(element);
103 const element2 = await builder.createTextEnd();
104 writer.writeElement(element2);
105
106 writer.end();
107 doc.pagePushBack(page); // add the page to the document
108
109 const docbuf = await doc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_linearized);
110 exports.saveBufferAsPDFDoc(docbuf, 'addimage.pdf');
111
112 console.log('Done. Result saved in addimage.pdf...');
113 } catch (err) {
114 console.log(err);
115 }
116 };
117 // add your own license key as the second parameter, e.g. PDFNet.runWithCleanup(main, 'YOUR_LICENSE_KEY')
118 PDFNet.runWithCleanup(main);
119 };
120})(window);
121// eslint-disable-next-line spaced-comment
122//# sourceURL=AddImageTest.js

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales