> 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/android/get-started/samples/sdftest.md).

# SDF

Sample Java code to edit an existing PDF document at the object level by using the Apryse SDK Cos/SDF low-level API.

Sample Java code for editing an existing PDF document at the object level by using the Apryse SDK Cos/SDF low-level API. Learn more about our [Android SDK](/core/get-started/languages/java.md) and [PDF Editing & Manipulation Library](/core/page-manipulation/manipulation.md).

{% tabs %}
{% tab title="Java" %}
{% code lineNumbers="true" %}

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

package com.pdftron.android.pdfnetsdksamples.samples;

import com.pdftron.android.pdfnetsdksamples.OutputListener;
import com.pdftron.android.pdfnetsdksamples.PDFNetSample;
import com.pdftron.android.pdfnetsdksamples.R;
import com.pdftron.android.pdfnetsdksamples.util.Utils;
import com.pdftron.filters.FilterReader;
import com.pdftron.filters.MappedFile;
import com.pdftron.sdf.DictIterator;
import com.pdftron.sdf.Obj;
import com.pdftron.sdf.SDFDoc;

import java.util.ArrayList;

public class SDFTest extends PDFNetSample {

	private static OutputListener mOutputListener;

	private static ArrayList<String> mFileList = new ArrayList<>();

    public SDFTest() {
        setTitle(R.string.sample_sdf_title);
        setDescription(R.string.sample_sdf_description);
    }

	@Override
	public void run(OutputListener outputListener) {
		super.run(outputListener);
		mOutputListener = outputListener;
		mFileList.clear();
		printHeader(outputListener);

        try {
            mOutputListener.println("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.
            SDFDoc doc = new SDFDoc((Utils.getAssetTempFile(INPUT_PATH + "fish.pdf").getAbsolutePath()));
            doc.initSecurityHandler();

            mOutputListener.println("Modifying info dictionary, adding custom properties, embedding a stream...");
            Obj trailer = doc.getTrailer();            // Get the trailer

            // Now we will change PDF document information properties using SDF API

            // Get the Info dictionary.
            DictIterator itr = trailer.find("Info");
            Obj info;
            if (itr.hasNext()) {
                info = itr.value();
                // Modify 'Producer' entry.
                info.putString("Producer", "PDFTron PDFNet");

                // Read title entry (if it is present)
                itr = info.find("Author");
                if (itr.hasNext()) {
                    String oldstr = itr.value().getAsPDFText();

                    info.putText("Author", oldstr + "- Modified");
                } else {
                    info.putString("Author", "Me, myself, and I");
                }
            } else {
                // Info dict is missing.
                info = trailer.putDict("Info");
                info.putString("Producer", "PDFTron PDFNet");
                info.putString("Title", "My document");
            }

            // Create a custom inline dictionary within Info dictionary
            Obj custom_dict = info.putDict("My Direct Dict");
            custom_dict.putNumber("My Number", 100);     // Add some key/value pairs
            custom_dict.putArray("My Array");

            // Create a custom indirect array within Info dictionary
            Obj custom_array = doc.createIndirectArray();
            info.put("My Indirect Array", custom_array);    // Add some entries

            // Create indirect link to root
            custom_array.pushBack(trailer.get("Root").value());

            // Embed a custom stream (file mystream.txt).
            MappedFile embed_file = new MappedFile(Utils.getAssetTempFile(INPUT_PATH + "my_stream.txt").getAbsolutePath());
            FilterReader mystm = new FilterReader(embed_file);
            custom_array.pushBack(doc.createIndirectStream(mystm));

            // Save the changes.
            mOutputListener.println("Saving modified test file...");
            doc.save(Utils.createExternalFile("sdftest_out.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.NO_FLAGS, null, "%PDF-1.4");
            doc.close();

            mOutputListener.println("Test completed.");
        } catch (Exception e) {
            mOutputListener.printError(e.getStackTrace());
        }

		for (String file : mFileList) {
			addToFileList(file);
		}
		printFooter(outputListener);
	}

}
```

{% endcode %}
{% endtab %}

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

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

package com.pdftron.android.pdfnetsdksamples.samples

import com.pdftron.android.pdfnetsdksamples.OutputListener
import com.pdftron.android.pdfnetsdksamples.PDFNetSample
import com.pdftron.android.pdfnetsdksamples.R
import com.pdftron.android.pdfnetsdksamples.util.Utils
import com.pdftron.filters.FilterReader
import com.pdftron.filters.MappedFile
import com.pdftron.sdf.Obj
import com.pdftron.sdf.SDFDoc
import java.util.*

class SDFTest : PDFNetSample() {
    init {
        setTitle(R.string.sample_sdf_title)
        setDescription(R.string.sample_sdf_description)
    }

    override fun run(outputListener: OutputListener?) {
        super.run(outputListener)
        mOutputListener = outputListener
        mFileList.clear()
        printHeader(outputListener!!)

        try {
            mOutputListener!!.println("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.
            val doc = SDFDoc(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "fish.pdf")!!.absolutePath)
            doc.initSecurityHandler()

            mOutputListener!!.println("Modifying info dictionary, adding custom properties, embedding a stream...")
            val trailer = doc.trailer            // Get the trailer

            // Now we will change PDF document information properties using SDF API

            // Get the Info dictionary.
            var itr = trailer.find("Info")
            val info: Obj
            if (itr.hasNext()) {
                info = itr.value()
                // Modify 'Producer' entry.
                info.putString("Producer", "PDFTron PDFNet")

                // Read title entry (if it is present)
                itr = info.find("Author")
                if (itr.hasNext()) {
                    val oldstr = itr.value().asPDFText

                    info.putText("Author", "$oldstr- Modified")
                } else {
                    info.putString("Author", "Me, myself, and I")
                }
            } else {
                // Info dict is missing.
                info = trailer.putDict("Info")
                info.putString("Producer", "PDFTron PDFNet")
                info.putString("Title", "My document")
            }

            // Create a custom inline dictionary within Info dictionary
            val custom_dict = info.putDict("My Direct Dict")
            custom_dict.putNumber("My Number", 100.0)     // Add some key/value pairs
            custom_dict.putArray("My Array")

            // Create a custom indirect array within Info dictionary
            val custom_array = doc.createIndirectArray()
            info.put("My Indirect Array", custom_array)    // Add some entries

            // Create indirect link to root
            custom_array.pushBack(trailer.get("Root").value())

            // Embed a custom stream (file mystream.txt).
            val embed_file = MappedFile(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "my_stream.txt")!!.absolutePath)
            val mystm = FilterReader(embed_file)
            custom_array.pushBack(doc.createIndirectStream(mystm))

            // Save the changes.
            mOutputListener!!.println("Saving modified test file...")
            doc.save(Utils.createExternalFile("sdftest_out.pdf", mFileList).absolutePath, SDFDoc.SaveMode.NO_FLAGS, null, "%PDF-1.4")
            doc.close()

            mOutputListener!!.println("Test completed.")
        } catch (e: Exception) {
            mOutputListener!!.printError(e.stackTrace)
        }

        for (file in mFileList) {
            addToFileList(file)
        }
        printFooter(outputListener)
    }

    companion object {

        private var mOutputListener: OutputListener? = null

        private val mFileList = ArrayList<String>()
    }

}
```

{% 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/android/get-started/samples/sdftest.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.
