javawindowsunixms-wordaspose.words

How can I make a Word document have the same font, font size, and format on UNIX and Windows?


I am using the aspose-words library in Java to read a word document. I can get the page number of an element using the LayoutCollector class in Aspose, but it is dependent on the fonts available and the rendering engine specific to that OS. I am using the following code


    Document document=new Document(filePath);
    LayoutCollector layoutCollector=new LayoutCollector(doc);
    NodeCollection paragraphNodes=document.getChildNodes(NodeType.Paragraph,true);
    for(Node node:paragraphNodes)
    {
     if(node.getType==NodeType.PARAGRAPH){
     int pageNumber=layoutCollector.getStartPageIndex(node);
    System.out.println(pageNumber);

}

Can I make a Word document render the same in both Windows and UNIX so that the code will work as expected? I prepare Word documents in Windows, and I run Java code on a UNIX server that gives a report of elements and page numbers, and I perform some operations in Word in Windows, but both are mismatching. How can I fix this issue?


Solution

  • If you want a Word document look the same on different platforms (not necessarily limited to different operating systems only), you have to make sure that the same fonts are installed on all those platforms. Same means same, sometimes it is not sufficient if they just have the same name.

    But even that does not guarantee that the document will be rendered the same. If you use Microsoft Word to render on Microsoft Windows, there is a very good chance, but already when using MS Word on a Mac, you may see differences, depending on how sophisticated your document is.

    A rendering engine (all rendering engines, not only for Microsoft Word) implements transformation rules how the binary stuff in the file has to be displayed on screen (or on paper), and if the understanding of these rules differ between the vendors of two rendering engines, you will get deviating results. You can even see that with browsers (that are rendering machines for HTML, CSS and alike).

    So having the same fonts installed on all platforms may solve your issue for your current test documents, but as long as you use different rendering engines from different vendors and/or on different operating systems, you always have to prepare yourself for surprises.

    If the documents do not need to be modified, you should think about using PDF instead of Microsoft Word (and here you need to embed the fonts into the PDF document).

    If you use the Word documents as templates for correspondence, you either have to live with some subtle, unexpected differences, or you should think about a different way to handle that correspondence generation.