> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/ios/get-started/samples/addimagetest.md).

# AddImage

Sample Obj-C code to use Apryse SDK for inserting various raster image formats (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) into a PDF document.

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](/ios/guides.md) and [PDF Editing & Manipulation Library](/core/page-manipulation/manipulation.md).

{% tabs %}
{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

#import <OBJC/PDFNetOBJC.h>
#import <Foundation/Foundation.h>
//-----------------------------------------------------------------------------------
// This sample illustrates how to embed various raster image formats
// (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) in a PDF document.
//
// Note: On Windows platform this sample utilizes GDI+ and requires GDIPLUS.DLL to
// be present in the system path.
//-----------------------------------------------------------------------------------

int main (int argc, const char * argv[])
{
    @autoreleasepool {

        int ret = 0;
        [PTPDFNet Initialize: 0];

        PTPDFDoc* doc = [[PTPDFDoc alloc] init];
        
        PTElementBuilder* builder = [[PTElementBuilder alloc] init];    // Used to build new Element objects
        PTElementWriter* writer = [[PTElementWriter alloc] init];      // Used to write Elements to the page  
        
        PTPDFRect * rect = [[PTPDFRect alloc] init]; 
        [rect Set: 0 y1: 0 x2: 612 y2: 792];
        PTPage* page = [doc PageCreate: rect];  // Start a new page
        [writer WriterBeginWithPage: page placement: e_ptoverlay page_coord_sys: YES compress: YES resources: NULL];            // Begin writing to this page

        // ----------------------------------------------------------
        // Add JPEG image to the output file
        PTImage* img = [PTImage Create: [doc GetSDFDoc] filename: @"../../TestFiles/peppers.jpg"];
        PTElement* element = [builder CreateImageWithCornerAndScale: img x: 50 y: 500 hscale: [img GetImageWidth]/2 vscale: [img GetImageHeight]/2];
        [writer WritePlacedElement: element];

        // ----------------------------------------------------------
        // Add a PNG image to the output file
        img = [PTImage Create: [doc GetSDFDoc] filename: @"../../TestFiles/butterfly.png"];
        element = [builder CreateImageWithMatrix: img mtx: [[PTMatrix2D alloc] initWithA: 100 b: 0 c: 0 d: 100 h: 300 v: 500]];
        [writer WritePlacedElement: element];
        
        // ----------------------------------------------------------
        // Add a GIF image to the output file
        img = [PTImage Create: [doc GetSDFDoc] filename: @"../../TestFiles/pdfnet.gif"];
        element = [builder CreateImageWithMatrix: img mtx: [[PTMatrix2D alloc] initWithA: [img GetImageWidth] b: 0 c: 0 d: [img GetImageHeight] h: 50 v: 350]];
        [writer WritePlacedElement: element];

        // ----------------------------------------------------------
        // Add a TIFF image to the output file
        
        img = [PTImage Create: [doc GetSDFDoc] filename: @"../../TestFiles/grayscale.tif"];
        element = [builder CreateImageWithMatrix: img mtx: [[PTMatrix2D alloc] initWithA: [img GetImageWidth] b: 0 c: 0 d: [img GetImageHeight] h: 10 v: 50]];
        [writer WritePlacedElement: element];
        
        [writer End];               // Save the page
        [doc PagePushBack: page];   // Add the page to the document page sequence

        // ----------------------------------------------------------
        // Embed a monochrome TIFF. Compress the image using lossy JBIG2 filter.
        
        page = [doc PageCreate: [[PTPDFRect alloc] initWithX1: 0 y1: 0 x2: 612 y2: 794]];
        [writer WriterBeginWithPage: page placement: e_ptoverlay page_coord_sys: YES compress: YES resources: NULL];    // begin writing to this page
        
        // Note: encoder hints can be used to select between different compression methods. 
        // For example to instruct PDFNet to compress a monochrome image using JBIG2 compression.
        PTObjSet* hint_set = [[PTObjSet alloc] init];
        PTObj* enc=[hint_set CreateArray];  // Initialize encoder 'hint' parameter 
        [enc PushBackName: @"JBIG2"];
        [enc PushBackName: @"Lossy"];
        
        img = [PTImage CreateWithFile: [doc GetSDFDoc] filename: @"../../TestFiles/multipage.tif" encoder_hints: enc];
        element = [builder CreateImageWithMatrix: img mtx: [[PTMatrix2D alloc] initWithA: 612 b: 0 c: 0 d: 794 h: 0 v: 0]];
        [writer WritePlacedElement: element];
        
        [writer End];               // Save the page
        [doc PagePushBack: page];   // Add the page to the document page sequence*/

        // ----------------------------------------------------------
        // Add a JPEG2000 (JP2) image to the output file
        
        // Create a new page 
        page = [doc PageCreate: [[PTPDFRect alloc] initWithX1: 0 y1: 0 x2: 612 y2: 794]];
        [writer WriterBeginWithPage: page placement: e_ptoverlay page_coord_sys: YES compress: YES resources: NULL];    // Begin writing to the page
        
        // Embed the image.
        img = [PTImage Create: [doc GetSDFDoc] filename: @"../../TestFiles/palm.jp2"];
        
        // Position the image on the page.
        element = [builder CreateImageWithMatrix: img mtx: [[PTMatrix2D alloc] initWithA: [img GetImageWidth] b: 0 c: 0 d: [img GetImageHeight] h: 96 v: 80]];
        [writer WritePlacedElement: element];
        
        // Write 'JPEG2000 Sample' text string under the image.
        [writer WriteElement: [builder CreateTextBeginWithFont: [PTFont Create: [doc GetSDFDoc] type: e_pttimes_roman embed: NO] font_sz: 32]];
        element = [builder CreateTextRun: @"JPEG2000 Sample"];
        [element SetTextMatrix: 1 b: 0 c: 0 d: 1 h: 190 v: 30];
        [writer WriteElement: element];
        [writer WriteElement: [builder CreateTextEnd]];
        
        [writer End]; // Finish writing to the page
        [doc PagePushBack: page];

        [doc SaveToFile: @"../../TestFiles/Output/addimage.pdf" flags: e_ptlinearized];
        NSLog(@"Done. Result saved in addimage.pdf...");
        [PTPDFNet Terminate: 0];
        return ret;
    }
}
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

import PDFNet
import Foundation

func runAddImageTest() -> Int {
    return autoreleasepool {
        var ret: Int = 0
        
        
        do {
            try PTPDFNet.catchException {
                let doc: PTPDFDoc = PTPDFDoc()
                
                let builder: PTElementBuilder = PTElementBuilder()    // Used to build new Element objects
                let writer: PTElementWriter = PTElementWriter()      // Used to write Elements to the page
                
                let rect: PTPDFRect = PTPDFRect()
                rect.set(0, y1: 0, x2: 612, y2: 792)
                var page: PTPage = doc.pageCreate(rect)    // Start a new page
                writer.writerBegin(with: page, placement: e_ptoverlay, page_coord_sys: true, compress: true, resources: nil)    // Begin writing to this page
                
                // ----------------------------------------------------------
                // Add JPEG image to the output file
                var img: PTImage = PTImage.create(doc.getSDFDoc(), filename: Bundle.main.path(forResource: "peppers", ofType: "jpg"))
                var element: PTElement = builder.createImage(withCornerAndScale: img, x: 50, y: 500, hscale: Double(img.getWidth() / 2), vscale: Double(img.getHeight() / 2))
                writer.writePlacedElement(element)
                
                // ----------------------------------------------------------
                // Add a PNG image to the output file
                img = PTImage.create(doc.getSDFDoc(), filename: Bundle.main.path(forResource: "butterfly", ofType: "png"))
                element = builder.createImage(withMatrix: img, mtx: PTMatrix2D(a: 100, b: 0, c: 0, d: 100, h: 300, v: 500))
                writer.writePlacedElement(element)
                
                // ----------------------------------------------------------
                // Add a TIFF image to the output file
                img = PTImage.create(doc.getSDFDoc(), filename: Bundle.main.path(forResource: "grayscale", ofType: "tif"))
                element = builder.createImage(withMatrix: img, mtx: PTMatrix2D(a: Double(img.getWidth()), b: 0, c: 0, d: Double(img.getHeight()), h: 10, v: 50))
                writer.writePlacedElement(element)
                
                writer.end()            // Save the page
                doc.pagePushBack(page)  // Add the page to the document page sequence
                
                // ----------------------------------------------------------
                // Embed a monochrome TIFF. Compress the image using lossy JBIG2 filter.
                page = doc.pageCreate(PTPDFRect(x1: 0, y1: 0, x2: 612, y2: 794))
                writer.writerBegin(with: page, placement: e_ptoverlay, page_coord_sys: true, compress: true, resources: nil)    // begin writing to this page
                
                // Note: encoder hints can be used to select between different compression methods.
                // For example to instruct PDFNet to compress a monochrome image using JBIG2 compression.
                let hint_set: PTObjSet = PTObjSet()
                let enc: PTObj = hint_set.createArray()    // Initialize encoder 'hint' parameter
                enc.pushBackName("JBIG2")
                enc.pushBackName("Lossy")
                
                img = PTImage.create(withFile: doc.getSDFDoc(), filename: Bundle.main.path(forResource: "multipage", ofType: "tif"), encoder_hints: enc)
                element = builder.createImage(withMatrix: img, mtx: PTMatrix2D(a: 612, b: 0, c: 0, d: 794, h: 0, v: 0))
                writer.writePlacedElement(element)
                
                writer.end()            // Save the page
                doc.pagePushBack(page)  // Add the page to the document page sequence*/
                
                // ----------------------------------------------------------
                // Add a JPEG2000 (JP2) image to the output file
                // Create a new page
                page = doc.pageCreate(PTPDFRect(x1: 0, y1: 0, x2: 612, y2: 794))
                writer.writerBegin(with: page, placement: e_ptoverlay, page_coord_sys: true, compress: true, resources: nil)    // Begin writing to the page
                
                // Embed the image.
                img = PTImage.create(doc.getSDFDoc(), filename: Bundle.main.path(forResource: "palm", ofType: "jp2"))
                
                // Position the image on the page.
                element = builder.createImage(withMatrix: img, mtx: PTMatrix2D(a: Double(img.getWidth()), b: 0, c: 0, d: Double(img.getHeight()), h: 96, v: 80))
                writer.writePlacedElement(element)
                
                // Write 'JPEG2000 Sample' text string under the image.
                writer.write(builder.createTextBegin(with: PTFont.create(doc.getSDFDoc(), type: e_pttimes_roman, embed: false), font_sz: 32))
                element = builder.createTextRun("JPEG2000 Sample")
                element.setTextMatrix(1, b: 0, c: 0, d: 1, h: 190, v: 30)
                writer.write(element)
                writer.write(builder.createTextEnd())
                
                writer.end()    // Finish writing to the page
                doc.pagePushBack(page)
                
                doc.save(toFile: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("AddImageTest.pdf").path, flags: e_ptlinearized.rawValue)
            }
        } catch let e as NSError {
            print("Uncaught exception: \(e)")
            ret = 1
        }
        
        print("Done.")
        return ret
    }
}
```

{% endcode %}
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/ios/get-started/samples/addimagetest.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
