Free Support Forum - aspose.com

Font formating problem

Hi, I have some problem with font formating. I am using aspose.words for Java. Here is my code snippet that can halp you to reproduct problem:
String testDoc = “aspose/words/test.doc”;

InputStream inputStream = this.getClass().getClassLoader()
.getResourceAsStream(testDoc);

Document document = new Document(inputStream);

DocumentBuilder builder = new DocumentBuilder(document);

Section section = document.getFirstSection();

Body body = section.getBody();

Paragraph paragraph = body.getFirstParagraph();

paragraph = (Paragraph) paragraph.getNextSibling();

NodeCollection paragraphChildes = paragraph.getChildNodes();

Shape shape = (Shape) paragraphChildes.get(3);

NodeCollection shapeChildes = shape.getChildNodes();

paragraph = (Paragraph) shapeChildes.get(1);

builder.moveTo(paragraph);

out.println(paragraph.getText());

out.println(builder.getBold());

out.println(builder.getFont().getBold());

Problem is that builder says that fonts in that particular paragraph are bold, but in attached test.doc file you can see that it is not true.

Thanks, Ivica.

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. The method MoveTo(Paragraph par) moves cursor to the end of paragraph. In your document if move cursor to the end of Paragraph then you will see the Word shows that it is bold. Please check this on your side.

For example if you try using the following code you will get “false”

System.out.println(builder.getCurrentParagraph().getParagraphFormat().getStyle().getFont().getBold());

Best regards.

Ok, I managed to repair font formating, but there is still problem with font name and size in current paragraph, see my code:
String testDoc = “aspose/words/bug771.doc”;

InputStream inputStream = this.getClass().getClassLoader()
.getResourceAsStream(testDoc);

Document document = new Document(inputStream);

DocumentBuilder builder = new DocumentBuilder(document);

Section section = document.getFirstSection();

HeadersFooters headersFooters = section.getHeadersFooters();

HeaderFooter headerFooter = headersFooters.get(1);

Paragraphs paragraphs = headerFooter.getParagraphs();

Paragraph paragraph = paragraphs.get(1);

builder.moveTo(paragraph);

Font builderParagraphFont = builder.getCurrentParagraph()
.getParagraphFormat().getStyle().getFont();

out.println(builderParagraphFont.getBold()); //false

out.println(builderParagraphFont.getSize()); //12.0

out.println(builderParagraphFont.getName()); //Times New Roman

if you look in same attached document, you will see that it is not correct font name and size.

Thanks, Ivica.

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. And again there is some formatting problems in your document. The second paragraph in the footer (paragraph that you process via your code) has Times New Roman 12 font. You can see this if select end of the paragraph (¶ symbol). So code works correct.

Best regards.

Yes, I saw that, can I somehow pick up formatting of paragraph font and not of just paragraph end?

Thanks, Ivica

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. There can be multiple Runs inside one Paragraph. Each Run can have its Font. You can try using the following code:

Paragraph builderParagraph = builder.getCurrentParagraph();

for(int runIndex=0; runIndex<builderParagraph.getRuns().getCount(); runIndex++ )

{

System.out.println("Run #" + runIndex);

System.out.println(builderParagraph.getRuns().get(runIndex).getFont().getBold());

System.out.println(builderParagraph.getRuns().get(runIndex).getFont().getSize());

System.out.println(builderParagraph.getRuns().get(runIndex).getFont().getName());

}

Hope this helps.

Best regards.

Hi, I have one more example, in attached document, if you look at the cell containing “Below” word, you see that font size is 10, but when you move documentBuilder to that paragraph, he shows to me that size is 12, and now I can not see any paragraph signs that is in different size. This is example code snippet:
public void test() throws Exception {
Document document = new Document(inputStream);

DocumentBuilder builder = new DocumentBuilder(document);

CompositeNode node = getCell(document).getNode();

builder.moveTo(node);

out.println(builder.getFont().getSize()); // this returns 12
}

private CompositeNodeWrapper getCell(Document document) throws Exception {
final CompositeNodeWrapper nodeWrapper = new CompositeNodeWrapper();

document.accept(new DocumentVisitor() {

@Override
public int visitParagraphStart(Paragraph arg0) throws Exception {
if (arg0.getText().contains(“Below”)) {
nodeWrapper.setNode(arg0);
}

return super.visitParagraphStart(arg0);
}

});

return nodeWrapper;
}

Can you please check why this is happening.

Thanks, Ivica

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. It seems that DocumentBuilder returns default font of current cell. If you remove all content from the cell you will see that it has font size 12. If you need get content formatting of cell you can try using the following code:

Document document = new Document("Tables.doc");

DocumentBuilder builder = new DocumentBuilder(document);

//Move cursor to cell

builder.moveToCell(1,2,3,-1);

//Get cell

Cell cell = (Cell)builder.getCurrentParagraph().getAncestor(NodeType.CELL);

NodeCollection runs = cell.getChildNodes(NodeType.RUN, true);

for(int i=0; i<runs.getCount();i++)

{

System.out.println(((Run)runs.get(i)).getFont().getSize());

}

I hope this could help you.

Best regards.