> 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/converttest.md).

# Convert

Sample Java code for direct, high-quality conversion between PDF, XPS, EMF, SVG, TIFF, PNG, JPEG, and other image formats ('pdftron.PDF.Convert' namespace). The sample also shows how to convert any pr

Sample Java code to use Apryse SDK for direct, high-quality conversion between PDF, XPS, SVG, TIFF, PNG, JPEG, and other image formats ('pdftron.PDF.Convert' namespace). The sample also shows how to convert MS Office files using our built in conversion. Learn more about our [Android SDK](/core/get-started/languages/java.md) and [PDF Conversion Library](/core/conversion/conversion.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.common.PDFNetException;
import com.pdftron.pdf.Convert;
import com.pdftron.pdf.PDFDoc;
import com.pdftron.sdf.Obj;
import com.pdftron.sdf.ObjSet;
import com.pdftron.sdf.SDFDoc;

import java.util.ArrayList;

public class ConvertTest extends PDFNetSample {

    private static OutputListener mOutputListener;

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

    public ConvertTest() {
        setTitle(R.string.sample_convert_title);
        setDescription(R.string.sample_convert_description);
    }

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

        String outputFile;

        mOutputListener.println("-------------------------------------------------");

        try {
            mOutputListener.println("Converting Text to PDF with options");
            ObjSet objset = new ObjSet();
            Obj options = objset.createDict();
            options.putNumber("FontSize", 15);
            options.putBool("UseSourceCodeFormatting", true);
            options.putNumber("PageWidth", 12);
            options.putNumber("PageHeight", 6);
            PDFDoc doc = new PDFDoc();
            outputFile = Utils.createExternalFile("simple-text.pdf", mFileList).getAbsolutePath();
            Convert.fromText(doc, Utils.getAssetTempFile(INPUT_PATH + "simple-text.txt").getAbsolutePath(), options);
            doc.save(outputFile, SDFDoc.SaveMode.LINEARIZED, null);
            mOutputListener.println("Result saved in " + outputFile);
            doc.close();
        } catch (PDFNetException e) {
            mOutputListener.println("Unable to convert Plain Text document to PDF, error:");
            mOutputListener.printError(e.getStackTrace());
        }

        // Convert the XPS document to PDF
        try {
            mOutputListener.println("Converting XPS document to PDF");
            PDFDoc doc = new PDFDoc();
            Convert.fromXps(doc, Utils.getAssetTempFile(INPUT_PATH + "simple-xps.xps").getAbsolutePath());
            outputFile = Utils.createExternalFile("xps2pdf_Java.pdf", mFileList).getAbsolutePath();
            doc.save(outputFile, SDFDoc.SaveMode.LINEARIZED, null);
            mOutputListener.println("Result saved in " + outputFile);
            doc.close();
        } catch (PDFNetException e) {
            mOutputListener.println("Unable to convert XPS document to PDF, error:");
            mOutputListener.printError(e.getStackTrace());
        }

        // Convert the PDF document to SVG
        try {
            mOutputListener.println("Converting XPS document to SVG");
            PDFDoc doc = new PDFDoc(Utils.getAssetTempFile(INPUT_PATH + "tiger.pdf").getAbsolutePath());
            Convert.fromXps(doc, Utils.getAssetTempFile(INPUT_PATH + "simple-xps.xps").getAbsolutePath());
            mOutputListener.println("Converting PDF document to SVG");
            outputFile = Utils.createExternalFile("pdf2svg_Java.svg", mFileList).getAbsolutePath();
            Convert.toSvg(doc, outputFile);
            mOutputListener.println("Result saved in " + outputFile);
            doc.close();
        } catch (PDFNetException e) {
            mOutputListener.println("Unable to convert PDF document to SVG, error:");
            mOutputListener.printError(e.getStackTrace());
        }

        // Convert the PDF document to XPS
        try {
            mOutputListener.println("Converting PDF document to XPS");
            PDFDoc doc = new PDFDoc(Utils.getAssetTempFile(INPUT_PATH + "newsletter.pdf").getAbsolutePath());
            outputFile = Utils.createExternalFile("pdf2xps_Java.xps", mFileList).getAbsolutePath();
            Convert.toXps(doc, outputFile);
            mOutputListener.println("Result saved in " + outputFile);
            doc.close();
        } catch (PDFNetException e) {
            mOutputListener.println("Unable to convert PDF document to SVG, error:");
            mOutputListener.printError(e.getStackTrace());
        }

        // Convert an image to PDF using internal converter
        try {
            mOutputListener.println("Converting PNG image to PDF");
            PDFDoc doc = new PDFDoc();
            Convert.toPdf(doc, Utils.getAssetTempFile(INPUT_PATH + "butterfly.png").getAbsolutePath());
            outputFile = Utils.createExternalFile("png2pdf_Java.pdf", mFileList).getAbsolutePath();
            doc.save(outputFile, SDFDoc.SaveMode.LINEARIZED, null);
            mOutputListener.println("Result saved in " + outputFile);
            doc.close();
        } catch (PDFNetException e) {
            mOutputListener.println("Unable to convert PNG image to XPS, error:");
            mOutputListener.printError(e.getStackTrace());
        }

        // Convert an image to XPS using internal converter
        try {
            mOutputListener.println("Converting PNG image to XPS");
            outputFile = Utils.createExternalFile("buttefly_Java.xps", mFileList).getAbsolutePath();
            Convert.toXps(Utils.getAssetTempFile(INPUT_PATH + "butterfly.png").getAbsolutePath(), outputFile);
            mOutputListener.println("Result saved in " + outputFile);
        } catch (PDFNetException e) {
            mOutputListener.println("Unable to convert PNG image to XPS, error:");
            mOutputListener.printError(e.getStackTrace());
        }

        // Convert a PDF document directly to XPS
        try {
            mOutputListener.println("Converting PDF to XPS");
            outputFile = Utils.createExternalFile("newsletter.xps", mFileList).getAbsolutePath();
            Convert.toXps(Utils.getAssetTempFile(INPUT_PATH + "newsletter.pdf").getAbsolutePath(), outputFile);
            mOutputListener.println("Result saved in " + outputFile);
        } catch (PDFNetException e) {
            mOutputListener.println("Unable to convert PDF document to XPS, error:");
            mOutputListener.printError(e.getStackTrace());
        }

        // Convert a PDF document to HTML
        try {
            mOutputListener.println("Converting PDF to HTML");
            outputFile = Utils.createExternalFile("newsletter", mFileList).getAbsolutePath();
            Convert.toHtml(Utils.getAssetTempFile(INPUT_PATH + "newsletter.pdf").getAbsolutePath(), outputFile);
            mOutputListener.println("Result saved in " + outputFile);
        } catch (PDFNetException e) {
            mOutputListener.println("Unable to convert PDF document to HTML, error:");
            mOutputListener.printError(e.getStackTrace());
        }

        // Convert a PDF document to EPUB
        try {
            mOutputListener.println("Converting PDF to EPUB");
            outputFile = Utils.createExternalFile("newsletter.epub", mFileList).getAbsolutePath();
            Convert.toEpub(Utils.getAssetTempFile(INPUT_PATH + "newsletter.pdf").getAbsolutePath(), outputFile);
            mOutputListener.println("Result saved in " + outputFile);
        } catch (PDFNetException e) {
            mOutputListener.println("Unable to convert PDF document to EPUB, error:");
            mOutputListener.printError(e.getStackTrace());
        }

        // Convert a PDF document to multipage TIFF
        try {
            mOutputListener.println("Converting PDF to multipage TIFF");
            outputFile = Utils.createExternalFile("newsletter.tiff", mFileList).getAbsolutePath();
            Convert.TiffOutputOptions tiff_options = new Convert.TiffOutputOptions();
            tiff_options.setDPI(200);
            tiff_options.setMono(true);
            tiff_options.setDither(true);
            Convert.toTiff(Utils.getAssetTempFile(INPUT_PATH + "newsletter.pdf").getAbsolutePath(), outputFile, tiff_options);
            mOutputListener.println("Result saved in " + outputFile);
        } catch (PDFNetException e) {
            mOutputListener.println("Unable to convert PDF document to TIFF, error:");
            mOutputListener.printError(e.getStackTrace());
        }

        mOutputListener.println("Done.");

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

{% endcode %}
{% endtab %}

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

```kotlin
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.common.PDFNetException
import com.pdftron.pdf.Convert
import com.pdftron.pdf.PDFDoc
import com.pdftron.sdf.ObjSet
import com.pdftron.sdf.SDFDoc
import java.util.ArrayList

class ConvertTest : PDFNetSample() {
    init {
        setTitle(R.string.sample_convert_title)
        setDescription(R.string.sample_convert_description)
    }

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

        var outputFile: String

        mOutputListener!!.println("-------------------------------------------------")

        try {
            mOutputListener!!.println("Converting Text to PDF with options")
            val objset = ObjSet()
            val options = objset.createDict()
            options.putNumber("FontSize", 15.0)
            options.putBool("UseSourceCodeFormatting", true)
            options.putNumber("PageWidth", 12.0)
            options.putNumber("PageHeight", 6.0)
            val doc = PDFDoc()
            outputFile = Utils.createExternalFile("simple-text.pdf", mFileList).absolutePath
            Convert.fromText(doc, Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "simple-text.txt")!!.absolutePath, options)
            doc.save(outputFile, SDFDoc.SaveMode.LINEARIZED, null)
            mOutputListener!!.println("Result saved in $outputFile")
            doc.close()
        } catch (e: PDFNetException) {
            mOutputListener!!.println("Unable to convert Plain Text document to PDF, error:")
            mOutputListener!!.printError(e.stackTrace)
        }

        // Convert the XPS document to PDF
        try {
            mOutputListener!!.println("Converting XPS document to PDF")
            val doc = PDFDoc()
            Convert.fromXps(doc, Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "simple-xps.xps")!!.absolutePath)
            outputFile = Utils.createExternalFile("xps2pdf_Java.pdf", mFileList).absolutePath
            doc.save(outputFile, SDFDoc.SaveMode.LINEARIZED, null)
            mOutputListener!!.println("Result saved in $outputFile")
            doc.close()
        } catch (e: PDFNetException) {
            mOutputListener!!.println("Unable to convert XPS document to PDF, error:")
            mOutputListener!!.printError(e.stackTrace)
        }

        // Convert the PDF document to SVG
        try {
            mOutputListener!!.println("Converting XPS document to SVG")
            val doc = PDFDoc(Utils.getAssetTempFile(INPUT_PATH + "tiger.pdf")!!.absolutePath)
            Convert.fromXps(doc, Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "simple-xps.xps")!!.absolutePath)
            mOutputListener!!.println("Converting PDF document to SVG")
            outputFile = Utils.createExternalFile("pdf2svg_Java.svg", mFileList).absolutePath
            Convert.toSvg(doc, outputFile)
            mOutputListener!!.println("Result saved in $outputFile")
            doc.close()
        } catch (e: PDFNetException) {
            mOutputListener!!.println("Unable to convert PDF document to SVG, error:")
            mOutputListener!!.printError(e.stackTrace)
        }

        // Convert the PDF document to XPS
        try {
            mOutputListener!!.println("Converting PDF document to XPS")
            val doc = PDFDoc(Utils.getAssetTempFile(INPUT_PATH + "newsletter.pdf")!!.absolutePath)
            outputFile = Utils.createExternalFile("pdf2xps_Java.xps", mFileList).absolutePath
            Convert.toXps(doc, outputFile)
            mOutputListener!!.println("Result saved in $outputFile")
            doc.close()
        } catch (e: PDFNetException) {
            mOutputListener!!.println("Unable to convert PDF document to SVG, error:")
            mOutputListener!!.printError(e.stackTrace)
        }

        // Convert an image to PDF using internal converter
        try {
            mOutputListener!!.println("Converting PNG image to PDF")
            val doc = PDFDoc()
            Convert.toPdf(doc, Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "butterfly.png")!!.absolutePath)
            outputFile = Utils.createExternalFile("png2pdf_Java.pdf", mFileList).absolutePath
            doc.save(outputFile, SDFDoc.SaveMode.LINEARIZED, null)
            mOutputListener!!.println("Result saved in $outputFile")
            doc.close()
        } catch (e: PDFNetException) {
            mOutputListener!!.println("Unable to convert PNG image to XPS, error:")
            mOutputListener!!.printError(e.stackTrace)
        }

        // Convert an image to XPS using internal converter
        try {
            mOutputListener!!.println("Converting PNG image to XPS")
            outputFile = Utils.createExternalFile("buttefly_Java.xps", mFileList).absolutePath
            Convert.toXps(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "butterfly.png")!!.absolutePath, outputFile)
            mOutputListener!!.println("Result saved in $outputFile")
        } catch (e: PDFNetException) {
            mOutputListener!!.println("Unable to convert PNG image to XPS, error:")
            mOutputListener!!.printError(e.stackTrace)
        }

        // Convert a PDF document directly to XPS
        try {
            mOutputListener!!.println("Converting PDF to XPS")
            outputFile = Utils.createExternalFile("newsletter.xps", mFileList).absolutePath
            Convert.toXps(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "newsletter.pdf")!!.absolutePath, outputFile)
            mOutputListener!!.println("Result saved in $outputFile")
        } catch (e: PDFNetException) {
            mOutputListener!!.println("Unable to convert PDF document to XPS, error:")
            mOutputListener!!.printError(e.stackTrace)
        }

        // Convert a PDF document to HTML
        try {
            mOutputListener!!.println("Converting PDF to HTML")
            outputFile = Utils.createExternalFile("newsletter", mFileList).absolutePath
            Convert.toHtml(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "newsletter.pdf")!!.absolutePath, outputFile)
            mOutputListener!!.println("Result saved in $outputFile")
        } catch (e: PDFNetException) {
            mOutputListener!!.println("Unable to convert PDF document to HTML, error:")
            mOutputListener!!.printError(e.stackTrace)
        }

        // Convert a PDF document to EPUB
        try {
            mOutputListener!!.println("Converting PDF to EPUB")
            outputFile = Utils.createExternalFile("newsletter.epub", mFileList).absolutePath
            Convert.toEpub(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "newsletter.pdf")!!.absolutePath, outputFile)
            mOutputListener!!.println("Result saved in $outputFile")
        } catch (e: PDFNetException) {
            mOutputListener!!.println("Unable to convert PDF document to EPUB, error:")
            mOutputListener!!.printError(e.stackTrace)
        }

        // Convert a PDF document to multipage TIFF
        try {
            mOutputListener!!.println("Converting PDF to multipage TIFF")
            outputFile = Utils.createExternalFile("newsletter.tiff", mFileList).absolutePath
            val tiff_options = Convert.TiffOutputOptions()
            tiff_options.setDPI(200.0)
            tiff_options.setMono(true)
            tiff_options.setDither(true)
            Convert.toTiff(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "newsletter.pdf")!!.absolutePath, outputFile, tiff_options)
            mOutputListener!!.println("Result saved in $outputFile")
        } catch (e: PDFNetException) {
            mOutputListener!!.println("Unable to convert PDF document to TIFF, error:")
            mOutputListener!!.printError(e.stackTrace)
        }

        mOutputListener!!.println("Done.")

        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/converttest.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.
