Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.
Fri Sep 02, 2022 12:55 pm
Dears,
kindly i need to remove spacing inside table
- Code: Select all
MainCLass
import java.util.HashMap;
import java.util.Map;
public class Main2 {
public static void main(String[] args)
{
String filename="H.docx";
String newFileName="H1.docx";
Map<String, String> map = new HashMap<String, String>();
map.put("${Name}", "SHADY WARDY");
map.put("${Phone}", "01154000641");
map.put("${Email}", "[email protected]");
map.put("${Address}", "37 AboElFadel St - Shoubra Masr - Cairo");
map.put("${linkedin}","Linkedin.com");
map.put("${ Mother_tongue}","Arabic");
map.put("${Second_Languages}","English");
map.put("${degree}","Civil Engineering");
map.put("${Faculty}","Cairo Engineering");
map.put("${Year}","2013");
map.put("${major}","Civil");
map.put("${Project_Name}","Constraction Project Mangmengt");
map.put("${Project_Grade}","Excellent");
map.put("${General grade}","Very Good");
map.put("${Certificate_1}","PMPFWEJDFBJKWBFJK");
String book_mark_number="table_bookmark_1";
String table_holder_number="${table_holder_1}";
String Position="Senior Civil Engineer";
String Project="New Telecom Tower";
String Company="ZTE";
String Scope=
"\u2023 "+"Hand over 1300 TK sites\n" +
"\u2023 "+"Project Cost Control\n" +
"\u2023 "+"Quality Control in all project phases\n" +
"\u2023 "+"Project planning regarding Sub Contractors resources and site requirements and mange site material\n" +
"\u2023 "+"Control site acceptance and billing\n" +
"\u2023 "+"Clearness & Acceptance for Backlog and Problematic Sites\n" +
"\u2023 "+"Coordinate between all project Stakeholder\n" +
"\u2023 "+"Interface with Customer to full fill their requirements and get needed approvals.\n" +
"\u2023 "+"Follow with the consultant for issues that needs modification\n" +
"\u2023 "+"Review and Audit Site folder Element ( As-built , Consultant Report , Consultant Certificate , Material Certificate , Test report & Certificates… )\n";
new ExperianceGenerate(filename,newFileName,book_mark_number,table_holder_number,Position,Project,Company,Scope);
//new WordTemplateCreator().findReplaceWord(newFileName,newFileName,map);
//new WordTemplateCreator().findReplaceImage(newFileName,newFileName,"website.jpg");
}
}
Experince Class
- Code: Select all
Class ExperianceGenerate
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.TextRange;
public class ExperianceGenerate {
ExperianceGenerate(String filename, String newfilename,
String book_mark_number, String table_holder_number,
String Position, String Project, String Company , String Scope){
Document doc = new Document();
doc.loadFromFile(filename);
//Find the keyword. it may like this "${table_holder}".
//here I just use the whole paragraph for example.
TextSelection selection= doc.findString(table_holder_number,false,true);
//Get the TextRange of the key word
TextRange range=selection.getAsOneRange();
//Get the owner paragraph of the key word
Paragraph owner=range.getOwnerParagraph();
//Get the index of this range in the owner paragraph
int index=owner.getChildObjects().indexOf(range);
//Insert a temporary bookmark to include the key word
BookmarkStart start=new BookmarkStart(doc,book_mark_number);
BookmarkEnd end=new BookmarkEnd(doc,book_mark_number);
owner.getChildObjects().insert(index,start);
owner.getChildObjects().insert(index+2,end);
//init a new table object
Table table=new Table(doc,true);
//reset rows and columns
table.resetCells(5,2);
//put some values in the table
table.get(0,0).addParagraph().appendText("Position:").getCharacterFormat().setBold(true);
table.get(1,0).addParagraph().appendText("Project:").getCharacterFormat().setBold(true);
table.get(2,0).addParagraph().appendText("Company:").getCharacterFormat().setBold(true);
table.get(3,0).addParagraph().appendText("Scope:").getCharacterFormat().setBold(true);
table.get(0,1).addParagraph().appendText(Position).getCharacterFormat().setBold(false);
table.get(1,1).addParagraph().appendText(Project ).getCharacterFormat().setBold(false);
table.get(2,1).addParagraph().appendText(Company ).getCharacterFormat().setBold(false);
table.get(4,1).addParagraph().appendText(Scope ).getCharacterFormat().setBold(false);
table.applyHorizontalMerge(3,0,1);
table.applyHorizontalMerge(4,0,1);
table.get(0,0).setCellWidth(5,CellWidthType.Percentage);
//init a TextBodyPart object
TextBodyPart body=new TextBodyPart(doc);
//add the table to the body
body.getBodyItems().add(table);
//Find the bookmark by name
BookmarksNavigator navigator=new BookmarksNavigator(doc);
navigator.moveToBookmark(book_mark_number);
//Replace all content in the bookmark with the new body
navigator.replaceBookmarkContent(body);
// If you need, remove the bookmark after replacemenet
//doc.getBookmarks().remove(navigator.getCurrentBookmark());
Paragraph paragraph= navigator.getCurrentBookmark().getBookmarkEnd().getOwnerParagraph();
DocumentObject parent=paragraph.getOwner();
parent.getChildObjects().remove(paragraph);
//Save the file
doc.saveToFile(newfilename, FileFormat.Docx);
}
}
Login to view the files attached to this post.
-

