Apply custom styles from template to paragraphs and tables of DOCX using Java

Thank you Tahir for the quick response.

We have tried your code. However, we did not get the expected output. Please refer to ‘expectedOutput.docx’ in the attachment which contains the same content from the source docx but with the desired styles applied from the template. Please note the styles applied for each node type in the output docx file. We require those custom styles to be applied to the respective node types.

ex: The images in the source file are formed with word shape drawings. But we need it to be as an image in the output file.toAspose.zip (490.2 KB)

Thanks,
Berlin

@bmathew_virtusa_com

In your case, we suggest you please call ParagraphFormat.ClearFormatting and Font.ClearFormatting methods as shown below to get the desired output.

Paragraph para = (Paragraph)doc.getChild(NodeType.RUN, 0, true);
para.getParagraphFormat().clearFormatting();
for(Run run : para.getRuns())
    run.getFont().clearFormatting();

 para.getParagraphFormat().setStyleName("Body");

Hi Tahir,

Thanks for your response. I made changes with the code you suggested but i did not get the desired result. To make it simple, AsposeSupport.zip (849.9 KB)

I am attaching a simple input docx(example1) , java program and output docx(example1). Can you please check why the below things are not working

  1. hyperlinks in the paragraph is not shown in the output docx
  2. Group Shape is not showing up in the output docx file.
  3. The last line in the output docx dont have space between them[Printed in USAs and C78-739361-00 07/17 ]
    4)The first page in the output docx is unnecessarily created.

Please suggest.

Thanks & Regards,
Berlin

@bmathew_virtusa_com

We are investigating this issue and will get back to you soon.

@bmathew_virtusa_com

In your case, you do not need to iterate over each node and insert it into new document. You can use Document.AppendDocument method to append your input document to template document.

You can also copy the styles from template document to input document as suggested in my previous post.

Clear the formatting of Paragraph and Run nodes and apply styles according to your requirement.

We suggest you please read the following articles.
Appending Documents
Using DocumentBuilder to Modify a Document
Use DocumentBuilder to Insert Document Elements

If you still face problem, please share complete detail of your requirement. We will then provide you more information about your query.

Hi Tahir,

Thanks for you response.
My requirement is that i have to apply few custom styles for each element(paragraph,table, groupshapes, images etc) in the source document. For that reason, in the attached java code i traversing all the nodes in the source document applying custom style and then adding/appending to the destination document. Can you please tell me where i have to change the code to achieve the following

  1. hyperlinks in the paragraph is not shown in the output docx
  2. Group Shape is not showing up in the output docx file.
  3. The last line in the output docx dont have space between them[Printed in USAs and C78-739361-00 07/17 ]
    4)The first page in the output docx is unnecessarily created.

Regards,
Berlin

Regards,
Berlin

@bmathew_virtusa_com

You can use Document.ImportNode method to import a node from another document to the current document. Following code example shows how to import nodes from one document to another. This code example also clears the formatting of paragraph and imports styles from template document to destination document.

Document doc = new Document(MyDir + "example1.docx");
Document dstDoc = new Document();

for(Node node : (Iterable<Node>)doc.getFirstSection().getBody().getChildNodes())
{
    Node newNode = dstDoc.importNode(node, true, ImportFormatMode.USE_DESTINATION_STYLES);
    if(newNode.getNodeType() == NodeType.PARAGRAPH)
    {
        ((Paragraph)newNode).getParagraphFormat().clearFormatting();
    }
    dstDoc.getFirstSection().getBody().appendChild(newNode);
}

Document template = new Document(MyDir + "DataSheetTemplate2019.dotx");

for (Style srcStyle : (Iterable<Style>)template.getStyles())
{
    if(srcStyle.getName().contains("Char"))
        continue;
    Style newStyle = dstDoc.getStyles().addCopy(srcStyle);
    newStyle.setName(srcStyle.getName());
}
dstDoc.save(MyDir + "output 19.12.docx");

This code example does not set the style of paragraphs and tables. You can set the the style according to your requirement.

Hi Tahir,

Thanks for your response. I updated the code and attached the input and output docx generated along with java file.
These are few things i observed,

  1. There was a problem with table style but i managed to give the styling using a for-loop. Please look into my code in the java file
  2. I noticed there is no gap between the last two words in the last paragraph/line. Can you please see why there is no gap between like input docx file
    Printed in USAs C78-739361-00 07/17

24Dec-1.zip (307.3 KB)
Thanks&Regards,
Berlin

@bmathew_virtusa_com

You are setting style of first paragraph of table’s cell. You need to iterate over all paragraphs of table’s cell and apply the style.

