Sample C# 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 Server SDK and PDF Editing & Manipulation Library.
1//
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3//
4
5using System;
6using pdftron;
7using pdftron.Common;
8using pdftron.Filters;
9using pdftron.SDF;
10using pdftron.PDF;
11
12namespace AddImageTestCS
13{
14 class Class1
15 {
16 private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
17 static Class1() {}
18
19 /// <summary>
20 //-----------------------------------------------------------------------------------
21 // This sample illustrates how to embed various raster image formats
22 // (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) in a PDF document.
23 //-----------------------------------------------------------------------------------
24 /// </summary>
25 static void Main(string[] args)
26 {
27 PDFNet.Initialize(PDFTronLicense.Key);
28
29 // Relative path to the folder containing test files.
30 string input_path = "../../../../TestFiles/";
31 string output_path = "../../../../TestFiles/Output/";
32
33 try
34 {
35 using (PDFDoc doc = new PDFDoc())
36 using (ElementBuilder bld = new ElementBuilder()) // Used to build new Element objects
37 using (ElementWriter writer = new ElementWriter()) // Used to write Elements to the page
38 {
39 Page page = doc.PageCreate(); // Start a new page
40 writer.Begin(page); // Begin writing to this page
41
42 // ----------------------------------------------------------
43 // Embed a JPEG image to the output document.
44 Image img = Image.Create(doc, input_path + "peppers.jpg");
45
46 // You can also directly add any .NET Bitmap. The following commented-out code
47 // is equivalent to the above line:
48 // System.Drawing.Bitmap bmp;
49 // System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(input_path + "peppers.jpg");
50 // Image img = Image.Create(doc, bmp);
51
52 Element element = bld.CreateImage(img, 50, 500, img.GetImageWidth() / 2, img.GetImageHeight() / 2);
53 writer.WritePlacedElement(element);
54
55 // ----------------------------------------------------------
56 // Add a PNG image to the output file
57 img = Image.Create(doc, input_path + "butterfly.png");
58 element = bld.CreateImage(img, new Matrix2D(100, 0, 0, 100, 300, 500));
59 writer.WritePlacedElement(element);
60
61 // ----------------------------------------------------------
62 // Add a GIF image to the output file
63 img = Image.Create(doc, input_path + "pdfnet.gif");
64 element = bld.CreateImage(img, new Matrix2D(img.GetImageWidth(), 0, 0, img.GetImageHeight(), 50, 350));
65 writer.WritePlacedElement(element);
66
67 // ----------------------------------------------------------
68 // Add a TIFF image to the output file
69 img = Image.Create(doc, input_path + "grayscale.tif");
70 element = bld.CreateImage(img, new Matrix2D(img.GetImageWidth(), 0, 0, img.GetImageHeight(), 10, 50));
71 writer.WritePlacedElement(element);
72
73 writer.End(); // Save the page
74 doc.PagePushBack(page); // Add the page to the document page sequence
75
76 // ----------------------------------------------------------
77 // Add a BMP image to the output file
78 /*
79 bmp = new System.Drawing.Bitmap(input_path + "pdftron.bmp");
80 img = Image.Create(doc, bmp);
81 element = bld.CreateImage(img, new Matrix2D(bmp.Width, 0, 0, bmp.Height, 255, 700));
82 writer.WritePlacedElement(element);
83
84 writer.End(); // Finish writing to the page
85 doc.PagePushBack(page);
86 */
87
88 // ----------------------------------------------------------
89 // Embed a monochrome TIFF. Compress the image using lossy JBIG2 filter.
90
91 page = doc.PageCreate(new pdftron.PDF.Rect(0, 0, 612, 794));
92 writer.Begin(page); // begin writing to this page
93
94 // Note: encoder hints can be used to select between different compression methods.
95 // For example to instruct PDFNet to compress a monochrome image using JBIG2 compression.
96 ObjSet hint_set = new ObjSet();
97 Obj enc = hint_set.CreateArray(); // Initialize encoder 'hint' parameter
98 enc.PushBackName("JBIG2");
99 enc.PushBackName("Lossy");
100
101 img = pdftron.PDF.Image.Create(doc, input_path + "multipage.tif", enc);
102 element = bld.CreateImage(img, new Matrix2D(612, 0, 0, 794, 0, 0));
103 writer.WritePlacedElement(element);
104
105 writer.End(); // Save the page
106 doc.PagePushBack(page); // Add the page to the document page sequence
107
108 // ----------------------------------------------------------
109 // Add a JPEG2000 (JP2) image to the output file
110
111 // Create a new page
112 page = doc.PageCreate();
113 writer.Begin(page); // Begin writing to the page
114
115 // Embed the image.
116 img = pdftron.PDF.Image.Create(doc, input_path + "palm.jp2");
117
118 // Position the image on the page.
119 element = bld.CreateImage(img, new Matrix2D(img.GetImageWidth(), 0, 0, img.GetImageHeight(), 96, 80));
120 writer.WritePlacedElement(element);
121
122 // Write 'JPEG2000 Sample' text string under the image.
123 writer.WriteElement(bld.CreateTextBegin(pdftron.PDF.Font.Create(doc, pdftron.PDF.Font.StandardType1Font.e_times_roman), 32));
124 element = bld.CreateTextRun("JPEG2000 Sample");
125 element.SetTextMatrix(1, 0, 0, 1, 190, 30);
126 writer.WriteElement(element);
127 writer.WriteElement(bld.CreateTextEnd());
128
129 writer.End(); // Finish writing to the page
130 doc.PagePushBack(page);
131
132
133 doc.Save(output_path + "addimage.pdf", SDFDoc.SaveOptions.e_linearized);
134 PDFNet.Terminate();
135 Console.WriteLine("Done. Result saved in addimage.pdf...");
136 }
137 }
138 catch (PDFNetException e)
139 {
140 Console.WriteLine(e.Message);
141 }
142
143 }
144 }
145}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6#include <PDF/PDFNet.h>
7#include <PDF/PDFDoc.h>
8#include <PDF/ElementBuilder.h>
9#include <PDF/ElementWriter.h>
10#include <PDF/ElementReader.h>
11#include <PDF/Image.h>
12#include <Filters/MappedFile.h>
13#include <Filters/FilterReader.h>
14#include <SDF/ObjSet.h>
15#include "../../LicenseKey/CPP/LicenseKey.h"
16
17#include <iostream>
18
19using namespace std;
20
21using namespace pdftron;
22using namespace Common;
23using namespace SDF;
24using namespace PDF;
25
26//-----------------------------------------------------------------------------------
27// This sample illustrates how to embed various raster image formats
28// (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) in a PDF document.
29//
30// Note: On Windows platform this sample utilizes GDI+ and requires GDIPLUS.DLL to
31// be present in the system path.
32//-----------------------------------------------------------------------------------
33
34int main(int argc, char *argv[])
35{
36 int ret = 0;
37 PDFNet::Initialize(LicenseKey);
38
39 // Relative path to the folder containing test files.
40 string input_path = "../../TestFiles/";
41 string output_path = "../../TestFiles/Output/";
42
43 try
44 {
45 PDFDoc doc;
46
47 ElementBuilder f; // Used to build new Element objects
48 ElementWriter writer; // Used to write Elements to the page
49
50 Page page = doc.PageCreate(); // Start a new page
51 writer.Begin(page); // Begin writing to this page
52
53 // ----------------------------------------------------------
54 // Add JPEG image to the output file
55 PDF::Image img = PDF::Image::Create(doc, (input_path + "peppers.jpg").c_str());
56 Element element = f.CreateImage(img, 50, 500, img.GetImageWidth()/2, img.GetImageHeight()/2);
57 writer.WritePlacedElement(element);
58
59 // ----------------------------------------------------------
60 // Add a PNG image to the output file
61 img = PDF::Image::Create(doc, (input_path + "butterfly.png").c_str());
62 element = f.CreateImage(img, Matrix2D(100, 0, 0, 100, 300, 500));
63 writer.WritePlacedElement(element);
64
65 // ----------------------------------------------------------
66 // Add a GIF image to the output file
67 img = PDF::Image::Create(doc, (input_path + "pdfnet.gif").c_str());
68 element = f.CreateImage(img, Matrix2D(img.GetImageWidth(), 0, 0, img.GetImageHeight(), 50, 350));
69 writer.WritePlacedElement(element);
70
71 // ----------------------------------------------------------
72 // Add a TIFF image to the output file
73
74 img = PDF::Image::Create(doc, (input_path + "grayscale.tif").c_str());
75 element = f.CreateImage(img, 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(PDF::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;
90 Obj enc=hint_set.CreateArray(); // Initialize encoder 'hint' parameter
91 enc.PushBackName("JBIG2");
92 enc.PushBackName("Lossy");
93
94 img = PDF::Image::Create(doc, (input_path + "multipage.tif").c_str(), enc);
95 element = f.CreateImage(img, 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, (input_path + "palm.jp2").c_str());
110
111 // Position the image on the page.
112 element = f.CreateImage(img, 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, 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
127 doc.Save((output_path + "addimage.pdf").c_str(), SDFDoc::e_linearized, 0);
128 cout << "Done. Result saved in addimage.pdf..." << endl;
129 }
130 catch(Common::Exception& e)
131 {
132 cout << e << endl;
133 ret = 1;
134 }
135 catch(...)
136 {
137 cout << "Unknown Exception" << endl;
138 ret = 1;
139 }
140
141 PDFNet::Terminate();
142 return ret;
143}
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}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
3// Consult LICENSE.txt regarding license information.
4//---------------------------------------------------------------------------------------
5
6package main
7import (
8 "fmt"
9 . "pdftron"
10)
11
12import "pdftron/Samples/LicenseKey/GO"
13
14//-----------------------------------------------------------------------------------
15// This sample illustrates how to embed various raster image formats
16// (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) in a PDF document.
17//
18// Note: On Windows platform this sample utilizes GDI+ and requires GDIPLUS.DLL to
19// be present in the system path.
20//-----------------------------------------------------------------------------------
21
22func main(){
23 PDFNetInitialize(PDFTronLicense.Key)
24 // Relative path to the folder containing test files.
25 var inputPath = "../../TestFiles/"
26 var outputPath = "../../TestFiles/Output/"
27 doc := NewPDFDoc()
28 f := NewElementBuilder() // Used to build new Element objects
29 writer := NewElementWriter() // Used to write Elements to the page
30 page := doc.PageCreate() // Start a new page
31 writer.Begin(page) // Begin writing to this page
32 // ----------------------------------------------------------
33 // Add JPEG image to the output file
34 img := ImageCreate(doc.GetSDFDoc(), inputPath + "peppers.jpg")
35 element := f.CreateImage(img, 50.0, 500.0, float64(img.GetImageWidth()/2), float64(img.GetImageHeight()/2))
36 writer.WritePlacedElement(element)
37
38 // ----------------------------------------------------------
39 // Add a PNG image to the output file
40 img = ImageCreate(doc.GetSDFDoc(), inputPath + "butterfly.png")
41 element = f.CreateImage(img, NewMatrix2D(100.0, 0.0, 0.0, 100.0, 300.0, 500.0))
42 writer.WritePlacedElement(element)
43
44 //----------------------------------------------------------
45 // Add a GIF image to the output file
46 img = ImageCreate(doc.GetSDFDoc(), inputPath + "pdfnet.gif")
47 element = f.CreateImage(img, NewMatrix2D(float64(img.GetImageWidth()), 0.0, 0.0, float64(img.GetImageHeight()), 50.0, 350.0))
48 writer.WritePlacedElement(element)
49
50 // ----------------------------------------------------------
51 // Add a TIFF image to the output file
52
53 img = ImageCreate(doc.GetSDFDoc(), (inputPath + "grayscale.tif"))
54 element = f.CreateImage(img, NewMatrix2D(float64(img.GetImageWidth()), 0.0, 0.0, float64(img.GetImageHeight()), 10.0, 50.0))
55 writer.WritePlacedElement(element)
56
57 writer.End() // Save the page
58 doc.PagePushBack(page) // Add the page to the document page sequence
59
60 // ----------------------------------------------------------
61 // Embed a monochrome TIFF. Compress the image using lossy JBIG2 filter.
62 page = doc.PageCreate(NewRect(0.0, 0.0, 612.0, 794.0))
63 writer.Begin(page) // begin writing to this page
64
65 // Note: encoder hints can be used to select between different compression methods.
66 // For example to instruct PDFNet to compress a monochrome image using JBIG2 compression.
67 hintSet := NewObjSet();
68 enc := hintSet.CreateArray(); // Initilaize encoder 'hint' parameter
69 enc.PushBackName("JBIG2");
70 enc.PushBackName("Lossy");
71
72 img = ImageCreate(doc.GetSDFDoc(), inputPath + "multipage.tif");
73 element = f.CreateImage(img, NewMatrix2D(612.0, 0.0, 0.0, 794.0, 0.0, 0.0));
74 writer.WritePlacedElement(element);
75
76 writer.End() // Save the page
77 doc.PagePushBack(page) // Add the page to the document page sequence
78
79 // ----------------------------------------------------------
80 // Add a JPEG2000 (JP2) image to the output file
81
82 // Create a new page
83 page = doc.PageCreate()
84 writer.Begin(page) // Begin writing to the page
85
86 // Embed the image
87 img = ImageCreate(doc.GetSDFDoc(), inputPath + "palm.jp2")
88
89 // Position the image on the page
90 element = f.CreateImage(img, NewMatrix2D(float64(img.GetImageWidth()), 0.0, 0.0, float64(img.GetImageHeight()), 96.0, 80.0))
91 writer.WritePlacedElement(element)
92
93 // Write 'JPEG2000 Sample' text string under the image
94 writer.WriteElement(f.CreateTextBegin(FontCreate(doc.GetSDFDoc(), FontE_times_roman), 32.0))
95 element = f.CreateTextRun("JPEG2000 Sample")
96 element.SetTextMatrix(1.0, 0.0, 0.0, 1.0, 190.0, 30.0)
97 writer.WriteElement(element)
98 writer.WriteElement(f.CreateTextEnd())
99
100 writer.End()
101 doc.PagePushBack(page)
102
103 doc.Save((outputPath + "addimage.pdf"), uint(SDFDocE_linearized));
104 doc.Close()
105
106 PDFNetTerminate()
107 fmt.Println("Done. Result saved in addimage.pdf...")
108}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6
7const { PDFNet } = require('@pdftron/pdfnet-node');
8const PDFTronLicense = require('../LicenseKey/LicenseKey');
9
10((exports) => {
11
12 exports.runAddImageTest = () => {
13
14 const main = async() => {
15 try {
16 // Relative path to the folder containing test files.
17 const inputURL = '../TestFiles/';
18
19 const doc = await PDFNet.PDFDoc.create();
20 doc.initSecurityHandler();
21
22 const builder = await PDFNet.ElementBuilder.create(); // ElementBuilder, used to build new element Objects
23 // create a new page writer that allows us to add/change page elements
24 const writer = await PDFNet.ElementWriter.create(); // ElementWriter, used to write elements to the page
25 // define new page dimensions
26 let page = await doc.pageCreate();
27
28 writer.beginOnPage(page, PDFNet.ElementWriter.WriteMode.e_overlay);
29
30 // Adding a JPEG image to output file
31 let img = await PDFNet.Image.createFromFile(doc, inputURL + 'peppers.jpg');
32 let imgWidth = await img.getImageWidth();
33 let imgHeight = await img.getImageHeight();
34 let element = await builder.createImageScaled(img, 50, 500, imgWidth/2, imgHeight/2);
35 writer.writePlacedElement(element);
36
37 // Add a PNG to output file
38 img = await PDFNet.Image.createFromFile(doc, inputURL + 'butterfly.png');
39 matrix = await PDFNet.Matrix2D.create(100, 0, 0, 100, 300, 500);
40 element = await builder.createImageFromMatrix(img, matrix);
41 writer.writePlacedElement(element);
42
43 // Add a GIF image to the output file
44 img = await PDFNet.Image.createFromFile(doc, inputURL + 'pdfnet.gif');
45 imgWidth = await img.getImageWidth();
46 imgHeight = await img.getImageHeight();
47 matrix = await PDFNet.Matrix2D.create(imgWidth, 0, 0, imgHeight, 50, 350);
48 element = await builder.createImageFromMatrix(img, matrix);
49 writer.writePlacedElement(element);
50
51 // Add a TIFF image to the output file
52 img = await PDFNet.Image.createFromFile(doc, inputURL + 'grayscale.tif');
53 imgWidth = await img.getImageWidth();
54 imgHeight = await img.getImageHeight();
55 matrix = await PDFNet.Matrix2D.create(imgWidth, 0, 0, imgHeight, 10, 50);
56 element = await builder.createImageFromMatrix(img, matrix);
57 writer.writePlacedElement(element);
58
59 writer.end();
60 doc.pagePushBack(page);
61
62 // Embed monochrome TIFF compressed using lossy JBIG2 filter
63 const pageRect = await PDFNet.Rect.init(0, 0, 612, 794);
64 page = await doc.pageCreate(pageRect);
65 writer.beginOnPage(page);
66
67 const hintSet = await PDFNet.ObjSet.create();
68 const enc = await hintSet.createArray();
69 await enc.pushBackName('JBIG2');
70 await enc.pushBackName('Lossy');
71
72 img = await PDFNet.Image.createFromFile(doc, inputURL + 'multipage.tif', enc);
73 matrix = await PDFNet.Matrix2D.create(612, 0, 0, 794, 0, 0);
74 element = await builder.createImageFromMatrix(img, matrix);
75 writer.writePlacedElement(element);
76
77 writer.end();
78 doc.pagePushBack(page);
79
80 // Add a JPEG200 to output file
81 page = await doc.pageCreate();
82 writer.beginOnPage(page);
83
84 img = await PDFNet.Image.createFromFile(doc, inputURL + 'palm.jp2');
85 imgWidth = await img.getImageWidth();
86 imgHeight = await img.getImageHeight();
87 matrix = await PDFNet.Matrix2D.create(imgWidth, 0, 0, imgHeight, 96, 80);
88 element = await builder.createImageFromMatrix(img, matrix);
89 writer.writePlacedElement(element);
90
91 // write 'JPEG2000 Sample' text under image
92 const timesFont = await PDFNet.Font.create(doc, PDFNet.Font.StandardType1Font.e_times_roman);
93 writer.writeElement(await builder.createTextBeginWithFont(timesFont, 32));
94 element = await builder.createNewTextRun('JPEG2000 Sample');
95 matrix = await PDFNet.Matrix2D.create(1, 0, 0, 1, 190, 30);
96 await element.setTextMatrix(matrix);
97 writer.writeElement(element);
98 const element2 = await builder.createTextEnd();
99 writer.writeElement(element2);
100
101 await writer.end();
102 doc.pagePushBack(page); // add the page to the document
103
104 await doc.save(inputURL + 'Output/addimage.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
105
106 console.log('Done. Result saved in addimage.pdf...');
107 } catch (err) {
108 console.log(err);
109 }
110 };
111 PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function(error){console.log('Error: ' + JSON.stringify(error));}).then(function(){return PDFNet.shutdown();});
112 };
113 exports.runAddImageTest();
114})(exports);
115// eslint-disable-next-line spaced-comment
116//# sourceURL=AddImageTest.js
1<?php
2//---------------------------------------------------------------------------------------
3// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
4// Consult LICENSE.txt regarding license information.
5//---------------------------------------------------------------------------------------
6if(file_exists("../../../PDFNetC/Lib/PDFNetPHP.php"))
7include("../../../PDFNetC/Lib/PDFNetPHP.php");
8include("../../LicenseKey/PHP/LicenseKey.php");
9
10//-----------------------------------------------------------------------------------
11// This sample illustrates how to embed various raster image formats
12// (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) in a PDF document.
13//
14// Note: On Windows platform this sample utilizes GDI+ and requires GDIPLUS.DLL to
15// be present in the system path.
16//-----------------------------------------------------------------------------------
17
18 PDFNet::Initialize($LicenseKey);
19 PDFNet::GetSystemFontList(); // Wait for fonts to be loaded if they haven't already. This is done because PHP can run into errors when shutting down if font loading is still in progress.
20
21 // Relative path to the folder containing the test files.
22 $input_path = getcwd()."/../../TestFiles/";
23 $output_path = $input_path."Output/";
24
25 $doc = new PDFDoc();
26 $builder = new ElementBuilder(); // Used to build new Element objects
27 $writer = new ElementWriter(); // Used to write Elements to the page
28
29 $page = $doc->PageCreate(); // Start a new page
30 $writer->Begin($page); // Begin writing to this page
31
32 // ----------------------------------------------------------
33 // Add JPEG image to the output file
34 $img = Image::Create($doc->GetSDFDoc(), $input_path."peppers.jpg");
35 $element = $builder->CreateImage($img, 50.0, 500.0, (double)($img->GetImageWidth())/2, (double)($img->GetImageHeight())/2);
36 $writer->WritePlacedElement($element);
37
38 // ----------------------------------------------------------
39 // Add a PNG image to the output file
40 $img = Image::Create($doc->GetSDFDoc(), $input_path."butterfly.png");
41 $element = $builder->CreateImage($img, new Matrix2D(100.0, 0.0, 0.0, 100.0, 300.0, 500.0));
42 $writer->WritePlacedElement($element);
43
44 // ----------------------------------------------------------
45 // Add a GIF image to the output file
46 $img = Image::Create($doc->GetSDFDoc(), $input_path."pdfnet.gif");
47 $element = $builder->CreateImage($img, new Matrix2D((double)($img->GetImageWidth()), 0.0, 0.0, (double)($img->GetImageHeight()), 50.0, 350.0));
48 $writer->WritePlacedElement($element);
49
50 // ----------------------------------------------------------
51 // Add a TIFF image to the output file
52
53 $img = Image::Create($doc->GetSDFDoc(), $input_path."grayscale.tif");
54 $element = $builder->CreateImage($img, new Matrix2D((double)($img->GetImageWidth()), 0.0, 0.0, (double)($img->GetImageHeight()), 10.0, 50.0));
55 $writer->WritePlacedElement($element);
56
57 $writer->End(); // Save the page
58 $doc->PagePushBack($page); // Add the page to the document page sequence
59
60 // ----------------------------------------------------------
61 // Embed a monochrome TIFF. Compress the image using lossy JBIG2 filter.
62
63 $page = $doc->PageCreate(new Rect(0.0, 0.0, 612.0, 794.0));
64 $writer->Begin($page); // begin writing to this page
65
66 // Note: encoder hints can be used to select between different compression methods.
67 // For example to instruct PDFNet to compress a monochrome image using JBIG2 compression.
68 $hint_set = new ObjSet();
69 $enc = $hint_set->CreateArray(); // Initilaize encoder 'hint' parameter
70 $enc->PushBackName("JBIG2");
71 $enc->PushBackName("Lossy");
72
73 $img = Image::Create($doc->GetSDFDoc(), $input_path."multipage.tif");
74 $element = $builder->CreateImage($img, new Matrix2D(612.0, 0.0, 0.0, 794.0, 0.0, 0.0));
75 $writer->WritePlacedElement($element);
76
77 $writer->End(); // Save the page
78 $doc->PagePushBack($page); // Add the page to the document page sequence
79
80 // ----------------------------------------------------------
81 // Add a JPEG2000 (JP2) image to the output file
82
83 // Create a new page
84 $page = $doc->PageCreate();
85 $writer->Begin($page); // Begin writing to the page
86
87 // Embed the image
88 $img = Image::Create($doc->GetSDFDoc(), $input_path."palm.jp2");
89
90 // Position the image on the page
91 $element = $builder->CreateImage($img, new Matrix2D((double)($img->GetImageWidth()), 0.0, 0.0, (double)($img->GetImageHeight()), 96.0, 80.0));
92 $writer->WritePlacedElement($element);
93
94 // Write 'JPEG2000 Sample' text string under the image
95 $writer->WriteElement($builder->CreateTextBegin(Font::Create($doc->GetSDFDoc(), Font::e_times_roman), 32.0));
96 $element = $builder->CreateTextRun("JPEG2000 Sample");
97 $element->SetTextMatrix(1.0, 0.0, 0.0, 1.0, 190.0, 30.0);
98 $writer->WriteElement($element);
99 $writer->WriteElement($builder->CreateTextEnd());
100
101 $writer->End(); // Finish writing to the page
102 $doc->PagePushBack($page);
103
104 $doc->Save(($output_path."addimage.pdf"), SDFDoc::e_linearized);
105 $doc->Close();
106 PDFNet::Terminate();
107 echo nl2br("Done. Result saved in addimage.pdf...\n");
108?>
1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6import site
7site.addsitedir("../../../PDFNetC/Lib")
8import sys
9from PDFNetPython import *
10
11sys.path.append("../../LicenseKey/PYTHON")
12from LicenseKey import *
13
14#-----------------------------------------------------------------------------------
15# This sample illustrates how to embed various raster image formats
16# (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) in a PDF document.
17#
18# Note: On Windows platform this sample utilizes GDI+ and requires GDIPLUS.DLL to
19# be present in the system path.
20#-----------------------------------------------------------------------------------
21
22def main():
23 PDFNet.Initialize(LicenseKey)
24
25 # Relative path to the folder containing test files.
26 input_path = "../../TestFiles/"
27 output_path = "../../TestFiles/Output/"
28
29 doc = PDFDoc()
30
31 f = ElementBuilder() # Used to build new Element objects
32 writer = ElementWriter() # Used to write Elements to the page
33
34 page = doc.PageCreate() # Start a new page
35 writer.Begin(page) # Begin writing to this page
36
37 # ----------------------------------------------------------
38 # Add JPEG image to the output file
39 img = Image.Create(doc.GetSDFDoc(), input_path + "peppers.jpg")
40 element = f.CreateImage(img, 50, 500, img.GetImageWidth()/2, img.GetImageHeight()/2)
41 writer.WritePlacedElement(element)
42
43 # ----------------------------------------------------------
44 # Add a PNG image to the output file
45 img = Image.Create(doc.GetSDFDoc(), input_path + "butterfly.png")
46 element = f.CreateImage(img, Matrix2D(100, 0, 0, 100, 300, 500))
47 writer.WritePlacedElement(element)
48
49 # ----------------------------------------------------------
50 # Add a GIF image to the output file
51 img = Image.Create(doc.GetSDFDoc(), input_path + "pdfnet.gif")
52 element = f.CreateImage(img, Matrix2D(img.GetImageWidth(), 0, 0, img.GetImageHeight(), 50, 350))
53 writer.WritePlacedElement(element)
54
55 # ----------------------------------------------------------
56 # Add a TIFF image to the output file
57
58 img = Image.Create(doc.GetSDFDoc(), (input_path + "grayscale.tif"))
59 element = f.CreateImage(img, Matrix2D(img.GetImageWidth(), 0, 0, img.GetImageHeight(), 10, 50))
60 writer.WritePlacedElement(element)
61
62 writer.End() # Save the page
63 doc.PagePushBack(page) # Add the page to the document page sequence
64
65 # ----------------------------------------------------------
66 # Embed a monochrome TIFF. Compress the image using lossy JBIG2 filter.
67
68 page = doc.PageCreate(Rect(0, 0, 612, 794))
69 writer.Begin(page) # begin writing to this page
70
71 # Note: encoder hints can be used to select between different compression methods.
72 # For example to instruct PDFNet to compress a monochrome image using JBIG2 compression.
73 hint_set = ObjSet();
74 enc = hint_set.CreateArray(); # Initilaize encoder 'hint' parameter
75 enc.PushBackName("JBIG2");
76 enc.PushBackName("Lossy");
77
78 img = Image.Create(doc.GetSDFDoc(), input_path + "multipage.tif");
79 element = f.CreateImage(img, Matrix2D(612, 0, 0, 794, 0, 0));
80 writer.WritePlacedElement(element);
81
82 writer.End() # Save the page
83 doc.PagePushBack(page) # Add the page to the document page sequence
84
85 # ----------------------------------------------------------
86 # Add a JPEG2000 (JP2) image to the output file
87
88 # Create a new page
89 page = doc.PageCreate()
90 writer.Begin(page) # Begin writing to the page
91
92 # Embed the image
93 img = Image.Create(doc.GetSDFDoc(), input_path + "palm.jp2")
94
95 # Position the image on the page
96 element = f.CreateImage(img, Matrix2D(img.GetImageWidth(), 0, 0, img.GetImageHeight(), 96, 80))
97 writer.WritePlacedElement(element)
98
99 # Write 'JPEG2000 Sample' text string under the image
100 writer.WriteElement(f.CreateTextBegin(Font.Create(doc.GetSDFDoc(), Font.e_times_roman), 32))
101 element = f.CreateTextRun("JPEG2000 Sample")
102 element.SetTextMatrix(1, 0, 0, 1, 190, 30)
103 writer.WriteElement(element)
104 writer.WriteElement(f.CreateTextEnd())
105
106 writer.End() # Finish writing to the page
107 doc.PagePushBack(page)
108
109 doc.Save((output_path + "addimage.pdf"), SDFDoc.e_linearized);
110 doc.Close()
111 PDFNet.Terminate()
112
113 print("Done. Result saved in addimage.pdf...")
114
115if __name__ == '__main__':
116 main()
1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6require '../../../PDFNetC/Lib/PDFNetRuby'
7include PDFNetRuby
8require '../../LicenseKey/RUBY/LicenseKey'
9
10$stdout.sync = true
11
12#-----------------------------------------------------------------------------------
13# This sample illustrates how to embed various raster image formats
14# (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) in a PDF document.
15#
16# Note: On Windows platform this sample utilizes GDI+ and requires GDIPLUS.DLL to
17# be present in the system path.
18#-----------------------------------------------------------------------------------
19
20 PDFNet.Initialize(PDFTronLicense.Key)
21
22 # Relative path to the folder containing test files.
23 input_path = "../../TestFiles/"
24 output_path = "../../TestFiles/Output/"
25
26 doc = PDFDoc.new()
27 f = ElementBuilder.new() # Used to build new Element objects
28 writer = ElementWriter.new() # Used to write Elements to the page
29
30 page = doc.PageCreate() # Start a new page
31 writer.Begin(page) # Begin writing to this page
32
33 # ----------------------------------------------------------
34 # Add JPEG image to the output file
35 img = Image.Create(doc.GetSDFDoc(), input_path + "peppers.jpg")
36 element = f.CreateImage(img, 50, 500, img.GetImageWidth()/2, img.GetImageHeight()/2)
37 writer.WritePlacedElement(element)
38
39 # ----------------------------------------------------------
40 # Add a PNG image to the output file
41 img = Image.Create(doc.GetSDFDoc(), input_path + "butterfly.png")
42 element = f.CreateImage(img, Matrix2D.new(100, 0, 0, 100, 300, 500))
43 writer.WritePlacedElement(element)
44
45 # ----------------------------------------------------------
46 # Add a GIF image to the output file (This section is not supported on Linux)
47 img = Image.Create(doc.GetSDFDoc(), input_path + "pdfnet.gif")
48 element = f.CreateImage(img, Matrix2D.new(img.GetImageWidth(), 0, 0, img.GetImageHeight(), 50, 350))
49 writer.WritePlacedElement(element)
50
51 # ----------------------------------------------------------
52 # Add a TIFF image to the output file
53
54 img = Image.Create(doc.GetSDFDoc(), (input_path + "grayscale.tif"))
55 element = f.CreateImage(img, Matrix2D.new(img.GetImageWidth(), 0, 0, img.GetImageHeight(), 10, 50))
56 writer.WritePlacedElement(element)
57
58 writer.End() # Save the page
59 doc.PagePushBack(page) # Add the page to the document page sequence
60
61 # ----------------------------------------------------------
62 # Embed a monochrome TIFF. Compress the image using lossy JBIG2 filter.
63
64 page = doc.PageCreate(Rect.new(0, 0, 612, 794))
65 writer.Begin(page) # begin writing to this page
66
67 # Note: encoder hints can be used to select between different compression methods.
68 # For example to instruct PDFNet to compress a monochrome image using JBIG2 compression.
69 hint_set = ObjSet.new();
70 enc = hint_set.CreateArray(); # Initilaize encoder 'hint' parameter
71 enc.PushBackName("JBIG2");
72 enc.PushBackName("Lossy");
73
74 img = Image.Create(doc.GetSDFDoc(), input_path + "multipage.tif");
75 element = f.CreateImage(img, Matrix2D.new(612, 0, 0, 794, 0, 0));
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 # Add a JPEG2000 (JP2) image to the output file
83
84 # Create a new page
85 page = doc.PageCreate()
86 writer.Begin(page) # Begin writing to the page
87
88 # Embed the image
89 img = Image.Create(doc.GetSDFDoc(), input_path + "palm.jp2")
90
91 # Position the image on the page
92 element = f.CreateImage(img, Matrix2D.new(img.GetImageWidth(), 0, 0, img.GetImageHeight(), 96, 80))
93 writer.WritePlacedElement(element)
94
95 # Write 'JPEG2000 Sample' text string under the image
96 writer.WriteElement(f.CreateTextBegin(Font.Create(doc.GetSDFDoc(), Font::E_times_roman), 32))
97 element = f.CreateTextRun("JPEG2000 Sample")
98 element.SetTextMatrix(1, 0, 0, 1, 190, 30)
99 writer.WriteElement(element)
100 writer.WriteElement(f.CreateTextEnd())
101
102 writer.End() # Finish writing to the page
103 doc.PagePushBack(page)
104
105 doc.Save((output_path + "addimage.pdf"), SDFDoc::E_linearized);
106 doc.Close()
107 PDFNet.Terminate
108 puts "Done. Result saved in addimage.pdf..."
1'
2' Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3'
4
5Imports System
6
7Imports pdftron
8Imports pdftron.Common
9Imports pdftron.Filters
10Imports pdftron.SDF
11Imports pdftron.PDF
12
13Module AddImageTestVB
14 Dim pdfNetLoader As PDFNetLoader
15 Sub New()
16 pdfNetLoader = pdftron.PDFNetLoader.Instance()
17 End Sub
18
19 '-----------------------------------------------------------------------------------
20 ' This sample illustrates how to embed various raster image formats
21 ' (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) in a PDF document.
22 '-----------------------------------------------------------------------------------
23 Sub Main()
24
25 PDFNet.Initialize(PDFTronLicense.Key)
26
27 ' Relative path to the folder containing test files.
28 Dim input_path As String = "../../../../TestFiles/"
29 Dim output_path As String = "../../../../TestFiles/Output/"
30
31 Try
32
33
34 Using doc As PDFDoc = New PDFDoc
35 Using bld As ElementBuilder = New ElementBuilder ' Used to build new Element objects
36 Using writer As ElementWriter = New ElementWriter ' Used to write Elements to the page
37
38 Dim page As Page = doc.PageCreate() ' Start a new page
39 writer.Begin(page) ' Begin writing to this page
40
41 ' ----------------------------------------------------------
42 ' Embed a JPEG image to the output document.
43
44 Dim img As Image = Image.Create(doc.GetSDFDoc(), input_path + "peppers.jpg")
45
46 ' You can also directly add any .NET Bitmap. The following commented-out code
47 ' is equivalent to the above line:
48 Dim bmp As System.Drawing.Bitmap
49 ' bmp = New System.Drawing.Bitmap(input_path + "peppers.jpg")
50 ' Dim img As Image = Image.Create(doc, bmp)
51
52 Dim element As Element = bld.CreateImage(img, 50, 500, img.GetImageWidth() / 2, img.GetImageHeight() / 2)
53 writer.WritePlacedElement(element)
54
55 ' ----------------------------------------------------------
56 ' Add a PNG image to the output file
57 img = Image.Create(doc.GetSDFDoc(), input_path + "butterfly.png")
58 element = bld.CreateImage(img, New Matrix2D(100, 0, 0, 100, 300, 500))
59 writer.WritePlacedElement(element)
60
61 ' ----------------------------------------------------------
62 ' Add a GIF image to the output file
63 img = Image.Create(doc.GetSDFDoc(), input_path + "pdfnet.gif")
64 element = bld.CreateImage(img, New Matrix2D(img.GetImageWidth(), 0, 0, img.GetImageHeight(), 50, 350))
65 writer.WritePlacedElement(element)
66
67 ' ----------------------------------------------------------
68 ' Add a TIFF image to the output file
69 img = Image.Create(doc.GetSDFDoc(), input_path + "grayscale.tif")
70 element = bld.CreateImage(img, New Matrix2D(img.GetImageWidth(), 0, 0, img.GetImageHeight(), 10, 50))
71 writer.WritePlacedElement(element)
72
73 writer.End() ' Save the page
74 doc.PagePushBack(page) ' Add the page to the document page sequence
75
76 ' ----------------------------------------------------------
77 ' Embed a multi-page TIFF to the output file
78
79 ' Create a new page
80 page = doc.PageCreate(New Rect(0, 0, 612, 794))
81 writer.Begin(page) ' Begin writing to the page
82
83 ' Embed the first TIFF page. Use JBIG2 Encoding
84
85 ' Use JBIG2 Encoding
86
87
88 Dim hint_set As ObjSet = New ObjSet
89 Dim enc As Obj = hint_set.CreateArray()
90 enc.PushBackName("JBIG2")
91 enc.PushBackName("Lossy")
92
93
94 img = Image.Create(doc.GetSDFDoc(), input_path + "multipage.tif", enc)
95 element = bld.CreateImage(img, New Matrix2D(612, 0, 0, 794, 0, 0))
96 writer.WritePlacedElement(element)
97
98
99
100 writer.End() ' Save the page
101 doc.PagePushBack(page) ' Add the page to the document page sequence
102
103 ' ----------------------------------------------------------
104 ' Add a JPEG2000 (JP2) image to the output file
105
106 ' Create a new page
107 page = doc.PageCreate()
108 writer.Begin(page) ' Begin writing to the page
109
110 ' Embed the image.
111 img = Image.Create(doc.GetSDFDoc(), input_path + "palm.jp2")
112
113 ' Position the image on the page.
114 element = bld.CreateImage(img, New Matrix2D(img.GetImageWidth(), 0, 0, img.GetImageHeight(), 96, 80))
115 writer.WritePlacedElement(element)
116
117 ' Write 'JPEG2000 Sample' text string under the image.
118 writer.WriteElement(bld.CreateTextBegin(Font.Create(doc, Font.StandardType1Font.e_times_roman), 32))
119 element = bld.CreateTextRun("JPEG2000 Sample")
120 element.SetTextMatrix(1, 0, 0, 1, 190, 30)
121 writer.WriteElement(element)
122 writer.WriteElement(bld.CreateTextEnd())
123
124 writer.End() ' Finish writing to the page
125 doc.PagePushBack(page)
126
127
128
129 End Using
130 End Using
131
132 doc.Save(output_path + "addimage.pdf", SDF.SDFDoc.SaveOptions.e_linearized)
133 Console.WriteLine("Done. Result saved in addimage.pdf...")
134 End Using
135
136 Catch e As PDFNetException
137 Console.WriteLine(e.Message)
138 End Try
139 PDFNet.Terminate()
140 End Sub
141
142End Module
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales