How to get font size of list item number?

Hi,
I need to get font size of list item number.
I’ve tried this code:

paragraph.getListFormat().getListLevel().getFont().getSize()

but it returns wrong size.
When I set the font size using same way:

paragraph.getListFormat().getListLevel().getFont().setSize()

it works fine.

Thanks.

Hi

Thanks for your request. I cannot reproduce the problem on my side using the latest version of Aspose.Words for Java (4.0.2). I use the following code for testing:

Document doc = new Document("C:\\Temp\\in.doc");
System.out.print(doc.getSections().get(0).getBody().getFirstParagraph().getListFormat().getListLevel().getFont().getSize());

This code returns 18, as expected.
The in.doc is attached.
Best regards,

Thanks for a reply.
Please, try my code and document.

Document doc = new Document("C:\test_fonts.doc");

NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);

for (Paragraph paragraph: paragraphs)
{
    if (paragraph.getListFormat().isListItem())
    {
        System.out.println(paragraph.getListFormat().getListLevel().getFont().getSize() + paragraph.getText());
    }
}

Thanks.

Hi
Thank you for additional information. Please try using the following code:

NodeCollection <Paragraph> paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
for (Paragraph paragraph: paragraphs)
{
    if (paragraph.getListFormat().isListItem())
    {
        System.out.println(paragraph.getParagraphBreakFont().getSize() + paragraph.getText());
    }
}

Best regards,

Hi, thank you for a reply.
I have another problem. I need to change the size of list item number fonts.
I’m using following code:

Document document = new Document("E:\TEST.doc");
NodeCollection paragraphs = document.getChildNodes(NodeType.PARAGRAPH, true);
List resized = new ArrayList();
for (Paragraph paragraph: paragraphs)
{
    if (paragraph.getListFormat().isListItem())
    {
        double listLevelFontSize = paragraph.getListFormat().getListLevel().getFont().getSize();
        double paraBreakFontSize = paragraph.getParagraphBreakFont().getSize();
        Font font = null;
        if (listLevelFontSize == paraBreakFontSize)
        {
            font = paragraph.getListFormat().getListLevel().getFont();
        }
        else
        {
            font = paragraph.getParagraphBreakFont();
        }
        if (!resized.contains(font))
        {
            resizeFont(font, -1.0);
            resized.add(font);
        }
    }
}

private static void resizeFont(Font font, double value) throws Exception
{
    if (font != null && font.getSize() + value> 0)
    {
        font.setSize(font.getSize() + value);
    }
}

In the result, the font of list item numbers is ok. However, the font in the part of third point has also changed. Can you help with this ?
I’m attaching document for testing.

Thanks

Hi

Thanks for your request. This can occur because text in your document consists of multiple Runs. Usually this occurs when you edit document multiple times in MS Word.
There is joinRunsWithSameFormatting method, which concatenates runs with same formatting. Please try using the following code:

Document doc = new Document("C:\\Temp\\TEST.doc");
doc.joinRunsWithSameFormatting();
NodeCollection <Paragraph> paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
for (Paragraph paragraph: paragraphs)
{
    if (paragraph.getListFormat().isListItem())
    {
        paragraph.getParagraphBreakFont().setSize(18);
    }
}
doc.save("C:\\Temp\\out.doc");

Best regards,

Hi,
thank you for a reply. Your solution works but not in every case.
I’am attaching another document. This time, whole text in point 3 has been resized.

Thanks

Hi

Thanks for your request. In this case please try changing ListLevel font size.
Please see the following code:

Document doc = new Document("C:\\Temp\\TEST_2.doc");
doc.joinRunsWithSameFormatting();
NodeCollection <Paragraph> paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
for (Paragraph paragraph: paragraphs)
{
    if (paragraph.getListFormat().isListItem())
    {
        paragraph.getListFormat().getListLevel().getFont().setSize(20);
    }
}
doc.save("C:\\Temp\\out.doc");

Hope this helps.
Best regards,

Hi, thank you for a reply.
How to recognize which font should I change ?
I was using this code:

double listLevelFontSize = paragraph.getListFormat().getListLevel().getFont().getSize();
double paraBreakFontSize = paragraph.getParagraphBreakFont().getSize();
Font font = null;
if (listLevelFontSize == paraBreakFontSize)
{
    font = paragraph.getListFormat().getListLevel().getFont();
}
else
{
    font = paragraph.getParagraphBreakFont();
}

But it’s not working in every case.

Thanks

Hi

Thanks for your inquiry. I think, in your case, you should just always set ListLevel.Font.
Best regards,

The problem is that i need to decrease font size (for example by 1 pt) in whole document.
So I need get the font size and then set the size decreased by 1 pt.

font.setSize(font.getSize()-1.0);

And I don’t know which font size should I get

paragraph.getParagraphBreakFont().getSize();

or

paragraph.getListFormat().getListLevel().getFont().getSize();

Sometimes works the first and sometimes the other.

Thanks

Hi

Thank you for additional information. I think you can achieve this using DocumentVisitor. To change font size you can try using FontSizeChanger class attached here.
Please see the following code:

Document document = new Document("TEST_2.doc");
FontSizeChanger changer = new FontSizeChanger(4);
document.Accept(changer);
document.Save("out.doc");

Please let me know in case of any issues. I will be glad to help you.
Best regards,

Hi,
thank you for reply.
I’ve already tried this approach. Have a look at: FitToPages
The approach with DocumentVisitor is not good because it decreases font too much.
For example:
TEST.doc is source document
TEST_res.doc is document in which I decreased font by 2 pt using DocumentVisitor and some of paragraphs have font decreased twice

I’m trying to implement it in other way, without using DocumentVisitor, everything works fine except the font in list numbers

Thanks

Now, i’m trying this code:

if (!paragraph.isListItem())
    ChangeFont(paragraph.getParagraphBreakFont());
else
{
    paragraph.getListFormat().getListLevel().getFont().setSize(paragraph.getParagraphBreakFont().getSize());
    ChangeFont(paragraph.getListFormat().getListLevel().getFont());
}

I think it will work this way.
Thank you for your support.

Hi

It is perfect, that you already resolved the problem. Please let me know in case of any issues. I will be glad to help you.
Best regards,