We have tested the scenario using the latest version of Aspose.Words for Java 19.12 with shared code example and have not found the issue. So, please use Aspose.Words for Java 19.12. We have attached the output document with this post for your kind reference. 19.12.zip (151.7 KB)

Hi Tahir,

We recently purchased the product (Aspose.Words for Java V19.11) on Dec 13th , 2019. Is there any workaround for the fix. Can i use 19.12 version jar in maven and still use 19.11 version licence key.

Thanks & Regards,
Berlin

@bmathew_virtusa_com

You can check the expiry date of your license by opening the license file in notepad. You will see the following tag in your license file:

<SubscriptionExpiry>20200218</SubscriptionExpiry>

It means that you can free upgrade to a version of Aspose.Words that is published before 02/18/2020.

Hi Tahir,

Thanks for your response.
How to convert a 2/3 column layout of a docx file to a 1 column layout? i would like to convert the attached docx file to 1 column layout ? All the pages in the docx file should have only 1 column.
sample.zip (3.7 MB)

Regards,
Berlin

@bmathew_virtusa_com

Please use TextColumnCollection.Count property as shown below to set the number of columns in the section of a document. Hope this helps you.

Document doc = new Document(MyDir + "sample.docx");
for(Section section : doc.getSections())
{
    if(section.getPageSetup().getTextColumns().getCount() > 1)
        section.getPageSetup().getTextColumns().setCount(1);
}
doc.save(MyDir + "19.12.docx");

Hi Tahir,
The table style is not getting applied even though i added the line
doc.setAutomaticallyUpdateSyles(true);AsposeSupport.zip (1.6 MB)

Also noticed that the even and odd are not working because of the merged rows and cells.
I attached input docx, output docx, template file and java file.
Please suggest.

Regards,
Berlin

@bmathew_virtusa_com

Could you please ZIP and attach your simplified input and expected output documents? We will investigate the issue and provide you more information on it.

Hi Tahir,

The expected table look[styling] should be same as the table in the template[DataSheetTemplate2019.dotx] that i attached in the zip.

Please notice the following

  1. Table header background color
  2. The table header cell have a style[Cellhead1, Chart_subhead]
  3. The table body cells have style[Chart_body],
  4. If the table body cell has list then the style is [CellBullet].

The input file[software-system-bulletin.docx] is present in the input folder. After i ran the attached java code[DocxConversion.java] the output file is saved in the output folder.

Basically, the content in the output file should be same as input file but the output file should have styles that are there in the template file[DataSheetTemplate2019.dotx]

Eg.
Headings :
ToC_Subhead1
ToC_Subhead2
ToC_Subhead3
ToC_Subhead4

Table:
Table Style
CellBody
CellBullet
Cellhead1
Chart_body
Chart_subhead

Figure Caption:
FigureCaption
FigureCaption_Text

Table Caption:
TableCaption

Paragraph:
Body

Paragraph List
Bullet

AsposeSupport.zip (1.6 MB)

Regards,
Berlin

@bmathew_virtusa_com

Thanks for sharing the detail. We are investigating this issue and will get back to you soon.

@bmathew_virtusa_com

We suggest you please copy the styles from the specified template to a document using Document.CopyStylesFromTemplate method. In your case, we need to clear the font formatting of Paragraph, Run and Table nodes.

Please check the following code example Hope this helps you.

Document srcDoc = new Document(MyDir + "software-system-bulletin.docx");

for(Paragraph paragraph : (Iterable<Paragraph>)srcDoc.getChildNodes(NodeType.PARAGRAPH, true))
{
    paragraph.getParagraphBreakFont().clearFormatting();
    paragraph.getParagraphFormat().clearFormatting();
    for (Run run : paragraph.getRuns())
    {
        run.getFont().clearFormatting();
    }
}

srcDoc.copyStylesFromTemplate(MyDir + "DataSheetTemplate2019.dotx");

for(Table table : (Iterable<Table>)srcDoc.getChildNodes(NodeType.TABLE, true))
{
    for(Paragraph paragraph : (Iterable<Paragraph>)table.getFirstRow().getChildNodes(NodeType.PARAGRAPH, true))
    {
        paragraph.getParagraphFormat().setStyleName("Cellhead1");
    }

    //Use the same approach to set paragraph's style for remaining rows

    for(Cell cell  : (Iterable<Cell>)table.getChildNodes(NodeType.CELL, true))
    {
        cell.getCellFormat().clearFormatting();
    }
    table.setStyleName("Table Style");
}
srcDoc.setAutomaticallyUpdateSyles(true);
srcDoc.save(MyDir + "20.1.docx");

2 posts were split to a new topic: Table should be editable table. [Not as image] after PDF to DOCX and HTML using Java

A post was split to a new topic: Table should be editable table. [Not as image] after PDF to DOCX and HTML using Java