More languages
Some test text!
More languages
Sample Swift code to use PDFTron 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 Swift PDF Library and PDF Editing & Manipulation Library.
Get Started Samples DownloadTo run this sample, get started with a free trial of PDFTron SDK.
//---------------------------------------------------------------------------------------
// 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
}
}
PDFTron SDK
COMPANY