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}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import PDFNet
7import Foundation
8
9func runAddImageTest() -> Int {
10 return autoreleasepool {
11 var ret: Int = 0
12
13
14 do {
15 try PTPDFNet.catchException {
16 let doc: PTPDFDoc = PTPDFDoc()
17
18 let builder: PTElementBuilder = PTElementBuilder() // Used to build new Element objects
19 let writer: PTElementWriter = PTElementWriter() // Used to write Elements to the page
20
21 let rect: PTPDFRect = PTPDFRect()
22 rect.set(0, y1: 0, x2: 612, y2: 792)
23 var page: PTPage = doc.pageCreate(rect) // Start a new page
24 writer.writerBegin(with: page, placement: e_ptoverlay, page_coord_sys: true, compress: true, resources: nil) // Begin writing to this page
25
26 // ----------------------------------------------------------
27 // Add JPEG image to the output file
28 var img: PTImage = PTImage.create(doc.getSDFDoc(), filename: Bundle.main.path(forResource: "peppers", ofType: "jpg"))
29 var element: PTElement = builder.createImage(withCornerAndScale: img, x: 50, y: 500, hscale: Double(img.getWidth() / 2), vscale: Double(img.getHeight() / 2))
30 writer.writePlacedElement(element)
31
32 // ----------------------------------------------------------
33 // Add a PNG image to the output file
34 img = PTImage.create(doc.getSDFDoc(), filename: Bundle.main.path(forResource: "butterfly", ofType: "png"))
35 element = builder.createImage(withMatrix: img, mtx: PTMatrix2D(a: 100, b: 0, c: 0, d: 100, h: 300, v: 500))
36 writer.writePlacedElement(element)
37
38 // ----------------------------------------------------------
39 // Add a TIFF image to the output file
40 img = PTImage.create(doc.getSDFDoc(), filename: Bundle.main.path(forResource: "grayscale", ofType: "tif"))
41 element = builder.createImage(withMatrix: img, mtx: PTMatrix2D(a: Double(img.getWidth()), b: 0, c: 0, d: Double(img.getHeight()), h: 10, v: 50))
42 writer.writePlacedElement(element)
43
44 writer.end() // Save the page
45 doc.pagePushBack(page) // Add the page to the document page sequence
46
47 // ----------------------------------------------------------
48 // Embed a monochrome TIFF. Compress the image using lossy JBIG2 filter.
49 page = doc.pageCreate(PTPDFRect(x1: 0, y1: 0, x2: 612, y2: 794))
50 writer.writerBegin(with: page, placement: e_ptoverlay, page_coord_sys: true, compress: true, resources: nil) // begin writing to this page
51
52 // Note: encoder hints can be used to select between different compression methods.
53 // For example to instruct PDFNet to compress a monochrome image using JBIG2 compression.
54 let hint_set: PTObjSet = PTObjSet()
55 let enc: PTObj = hint_set.createArray() // Initialize encoder 'hint' parameter
56 enc.pushBackName("JBIG2")
57 enc.pushBackName("Lossy")
58
59 img = PTImage.create(withFile: doc.getSDFDoc(), filename: Bundle.main.path(forResource: "multipage", ofType: "tif"), encoder_hints: enc)
60 element = builder.createImage(withMatrix: img, mtx: PTMatrix2D(a: 612, b: 0, c: 0, d: 794, h: 0, v: 0))
61 writer.writePlacedElement(element)
62
63 writer.end() // Save the page
64 doc.pagePushBack(page) // Add the page to the document page sequence*/
65
66 // ----------------------------------------------------------
67 // Add a JPEG2000 (JP2) image to the output file
68 // Create a new page
69 page = doc.pageCreate(PTPDFRect(x1: 0, y1: 0, x2: 612, y2: 794))
70 writer.writerBegin(with: page, placement: e_ptoverlay, page_coord_sys: true, compress: true, resources: nil) // Begin writing to the page
71
72 // Embed the image.
73 img = PTImage.create(doc.getSDFDoc(), filename: Bundle.main.path(forResource: "palm", ofType: "jp2"))
74
75 // Position the image on the page.
76 element = builder.createImage(withMatrix: img, mtx: PTMatrix2D(a: Double(img.getWidth()), b: 0, c: 0, d: Double(img.getHeight()), h: 96, v: 80))
77 writer.writePlacedElement(element)
78
79 // Write 'JPEG2000 Sample' text string under the image.
80 writer.write(builder.createTextBegin(with: PTFont.create(doc.getSDFDoc(), type: e_pttimes_roman, embed: false), font_sz: 32))
81 element = builder.createTextRun("JPEG2000 Sample")
82 element.setTextMatrix(1, b: 0, c: 0, d: 1, h: 190, v: 30)
83 writer.write(element)
84 writer.write(builder.createTextEnd())
85
86 writer.end() // Finish writing to the page
87 doc.pagePushBack(page)
88
89 doc.save(toFile: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("AddImageTest.pdf").path, flags: e_ptlinearized.rawValue)
90 }
91 } catch let e as NSError {
92 print("Uncaught exception: \(e)")
93 ret = 1
94 }
95
96 print("Done.")
97 return ret
98 }
99}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales