Hi Sonam,
Thanks for your inquiry. I have executed the following code using Aspose.Words for Java 14.5.0 and produced “out-awjava-14.5.0.docx” from your “InputWordFile±-+11thJune.docx” (see attached “out-awjava-14.5.0.docx”). I could not see any issue with the attached output document, could you please clarify where the issue is?
Document doc = new Document(getMyDir() + "InputWordFile±-+11thJune.docx");
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-awjava-14.5.0.docx");
Best regards,
Hi Awais,
The above code is working when I am running it as standalone java code.But its showing unusual output as I incorporate this with my below code.Please assist.
Document doc = new Document(InputFile);
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);
level1.setTrailingCharacter(ListTrailingCharacter.TAB);
para.getParagraphFormat().setSpaceAfter(k);
doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat().setLeftIndent(0.13 * 72);
doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat().setFirstLineIndent(-0.13 * 72);
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);
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.getFirstSection().getBody().getFirstParagraph().getParagraphFormat().setLeftIndent(0.13 * 72);
doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat().setFirstLineIndent(-0.13 * 72);
}
}
doc.save(OutputFile);
Thanks
Sonam
Hi Awais,
With the above code…I am generating the OutputFile as attached.
InputHtml file ia attached too.
As I am integrating yout given code in my code.the output got unusual.However your code is working as standalone code.
I have embedded your piece of code at every step.Please have a look and kindly suggest where can i place your piece of code so that only hanging indent at any line is handled and will not affect other functionalities.
Thanks
Waiting for soon reply.
Regards
Sonam
Hi Sonam,
Thanks for your inquiry. I am afraid, I couldn’t understand what you are trying to achieve with this code. Could you please provide comments in your code describing the purpose of these code lines? This will definitely help us get to the bottom of your requirement.
Best regards,
Hi Awais,
Hope the below code helps.
Document doc = new Document(InputFile); // get the document
DocumentBuilder builder1 = new DocumentBuilder(doc);
int count = 0; //initialize counter count which will which will be incremented and if the left indent is same as the first line it will again be set to zero as in below code.
int i = 0; //initialize i that is counter for setting number position.
int j = 10; //initialize j that is counter for setting tab position.
double k = 4.26; //initialize k that is counter for setting space this is decremented.
double lf = 0.0; // initialize as 0 and will get the left indent just print the left indent value.
double leftIndent = 0.0; // this is first line left indent value
for (Paragraph para : (Iterable)doc.getChildNodes(NodeType.PARAGRAPH, true))
{
if (para.isListItem())
{
//This if will check if the paragraph left indent is same as first line left indent if yes,then it will initialize the count ,i,j and k.
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 is equal to one. make the settings as below.
if (COUNT.equalsIgnoreCase(ONE))
{
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);
level1.setTrailingCharacter(ListTrailingCharacter.TAB);
para.getParagraphFormat().setSpaceAfter(k);
doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat().setLeftIndent(0.13 * 72);
doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat().setFirstLineIndent(-0.13 * 72); //handle first line hanging indent.
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);
level0.setTrailingCharacter(ListTrailingCharacter.TAB);
para.getParagraphFormat().setSpaceAfter(k);
System.out.println(para.getText());
lf = para.getParagraphFormat().getLeftIndent();
System.out.println(lf); //just printing the leftindent of paragraph.
System.out.println("");
i += 11;
j += 11;
if (k != 0.48)
{
k -= 1.26;
}
}
}
//if paragraph is not list item even then handle the spacing and hanging indent.
else
{
para.getParagraphFormat().setSpaceAfter(5.52);
doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat().setLeftIndent(0.13 * 72);
doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat().setFirstLineIndent(-0.13 * 72);
}
}
doc.save(OutputFile);//save the doc
Waiting for soon reply.
Thanks
Sonam Arora
Hi Awais,
Any update ?
Revert if you have any concerns.
Thanks
Sonam
Hi Awais,
One more query I have here.The below code provide by you is handling the hanging indents only so when added the level1.setTabPosition(indent * 20); in that code to handle spacing between bullet and text,level1.setTabPosition(indent * 20) got unaffected.
Document doc = new Document("D://Test//InputWordFile – 11thJune.docx");
for (int n = 0; n < doc.getLists().getCount(); n++)
{
List list = doc.getLists().get(n);
double indent = 0;
double firstlineleftindent = 0.0;
for (Paragraph para : (Iterable)doc.getChildNodes(NodeType.PARAGRAPH, true))
{
if (para.isListItem() && para.getListFormat().getList() == list)
{
List list1 = para.getListFormat().getList();
ListLevel level1 = list1.getListLevels().get(para.getListFormat().getListLevelNumber());
para.getParagraphFormat().setLeftIndent(indent * 72);
indent += 0.12;
level1.setTabPosition(indent * 20); // Added to handle the spacing between text and bullet.
}
}
}
doc.save("D://Test//OutputWord-aspose.docx");
Seems level1.setTabPosition(); and para.getParagraphFormat().setLeftIndent(indent * 72); are contraditory ,hence cannot be used together.So,How can I use : para.getParagraphFormat().setLeftIndent(indent * 72); without affecting the functionality craeted by settabposition method??
Appreciate your kind help here.
Thanks
Regards
Sonam Arora
Hi Sonam,
Thanks for the additional information. We are checking your scenarios and will get back to you soon.
Best regards,
Hi Awais,
I have optimize my code below.This will help u alot.
Document doc = new Document(fileNameandPath);
DocumentBuilder builder = new DocumentBuilder(doc);
double k = 0;
double lf = 0.0;
int levelnumber;
for (Paragraph para : (Iterable)doc.getChildNodes(NodeType.PARAGRAPH , true))
{
if(para.isListItem())
{
List list0 = para.getListFormat().getList();
ListLevel level0 = list0.getListLevels().get(para.getListFormat().getListLevelNumber());
levelnumber = para.getListFormat().getListLevelNumber(); //get level number
System.out.println("Level Number " +levelnumber); //prints level number
level0.setNumberPosition(levelnumber11); //Returns and sets the number position according to list level number
level0.setTabPosition(10+(levelnumber11)); // Returns or sets the tab position (in points) for the list level according to list level number
level0.setTrailingCharacter(ListTrailingCharacter.TAB);
if (k!=0.47999999999999954){
k = 4.26 - (levelnumber1.26);
}
para.getParagraphFormat().setSpaceAfter(k);//ste spacing acording to list level number
System.out.println(para.getText()); ///prints the text of bullet level.
lf = para.getParagraphFormat().getLeftIndent(); // gets the left indent value
System.out.println(lf); //print the left indent value
System.out.println("Level Number " +levelnumber);
}else{
// para.getParagraphFormat().setLeftIndent(lf);
doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat().setLeftIndent(0.13 * 72);
doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat().setFirstLineIndent(-0.13 * 72);
para.getParagraphFormat().setSpaceAfter(5.52);
}
}
doc.save(DestinantionFilePath);
Attached are the input and output Actual WordFiles.
Please suggest how to achieve the hanging indents in other lines with first line as well.As i incorporate below code.other indenst got affected.How to achieve the hanging indents without affecting the spacing ?
Below is the code given by you to achieve hanging indents which is affecting the spacing…
Document doc = new Document(getMyDir() + "InputWordFile±-+11thJune.docx");
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-awjava-14.5.0.docx");
Thanks
Sonam Arora
Hi Sonam,
Thanks for simplifying your code. Please see the following changes to achieve this:
Document doc = new Document(getMyDir() + "InputWordFile±+16June.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
double k = 0;
double lf = 0.0;
int levelnumber;
for (Paragraph para : (Iterable<Paragraph>)doc.getChildNodes(NodeType.PARAGRAPH, true)) {
if (para.isListItem()) {
List list0 = para.getListFormat().getList();
ListLevel level0 = list0.getListLevels().get(para.getListFormat().getListLevelNumber());
levelnumber = para.getListFormat().getListLevelNumber(); //get level number*
System.out.println("Level Number " + levelnumber); //prints level number*
level0.setNumberPosition(levelnumber * 11); //Returns and sets the number position according to list level number*
level0.setTabPosition(10 + (levelnumber * 11)); // Returns or sets the tab position (in points) for the list level according to list level number*
level0.setTrailingCharacter(ListTrailingCharacter.TAB);
level0.setTextPosition(level0.getTabPosition());
if (k != 0.47999999999999954) {
k = 4.26 - (levelnumber * 1.26);
}
para.getParagraphFormat().setSpaceAfter(k);//ste spacing acording to list level number
System.out.println(para.getText()); ///prints the text of bullet level.
lf = para.getParagraphFormat().getLeftIndent(); // gets the left indent value
System.out.println(lf); //print the left indent value
System.out.println("Level Number " + levelnumber);
} else {
// para.getParagraphFormat().setLeftIndent(lf);
// doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat().setLeftIndent(0.13 * 72);
// doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat().setFirstLineIndent(-0.13 * 72);
// para.getParagraphFormat().setSpaceAfter(5.52);
}
}
doc.save(getMyDir() + "out.docx");
I hope, this helps.
Best regards,
Hi Awais,
Thanks a ton for your kind help.This is working fine.
I will get back for any other queries.
Regards
Sonam Arora
Hi Awais,
With the start of this thread I confirmed that we are doing the below steps for our project.
-
Taking default Word
Empty document from project workspace.
-
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);
- Inserting htmlInput to this asposeDoc using below code.
builder.insertHtml(Inputhtml);
- Saving builder.getdocument to designated path using below code :
builder.getDocument().save(designatedPath);
- Then applying the indentation to bullets and text spacing.
Now ,here I am facing the below issue:
When I am using the arrow tags from my application.The HTML file generated has style tag as : <ul style="list-style-image: url(…/ckeditor/plugins/arrow.jpg)">
Attached the input html file generated.The Word document generated from this after inserting this html to builder document contains disc circle and square tags randomly.
The expected word document should have arrow bullets as they are in html file. Attached the expected word doc too.
*
Please have look into this issue that when arrow/hypen tags are there in html.This html when inserted to empty word document created using aspose.The word document doesnot take hypen or arrow tags.
Kindly assist.
Thanks
Sonam
Hey Awais,
One more query I have ,this is related to indentation side.
The below code of mine is workign fine with disc circle and bullet tags.whenever I add arrow diamond or check tags the spacing got affected attached the Input Wordfile,ActualOutput And expected Output.Please assist.
Abnormal Cases are :
Arrow tag and text spacing is reduced from normal.
Diamond tag and text spacing is increased from normal.
Check tag and text spacing is reduced from normal.
Document doc = new Document(getMyDir() + "InputWordFile±+16June.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
double k = 0;
double lf = 0.0;
int levelnumber;
for (Paragraph para : (Iterable<Paragraph>)doc.getChildNodes(NodeType.PARAGRAPH, true)) {
if (para.isListItem()) {
List list0 = para.getListFormat().getList();
ListLevel level0 = list0.getListLevels().get(para.getListFormat().getListLevelNumber());
levelnumber = para.getListFormat().getListLevelNumber(); //get level number*
System.out.println("Level Number " + levelnumber); //prints level number*
level0.setNumberPosition(levelnumber * 11); //Returns and sets the number position according to list level number*
level0.setTabPosition(10 + (levelnumber * 11)); // Returns or sets the tab position (in points) for the list level according to list level number*
level0.setTrailingCharacter(ListTrailingCharacter.TAB);
level0.setTextPosition(level0.getTabPosition());
if (k != 0.47999999999999954) {
k = 4.26 - (levelnumber * 1.26);
}
para.getParagraphFormat().setSpaceAfter(k);//ste spacing acording to list level number*
System.out.println(para.getText()); ///prints the text of bullet level.*
lf = para.getParagraphFormat().getLeftIndent(); // gets the left indent value*
System.out.println(lf); //print the left indent value*
System.out.println("Level Number " + levelnumber);
} else {
}
}
doc.save(getMyDir() + "out.docx");
Thanks
Sonam
Hi Sonam,
Thanks for your inquiry. I am checking with these scenarios and will get back to you soon.
Best regards,
Hi Sonam,
Thanks for your inquiry. Regarding your query you posted here, I am afraid, I couldn’t reproduce this issue on my side. Please make sure the path to bullet image (i.e. …/ckeditor/plugins/arrow.jpg) is correct. I have used the following code for testing:
Document doc = new Document(getMyDir() + "in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
byte[] encoded = Files.readAllBytes(Paths.get(getMyDir() + "HtmlInputFile±+18thJune.html"));
String html = Charset.forName("UTF8").decode(ByteBuffer.wrap(encoded)).toString();
builder.insertHtml(html);
doc.save(getMyDir() + "out.docx");
In case the problem still remains, please share a simple piece of code to reproduce this issue on our end.
Best regards,
Hi Sonam,
Regarding your query here, it seems you just need to specify a higher value in the following line of code:
level0.setTabPosition(17 + (levelnumber * 11)); // Returns or sets the tab position (in points) for the list level according to list level number
I hope, this helps.
Best regards,
Hi Awais,
Thanks for update.However , Changing the level.setTabPosition from :
Level0.setTabPosition(10 + (levelnumber * 11));
to
level0.setTabPosition(17 + (levelnumber * 11));
is not producing the desired output.Attached the ActualOutput file.
Thanks
Sonam