Some test text!

< iOS samples

SDFTest - Swift

The sample illustrates how to use basic Cos/SDF API to edit an existing document.

//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2017 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"))
                doc.initSecurityHandler()
                
                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
                custom_array.pushBack(trailer.get("Root").value())
                
                // 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)
                custom_array.pushBack(doc.createIndirectStream(mystm))
                
                // Save the changes.
                print("Saving modified test file...")
                doc.save(toFile: 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 {
            print("\(e)")
            ret = 1
        }
        return ret
    }
}