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

Free Support Forum - aspose.com

Issue with html input inserting itno Worddoc and editing Worddoc

Hi Team,

For my project ,I am taking the word document(html) and converting it into pdf.Steps that we are following are :


  1. Taking default Word Empty document from project workspace.

  2. Creating builder of this word file using aspose code :

final com.aspose.words.Document asposeDoc = new com.aspose.words.Document(defaultWordFile);
final DocumentBuilder builder = new DocumentBuilder(asposeDoc);
  1. Inserting htmlInput to this asposeDoc using below code.
    builder.insertHtml(Inputhtml);

  2. Saving builder.getdocument to designated path using below code :
    builder.getDocument().save(designatedPath);

  3. Applying indentation formatting to Word document in designated path to correct bullet spacing and text spacing and finally saving the Word file again there .The code (correctindentationusingaspose) is given below.

  4. The word file is finally converted to PDF using java Method.

The issue that I am facing here is as follows:

  1. Inputhtml file has to be in the format as attached.I want it should able to work with Expected htmlInput.

  2. If I am giving the ExpectedhtmlInput to asposeDoc.The correct indentation code is not working on that.

Below is correct indentation code I am using:

private Input Stream correctindentationusingaspose(final String designatedPath) throws Exception {
    Document doc = new Document(designatedPath);
    DocumentBuilder builder = new DocumentBuilder(designatedPath);
    int count = 0;
    int i = 0;
    int j = 8;
    for (Paragraph para : (Iterable)doc.getChildNodes(NodeType.PARAGRAPH , true))
    {
        if(para.isListItem())
        {
            count++;
            final int one = 1;
            final String ONE = Integer.toString(one);
            final String COUNT = Integer.toString(count);
            if (COUNT.equalsIgnoreCase(ONE)){
                com.aspose.words.List list1 = para.getListFormat().getList();
                ListLevel level1 = list1.getListLevels().get(0);
                level1.setNumberPosition(i);
                level1.setTabPosition(j); level1.setTrailingCharacter(ListTrailingCharacter.TAB);
                i=i+10;
                j=j+10;
            }
            else{
                com.aspose.words.List list0 = para.getListFormat().getList();
                ListLevel level0 = list0.getListLevels().get(0);
                level0.setNumberPosition(i);
                level0.setTabPosition(j);
                level0.setTrailingCharacter(ListTrailingCharacter.TAB);
                i=i+10;
                j=j+10;
            }
        }
    }
    doc.save(designatedPath);
    InputStream stream = new FileInputStream(designatedPath);
    return stream;
}

Kindly assist here why this is happening here.What step I can modiy or add so that ExpectedHtmlInput added to asposeDoc can also be edited.

Attached are HtmlInput files,InputWordfile created that is asposeDoc and Output wordFiles for both cases.Please let me know if u nedd more info.

Appreciate for soon reply.

Regards

Thanks

Sonam Arora

Hi Team,

Any update on below?

Thanks

Sonam Arora

Hi Sonam,

Thanks for your inquiry. We are checking with this scenario and will get back to you soon.

Bets regards,

Hi Awais,

Any Update…!

Thanks

Sonam Arora

Hi Team,

Waiting for your resposne.

Thanks

Sonam Arora

Hi Sonam,

Thanks for your inquiry. Please spare us some more time for the analysis of these issues. We will get back to you soon.

Best regards,

Hi Awais,

Waiting for your reply.

Thank you.

Regards

Sonam

Hi Awais,

Is the normal funtionality is affected while you are checking on your side as I am Unable to view the space change between bullet and text in OutputWord file.

Please assist…

Thanks

Sonam

Change the JAR and its on track.

Awais,

Kindly revert on issue with HTML input.Waiting for soon reply.

Thanks
Sonam Arora

Hi Sonam,

Thanks for your inquiry and sorry for the delayed response. I noticed that in your code you’re applying adjustments to only 0th ListLevl, instead, please try running the following code:

