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.

Sat Mar 30, 2024 8:38 pm

hi
i want to draw a table in word by using spire.doc 12.2.0 but the tutorial code disent draw borders
wahts the problem?

code:

Code: Select all
import math
from spire.doc import *
from spire.doc.common import *
# License.SetLicenseKey("license.elic.xml")

# Create a Document object
document = Document()

# Add a section
section = doc.AddSection()

# Create a table
table = Table(doc)

# Set the width of table
table.PreferredWidth = PreferredWidth(WidthType.Percentage, int(100))

# Set the border of table
table.TableFormat.Borders.BorderType = BorderStyle.Single
table.TableFormat.Borders.Color = Color.get_Black()

# Add a row
row = table.AddRow(False, 3)
row.Height = 20.0

# Add data to the cells
cell = row.Cells[0]
cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle
paragraph = cell.AddParagraph()
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center
paragraph.AppendText("Row 1, Col 1")
cell = row.Cells[1]
cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle
paragraph = cell.AddParagraph()
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center
paragraph.AppendText("Row 1, Col 2")
cell = row.Cells[2]
cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle
paragraph = cell.AddParagraph()
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center
paragraph.AppendText("Row 1, Col 3")

# Add the second row
row = table.AddRow(False, 3)
row.Height = 20.0
cell = row.Cells[0]
cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle
paragraph = cell.AddParagraph()
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center
paragraph.AppendText("Row 2, Col 1")
cell = row.Cells[1]
cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle
paragraph = cell.AddParagraph()
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center
paragraph.AppendText("Row 2, Col 2")
cell = row.Cells[2]
cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle
paragraph = cell.AddParagraph()
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center
paragraph.AppendText("Row 2, Col 3")

# Add the table to the section
section.Tables.Add(table)

# Save the document
doc.SaveToFile("output/CreateTable.docx", FileFormat.Docx2013)
doc.Close()


Image

XxLonely
 
Posts: 19
Joined: Sat Mar 30, 2024 5:11 pm

Mon Apr 01, 2024 2:46 am

Hi,

Thanks for your inquiry.
Please set the second parameter to true to show the table border when initialize Table object, as the following code snippet:
Code: Select all
table = Table(doc,True)

In addition, I found a error in the code you provided, as shown in following screenshot:
error.png


Sincerely
Abel
E-iceblue support team
User avatar

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

Mon Apr 01, 2024 9:20 am

Thank you very much for the advice
please correct the tutorial the isn't table = Table(doc,True) with True parameter
:)
regards

XxLonely
 
Posts: 19
Joined: Sat Mar 30, 2024 5:11 pm

Mon Apr 01, 2024 9:52 am

Hi,

Thanks for your advice.
I have asked the relevant personnel to revise it.

Sincerely
Abel
E-iceblue support team
User avatar

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

Return to Spire.Doc