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

# OfficeToPDF

Sample Java code for using Apryse SDK to convert Office documents to PDF (including Word, Excel, PowerPoint and Publisher) without needing any external dependencies or MS Office licenses.

Sample Java code for using Apryse SDK to convert Office documents to PDF (including Word, Excel, PowerPoint and Publisher) without needing any external dependencies or MS Office licenses. Office to PDF conversion can be performed on a Linux or Windows server to automate Office-centric workflows, or entirely in the user's client (web browser, mobile device). The conversion functionality can be combined with our Viewer to display or annotate Office files (docx, xlsx, pptx) on all major platforms, including Web, Android, iOS, Xamarin, UWP, and Windows. Learn more about our [Android SDK](/core/get-started/languages/java.md) and [Office Document Conversion Library](https://apryse.com/products/core-sdk/office).

{% 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 android.content.Context;

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.DocumentConversion;
import com.pdftron.pdf.OfficeToPDFOptions;
import com.pdftron.pdf.PDFDoc;
import com.pdftron.pdf.PDFNet;
import com.pdftron.sdf.SDFDoc;

import java.io.File;
import java.util.ArrayList;

/**
 * The following sample illustrates how to use the PDF.Convert utility class to convert
 * .docx files to PDF
 * <p>
 * This conversion is performed entirely within the PDFNet and has *no* external or
 * system dependencies dependencies -- Conversion results will be the sam whether
 * on Windows, Linux or Android.
 * <p>
 * Please contact us if you have any questions.
 */
public class OfficeToPDFTest extends PDFNetSample {

	private static OutputListener mOutputListener;

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

	private static String sLayoutSmartPluginPath;

    public OfficeToPDFTest(Context context) {
        try {
            String layoutPluginPath = Utils.copyResourceToTempFolder(context, R.raw.pdftron_layout_resources, false, "pdftron_layout_resources.plugin");
            PDFNet.addResourceSearchPath(layoutPluginPath);
            sLayoutSmartPluginPath = Utils.copyResourceToTempFolder(context, R.raw.pdftron_smart_substitution, false, "pdftron_smart_substitution.plugin");
            PDFNet.addResourceSearchPath(sLayoutSmartPluginPath);
        } catch (Exception e) {
            mOutputListener.printError(e.getStackTrace());
        }

        setTitle(R.string.sample_officetopdf_title);
        setDescription(R.string.sample_officetopdf_description);
    }

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

        // first the one-line conversion interface
        simpleDocxConvert("Fishermen.docx", "Fishermen.pdf");

        // then the more flexible line-by-line interface
        flexibleDocxConvert("the_rime_of_the_ancient_mariner.docx", "the_rime_of_the_ancient_mariner.pdf");
       
        // conversion of RTL content
        flexibleDocxConvert("factsheet_Arabic.docx", "factsheet_Arabic.pdf");

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


    public static void simpleDocxConvert(String inputFilename, String outputFilename) {
        try (PDFDoc doc = new PDFDoc()) {

            // perform the conversion with no optional parameters
            PDFDoc pdfdoc = new PDFDoc();
            Convert.officeToPdf(pdfdoc, Utils.getAssetTempFile(INPUT_PATH + inputFilename).getAbsolutePath(), null);

            // save the result
            pdfdoc.save(Utils.createExternalFile(outputFilename, mFileList).getAbsolutePath(), SDFDoc.SaveMode.INCREMENTAL, null);

            // And we're done!
            mOutputListener.println("Done conversion " + Utils.createExternalFile(outputFilename, mFileList).getAbsolutePath());
        } catch (PDFNetException e) {
            mOutputListener.println("Unable to convert MS Office document, error:");
            mOutputListener.printError(e.getStackTrace());
            mOutputListener.printError(e.getStackTrace());
        }
    }

    public static void flexibleDocxConvert(String inputFilename, String outputFilename) {
        try {
            OfficeToPDFOptions options = new OfficeToPDFOptions();
            options.setSmartSubstitutionPluginPath(sLayoutSmartPluginPath);

            // create a conversion object -- this sets things up but does not yet
            // perform any conversion logic.
            // in a multithreaded environment, this object can be used to monitor
            // the conversion progress and potentially cancel it as well
            DocumentConversion conversion = Convert.streamingPdfConversion(
                    Utils.getAssetTempFile(INPUT_PATH + inputFilename).getAbsolutePath(), options);

            mOutputListener.println(inputFilename + ": " + Math.round(conversion.getProgress() * 100.0)
                    + "% " + conversion.getProgressLabel());

            // actually perform the conversion
            while (conversion.getConversionStatus() == DocumentConversion.e_incomplete) {
                conversion.convertNextPage();
                mOutputListener.println(inputFilename + ": " + Math.round(conversion.getProgress() * 100.0)
                        + "% " + conversion.getProgressLabel());
            }

            if (conversion.tryConvert() == DocumentConversion.e_success) {
                int num_warnings = conversion.getNumWarnings();

                // print information about the conversion
                for (int i = 0; i < num_warnings; ++i) {
                    mOutputListener.println("Warning: " + conversion.getWarningString(i));
                }

                // save the result
                try (PDFDoc doc = conversion.getDoc()) {
                    doc.save(Utils.createExternalFile(outputFilename, mFileList).getAbsolutePath(), SDFDoc.SaveMode.INCREMENTAL, null);
                }

                // done
                mOutputListener.println("Done conversion " + Utils.createExternalFile(outputFilename, mFileList).getAbsolutePath());
            } else {
                mOutputListener.println("Encountered an error during conversion: " + conversion.getErrorString());
            }
        } catch (PDFNetException e) {
            mOutputListener.println("Unable to convert MS Office document, error:");
            mOutputListener.printError(e.getStackTrace());
            mOutputListener.printError(e.getStackTrace());
        }
    }

}
```

{% 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.common.PDFNetException
import com.pdftron.pdf.PDFDoc
import com.pdftron.pdf.PageIterator
import com.pdftron.pdf.struct.ContentItem
import com.pdftron.pdf.struct.SElement
import com.pdftron.pdf.struct.STree
import com.pdftron.sdf.SDFDoc

class LogicalStructureTest : PDFNetSample() {
    override fun run(outputListener: OutputListener?) {
        super.run(outputListener)
        mOutputListener = outputListener
        mFileList.clear()
        printHeader(outputListener!!)
        try  // Extract logical structure from a PDF document
        {
            PDFDoc(Utils.getAssetTempFile(INPUT_PATH.toString() + "tagged.pdf")!!.getAbsolutePath()).use { doc ->
                doc.initSecurityHandler()
                mOutputListener!!.println("____________________________________________________________")
                mOutputListener!!.println("Sample 1 - Traverse logical structure tree...")
                run({
                    val tree: STree = doc.getStructTree()
                    if (tree.isValid()) {
                        mOutputListener!!.println("Document has a StructTree root.")
                        for (i in 0 until tree.getNumKids()) {
                            // Recursively get structure  info for all all child elements.
                            ProcessStructElement(tree.getKid(i), 0)
                        }
                    } else {
                        mOutputListener!!.println("This document does not contain any logical structure.")
                    }
                })
                mOutputListener!!.println("\nDone 1.")
                mOutputListener!!.println("____________________________________________________________")
                mOutputListener!!.println("Sample 2 - Get parent logical structure elements from")
                mOutputListener!!.println("layout elements.")
                run({
                    val reader: com.pdftron.pdf.ElementReader = com.pdftron.pdf.ElementReader()
                    val itr: PageIterator = doc.getPageIterator()
                    while (itr.hasNext()) {
                        reader.begin(itr.next())
                        ProcessElements(reader)
                        reader.end()
                    }
                })
                mOutputListener!!.println("\nDone 2.")
                mOutputListener!!.println("____________________________________________________________")
                mOutputListener!!.println("Sample 3 - 'XML style' extraction of PDF logical structure and page content.")
                run({

                    //A map which maps page numbers(as Integers)
                    //to page Maps(which map from struct mcid(as Integers) to
                    //text Strings)
                    val mcid_doc_map: MutableMap<Int, Map<Int, String>> = java.util.TreeMap<Int, Map<Int, String>>()
                    val reader: com.pdftron.pdf.ElementReader = com.pdftron.pdf.ElementReader()
                    val itr: PageIterator = doc.getPageIterator()
                    while (itr.hasNext()) {
                        val current: com.pdftron.pdf.Page? = itr.next()
                        reader.begin(current)
                        val page_mcid_map: MutableMap<Int, String> = java.util.TreeMap<Int, String>()
                        mcid_doc_map.put(current!!.getIndex(), page_mcid_map)
                        ProcessElements2(reader, page_mcid_map)
                        reader.end()
                    }
                    val tree: STree = doc.getStructTree()
                    if (tree.isValid()) {
                        for (i in 0 until tree.getNumKids()) {
                            ProcessStructElement2(tree.getKid(i), mcid_doc_map, 0)
                        }
                    }
                })
                mOutputListener!!.println("\nDone 3.")
                doc.save(Utils.createExternalFile("LogicalStructure.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.LINEARIZED, null)
            }
        } catch (e: java.lang.Exception) {
            mOutputListener!!.printError(e.getStackTrace())
        }
        for (file in mFileList) {
            addToFileList(file)
        }
        printFooter(outputListener)
    }

    companion object {
        private var mOutputListener: OutputListener? = null
        private val mFileList: java.util.ArrayList<String> = java.util.ArrayList<String>()
        fun PrintIndent(indent: Int) {
            mOutputListener!!.println()
            for (i in 0 until indent) mOutputListener!!.print("  ")
        }

        // Used in code snippet 1.
        @Throws(PDFNetException::class)
        fun ProcessStructElement(element: SElement, indent: Int) {
            var indent = indent
            if (!element.isValid()) {
                return
            }

            // Print out the type and title info, if any.
            PrintIndent(indent++)
            mOutputListener!!.print("Type: " + element.getType())
            if (element.hasTitle()) {
                mOutputListener!!.print(". Title: " + element.getTitle())
            }
            val num: Int = element.getNumKids()
            for (i in 0 until num) {
                // Check is the kid is a leaf node (i.e. it is a ContentItem).
                if (element.isContentItem(i)) {
                    val cont: ContentItem = element.getAsContentItem(i)
                    val type: Int = cont.getType()
                    val page: com.pdftron.pdf.Page = cont.getPage()
                    PrintIndent(indent)
                    mOutputListener!!.print("Content Item. Part of page #" + page.getIndex())
                    PrintIndent(indent)
                    when (type) {
                        ContentItem.e_MCID, ContentItem.e_MCR -> mOutputListener!!.print("MCID: " + cont.getMCID())
                        ContentItem.e_OBJR -> {
                            mOutputListener!!.print("OBJR ")
                            val ref_obj: com.pdftron.sdf.Obj = cont.getRefObj()
                            if (ref_obj != null) mOutputListener!!.print("- Referenced Object#: " + ref_obj.getObjNum())
                        }
                        else -> {
                        }
                    }
                } else {  // the kid is another StructElement node.
                    ProcessStructElement(element.getAsStructElem(i), indent)
                }
            }
        }

        // Used in code snippet 2.
        @Throws(PDFNetException::class)
        fun ProcessElements(reader: com.pdftron.pdf.ElementReader) {
            var element: com.pdftron.pdf.Element?
            // Read page contents
            while (true) {
                element = reader.next()
                if (element == null) {
                    break
                }
                // In this sample we process only paths & text, but the code can be
                // extended to handle any element type.
                val type: Int = element.getType()
                if (type == com.pdftron.pdf.Element.e_path || type == com.pdftron.pdf.Element.e_text || type == com.pdftron.pdf.Element.e_path) {
                    when (type) {
                        com.pdftron.pdf.Element.e_path -> mOutputListener!!.print("\nPATH: ")
                        com.pdftron.pdf.Element.e_text -> mOutputListener!!.print("""
    
    TEXT: ${element.getTextString()}
    
    """.trimIndent())
                        com.pdftron.pdf.Element.e_form -> mOutputListener!!.print("\nFORM XObject: ")
                    }

                    // Check if the element is associated with any structural element.
                    // Content items are leaf nodes of the structure tree.
                    val struct_parent: SElement = element.getParentStructElement()
                    if (struct_parent.isValid()) {
                        // Print out the parent structural element's type, title, and object number.
                        mOutputListener!!.print(" Type: " + struct_parent.getType()
                                + ", MCID: " + element.getStructMCID())
                        if (struct_parent.hasTitle()) {
                            mOutputListener!!.print(". Title: " + struct_parent.getTitle())
                        }
                        mOutputListener!!.print(", Obj#: " + struct_parent.getSDFObj().getObjNum())
                    }
                }
            }
        }

        // Used in code snippet 3.
        //typedef map<int, string> MCIDPageMap;
        //typedef map<int, MCIDPageMap> MCIDDocMap;
        // Used in code snippet 3.
        @Throws(PDFNetException::class)
        fun ProcessElements2(reader: com.pdftron.pdf.ElementReader, mcid_page_map: MutableMap<Int, String>) {
            var element: com.pdftron.pdf.Element?
            // Read page contents
            while (true) {
                element = reader.next()
                if (element == null) {
                    break
                }
                // In this sample we process only text, but the code can be extended
                // to handle paths, images, or any other Element type.
                val mcid: Int = element.getStructMCID()
                if (mcid >= 0 && element.getType() == com.pdftron.pdf.Element.e_text) {
                    val `val`: String = element.getTextString()
                    if (mcid_page_map.containsKey(mcid)) mcid_page_map.put(mcid, mcid_page_map[mcid] + `val`) else mcid_page_map.put(mcid, `val`)
                }
            }
        }

        // Used in code snippet 3.
        @Throws(PDFNetException::class)
        fun ProcessStructElement2(element: SElement, mcid_doc_map: Map<Int, Map<Int, String>>, indent: Int) {
            if (!element.isValid()) {
                return
            }

            // Print out the type and title info, if any.
            PrintIndent(indent)
            mOutputListener!!.print("<" + element.getType())
            if (element.hasTitle()) {
                mOutputListener!!.print(" title=\"" + element.getTitle() + "\"")
            }
            mOutputListener!!.print(">")
            val num: Int = element.getNumKids()
            for (i in 0 until num) {
                if (element.isContentItem(i)) {
                    val cont: ContentItem = element.getAsContentItem(i)
                    if (cont.getType() == ContentItem.e_MCID) {
                        val page_num: Int = cont.getPage().getIndex()
                        if (mcid_doc_map.containsKey(page_num)) {
                            val mcid_page_map = mcid_doc_map[page_num]!!
                            val mcid_key: Int = cont.getMCID()
                            if (mcid_page_map.containsKey(mcid_key)) {
                                mOutputListener!!.print(mcid_page_map[mcid_key])
                            }
                        }
                    }
                } else {  // the kid is another StructElement node.
                    ProcessStructElement2(element.getAsStructElem(i), mcid_doc_map, indent + 1)
                }
            }
            PrintIndent(indent)
            mOutputListener!!.print("</" + element.getType() + ">")
        }
        /**
         * @param args
         */
    }

    init {
        setTitle(R.string.sample_logicalstructure_title)
        setDescription(R.string.sample_logicalstructure_description)
    }
}
```

{% 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/officetopdftest.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.