Document doc = new Document(getMyDir() + "OutputWordFile±+Generated+fromExpectedHtmlInputFile±+30thMay.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
int count = 0;
int i = 0;
int j = 8;
for (Paragraph para : (Iterable<Paragraph>)doc.getChildNodes(NodeType.PARAGRAPH, true))
{
    if (para.isListItem())
    {
        count++;
        final int one = 1;
        final String ONE = Integer.toString(one);
        final String COUNT = Integer.toString(count);
        if (COUNT.equalsIgnoreCase(ONE))
        {
            com.aspose.words.List list1 = para.getListFormat().getList();
            ListLevel level1 = list1.getListLevels().get(para.getListFormat().getListLevelNumber());
            level1.setNumberPosition(i);
            level1.setTabPosition(j);
            level1.setTrailingCharacter(ListTrailingCharacter.TAB);
            i = i + 10;
            j = j + 10;
        }
        else
        {
            com.aspose.words.List list0 = para.getListFormat().getList();
            ListLevel level0 = list0.getListLevels().get(para.getListFormat().getListLevelNumber());
            level0.setNumberPosition(i);
            level0.setTabPosition(j);
            level0.setTrailingCharacter(ListTrailingCharacter.TAB);
            i = i + 10;
            j = j + 10;
        }
    }
}
doc.save(getMyDir() + "out.docx");

I hope, this helps.

Best regards,

Hi Awais,
I will try with above code and will revert if there any issues

Thanks
Sonam

Hi Awais,
The above code is working perfectly fine.Thanks for same.
Need a clarification : Is this code change getting the list format of the doc file?

Thanks
Sonam

Hi Awais,
I have one more query here.
In the same code.I am using paragraph class to iterate through each line and check if the paragraph has bullet and then adjust the spacing accordingly.If my input html is as attached and output Word file is also attached .But I want the Output Word file as ExpectedOutputFile.Ple suggest what the fix for that??

I have tried using para.hasChild(); to check if the para has further children but its returning true even if there’s no child.
Please suggest if I am going wrong anywhere.

Thanks
Sonam

Hi Sonam,

Thanks for your inquiry. First off, I would suggest you please upgrade to the latest version of Aspose.Words for Java 14.5.0 from the following link:

https://downloads.aspose.com/words/java

Secondly, please execute the following simple code to achieve this:

Document doc = new Document(getMyDir() + "HtmlInputFile±+9thJune.html");
for (int i = 0; i < doc.getLists().getCount(); i++)
{
    List list = doc.getLists().get(i);
    double indent = 0;
    for (Paragraph para : (Iterable<Paragraph>)doc.getChildNodes(NodeType.PARAGRAPH, true))
    {
        if (para.isListItem() && para.getListFormat().getList() == list)
        {
            para.getParagraphFormat().setLeftIndent(indent  72);
            indent += 0.15;
        }
    }
}
doc.save(getMyDir() + "out.docx");

I hope, this helps.

Bets regards,

Hi Awais,
I have tried the below code,its not working out.Even after upgrading to latest jar.

Document doc = new Document(getMyDir() + "HtmlInputFile±+9thJune.html");
DocumentBuilder builder1 = new DocumentBuilder(doc);

int count = 0;
int i = 0;
int j = 10;
double k = 4.26;
for (int n = 0; n < doc.getLists().getCount(); n++)
{
    com.aspose.words.List list = doc.getLists().get(n);
    double indent = 0;
    for (Paragraph para : (Iterable)doc.getChildNodes(NodeType.PARAGRAPH, true))
    {
        if (para.isListItem() && para.getListFormat().getList() == list)
        {
            count++;
            final int one = 1;
            final String ONE = Integer.toString(one);
            final String COUNT = Integer.toString(count);

            if (COUNT.equalsIgnoreCase(ONE))
            {
                com.aspose.words.List list1 = para.getListFormat().getList();
                ListLevel level1 = list1.getListLevels().get(para.getListFormat().getListLevelNumber());
                level1.setNumberPosition(i);
                level1.setTabPosition(j); // Returns or sets the tab position (in points) for the list level.
                level1.setTrailingCharacter(ListTrailingCharacter.TAB);
                para.getParagraphFormat().setSpaceAfter(k);

                i += 11;
                j += 11;
                k -= 1.26;
            }
            else
            {

                com.aspose.words.List list0 = para.getListFormat().getList();
                ListLevel level0 = list0.getListLevels().get(para.getListFormat().getListLevelNumber());

                level0.setNumberPosition(i);
                level0.setTabPosition(j); // Returns or sets the tab position (in points) for the list level.
                level0.setTrailingCharacter(ListTrailingCharacter.TAB);
                para.getParagraphFormat().setSpaceAfter(k);
                i += 11;
                j += 11;

                if (k != 0.48)
                {
                    k -= 1.26;
                }

            }
        }
        else
        {
            para.getParagraphFormat().setSpaceAfter(5.52);
        }

    }
}

doc.save(getMyDir() + "out.docx");

Thanks
Sonam

Hi Awais,
My requirement is that the below for loop should be different for two levels.Kindly assist.
for (Paragraph para : (Iterable)doc.getChildNodes(NodeType.PARAGRAPH , true))

Levels are :

  • One
  • Two
  • Three
  • Four
  • Five
  • Six
  • Seven
  • Eight
  • One
  • Two
  • Three
  • Four
  • Five
  • Six
  • Seven
  • Eight

Thanks
Sonam Arora

Hi Awais,

Thanks for your support.Well I have tried my below code.And able to achieve desired output

Document doc = new Document(fileNameandPath);
DocumentBuilder builder1 = new DocumentBuilder(doc);
int count = 0;
int i = 0;
int j = 10;
double k = 4.26;
double lf = 0.0;
double leftIndent = 0.0;

for (Paragraph para : (Iterable)doc.getChildNodes(NodeType.PARAGRAPH, true))
{
    if (para.isListItem())
    {

        if (para.getParagraphFormat().getLeftIndent() == leftIndent)
        {
            count = 0;
            i = 0;
            j = 10;
            k = 4.26;
        }
        count++;
        final int one = 1;
        final String ONE = Integer.toString(one);
        final String COUNT = Integer.toString(count);

        if (COUNT.equalsIgnoreCase(ONE))
        {
            System.out.println(para.getText());
            leftIndent = para.getParagraphFormat().getLeftIndent();
            System.out.println(leftIndent);
            System.out.println("");
            com.aspose.words.List list1 = para.getListFormat().getList();
            ListLevel level1 = list1.getListLevels().get(para.getListFormat().getListLevelNumber());
            level1.setNumberPosition(i);
            level1.setTabPosition(j); // Returns or sets the tab position (in points) for the list level.
            level1.setTrailingCharacter(ListTrailingCharacter.TAB);
            para.getParagraphFormat().setSpaceAfter(k);

            i += 11;
            j += 11;
            k -= 1.26;

        }
        else
        {

            com.aspose.words.List list0 = para.getListFormat().getList();
            ListLevel level0 = list0.getListLevels().get(para.getListFormat().getListLevelNumber());
            level0.setNumberPosition(i);
            level0.setTabPosition(j); // Returns or sets the tab position (in points) for the list level.
            level0.setTrailingCharacter(ListTrailingCharacter.TAB);
            para.getParagraphFormat().setSpaceAfter(k);
            System.out.println(para.getText());
            lf = para.getParagraphFormat().getLeftIndent();
            System.out.println(lf);
            System.out.println("");

            i += 11;
            j += 11;

            if (k != 0.48)
            {
                k -= 1.26;
            }

        }
    }
    else
    {
        para.getParagraphFormat().setSpaceAfter(5.52);
    }

}

doc.save(fileNameandPath); 

Thanks
Sonam

Hi Awais,

The above issue related to two levels have been solved now.
Thanks for same.

I have one added query.

Attached are the below files :
HtmlInput :
InputWordfile :
ActualOutputWordFile
ExpectedOutputWord :

I am using the below code :But its not keeping two lines of same paragraph on same indent.Please suggest how to achieve that?

Document doc = new Document(fileNameandPath);
DocumentBuilder builder1 = new DocumentBuilder(doc);
int count = 0;
int i = 0;
int j = 10;
double k = 4.26;
double lf = 0.0;
double leftIndent = 0.0;

for (Paragraph para : (Iterable)doc.getChildNodes(NodeType.PARAGRAPH, true))
{
    if (para.isListItem())
    {

        if (para.getParagraphFormat().getLeftIndent() == leftIndent)
        {
            count = 0;
            i = 0;
            j = 10;
            k = 4.26;
        }
        count++;
        final int one = 1;
        final String ONE = Integer.toString(one);
        final String COUNT = Integer.toString(count);

        if (COUNT.equalsIgnoreCase(ONE))
        {
            System.out.println(para.getText());
            leftIndent = para.getParagraphFormat().getLeftIndent();
            System.out.println(leftIndent);
            System.out.println("");
            com.aspose.words.List list1 = para.getListFormat().getList();
            ListLevel level1 = list1.getListLevels().get(para.getListFormat().getListLevelNumber());
            level1.setNumberPosition(i);
            level1.setTabPosition(j); // Returns or sets the tab position (in points) for the list level.
            level1.setTrailingCharacter(ListTrailingCharacter.TAB);
            para.getParagraphFormat().setSpaceAfter(k);
            i += 11;
            j += 11;
            k -= 1.26;
        }
        else
        {

            com.aspose.words.List list0 = para.getListFormat().getList();
            ListLevel level0 = list0.getListLevels().get(para.getListFormat().getListLevelNumber());
            level0.setNumberPosition(i);
            level0.setTabPosition(j); // Returns or sets the tab position (in points) for the list level.
            level0.setTrailingCharacter(ListTrailingCharacter.TAB);
            para.getParagraphFormat().setSpaceAfter(k);
            System.out.println(para.getText());
            lf = para.getParagraphFormat().getLeftIndent();
            System.out.println(lf);
            System.out.println("");

            i += 11;
            j += 11;

            if (k != 0.48)
            {
                k -= 1.26;
            }
        }
    }
    else
    {
        para.getParagraphFormat().setLeftIndent(lf);
        para.getParagraphFormat().setSpaceAfter(5.52);
    }
}
doc.save(fileNameandPath);

Thanks
Sonam

Hi Sonam,

Thanks for your inquiry. You can correct “ActualOutputWordFile–10June.docx” by simply supplying a correct “Hanging Indent” value to the first Paragraph. Please see the following code:

Document doc = new Document(getMyDir() + "ActualOutputWordFile–10June.docx");
doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat().setLeftIndent(0.13  72);
doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat().setFirstLineIndent(-0.13  72);
doc.save(getMyDir() + "out.docx");

I hope, this helps.

Best regards,

Hi Awais,

Thanks for above assistance ,this works.But this is setting the first hanging indent only.If the same happens in second line or third.Its not handling.there’s no such method like : setLineIndent(-0.13 * 72);

Attached is the Actual Output generated .

Please assist.

Thanks
Sonam Arora