Some test text!

platform
search
< Android samples

DOCX to PDF Conversion in Java

Sample Java code to use PDFTron SDK for direct, high-quality Word to PDF conversion by using the pdftron.PDF.Convert utility class. This conversion is performed entirely within PDFTron SDK and has no external or system dependencies. Conversion results will be the same on all platforms. Learn more about our PDF Conversion Library.

Step 1: Get your free trial license key, or sign in

Start Trial
Sign in

Step 2: Add the code:

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

import com.pdftron.common.PDFNetException;
import com.pdftron.pdf.*;
import com.pdftron.sdf.Obj;
import com.pdftron.sdf.ObjSet;
import com.pdftron.sdf.SDFDoc;

//---------------------------------------------------------------------------------------
// The following sample illustrates how to use the PDF.Convert utility class to convert 
// .docx files to PDF
//
// 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.
//
// Please contact us if you have any questions.	
//---------------------------------------------------------------------------------------
public class WordToPDFTest {

	static String inputPath = "../../TestFiles/";
	static String outputPath = "../../TestFiles/Output/";

	public static void main(String[] args)
	{
		PDFNet.initialize();
		PDFNet.setResourcesPath("../../../Resources");

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

		// then the more flexible line-by-line interface
		flexibleDocxConvert("the_rime_of_the_ancient_mariner.docx", "the_rime_of_the_ancient_mariner.pdf");

		// then one more, with some advanced layout features
		flexibleDocxConvert("wrap_poly_demo.docx", "wrap_poly_demo.pdf");
		PDFNet.terminate();
	}

	public static void simpleDocxConvert(String inputFilename, String outputFilename)
	{
		try
		{
			// Start with a PDFDoc (the conversion destination)
			PDFDoc pdfdoc = new PDFDoc();

			// perform the conversion with no optional parameters
			Convert.wordToPdf(pdfdoc, inputPath + inputFilename, null);

			// save the result
			pdfdoc.save(outputPath + outputFilename, SDFDoc.SaveMode.LINEARIZED, null);
			
			// And we're done!
			System.out.println("Done conversion " + outputPath + outputFilename);
		}
		catch(PDFNetException e)
		{
			System.out.println("Unable to convert MS Word document, error:");
			e.printStackTrace();
			System.out.println(e);
		}
	}

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

			// 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.universalConversion(
				inputPath + inputFilename, options);

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

			// actually perform the conversion
			while(conversion.getConversionStatus() == DocumentConversion.e_incomplete){
				conversion.convertNextPage();
				System.out.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)
				{
					System.out.println("Warning: "+ conversion.getWarningString(i));
				}

				// save the result
				PDFDoc doc = conversion.getDoc();
				doc.save(outputPath + outputFilename, SDFDoc.SaveMode.LINEARIZED, null);
				
				// done
				System.out.println("Done conversion " + outputPath + outputFilename);
			}
			else
			{
				System.out.println("Encountered an error during conversion: " + conversion.getErrorString());
			}
		}
		catch(PDFNetException e)
		{
			System.out.println("Unable to convert MS Word document, error:");
			e.printStackTrace();
			System.out.println(e);
		}
	}

}