shadywardy
-
- Posts: 19
- Joined: Thu Aug 18, 2022 2:25 pm
Mon Sep 05, 2022 11:32 am
Hello,
Thanks for your inquiry.
For your requirement, you can refer to the following code snippet, and I put the complete code below.
code snippet:
- Code: Select all
//Travel each row of table
for (int i = 0; i < table.getRows().getCount(); i++) {
//Travel each cell of row
for (int j = 0; j < table.getRows().get(i).getCells().getCount(); j++) {
//Travel each child object of cell
for (int k = 0; k < table.getRows().get(i).getCells().get(j).getChildObjects().getCount(); k++) {
//Gets the paragraph in the cell
if (table.getRows().get(i).getCells().get(j).getChildObjects().get(k) instanceof Paragraph) {
Paragraph para = (Paragraph) table.getRows().get(i).getCells().get(j).getChildObjects().get(k);
//Set the interval after paragraph to 0
para.getFormat().setAfterSpacing(0f);
}
}
}
}
complete code:(ExperianceGenerate.java)
- Code: Select all
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.TextRange;
import com.spire.pdf.graphics.PdfGraphicsUnit;
import com.spire.pdf.graphics.PdfUnitConvertor;
public class ExperianceGenerate {
ExperianceGenerate(String filename, String newfilename,
String book_mark_number, String table_holder_number,
String Position, String Project, String Company, String Scope) {
Document doc = new Document();
doc.loadFromFile(filename);
//Find the keyword. it may like this "${table_holder}".
//here I just use the whole paragraph for example.
TextSelection selection = doc.findString(table_holder_number, false, true);
//Get the TextRange of the key word
TextRange range = selection.getAsOneRange();
//Get the owner paragraph of the key word
Paragraph owner = range.getOwnerParagraph();
//Get the index of this range in the owner paragraph
int index = owner.getChildObjects().indexOf(range);
//Insert a temporary bookmark to include the key word
BookmarkStart start = new BookmarkStart(doc, book_mark_number);
BookmarkEnd end = new BookmarkEnd(doc, book_mark_number);
owner.getChildObjects().insert(index, start);
owner.getChildObjects().insert(index + 2, end);
//init a new table object
Table table = new Table(doc, true);
//reset rows and columns
table.resetCells(5, 2);
//put some values in the table
table.get(0, 0).addParagraph().appendText("Position:").getCharacterFormat().setBold(true);
table.get(1, 0).addParagraph().appendText("Project:").getCharacterFormat().setBold(true);
table.get(2, 0).addParagraph().appendText("Company:").getCharacterFormat().setBold(true);
table.get(3, 0).addParagraph().appendText("Scope:").getCharacterFormat().setBold(true);
table.get(0, 1).addParagraph().appendText(Position).getCharacterFormat().setBold(false);
table.get(1, 1).addParagraph().appendText(Project).getCharacterFormat().setBold(false);
table.get(2, 1).addParagraph().appendText(Company).getCharacterFormat().setBold(false);
table.get(4, 1).addParagraph().appendText(Scope).getCharacterFormat().setBold(false);
table.applyHorizontalMerge(3, 0, 1);
table.applyHorizontalMerge(4, 0, 1);
table.get(0, 0).setCellWidth(5, CellWidthType.Percentage);
//Set the width of table
table.setPreferredWidth(new PreferredWidth(WidthType.Percentage, (short) 77));
//Set left indent of table
PdfUnitConvertor unitConvertor = new PdfUnitConvertor();
float valueInPixel = unitConvertor.convertUnits(6, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
table.getTableFormat().setLeftIndent(valueInPixel);
//Travel each row of table
for (int i = 0; i < table.getRows().getCount(); i++) {
//Travel each cell of row
for (int j = 0; j < table.getRows().get(i).getCells().getCount(); j++) {
//Travel each child object of cell
for (int k = 0; k < table.getRows().get(i).getCells().get(j).getChildObjects().getCount(); k++) {
//Gets the paragraph in the cell
if (table.getRows().get(i).getCells().get(j).getChildObjects().get(k) instanceof Paragraph) {
Paragraph para = (Paragraph) table.getRows().get(i).getCells().get(j).getChildObjects().get(k);
//Set the interval after paragraph to 0
para.getFormat().setAfterSpacing(0f);
}
}
}
}
//init a TextBodyPart object
TextBodyPart body=new TextBodyPart(doc);
//add the table to the body
body.getBodyItems().add(table);
//Find the bookmark by name
BookmarksNavigator navigator=new BookmarksNavigator(doc);
navigator.moveToBookmark(book_mark_number);
//Replace all content in the bookmark with the new body
navigator.replaceBookmarkContent(body);
// If you need, remove the bookmark after replacemenet
//doc.getBookmarks().remove(navigator.getCurrentBookmark());
Paragraph paragraph= navigator.getCurrentBookmark().getBookmarkEnd().getOwnerParagraph();
DocumentObject parent=paragraph.getOwner();
parent.getChildObjects().remove(paragraph);
//Save the file
doc.saveToFile(newfilename, FileFormat.Docx);
Sincerely
Abel
E-iceblue support team
-


Abel.He
-
- Posts: 1010
- Joined: Tue Mar 08, 2022 2:02 am