Cannot Cast DocumentInfo

Hello,

I’m trying to obtain information regarding the document I’m trying to convert to a PDF.
I would like to know if I’m processing a landscape or portrait document.

I’m getting the following error:

Failed to generate pdf for document ‘PwC Config.docx’ - class com.groupdocs.conversion.contracts.documentinfo.WordprocessingDocumentInfo cannot be cast to class com.groupdocs.conversion.contracts.documentinfo.PdfDocumentInfo (com.groupdocs.conversion.contracts.documentinfo.WordprocessingDocumentInfo and com.groupdocs.conversion.contracts.documentinfo.PdfDocumentInfo are in unnamed module of loader com.mendix.container.deployment.internal.Interpreters$ClassLoaders$$anon$1 @6bca7e0d)

code:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import com.groupdocs.conversion.Converter;
import com.groupdocs.conversion.options.convert.PdfConvertOptions;
import com.groupdocs.conversion.contracts.documentinfo.*;
import com.mendix.core.Core;
import com.mendix.logging.ILogNode;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.webui.CustomJavaAction;
import com.mendix.systemwideinterfaces.core.IMendixObject;

public class ConvertToPDF extends CustomJavaAction<java.lang.Void>
{
	private IMendixObject __InputFile;
	private system.proxies.FileDocument InputFile;
	private IMendixObject __OutputFile;
	private system.proxies.FileDocument OutputFile;

	public ConvertToPDF(IContext context, IMendixObject InputFile, IMendixObject OutputFile)
	{
		super(context);
		this.__InputFile = InputFile;
		this.__OutputFile = OutputFile;
	}

	@java.lang.Override
	public java.lang.Void executeAction() throws Exception
	{
		this.InputFile = __InputFile == null ? null : system.proxies.FileDocument.initialize(getContext(), __InputFile);

		this.OutputFile = __OutputFile == null ? null : system.proxies.FileDocument.initialize(getContext(), __OutputFile);

		// BEGIN USER CODE
		
		ILogNode logger = Core.getLogger("ConvertToPDF");
		
		try(ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
			Converter converter = new Converter(Core.getFileDocumentContent(getContext(), __InputFile));
					
	        PdfConvertOptions options = new PdfConvertOptions();
	        converter.convert(outputStream, options);
	        
	        IDocumentInfo info = converter.getDocumentInfo();
			PdfDocumentInfo pdfInfo = (PdfDocumentInfo) info;
	        	        
	        //logger.info("Is Landscape: " + pdfInfo.isLandscape());
	        //logger.info(pdfInfo.getHeight());
	        //logger.info(pdfInfo.getWidth());
	        
            Core.storeFileDocumentContent(getContext(), __OutputFile, new ByteArrayInputStream(outputStream.toByteArray()));
        } catch (IOException e) {
        	logger.error(e.getMessage());
        }
	
		return null;
		
		// END USER CODE
	}

	/**
	 * Returns a string representation of this action
	 */
	@java.lang.Override
	public java.lang.String toString()
	{
		return "ConvertToPDF";
	}

	// BEGIN EXTRA CODE
	// END EXTRA CODE
}

Can anyone tell me where I’m going wrong, I’m new to Java so any help would be appreciated.

Regards
Adrian

1 Like

@ateale

In order to obtain Word document information. Please use WordprocessingDocumentInfo instead of PdfDocumentInfo.

IDocumentInfo info = converter.getDocumentInfo();
WordprocessingDocumentInfo wordInfo = (WordprocessingDocumentInfo) info; 

Hi Atir,

Thanks for the reply.

I was hoping to not need to know what type of file I am processing.
Is there a way then to get the info on the generated PDF document instead?

Regards
Adrian

@ateale

The workaround is, detect the file type and then use relevant DocumentInfo class (e.g. WordprocessingDocumentInfo, PdfDocumentInfo).

This is how you get info from the PDF file:

Converter converter = new Converter("resultant pdf file");
IDocumentInfo info = converter.getDocumentInfo();
PdfDocumentInfo pdfInfo = (PdfDocumentInfo) info;
System.out.print("Author: "+ pdfInfo.getAuthor());

Once the Word file is converted to PDF, you can pass the resultant PDF to Converter class as shown above. Please go through Get Document Info article for more details.

Got it!

Thanks again Atir :slight_smile:

1 Like

@ateale

You are welcome.