AddImage

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}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales