Bug Report: Alignment Styles not applied correctly

Hi,

I’m using Aspose to have a input.doc “template file” where I set my “Normal” style the way I like (for example, setting to Arial and alignment justified. My app reads this file, does some insertHTML to it and exports to a new “output” file.

The problem is that the text is correctly set to the “Normal” style and the font is correct but the alignment is not applied correctly. If I open my output file and re-set the style to “Normal” the alignment is corrected (justified).

What’s the problem that is going on and is there any workaround / fix for this?
This is really a crucial feature for my client that already paid for Aspose.Words…

Code:

String html = "<p>aslkdj alkj dsakjsd</p>\n" +
        "<p>asdlka shkjdahksld halkjsd halkjshdkjahsdkjahsjd &nbsp;asdh ajsdha sgda sdahs dasd la sdahsld alsdasldka hsdk ashd asdlgajksdgaljsdgasd ajkshdasd kajhs djasdashdkj ahsjdk ahskjdha skljdha dlha ksjdh kjh lkhlk hlkjhlk hkh kjh klhkl hkj haskdjahskjdhajksdhasd oi jhkjl1h23k12 312 312 3 12 312 3 12 &nbsp;31 23 1 23 12 3 12 3 12 3123 12 3123 12 312 3 jqwh jakhs djkahds kajs hdkahsdak hs asd asd adsads ad</p>";
Document doc2 = new Document("D:\\docx_test\\input.doc");
DocumentBuilder db = new DocumentBuilder(doc2);
db.insertHtml(html);
doc2.save("D:\\docx_test\\1.docx", SaveFormat.DOCX);

Attached is an example input and output file.

Also, using another style in the template doesn’t work. Alignment is not applied but everything else is (font size, font type, etc)

Code:

String html = "<p>aslkdj alkj dsakjsd</p>\n" +
        "<p>asdlka shkjdahksld halkjsd halkjshdkjahsdkjahsjd &nbsp;asdh ajsdha sgda sdahs dasd la sdahsld alsdasldka hsdk ashd asdlgajksdgaljsdgasd ajkshdasd kajhs djasdashdkj ahsjdk ahskjdha skljdha dlha ksjdh kjh lkhlk hlkjhlk hkh kjh klhkl hkj haskdjahskjdhajksdhasd oi jhkjl1h23k12 312 312 3 12 312 3 12 &nbsp;31 23 1 23 12 3 12 3 12 3123 12 3123 12 312 3 jqwh jakhs djkahds kajs hdkahsdak hs asd asd adsads ad</p>";
Document doc2 = new Document("D:\\docx_test\\input.doc");
DocumentBuilder db = new DocumentBuilder(doc2);
db.insertHtml(html);
NodeCollection paragraphs = doc2.getChildNodes(NodeType.PARAGRAPH, true);
for (Object node : paragraphs){
    Paragraph paragraph = (Paragraph) node;
    paragraph.getParagraphFormat().setStyleName("mynewstyle");
}
doc2.save("D:\\docx_test\\1.docx", SaveFormat.DOCX);

Hi

Thanks for your request. You can easily work the problem around using code like the following:

// Open document.
Document doc = new Document("input.doc");

// DocumenBuilder will help us to insert bookmaks.
DocumentBuilder builder = new DocumentBuilder(doc);

String html = "<p>aslkdjalkj dsakjsd</p>\n" +
    "<p>asdlka shkjdahksld halkjsd halkjshdkjahsdkjahsjd &nbsp;asdh ajsdha sgda sdahs dasd la sdahsld alsdasldka hsdk ashd asdlgajksdgaljsdgasd ajkshdasd kajhs djasdashdkj ahsjdk ahskjdha skljdha dlha ksjdh kjh lkhlk hlkjhlk hkh kjh klhkl hkj haskdjahskjdhajksdhasd oi jhkjl1h23k12 312 312 3 12 312 3 12 &nbsp;31 23 1 23 12 3 12 3 12 3123 12 3123 12 312 3 jqwh jakhs djkahds kajs hdkahsdak hs asd asd adsads ad</p>";

builder.insertHtml(html);

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

for (int i = 0; i < paragraphs.getCount(); i++ )
{
    Paragraph par = (Paragraph) paragraphs.get(i);
    if (par.getParagraphFormat().getStyle().getName()== "Normal")
    {
        par.getParagraphFormat().clearFormatting();
        par.getParagraphFormat().setStyleIdentifier(StyleIdentifier.NORMAL);
    }
}

doc.save("out.docx", SaveFormat.DOCX);

Hope this helps. Please let me know if you need more assistance, I will be glad to help you.

Best regards,

Hi,

thanks for the reply.
I didn’t test your fix but anyway I noticed that this doesn’t fix my problem because I noticed that my “if (par.getParagraphFormat().getStyle().getName() == “Normal”)” isn’t enough.

In my input template file I have paragraphs that still report having the “Normal” style (maybe as a base) and then have custom styling: right alignment, bigger font, different font type, etc, but still their style name is the “Normal”.

So this means your code changes all the paragraphs, instead of only the inserted paragraphs in the “insertHTML” statement.

Any way to achieve this?

Also, can you check if this bug still happens in your current trunk code? Because I still think this is a bug that should be fixed… don’t you agree?

Hi Paulo,
Thanks for your inquiry.
Yes from what it appears this is a bug, it happens in the .NET version of Aspose.Words as well. I have logged this issue and you will be informed when it’s fixed.
In the mean time you can use a work around like below which will insert the HTML with proper formatting and alignment without the risk of changing the formatting of any other paragraphs.

Document doc = new Document("input.doc");
 
DocumentBuilder builder = new DocumentBuilder(doc);
            
Paragraph tempPara = builder.InsertParagraph();            
tempPara.ParagraphFormat.ClearFormatting();
tempPara.ParagraphFormat.StyleIdentifier = StyleIdentifier.Normal;
            
String html = "<p>aslkdj alkj dsakjsd</p>\n" +
    "<p>asdlka shkjdahksld halkjsd halkjshdkjahsdkjahsjd &nbsp;asdh ajsdha sgda sdahs dasd la sdahsld alsdasldka hsdk ashd asdlgajksdgaljsdgasd ajkshdasd kajhs djasdashdkj ahsjdk ahskjdha skljdha dlha ksjdh kjh lkhlk hlkjhlk hkh kjh klhkl hkj haskdjahskjdhajksdhasd oi jhkjl1h23k12 312 312 3 12 312 3 12 &nbsp;31 23 1 23 12 3 12 3 12 3123 12 3123 12 312 3 jqwh jakhs djkahds kajs hdkahsdak hs asd asd adsads ad</p>";
 
builder.InsertHtml(html);

tempPara.Remove();

Thanks,

Any news about this issue? It seems to happen in recent version also…

P.S. - My client is waiting to buy a new Aspose subscription when these kind of bugs are corrected

Hi Paulo,

Thanks for the inquiry. Unfortunately, your issues is pending for analysis. Once our developers analyze this issue, we will be able to provide you an estimate. You will be notify as soon as it is fixed. Sorry for inconvenience.

Hi Paulo,
Thanks for your inquiry.
For the time being I’ve found you can work around the issue by first clearing ParagraphFormatting.

String html = "<p>aslkdj alkj dsakjsd</p>\n" + 
  "<p>asdlka shkjdahksld halkjsd halkjshdkjahsdkjahsjd &nbsp;asdh ajsdha sgda sdahs dasd la sdahsld alsdasldka hsdk ashd asdlgajksdgaljsdgasd ajkshdasd kajhs djasdashdkj ahsjdk ahskjdha skljdha dlha ksjdh kjh lkhlk hlkjhlk hkh kjh klhkl hkj haskdjahskjdhajksdhasd oi jhkjl1h23k12 312 312 3 12 312 3 12 &nbsp;31 23 1 23 12 3 12 3 12 3123 12 3123 12 312 3 jqwh jakhs djkahds kajs hdkahsdak hs asd asd adsads ad</p>";

builder.ParagraphFormat.ClearFormatting();

builder.InsertHtml(html);

Thanks,

The issues you have found earlier (filed as WORDSNET-4257) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.