Hi everybody,
we have an problem with one DocX. It can’t be saved as PDF with aspose.Words 19.1 and below.
But if we save it with aspose.Words as DocX again than it’s possible to convert the created DocX into PDF.
Here the code to reproduce it:
import com.aspose.words.Document;
import com.aspose.words.OoxmlSaveOptions;
import com.aspose.words.PdfSaveOptions;
import com.aspose.words.SaveOptions;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.UUID;
public class Main {
private final ClassLoader classLoader = getClass().getClassLoader();
private final File tmpFolder = new File(System.getProperty("java.io.tmpdir"));
public static void main(String[] args) {
try {
System.out.println("Convert Docx to PDF");
new Main().createsPdfFromDocx_onlyConvertToPdf();
System.out.println("Success");
} catch (Exception e) {
System.out.println("Failed to convert Docx to PDF ");
e.printStackTrace();
}
System.out.println("==============================================================");
try {
System.out.println("Convert Docx to Docx and the saved Docx to PDF");
new Main().createsPdfFromDocx_saveAsDocxBeforeConvertingToPdf();
System.out.println("Success");
} catch (Exception e) {
System.out.println("Failed to convert Docx to PDF with saving it as Docx in between");
e.printStackTrace();
}
}
/**
* NPE with "com.aspose" "aspose-words" 19.1 and below
*
* <dependency> <groupId>com.aspose</groupId>
* <artifactId>aspose-words</artifactId> <version>19.1</version>
* <classifier>jdk16</classifier> </dependency>
* <p>
* java.lang.NullPointerException at com.aspose.words.zzYXC.zzZey(Unknown
* Source) at com.aspose.words.zzYXC.zzZg0(Unknown Source) at
* com.aspose.words.zzZL6.zzo(Unknown Source) at
* com.aspose.words.zzZL5.zzV(Unknown Source) at
* com.aspose.words.zzZLE.zzR(Unknown Source) at
* com.aspose.words.zzZLD.zzY(Unknown Source) at
* com.aspose.words.zzW3.zzZ(Unknown Source) at
* com.aspose.words.zzYYB.zzZ(Unknown Source) at
* com.aspose.words.zzX.zzZ(Unknown Source) at com.aspose.words.zzX.zzP(Unknown
* Source) at com.aspose.words.zzW3.zzZ(Unknown Source) at
* com.aspose.words.zzYYB.zzZ(Unknown Source) at
* com.aspose.words.zzXN.zzsg(Unknown Source) at
* com.aspose.words.zzXN.zzY(Unknown Source) at
* com.aspose.words.zzZ7E.zzZrF(Unknown Source) at
* com.aspose.words.zzZ7E.zzS(Unknown Source) at
* com.aspose.words.zzZ6S.zzJ(Unknown Source) at
* com.aspose.words.zzYWB.zzn(Unknown Source) at
* com.aspose.words.zz93.zzJ(Unknown Source) at
* com.aspose.words.zz94.zz3D(Unknown Source) at
* com.aspose.words.zzZML.zzZ(Unknown Source) at
* com.aspose.words.Document.updatePageLayout(Unknown Source) at
* com.aspose.words.zz4D.perform(Unknown Source) at
* com.aspose.words.zz4E.zzLN(Unknown Source) at
* com.aspose.words.zz1F.zzYY(Unknown Source) at
* com.aspose.words.zz1F.zzZ(Unknown Source) at
* com.aspose.words.zz1F.zzZx(Unknown Source) at
* com.aspose.words.Document.zzZ(Unknown Source) at
* com.aspose.words.Document.zzZ(Unknown Source) at
* com.aspose.words.Document.zzZ(Unknown Source) at
* com.aspose.words.Document.save(Unknown Source) at
* com.orgavision.service.aspose.WordFileHandlerIT.createsPdfFromDocx_onlyConvertToPdf
*/
public void createsPdfFromDocx_onlyConvertToPdf() throws Exception {
// Arrange
final InputStream originalDocument = classLoader.getResourceAsStream("./errorConvertToPdf.docx");
final File tempPdfFile = new File(tmpFolder.getAbsolutePath() + "/" + UUID.randomUUID().toString());
final FileOutputStream fileOutputStream = new FileOutputStream(tempPdfFile);
Document doc = new Document(originalDocument);
SaveOptions saveOptions = new PdfSaveOptions();
// Act
// -- this causes a NPE
doc.save(fileOutputStream, saveOptions);
// Assert
System.out.println("Saved pdf here: " + tempPdfFile.getAbsolutePath());
// ...
}
/**
* Saving the docx as docx and than as pdf works fine
*
* <dependency> <groupId>com.aspose</groupId>
* <artifactId>aspose-words</artifactId> <version>19.1</version>
* <classifier>jdk16</classifier> </dependency>
*/
public void createsPdfFromDocx_saveAsDocxBeforeConvertingToPdf() throws Exception {
// Arrange
final InputStream originalDocument = classLoader.getResourceAsStream("errorConvertToPdf.docx");
final File tempDocFile = new File(tmpFolder.getAbsolutePath() + "/" + UUID.randomUUID().toString());
final File tempPdfFile = new File(tmpFolder.getAbsolutePath() + "/" + UUID.randomUUID().toString());
// Act
// - 1. step, save docx as docx
Document doc = new Document(originalDocument);
doc.save(new FileOutputStream(tempDocFile), new OoxmlSaveOptions());
System.out.println("Saved docx here: " + tempDocFile.getAbsolutePath());
// - 2. step, same as pdf
Document doc2 = new Document(new FileInputStream(tempDocFile));
doc2.save(new FileOutputStream(tempPdfFile), new PdfSaveOptions());
// Assert
System.out.println("Saved pdf here: " + tempPdfFile.getAbsolutePath());
// ...
}
}
Here is the main-class to reproduce the issue with the docx-file.
MainAndDocX.zip (26.8 KB)
We use Java 1.8.
Can you tell us where the problem is?
If you need more information, just let me know.
Thank you and best regards