Some test text!

< Xamarin samples

WordToPDFTest - C# (Xamarin)

These samples illustrate how to use the pdftron.PDF.Convert utility class to convert DOCX files to PDF. This conversion is performed entirely within PDFNet and has no external or system dependencies dependencies. Conversion results will be the same on all platforms.

//
// Copyright (c) 2001-2018 by PDFTron Systems Inc. All Rights Reserved.
//

using System;
using System.Drawing;

using pdftron;
using pdftron.Common;
using pdftron.Filters;
using pdftron.SDF;
using pdftron.PDF;

using NUnit.Framework;

namespace MiscellaneousSamples
{
	/// <summary>
    ///---------------------------------------------------------------------------------------
    /// 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
    ///
    /// Please contact us if you have any questions.	
    ///---------------------------------------------------------------------------------------
	/// </summary>



	[TestFixture]
	public class WordToPDFTest
	{

		const string input_path =  "TestFiles/";

		static void SimpleDocxConvert(String input_filename, String output_filename)
		{
            // Start with a PDFDoc (the conversion destination)
            using (PDFDoc pdfdoc = new PDFDoc())
            {
                // perform the conversion with no optional parameters
                pdftron.PDF.Convert.WordToPDF(pdfdoc, Utils.GetAssetTempFile(input_path + input_filename), null);

                // save the result
                pdfdoc.Save(Utils.CreateExternalFile(output_filename), SDFDoc.SaveOptions.e_linearized);

                // And we're done!
                Console.WriteLine("Saved " + output_filename);
            }   
		}

		static void FlexibleDocxConvert(String input_filename, String output_filename)
		{
			// Start with a PDFDoc (the conversion destination)
            using (PDFDoc pdfdoc = new PDFDoc())
            {
                MappedFile in_stream = new MappedFile(Utils.GetAssetTempFile(input_path + input_filename));

                WordToPDFOptions options = new WordToPDFOptions();
                string res = Utils.GetAssetTempFile(input_path + "pdftron_smart_substitution.plugin");
                options.SetSmartSubstitutionPluginPath(res);
                // 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 = pdftron.PDF.Convert.WordToPDFConversion(
                    pdfdoc, in_stream, options);

                // actually perform the conversion
                // this particular method will not throw on conversion failure, but will
                // return an error status instead
                if (conversion.TryConvert() == DocumentConversionResult.e_document_conversion_success)
                {
                    int num_warnings = conversion.GetNumWarnings();

                    // print information about the conversion 
                    for (int i = 0; i < num_warnings; ++i)
                    {
                        Console.WriteLine("Warning: " + conversion.GetWarningString(i));
                    }

                    // save the result
                    pdfdoc.Save(Utils.CreateExternalFile(output_filename), SDFDoc.SaveOptions.e_linearized);
                    // done
                    Console.WriteLine("Saved " + output_filename);
                }
                else
                {
                    Console.WriteLine("Encountered an error during conversion: " + conversion.GetErrorString());
                }
            }
		}

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[Test]
		public static void Sample()
		{

			try
			{
				// first the one-line conversion method
				SimpleDocxConvert("simple-word_2007.docx", "simple-word_2007.pdf");

                // then the more flexible line-by-line conversion API
                FlexibleDocxConvert("the_rime_of_the_ancient_mariner.docx", "the_rime_of_the_ancient_mariner.pdf");

                // and one more for the road...
                SimpleDocxConvert("wrap_poly_demo.docx", "wrap_poly_demo.pdf");
			}
			catch (pdftron.Common.PDFNetException e)
			{
				Console.WriteLine( e.GetMessage() );
			}
			catch (Exception e)
			{
				Console.WriteLine("Unrecognized Exception: " + e.Message );
			}

            Console.WriteLine("Done.");
		}
	}
}