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 UWP SDK and PDF Editing & Manipulation Library.
1//
2// Copyright (c) 2001-2020 by PDFTron Systems Inc. All Rights Reserved.
3//
4
5using System;
6using System.IO;
7using System.Threading.Tasks;
8using Windows.Foundation;
9
10using pdftron.PDF;
11using pdftron.SDF;
12
13using pdftron.Common;
14using PDFNetUniversalSamples.ViewModels;
15
16namespace PDFNetSamples
17{
18 /// <summary>
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 /// </summary>
24 public sealed class AddImageTest : Sample
25 {
26 public AddImageTest() :
27 base("AddImage", "This sample illustrates how to embed various raster image formats (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) in a PDF document.")
28 {
29 }
30
31 public override IAsyncAction RunAsync()
32 {
33 return Task.Run(new System.Action(async () =>
34 {
35 WriteLine("--------------------------------");
36 WriteLine("Starting Add Image Test...");
37 WriteLine("--------------------------------\n");
38 try
39 {
40 PDFDoc doc = new PDFDoc();
41 SDFDoc sdfDoc = doc.GetSDFDoc();
42
43 ElementBuilder bld = new ElementBuilder(); // Used to build new Element objects
44 ElementWriter writer = new ElementWriter(); // Used to write Elements to the page
45
46 Page page = doc.PageCreate(); // Start a new page
47 writer.Begin(page); // Begin writing to this page
48
49 // ----------------------------------------------------------
50 // Embed a JPEG image to the output document.
51 Image img = await Image.CreateAsync(sdfDoc, Path.Combine(InputPath, "peppers.jpg"));
52
53 Element element = bld.CreateImage(img, new Matrix2D(200, 0, 0, 250, 50, 500));
54 writer.WritePlacedElement(element);
55
56
57 // ----------------------------------------------------------
58 // Add a TIFF image to the output file
59 img = await Image.CreateAsync(sdfDoc, Path.Combine(InputPath, "grayscale.tif"));
60 element = bld.CreateImage(img, new Matrix2D(img.GetImageWidth(), 0, 0, img.GetImageHeight(), 10, 50));
61 writer.WritePlacedElement(element);
62
63 writer.End(); // Finish writing to the page
64 doc.PagePushBack(page);
65
66
67 // ----------------------------------------------------------
68 // Embed a TIFF image using JBIG2 Encoding
69
70 // Create a new page
71 page = doc.PageCreate(new pdftron.PDF.Rect(0, 0, 612, 794));
72 writer.Begin(page); // Begin writing to the page
73
74 // Use JBIG2 Encoding
75 ObjSet objset = new ObjSet();
76 Obj jbig2_hint = objset.CreateName("JBIG2");
77
78 img = await Image.CreateAsync(sdfDoc, Path.Combine(InputPath, "multipage.tif"), jbig2_hint);
79 element = bld.CreateImage(img, new Matrix2D(612, 0, 0, 794, 0, 0));
80 writer.WritePlacedElement(element);
81
82 writer.End(); // Finish writing to the page
83 doc.PagePushBack(page);
84
85 // Embed the second TIFF frame. Use JBIG2 Encoding
86 page = doc.PageCreate(); // Create a new page
87 writer.Begin(page); // Begin writing to this page
88
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 = await Image.CreateAsync(sdfDoc, Path.Combine(InputPath, "palm.jp2"));
99
100 // Position the image on the page.
101 element = bld.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(bld.CreateTextBegin(Font.Create(doc, FontStandardType1Font.e_times_roman), 32));
106 element = bld.CreateTextRun("JPEG2000 Sample");
107 element.SetTextMatrix(1, 0, 0, 1, 190, 30);
108 writer.WriteElement(element);
109 writer.WriteElement(bld.CreateTextEnd());
110
111 writer.End(); // Finish writing to the page
112 doc.PagePushBack(page);
113
114 // Calling Dispose() results in increased performance and lower memory consumption.
115 bld.Dispose();
116 writer.Dispose();
117
118 String output_file_path = Path.Combine(OutputPath, "addimage.pdf");
119 await doc.SaveAsync(output_file_path, SDFDocSaveOptions.e_linearized);
120 WriteLine("Done. Results saved in " + output_file_path);
121 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
122
123 doc.Destroy();
124 }
125 catch (Exception e)
126 {
127 WriteLine(GetExceptionMessage(e));
128 }
129
130 WriteLine("\n--------------------------------");
131 WriteLine("Done Annotation Test.");
132 WriteLine("--------------------------------\n");
133 })).AsAsyncAction();
134 }
135 }
136}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales