Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Added parsing word table properties Caption and Description #745

Merged
merged 3 commits into from
Feb 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions OpenXmlFormats/Wordprocessing/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,10 @@ public class CT_TblPrBase

private CT_ShortHexNumber tblLookField;

private CT_String tblCaptionField;

private CT_String tblDescriptionField;

public CT_TblPrBase()
{
}
Expand Down Expand Up @@ -1216,6 +1220,10 @@ public static CT_TblPrBase Parse(XmlNode node, XmlNamespaceManager namespaceMana
ctObj.tblCellMar = CT_TblCellMar.Parse(childNode, namespaceManager);
else if (childNode.LocalName == "tblLook")
ctObj.tblLook = CT_ShortHexNumber.Parse(childNode, namespaceManager);
else if (childNode.LocalName == "tblCaption")
ctObj.tblCaption = CT_String.Parse(childNode, namespaceManager);
else if (childNode.LocalName == "tblDescription")
ctObj.tblDescription = CT_String.Parse(childNode, namespaceManager);
}
return ctObj;
}
Expand Down Expand Up @@ -1256,6 +1264,10 @@ internal void Write(StreamWriter sw, string nodeName)
this.tblCellMar.Write(sw, "tblCellMar");
if (this.tblLook != null)
this.tblLook.Write(sw, "tblLook");
if (this.tblCaption != null)
this.tblCaption.Write(sw, "tblCaption");
if (this.tblDescription != null)
this.tblDescription.Write(sw, "tblDescription");
sw.Write(string.Format("</w:{0}>", nodeName));
}

Expand Down Expand Up @@ -1454,6 +1466,32 @@ public CT_ShortHexNumber tblLook
}
}

[XmlElement(Order = 15)]
public CT_String tblCaption
{
get
{
return this.tblCaptionField;
}
set
{
this.tblCaptionField = value;
}
}

[XmlElement(Order = 16)]
public CT_String tblDescription
{
get
{
return this.tblDescriptionField;
}
set
{
this.tblDescriptionField = value;
}
}

public bool IsSetTblW()
{
return this.tblW != null;
Expand Down Expand Up @@ -2776,6 +2814,10 @@ public class CT_TblPr : CT_TblPrBase
ctObj.tblCellMar = CT_TblCellMar.Parse(childNode, namespaceManager);
else if (childNode.LocalName == "tblLook")
ctObj.tblLook = CT_ShortHexNumber.Parse(childNode, namespaceManager);
else if (childNode.LocalName == "tblCaption")
ctObj.tblCaption = CT_String.Parse(childNode, namespaceManager);
else if (childNode.LocalName == "tblDescription")
ctObj.tblDescription = CT_String.Parse(childNode, namespaceManager);
}
return ctObj;
}
Expand Down Expand Up @@ -2818,6 +2860,10 @@ public class CT_TblPr : CT_TblPrBase
this.tblCellMar.Write(sw, "tblCellMar");
if (this.tblLook != null)
this.tblLook.Write(sw, "tblLook");
if (this.tblCaption != null)
this.tblCaption.Write(sw, "tblCaption");
if (this.tblDescription != null)
this.tblDescription.Write(sw, "tblDescription");
sw.Write(string.Format("</w:{0}>", nodeName));
}

Expand Down
53 changes: 53 additions & 0 deletions ooxml/XWPF/Usermodel/XWPFTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,58 @@ public int CellMarginRight
}
}

public string TableCaption
{
get
{
CT_TblPr tblPr = GetTrPr();
if (tblPr.tblCaption != null)
return tblPr.tblCaption.val;
else
return string.Empty;
}
set
{
CT_TblPr tblPr = GetTrPr();
if (tblPr.tblCaption == null)
{
CT_String caption = new CT_String();
caption.val = value;
tblPr.tblCaption = caption;
}
else
{
tblPr.tblCaption.val = value;
}
}
}

public string TableDescription
{
get
{
CT_TblPr tblPr = GetTrPr();
if (tblPr.tblDescription != null)
return tblPr.tblDescription.val;
else
return string.Empty;
}
set
{
CT_TblPr tblPr = GetTrPr();
if (tblPr.tblDescription == null)
{
CT_String desc = new CT_String();
desc.val = value;
tblPr.tblDescription = desc;
}
else
{
tblPr.tblDescription.val = value;
}
}
}

public void SetCellMargins(int top, int left, int bottom, int right)
{
CT_TblPr tblPr = GetTrPr();
Expand Down Expand Up @@ -807,6 +859,7 @@ public XWPFTableRow GetRow(CT_Row row)
}
return null;
}

}// end class

}
49 changes: 49 additions & 0 deletions testcases/ooxml/XWPF/UserModel/TestXWPFTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ public void TestSetGetMargins()
int r = table.CellMarginRight;
Assert.AreEqual(450, r);
}

[Test]
public void TestSetGetHBorders()
{
Expand Down Expand Up @@ -266,5 +267,53 @@ public void TestCreateTable()
}
doc.Package.Revert();
}

[Test]
public void TestSetTableCaption()
{
// create a table
XWPFDocument doc = new XWPFDocument();
CT_Tbl ctTable = new CT_Tbl();
XWPFTable table = new XWPFTable(ctTable, doc);

//Set Caption
table.TableCaption = "%TABLECAPTION%";

//Check
CT_String tblCaption = table.GetCTTbl().tblPr.tblCaption;
Assert.IsNotNull(tblCaption);
Assert.AreEqual("%TABLECAPTION%", tblCaption.val);
}

[Test]
public void TestSetTableDescription()
{
// create a table
XWPFDocument doc = new XWPFDocument();
CT_Tbl ctTable = new CT_Tbl();
XWPFTable table = new XWPFTable(ctTable, doc);

//Set Description
table.TableDescription = "%TABLEDESCRIPTION%";

//Check
CT_String tblDesc = table.GetCTTbl().tblPr.tblDescription;
Assert.IsNotNull(tblDesc);
Assert.AreEqual("%TABLEDESCRIPTION%", tblDesc.val);
}

[Test]
public void TestReadTableCaptionAndDescription()
{
// open an document with table containing Table caption and Table Description
XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("table_properties.docx");

//Get Table
var table = doc.Tables[0];

//Assert Table Caption & Description
Assert.AreEqual("Table Title", table.TableCaption);
Assert.AreEqual("Table Description", table.TableDescription);
}
}
}
Binary file not shown.