We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How do I find the Font properties of a Paragraph


I’m having an issue retrieving the font/style properties of a paragraph. In my situation, I’m creating a new dynamically-created Document to be inserted into an existing parent Document at a merge field. The problem is, when I insert my new sub-document, it’s taking on the complete default Word Style (Style: “Normal”, Font: Calibri, Size: 11pt, Line Spacing: 10pt) rather than inheriting the font properties of paragraph being inserted into (Style: “Normal”, Font: Arial, Size: 12pt, Line Spacing: 0pt). This happens regardless of using ImportFormatMode.USE_DESTINATION_STYLES or ImportFormatMode.KEEP_SOURCE_FORMATTING during the sub-doc insertion.

Before doing a builder.write, I try to grab the font properties from the parent at the merge field location and use those properties when writing to the sub-doc.

Boolean foundMrgFld = parentDocBuilder.moveToMergeField(myMergeField);
if (foundMrgFld) {
Node insertAfterNode = parentDocBuilder.getCurrentParagraph();
if (insertAfterNode.getNodeType() == (NodeType.PARAGRAPH)) {
Paragraph para = (Paragraph)insertAfterNode;
Style insertStyle = para.getParagraphFormat().getStyle();

In this case, even though the insertAfterNode is 12pt Arial, the insertStyle returned is the default “Normal” Style 11pt Calibri (confirmed via insertStyle.getFont().getName() / getSize() / etc while debugging). It seems perhaps the issue is because the paragraph Style is Normal" in both cases, even though a custom font and size have been set in the parent document. Can anyone provide a quick example of how to return the actual font properties of a Paragraph and not just the Style’s font properties?

I really can’t attach examples because of the sheer volume of code that went into generating the dynamic child document, but potentially someone can take a basic Word document and easily recreate this. Just use custom fonts/size but keep the Style as default, then test retrieving the Style through Aspose at that paragraph and checking the font/size it returns. That should be sufficient to test.

Thanks for any help.

Hi John,

Thanks for your inquiry. All text of the document is stored in runs of text and Run can only be a child of Paragraph. I believe, you can copy direct formatting attributes of a source Run into a destination Run of text by using the following code:
public static void copyFormatting(Object source, Object dest) throws Exception {
    if (source.getClass() != dest.getClass())
        throw new Exception("All objects must be of the same type");

    Method methodlist[] = source.getClass().getDeclaredMethods();

    // Iterate through each property in the source object.
    for (Method prop : methodlist) {
        // Continue processing only if the method starts with ‘get’.
        if (!prop.getName().startsWith("get"))

        // Skip indexed access items. Skip setting the internals of a style as these should not be changed.
        if (prop.getName() == "getItem" || prop.getName() == "getStyle" ||
                prop.getName() == "getStyleName" || prop.getName() == "getStyleIdentifier")

        Object value;

        // Wrap this call as it can throw an exception. Skip if thrown
        try {
            // Get value by invoking getter method.
            value = prop.invoke(source);
        } catch (Exception e) {

        // Get the corresponding setter method.
        Method setter = null;
        try {
            setter = source.getClass().getDeclaredMethod(prop.getName().replace("get", "set"), prop.getReturnType());
        } catch (Exception e) {
            // do nothing if throws.

        // Skip if value can not be retrieved.
        if (value != null) {
            // If this property returns a class which belongs to the
            if (prop.getReturnType().getPackage() != null && prop.getReturnType().getPackage().getName().equals("com.aspose.words")) {
                // Recurse into this class.
                copyFormatting(prop.invoke(source), prop.invoke(dest));
            } else if (setter != null) {
                // If we can write to this property then copy the value across.
                setter.invoke(dest, prop.invoke(source));

I hope, this helps.
Best regards,