Word Document to PDF using Aspose Java

Hi,

Anyone kindly share me the piece of code to convert the given word document(.doc or .docx) to PDF using aspose in Java.

I am using this piece of code, but no luck.

doc = new Document(“C:/Sample123.docx”);
doc.save(“C:/sampleTest.pdf”, SaveFormat.Pdf);

Thanks,
Suresh

@SureshSA

Thanks for your inquiry. Please use the following code example to convert the Word document to PDF. We suggest you please read the following article.
Saving a Document

Document document = new Document(MyDir + "input.docx");
document.save(MyDir + "output.pdf", SaveFormat.PDF);

I tried this way but still not able to create a PDF.

@SureshSA

Thanks for your inquiry. The shared code in my previous post is correct. Could you please share some more detail about the issue you are facing? Please also share the input document here for testing. We will investigate the issue on our side and provide you more information.

Hi Tahir,

Below is a piece of code, used for conversion. Actually document contains some table as well as headers. Have attached a document contents it have some table and data under those columns i just removed data from that for security purpose. Kindly assist me on the solution for it.

String dir = “C:/Users/testuser/Desktop/Suresh/”;

	Document doc = new Document(dir + "Demo.docx");

	doc.save(dir+ "Demo.pdf", SaveFormat.Pdf);
	
	System.out.println("Converted");<a class="attachment" href="/uploads/default/24096">Demo.png</a> (122.8 KB)

I am ended up in getting below exception,
Exception in thread “main” class com.aspose.pdf.exceptions.InvalidPdfFileFormatException: Incorect file format
com.aspose.pdf.internal.l23l.I07.lif(Unknown Source)
com.aspose.pdf.internal.l144.I4l$I7.deserialize(Unknown Source)
com.aspose.pdf.internal.l237.I0I.deserialize(Unknown Source)
com.aspose.pdf.internal.l214.I2l.(Unknown Source)
com.aspose.pdf.internal.l214.I2l.(Unknown Source)
com.aspose.pdf.internal.l151.Il.lIF(Unknown Source)
com.aspose.pdf.internal.l214.I27.(Unknown Source)
com.aspose.pdf.internal.l151.Il.lIf(Unknown Source)
com.aspose.pdf.internal.l3l.I1I.lif(Unknown Source)
com.aspose.pdf.internal.l3l.I1I.(Unknown Source)
com.aspose.pdf.ADocument.lif(Unknown Source)
com.aspose.pdf.ADocument.(Unknown Source)
com.aspose.pdf.Document.(Unknown Source)
com.webapp.CreatePDF.main(CreatePDF.java:32) (Document doc = new Document()// this line.

@SureshSA

Thanks for sharing the detail. You are using Aspose.PDF to convert DOCX to PDF. This is the reason you are getting the exception. Please use the Aspose.Words for Java to convert DOCX to PDF.

If you are using Aspose.Words and Aspose.PDF, please use the following code example.

com.aspose.words.Document document = new com.aspose.words.Document(MyDir + "input.docx");
document.save(MyDir + "output.txt", com.aspose.words.SaveFormat.HTML);

Hi Tahir,

Can u please share Aspose.Words jar file for this PDF generation i tried to download from aspose website but not able to download.

Thanks,
Suresh

@SureshSA

Thanks for your inquiry. You can download the Aspose.Words for Java from following links.
Download Aspose.Words for Java 19.2
Download Aspose.Words Jar file

Hi Tahir,

I need some input of below scenario how to show/hide a table in word template while converting into PDF using aspose words. Is it any API we can achieve or any aspose words interface to do it.

Scenario:

If A= Yes show Table 1. else hide Table 1.

Kindly provide some input on how to achieve this one using aspose.

TIA,
Suresh

@SureshSA

Yes, you can use IF field to show or hide the table in output document. Please check the attached sample Word document. input.zip (9.5 KB)

Hi Tahir,

Thanks for the input, but i need a requirement as below.
in the docx i have a table which contains 2 rows and 3 columns and i have to hide this entire table when if A= “Yes” else i have to show this table how to achieve this. provided input helps me to show/hide a piece of text in the document. But i have a table to be show/hide based on condition. Any inputs kindly let me know.

Thanks,
Suresh

@SureshSA,

Please follow your other thread for further proceedings.