Some test text!

Hamburger Icon

Swift cos/SDF low-level API to edit PDF files

More languages

More languages
Java (Android)
C# (.NET Core)
JS (Node.js)
C# (UWP)
C# (Xamarin)

Sample Swift code for editing an existing PDF document at the object level by using the PDFTron SDK Cos/SDF low-level API. Learn more about our Swift PDF Library and PDF Editing & Manipulation Library.

Get Started Samples Download

To run this sample, get started with a free trial of Apryse SDK.

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

import PDFNet
import Foundation

// This sample illustrates how to use basic SDF API (also known as Cos) to edit an
// existing document.

func runSDFTest() -> Int {
    return autoreleasepool {
        var ret: Int = 0

        do {
            try PTPDFNet.catchException {
                print("Opening the test file...")
                // Here we create a SDF/Cos document directly from PDF file. In case you have
                // PDFDoc you can always access SDF/Cos document using PDFDoc.GetSDFDoc() method.
                let doc: PTSDFDoc = PTSDFDoc(filepath: Bundle.main.path(forResource: "fish", ofType: "pdf"))
                print("Modifying info dictionary, adding custom properties, embedding a stream...")
                let trailer: PTObj = doc.getTrailer()  // Get the trailer

                // Now we will change PDF document information properties using SDF API
                // Get the Info dictionary.
                var itr: PTDictIterator = trailer.find("Info")
                var info: PTObj = PTObj()
                if itr.hasNext() {
                    info = itr.value()
                    // Modify 'Producer' entry.
                    info.put("Producer", value: "PDFTron PDFNet")
                    // Read title entry (if it is present)
                    itr = info.find("Author")
                    if itr.hasNext() {
                        let oldstr: String = itr.value().getAsPDFText()
                        info.putText("Author", value: oldstr + "- Modified")
                    else {
                        info.put("Author", value: "Me, myself, and I")
                else {
                    // Info dict is missing.
                    info = (trailer.putDict("Info"))!
                    info.put("Producer", value: "PDFTron PDFNet")
                    info.put("Title", value: "My document")

                // Create a custom inline dictionary within Info dictionary
                let custom_dict: PTObj = info.putDict("My Direct Dict")
                custom_dict.putNumber("My Number", value: 100)  // Add some key/value pairs
                custom_dict.putArray("My Array")
                // Create a custom indirect array within Info dictionary
                let custom_array: PTObj = doc.createIndirectArray()
                info.put("My Indirect Array", obj: custom_array)    // Add some entries
                // Create indirect link to root
                // Embed a custom stream (file mystream.txt).
                let embed_file = PTMappedFile(filename: Bundle.main.path(forResource: "my_stream", ofType: "txt"))
                let mystm = PTFilterReader(filter: embed_file)
                // Save the changes.
                print("Saving modified test file...")
       URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("sdftest_out.pdf").path, flags: 0, header: "%PDF-1.4")
                print("Test completed.")
        } catch let e as NSError {
            ret = 1
        return ret