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 10:46 am

Dears,
iam working on application to create CV Word automatic by some Classes
So i create Class Name "ExperianceGenerate" This class use to create tables include pervious experience
i create in origin Word Template ${table_holder_1} to replace it by table include pervious experience
but unfortunately table generate in wrong location and Wrong dimensions

Main Class

Code: Select all

import java.util.HashMap;
import java.util.Map;

public class Main2 {

    public static void main(String[] args)
    {

       

        String filename="New Microsoft Word Document.docx";
        String newFileName="testoutput.docx";

        Map<String, String> map = new HashMap<String, String>();
        map.put("${Name}", "SHADY WARDY");
        map.put("${Phone}", "01154000641");
        map.put("${Email}", "ShadyWardy@Gmail.com");
        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 "+"\tHand over 1300 TK sites\n" +
                "\u2023 "+"\tProject Cost Control\n" +
                "\u2023 "+"\tQuality Control in all project phases\n" +
                "\u2023 "+"\tProject planning regarding Sub Contractors resources and site requirements and mange site material\n" +
                "\u2023 "+"\tControl site acceptance and billing\n" +
                "\u2023 "+"\tClearness & Acceptance for Backlog and Problematic Sites\n" +
                "\u2023 "+"\tCoordinate between all project Stakeholder\n" +
                "\u2023 "+"\tInterface with Customer to full fill their requirements and get needed approvals.\n" +
                "\u2023 "+"\tFollow with the consultant for issues that needs modification\n" +
                "\u2023 "+"\tReview 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");

    }

}





Used Class ExperianceGenerate

Code: Select all
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);
}
    }



shadywardy
 
Posts: 19
Joined: Thu Aug 18, 2022 2:25 pm

Mon Sep 05, 2022 10:48 am

Hello,

Thanks for your inquiry.
For your requirement, you can refer to the following code to set the location of table. And I attached the complete code (ExperianceGenerate.java).
If you have any issue, please don’t contact us.

Code: Select all
//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);


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);


        //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
User avatar

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

Return to Spire.Doc