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.

Tue Dec 21, 2021 10:03 am

Hi Team

Please let me know is it possible to extract the inbuilt table style to create new table with same style.

I have written code mentioned below it is not working.

if(documentObject.getDocumentObjectType()==DocumentObjectType.Text_Range){
TextRange textRange = (TextRange) documentObject;
cellTextRed = textRange.getCharacterFormat().getTextColor().getRed();
cellTextGreen = textRange.getCharacterFormat().getTextColor().getGreen();
cellTextBlue = textRange.getCharacterFormat().getTextColor().getBlue();
fontSize = textRange.getCharacterFormat().getFontSize(); // Font Size
fontFamily = textRange.getCharacterFormat().getFontName();
isBold = textRange.getCharacterFormat().getBold();
}
}
cellProps.append("<td ");
cellProps.append("style=\"");
cellProps.append("border:1px solid black;");
cellProps.append("width:" + cellWidth + "px;");
cellProps.append("height:" + cellHeight + "px;");
cellProps.append("background-color:rgb(" + cellBgRed + "," + cellBgGreen + "," + cellBgBlue + ");");
cellProps.append("color:rgb(" + cellTextRed + "," + cellTextGreen + "," + cellTextBlue + ");");
cellProps.append("font-size:" + fontSize + ";");
cellProps.append("font-family:'" + fontFamily + "';");

if(isBold) {
cellProps.append("font-weight:bold;");
}
cellProps.append("padding:" + vPadding + "px " + hPadding + "px" + ";");
cellProps.append("\" ");
cellProps.append("align=\"" + hAlign + "\" ");
cellProps.append("valign=\"" + vAlign + "\" ");


Thanks in advance

pr20080798
 
Posts: 146
Joined: Wed Jan 20, 2021 1:15 pm

Wed Dec 22, 2021 3:41 am

Hello,

Thank you for your inquiry.
According to your description, I suggest you clone the table directly, so that the style of the new table is consistent with the style of the original table. Please refer to the code below to clone the source table into a new file.
Code: Select all
Document document = new Document();
document.loadFromFile(inputFile);
Section section = document.getSections().get(0);
//Get the specified table
Table original_Table = section.getTables().get(0);
//Cloning of the table
Table copy_Table = original_Table.deepClone();
//Modify the copy_Table data
copy_Table.getRows().get(2).getCells().get(1).getParagraphs().get(0). setText("new Table");
//Creating a new document
Document newDocument = new Document();
Section section1 = newDocument.addSection();
//Add copied_Table in section1
section1.getTables().add(copy_Table);
newDocument.saveToFile("output.docx", FileFormat.Docx);

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1643
Joined: Wed Apr 07, 2021 2:50 am

Wed Dec 22, 2021 4:44 am

Hi Annika

Thank you for your response.

But in my application I am using html tag to create new table . Please let me know is it possible to append original table style to html tag to create new table.

Thanks in advance

pr20080798
 
Posts: 146
Joined: Wed Jan 20, 2021 1:15 pm

Wed Dec 22, 2021 8:04 am

Hello,

Thank you for your feedback.
You mentioned "use html to create a new table with the same style as the source table", I suggest you first extract the source table to a new Word file, then convert the Word file to HTML, and finally get the style of the source table by reading the tags in the HTML file. The style of the source table obtained in this way is more comprehensive and correct.
Code: Select all
Document document = new Document(); 
document.loadFromFile(sourceFile); 
Section section = document.getSections().get(0); 
//Get the specified table 
Table original_Table = section.getTables().get(0); 
//Cloning of the table 
Table copy_Table = original_Table.deepClone(); 
//Creating a new document 
Document newDocument = new Document(); 
Section section1 = newDocument.addSection(); 
//Add copied_Table in section1 
section1.getTables().add(copy_Table); 
//Save the newDocument to a html file 
newDocument.saveToFile("result.html",FileFormat.Html);
...

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1643
Joined: Wed Apr 07, 2021 2:50 am

Wed Dec 22, 2021 9:27 am

Hi Annika

Thank you.

My requirement is I have to create new table using HTML tag.

I am able to extract few styles of original table .Please let me know how can I extract the background color of text or cell.I have attached sample image for your reference(Same style should reflect in new table )

//My code
private StringBuilder htmlTable = new StringBuilder("<table style=\"border: 1px solid black;border-collapse: collapse;\">");

TextRange textRange = (TextRange) documentObject;
cellTextRed = textRange.getCharacterFormat().getTextColor().getRed();
cellTextGreen = textRange.getCharacterFormat().getTextColor().getGreen();
cellTextBlue = textRange.getCharacterFormat().getTextColor().getBlue();
fontSize = textRange.getCharacterFormat().getFontSize(); // Font Size

fontFamily = textRange.getCharacterFormat().getFontName();

isBold = textRange.getCharacterFormat().getBold();


Thanks in advance
Last edited by pr20080798 on Wed Dec 22, 2021 12:16 pm, edited 2 times in total.

pr20080798
 
Posts: 146
Joined: Wed Jan 20, 2021 1:15 pm

Wed Dec 22, 2021 10:13 am

Hello,

Thank you for your feedback.
Please use the code in the attachment to achieve your requirement.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1643
Joined: Wed Apr 07, 2021 2:50 am

Thu Dec 30, 2021 8:08 am

Hi Annika

Thank you for your response.

Unfortunately your code is not working for all the tables. I have attached sample tables screen shot .I am able to extract only 4th tables background color through your code.

pr20080798
 
Posts: 146
Joined: Wed Jan 20, 2021 1:15 pm

Thu Dec 30, 2021 10:41 am

Hello,

Thank you for your feedback.
I will investigate and report the results to you as soon as possible.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1643
Joined: Wed Apr 07, 2021 2:50 am

Fri Dec 31, 2021 8:46 am

Hello,

Thank you for your feedback.
The first three tables you provided apply the "Table Style" of MS Word. Currently, our Spire.Doc does not support obtaining the styles in the "Table Style". I have submitted this requirement to our Dev team and the ticket number is SPIREDOC-7186. Once it is achieved in the future, I will inform you in time.
In addition, please use the following code to get the text background color:
Code: Select all
Color color =textRange.getCharacterFormat().getHighlightColor();

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1643
Joined: Wed Apr 07, 2021 2:50 am

Thu Mar 31, 2022 8:12 am

Hi Team

Please let me know about this ticket SPIREDOC-7186.

Thanks

pr20080798
 
Posts: 146
Joined: Wed Jan 20, 2021 1:15 pm

Thu Mar 31, 2022 10:25 am

Hello,

Thank you for following up.
Sorry, the ticket SPIREDOC-7186 has not been implemented due to the complexity of the function. I have urged our developer to deal with it as soon as possible. Please spare us more time, if there is any update, I will inform you in time. Thank you for your understanding.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1643
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.Doc