Hello,
I am trying to convert a docx document to pdf and am having trouble getting the document’s formula to show up correctly in my pdf. I am running Office 2007 and using the “insert” function to insert a document. I create a copy of the word document as well so I can make sure everything is being done correctly. Everything converts well except for the formulas.
I am using Aspose.Words 10.8.0, Aspose.Pdf 3.0.1, and Aspose.Pdf.Kit 4.1.0
Here is the code I am using:
public class pdfCreate
{
/**
* Main method.
*
* @param args
* @throws DocumentException
* @throws FileNotFoundException
* @throws IOException
* @throws Exception
*/
public static void main(String[] args) throws DocumentException, FileNotFoundException, IOException, Exception
{
pdfCreate test1 = new pdfCreate();
// test1.generateSectionOne();
test1.testGenerate();
}
public void testGenerate() throws Exception
{
Document document = new Document("C:\Users\michael.a.platt.ctr\Desktop\EditTemplate.docx");
Document doc2 = new Document("C:\Users\michael.a.platt.ctr\Desktop\tester.docx");
DocumentBuilder docBuilder = new DocumentBuilder(document);
docBuilder.moveToMergeField("testTable");
Table table = new Table(document);
docBuilder.insertParagraph();
insertDocument(docBuilder.getCurrentParagraph(), doc2);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
document.save(bos, SaveFormat.DOCX);
byte[] bytes = bos.toByteArray();
File file = new File("C:\Users\michael.a.platt.ctr\Desktop\test.zip");
FileOutputStream fos = new FileOutputStream(file);
ZipOutputStream out = new ZipOutputStream(fos);
ZipEntry zipAdd = new ZipEntry("EditTemplate2.docx");
out.putNextEntry(zipAdd);
out.write(bytes);
bos.reset();
document.save(bos, SaveFormat.PDF);
bytes = bos.toByteArray();
zipAdd = new ZipEntry("EditTemplate2.pdf");
out.putNextEntry(zipAdd);
out.write(bytes);
out.close();
fos.close();
bos.close();
}
public static void insertDocument(Node insertAfterNode, Document srcDoc) throws Exception
{
// Make sure that the node is either a paragraph or table.
if ((insertAfterNode.getNodeType() != NodeType.PARAGRAPH) &
(insertAfterNode.getNodeType() != NodeType.TABLE))
{
throw new IllegalArgumentException("The destination node should be either a paragraph or table.");
}
// We will be inserting into the parent of the destination paragraph.
CompositeNode dstStory = insertAfterNode.getParentNode();
// This object will be translating styles and lists during the import.
NodeImporter importer = new NodeImporter(srcDoc, insertAfterNode.getDocument(), ImportFormatMode.KEEP_SOURCE_FORMATTING);
// Loop through all sections in the source document.
for (Section srcSection: srcDoc.getSections())
{
// Loop through all block level nodes (paragraphs and tables) in the body of the section.
for (Node srcNode: (Iterable) srcSection.getBody())
{
// Let's skip the node if it is a last empty paragraph in a section.
if (srcNode.getNodeType() == (NodeType.PARAGRAPH))
{
Paragraph para = (Paragraph) srcNode;
if (para.isEndOfSection() && !para.hasChildNodes())
{
continue;
}
}
// This creates a clone of the node, suitable for insertion into the destination document.
Node newNode = importer.importNode(srcNode, true);
// Insert new node after the reference node.
dstStory.insertAfter(newNode, insertAfterNode);
insertAfterNode = newNode;
}
}
}
}
I have also attached the output documents for viewing.