AddImage

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

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#import <OBJC/PDFNetOBJC.h>
7#import <Foundation/Foundation.h>
8//-----------------------------------------------------------------------------------
9// This sample illustrates how to embed various raster image formats
10// (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) in a PDF document.
11//
12// Note: On Windows platform this sample utilizes GDI+ and requires GDIPLUS.DLL to
13// be present in the system path.
14//-----------------------------------------------------------------------------------
15
16int main (int argc, const char * argv[])
17{
18 @autoreleasepool {
19
20 int ret = 0;
21 [PTPDFNet Initialize: 0];
22
23 PTPDFDoc* doc = [[PTPDFDoc alloc] init];
24
25 PTElementBuilder* builder = [[PTElementBuilder alloc] init]; // Used to build new Element objects
26 PTElementWriter* writer = [[PTElementWriter alloc] init]; // Used to write Elements to the page
27
28 PTPDFRect * rect = [[PTPDFRect alloc] init];
29 [rect Set: 0 y1: 0 x2: 612 y2: 792];
30 PTPage* page = [doc PageCreate: rect]; // Start a new page
31 [writer WriterBeginWithPage: page placement: e_ptoverlay page_coord_sys: YES compress: YES resources: NULL]; // Begin writing to this page
32
33 // ----------------------------------------------------------
34 // Add JPEG image to the output file
35 PTImage* img = [PTImage Create: [doc GetSDFDoc] filename: @"../../TestFiles/peppers.jpg"];
36 PTElement* element = [builder CreateImageWithCornerAndScale: img x: 50 y: 500 hscale: [img GetImageWidth]/2 vscale: [img GetImageHeight]/2];
37 [writer WritePlacedElement: element];
38
39 // ----------------------------------------------------------
40 // Add a PNG image to the output file
41 img = [PTImage Create: [doc GetSDFDoc] filename: @"../../TestFiles/butterfly.png"];
42 element = [builder CreateImageWithMatrix: img mtx: [[PTMatrix2D alloc] initWithA: 100 b: 0 c: 0 d: 100 h: 300 v: 500]];
43 [writer WritePlacedElement: element];
44
45 // ----------------------------------------------------------
46 // Add a GIF image to the output file
47 img = [PTImage Create: [doc GetSDFDoc] filename: @"../../TestFiles/pdfnet.gif"];
48 element = [builder CreateImageWithMatrix: img mtx: [[PTMatrix2D alloc] initWithA: [img GetImageWidth] b: 0 c: 0 d: [img GetImageHeight] h: 50 v: 350]];
49 [writer WritePlacedElement: element];
50
51 // ----------------------------------------------------------
52 // Add a TIFF image to the output file
53
54 img = [PTImage Create: [doc GetSDFDoc] filename: @"../../TestFiles/grayscale.tif"];
55 element = [builder CreateImageWithMatrix: img mtx: [[PTMatrix2D alloc] initWithA: [img GetImageWidth] b: 0 c: 0 d: [img GetImageHeight] h: 10 v: 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: [[PTPDFRect alloc] initWithX1: 0 y1: 0 x2: 612 y2: 794]];
65 [writer WriterBeginWithPage: page placement: e_ptoverlay page_coord_sys: YES compress: YES resources: NULL]; // 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 PTObjSet* hint_set = [[PTObjSet alloc] init];
70 PTObj* enc=[hint_set CreateArray]; // Initialize encoder 'hint' parameter
71 [enc PushBackName: @"JBIG2"];
72 [enc PushBackName: @"Lossy"];
73
74 img = [PTImage CreateWithFile: [doc GetSDFDoc] filename: @"../../TestFiles/multipage.tif" encoder_hints: enc];
75 element = [builder CreateImageWithMatrix: img mtx: [[PTMatrix2D alloc] initWithA: 612 b: 0 c: 0 d: 794 h: 0 v: 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: [[PTPDFRect alloc] initWithX1: 0 y1: 0 x2: 612 y2: 794]];
86 [writer WriterBeginWithPage: page placement: e_ptoverlay page_coord_sys: YES compress: YES resources: NULL]; // Begin writing to the page
87
88 // Embed the image.
89 img = [PTImage Create: [doc GetSDFDoc] filename: @"../../TestFiles/palm.jp2"];
90
91 // Position the image on the page.
92 element = [builder CreateImageWithMatrix: img mtx: [[PTMatrix2D alloc] initWithA: [img GetImageWidth] b: 0 c: 0 d: [img GetImageHeight] h: 96 v: 80]];
93 [writer WritePlacedElement: element];
94
95 // Write 'JPEG2000 Sample' text string under the image.
96 [writer WriteElement: [builder CreateTextBeginWithFont: [PTFont Create: [doc GetSDFDoc] type: e_pttimes_roman embed: NO] font_sz: 32]];
97 element = [builder CreateTextRun: @"JPEG2000 Sample"];
98 [element SetTextMatrix: 1 b: 0 c: 0 d: 1 h: 190 v: 30];
99 [writer WriteElement: element];
100 [writer WriteElement: [builder CreateTextEnd]];
101
102 [writer End]; // Finish writing to the page
103 [doc PagePushBack: page];
104
105 [doc SaveToFile: @"../../TestFiles/Output/addimage.pdf" flags: e_ptlinearized];
106 NSLog(@"Done. Result saved in addimage.pdf...");
107 [PTPDFNet Terminate: 0];
108 return ret;
109 }
110}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales