create dynamic table in powerpoint using openXML with c# and ASP.net - c#

I have used these links and got a working code where I can use a template report(containing placeolders) and generate new PPTX report with data I get from database. I have 4 more placeholders where I need to populate 4 different data tables. Currently I am using that template to create new slide and replacing placeholder for text but for tables I couldn't figure out. I am able to generate the table using below code but not in placeholder's location. Table always appear in center of screen.
Links used: https://blogs.msdn.microsoft.com/brian_jones/2008/11/18/creating-a-presentation-report-based-on-data/
https://code.msdn.microsoft.com/office/How-to-add-a-table-with-03578dde
Call this from some button click event:
using (PresentationDocument presentationDocument = PresentationDocument.Open(slideName, true))
{
//Get the first slide from presentation
SlidePart intitalSlide = presentationDocument.PresentationPart.SlideParts.First();
AddNewSlide(presentationDocument, intitalSlide, 1045);
}
private void AddNewSlide(PresentationDocument presentationDocument, SlidePart _slideTemplate, int projectID)
{
PresentationPart parent = presentationDocument.PresentationPart;
System.Data.DataTable dt = GetValueForPPTReport(projectID);
var newSlidePart = parent.AddNewPart<SlidePart>("newSlide1");
//copy the contents of the template slide to the new slide and attach the appropriate layout
newSlidePart.FeedData(_slideTemplate.GetStream(FileMode.Open));
newSlidePart.AddPart(_slideTemplate.SlideLayoutPart, _slideTemplate.GetIdOfPart(_slideTemplate.SlideLayoutPart));
//Alter the placeholder text in new slide
SetPlaceholder(newSlidePart, "txtProjectIDName", dt.Rows[0]["projName"].ToString());
SetPlaceholder(newSlidePart, "txtProjType", dt.Rows[0]["proj_type"].ToString());
SetPlaceholder(newSlidePart, "txtProbSt", dt.Rows[0]["proj_problem_state"].ToString());
SetPlaceholder(newSlidePart, "txtGoal", dt.Rows[0]["proj_goal_obj"].ToString());
SetPlaceholder(newSlidePart, "txtInScope", dt.Rows[0]["proj_in_scope"].ToString());
SetPlaceholder(newSlidePart, "txtOutOfScope", dt.Rows[0]["proj_out_scope"].ToString());
SetPlaceholder(newSlidePart, "txtCustomer", dt.Rows[0]["proj_who_customer"].ToString());
SetPlaceholder(newSlidePart, "txtCTQ", dt.Rows[0]["proj_critical_to_qlty"].ToString());
SetPlaceholder(newSlidePart, "txtDefect", dt.Rows[0]["proj_what_defect"].ToString());
SetPlaceholder(newSlidePart, "txtDate", System.DateTime.Now.ToString("MM/dd/yyyy HH:mm"));
//Add Tables here
//tblXBenefit
System.Data.DataTable dtXb = GetValueForPPTReportBenefit(1045);
string placeholder = "tblXBenefit";
List<D.Text> textListExif1 = newSlidePart.Slide.Descendants<D.Text>().Where(t => t.Text.Equals(placeholder)).ToList();
if (textListExif1.Count == 1)
{
}
List<OpenXmlElement> elements = new List<OpenXmlElement>();
elements.Add(new P.NonVisualGraphicFrameProperties
(new P.NonVisualDrawingProperties() { Id = 1, Name = "xyz" }, new P.NonVisualGraphicFrameDrawingProperties(), new ApplicationNonVisualDrawingProperties()));
// Declare and instantiate the graphic Frame of the new slide
P.GraphicFrame graphicFrame = newSlidePart.Slide.CommonSlideData.ShapeTree.AppendChild(new P.GraphicFrame());
// Specify the required Frame properties of the graphicFrame
ApplicationNonVisualDrawingPropertiesExtension applicationNonVisualDrawingPropertiesExtension = new ApplicationNonVisualDrawingPropertiesExtension() { Uri = "{D42A27DB-BD31-4B8C-83A1-F6EECF244321}" };
P14.ModificationId modificationId1 = new P14.ModificationId() { Val = 3229994563U };
modificationId1.AddNamespaceDeclaration("p14", "http://schemas.microsoft.com/office/powerpoint/2010/main");
applicationNonVisualDrawingPropertiesExtension.Append(modificationId1);
graphicFrame.NonVisualGraphicFrameProperties = new P.NonVisualGraphicFrameProperties
(new D.NonVisualDrawingProperties() { Id = 5, Name = "table 1" },
new D.NonVisualGraphicFrameDrawingProperties(new D.GraphicFrameLocks() { NoGrouping = true }),
new ApplicationNonVisualDrawingProperties(new ApplicationNonVisualDrawingPropertiesExtensionList(applicationNonVisualDrawingPropertiesExtension)));
graphicFrame.Transform = new Transform(new D.Offset() { X = 1650609L, Y = 4343400L }, new D.Extents() { Cx = 6096000L, Cy = 741680L });
// Specify the Griaphic of the graphic Frame
graphicFrame.Graphic = new D.Graphic(new D.GraphicData(GenerateTable()) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/table" });
//save the changes to the slide
newSlidePart.Slide.Save();
//need to assign an id to the new slide and add it to the slideIdList
//first figure out the largest existing id
DocumentFormat.OpenXml.Presentation.SlideIdList slideIdList = parent.Presentation.SlideIdList;
uint maxSlideId = 1;
foreach (DocumentFormat.OpenXml.Presentation.SlideId slideId in slideIdList.ChildElements)
{
if (slideId.Id > maxSlideId) maxSlideId = slideId.Id;
}
//assign an id and add the new slide at the end of the list
DocumentFormat.OpenXml.Presentation.SlideId newSlideId = new DocumentFormat.OpenXml.Presentation.SlideId { Id = ++maxSlideId, RelationshipId = parent.GetIdOfPart(newSlidePart) };
slideIdList.Append(newSlideId);
//Delete first template slide
SlideId tempSlideId = slideIdList.ChildElements[0] as SlideId;
slideIdList.RemoveChild(tempSlideId);
}
private void SetPlaceholder(SlidePart slidePart, string placeholder, string value)
{
List<D.Text> textListExif1 = slidePart.Slide.Descendants<D.Text>().Where(t => t.Text.Equals(placeholder)).ToList();
foreach (D.Text text in textListExif1)
{
text.Text = value;
}
}
#region tables
/// <summary>
/// Generate Table as below order:
/// a:tbl(Table) ->a:tr(TableRow)->a:tc(TableCell)
/// We can return TableCell object with CreateTextCell method
/// and Append the TableCell object to TableRow
/// </summary>
/// <returns>Table Object</returns>
private static D.Table GenerateTable()
{
string[,] tableSources = new string[,] { { "name", "age" }, { "Tom", "25" } };
// Declare and instantiate table
D.Table table = new D.Table();
// Specify the required table properties for the table
D.TableProperties tableProperties = new D.TableProperties() { FirstRow = true, BandRow = true };
D.TableStyleId tableStyleId = new D.TableStyleId();
tableStyleId.Text = "{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}";
tableProperties.Append(tableStyleId);
// Declare and instantiate tablegrid and colums
D.TableGrid tableGrid1 = new D.TableGrid();
D.GridColumn gridColumn1 = new D.GridColumn() { Width = 3048000L };
D.GridColumn gridColumn2 = new D.GridColumn() { Width = 3048000L };
tableGrid1.Append(gridColumn1);
tableGrid1.Append(gridColumn2);
table.Append(tableProperties);
table.Append(tableGrid1);
for (int row = 0; row < tableSources.GetLength(0); row++)
{
// Instantiate the table row
D.TableRow tableRow = new D.TableRow() { Height = 370840L };
for (int column = 0; column < tableSources.GetLength(1); column++)
{
tableRow.Append(CreateTextCell(tableSources.GetValue(row, column).ToString()));
}
table.Append(tableRow);
}
return table;
}
/// <summary>
/// Create table cell with the below order:
/// a:tc(TableCell)->a:txbody(TextBody)->a:p(Paragraph)->a:r(Run)->a:t(Text)
/// </summary>
/// <param name="text">Inserted Text in Cell</param>
/// <returns>Return TableCell object</returns>
private static D.TableCell CreateTextCell(string text)
{
if (string.IsNullOrEmpty(text))
{
text = string.Empty;
}
// Declare and instantiate the table cell
// Create table cell with the below order:
// a:tc(TableCell)->a:txbody(TextBody)->a:p(Paragraph)->a:r(Run)->a:t(Text)
D.TableCell tableCell = new D.TableCell();
// Declare and instantiate the text body
D.TextBody textBody = new D.TextBody();
D.BodyProperties bodyProperties = new D.BodyProperties();
D.ListStyle listStyle = new D.ListStyle();
D.Paragraph paragraph = new D.Paragraph();
D.Run run = new D.Run();
D.RunProperties runProperties = new D.RunProperties() { Language = "en-US", Dirty = false };
D.Text text2 = new D.Text();
text2.Text = text;
run.Append(runProperties);
run.Append(text2);
D.EndParagraphRunProperties endParagraphRunProperties = new D.EndParagraphRunProperties() { Language = "en-US", Dirty = false };
paragraph.Append(run);
paragraph.Append(endParagraphRunProperties);
textBody.Append(bodyProperties);
textBody.Append(listStyle);
textBody.Append(paragraph);
D.TableCellProperties tableCellProperties = new D.TableCellProperties();
tableCell.Append(textBody);
tableCell.Append(tableCellProperties);
return tableCell;
}
#endregion

Okay, I can place the table at any location I want to by changing values in this.. for X and Y.. not sure what Cx and Cy values does. So that works
graphicFrame.Transform = new Transform(new D.Offset() { X = 1650609L, Y = 4343400L }, new D.Extents() { Cx = 6096000L, Cy = 741680L });
But now the new issue is I am unable to reduce the font and table row height.
I tried changing this values but nothing works. Will post here when I find out.
D.TableRow tableRow = new D.TableRow() { Height = 370840L };

I had to to make this change in createTextCell method: added FontSize=800
D.RunProperties runProperties = new D.RunProperties() { Language = "en-US", Dirty = false, FontSize=800 };

Related

c# : gridpanel with url in each row to download files

This is my grid in code behind:
GridPanel grid = new GridPanel
{
Height = 200,
EnableColumnHide = false,
EmptyText = "No Documents",
Store =
{
new Store
{
Model =
{
new Model
{
Fields = { new ModelField("Name") }
}
},
DataSource = data
}
},
ColumnModel =
{
Columns =
{
new Column { ID = "clmDoc", Text = "Documents Exported", DataIndex = "Name", Width = 400 }
}
}
};
How I can show a url to the file in each row so the user can download the file? Files are stored in ~/Files/Export/Documents/file1.pdf etc
Declare your column like this:
new Column()
{
Text = "MyColumn",
DataIndex = "UrlField",
Width = 200,
Renderer = new Renderer("function (value, metadata, record) { return '<a href=\"http://www.mylink.com\" target=top >'+value+'<a/>'} ")
}

How to use Eto.Forms TreeGridView

I've been at this for a while now and cannot seem to figure out how to get the Eto.Forms TreeGridView Control to properly render. I'm trying to just add a few GridViewItem's at the moment and I just get a small gray bar at the top:
Here is my code:
List<ITreeGridItem> treeGridItems = new List<ITreeGridItem>();
foreach (var contentType in contentTypes)
{
treeGridItems.Add(new TreeGridItem(contentType.Name));
}
Content = new DocumentPage(new TreeGridView
{
DataStore = new TreeGridItemCollection(treeGridItems)
}, new Padding(20));
I'm not even really sure where to start, I just want to get a tree with text to show for each node at the moment and I can't even do that.
After a bit of trial and error I figured out how to use the tree view:
var treeGridView = new TreeGridView
{
BackgroundColor = Colors.White
};
treeGridView.Columns.Add(new GridColumn
{
HeaderText = "Content Type",
DataCell = new TextBoxCell(0)
});
treeGridView.Columns.Add(new GridColumn
{
HeaderText = "Create",
DataCell = new CustomCell
{
CreateCell = r =>
{
TreeGridItem item = r.Item as TreeGridItem;
ContentTypeTag tag = (ContentTypeTag)item.Tag;
var contentType = _siteManager.CurrentSite.ContentTypes.First(x => x.Name.Equals(tag.ClassName));
void Click(object btnSender, EventArgs btnArgs)
{
//Your Event
}
var button = new LinkButton
{
Style = "primary-link-btn",
Text = $"Create {contentType.Name.ToSentenceCase()}",
Command = new Command(Click)
};
return button;
}
}
});
treeGridView.Columns.Add(new GridColumn
{
HeaderText = "Show All",
DataCell = new CustomCell
{
CreateCell = r =>
{
TreeGridItem item = r.Item as TreeGridItem;
ContentTypeTag tag = (ContentTypeTag)item.Tag;
var contentType = _siteManager.CurrentSite.ContentTypes.First(x => x.Name.Equals(tag.ClassName));
void Click(object btnSender, EventArgs btnArgs)
{
//Your Event
}
var button = new LinkButton
{
Style = "primary-link-btn",
Text = $"Show All {contentType.Name.ToSentenceCase()}",
Command = new Command(Click)
};
return button;
}
}
});
var treeGridItemCollection = new TreeGridItemCollection();
foreach (var contentType in _siteManager.CurrentSite.ContentTypes)
{
var item = new TreeGridItem
{
Values = new string[] { contentType.Name.ToSentenceCase(), "Create", "Show All" },
Tag = new ContentTypeTag
{
ClassName = contentType.Name
}
};
treeGridItemCollection.Add(item);
}
treeGridView.DataStore = treeGridItemCollection;
You create the header columns to start and then create a TreeGridItemCollection and set the datastore to that. The values for each column of the row is set in a string array to the Values property of the TreeGridItem.

Open XML Table Generation Using Generic List

I have written a method to insert a table in a word document using Open XML. The method accepts a generic list and a few parameters to control number of columns, column headings etc.
That all works fine.
However when populating the cells in the table I want to pull out the values for each row and place them in their corresponding columns. Given the names of the properties are going to change depending on the contents of the generic list, I am not sure how to accomplish this.
Anyone that can point me in the right direction it would be appreciated.
void InsertTable<T>(List<T> tableData, int[] tableHeadingCount, string[] columnHeadings, string locationInDocument)
{
using (WordprocessingDocument myDoc = WordprocessingDocument.Open(_newDocument, true))
{
var docPart = myDoc.MainDocumentPart;
var doc = docPart.Document;
var table = new Table();
var tableBorderTop = new TopBorder();
var tableBorderBottom = new BottomBorder();
var tableBorderLeft = new LeftBorder();
var tableBorderRight = new RightBorder();
var tableBorderHorizontal = new InsideHorizontalBorder();
var tableBorderVertical = new InsideVerticalBorder();
var tableProperties = new TableProperties();
var borders = new TableBorders();
// Set Border Styles for Table
tableBorderTop.Val = BorderValues.Single;
tableBorderTop.Size = 6;
tableBorderBottom.Val = BorderValues.Single;
tableBorderBottom.Size = 6;
tableBorderLeft.Val = BorderValues.Single;
tableBorderLeft.Size = 6;
tableBorderRight.Val = BorderValues.Single;
tableBorderRight.Size = 6;
tableBorderHorizontal.Val = BorderValues.Single;
tableBorderHorizontal.Size = 6;
tableBorderVertical.Val = BorderValues.Single;
tableBorderVertical.Size = 6;
// Assign Border Styles to Table Borders
borders.TopBorder = tableBorderTop;
borders.BottomBorder = tableBorderBottom;
borders.LeftBorder = tableBorderLeft;
borders.RightBorder = tableBorderRight;
borders.InsideHorizontalBorder = tableBorderHorizontal;
borders.InsideVerticalBorder = tableBorderVertical;
// Append Border Styles to Table Properties
tableProperties.Append(borders);
// Assign Table Properties to Table
table.Append(tableProperties);
var tableRowHeader = new TableRow();
tableRowHeader.Append(new TableRowHeight() { Val = 2000 });
for (int i = 0; i < tableHeadingCount.Length; i++)
{
var tableCellHeader = new TableCell();
//Assign Font Properties to Run
var runPropHeader = new RunProperties();
runPropHeader.Append(new Bold());
runPropHeader.Append(new Color() { Val = "000000" });
//Create New Run
var runHeader = new Run();
//Assign Font Properties to Run
runHeader.Append(runPropHeader);
var columnHeader = new Text();
//Assign the Pay Rule Name to the Run
columnHeader = new Text(columnHeadings[i]);
runHeader.Append(columnHeader);
//Create Properties for Paragraph
var justificationHeader = new Justification();
justificationHeader.Val = JustificationValues.Left;
var paraPropsHeader = new ParagraphProperties(justificationHeader);
SpacingBetweenLines spacing = new SpacingBetweenLines() { Line = "240", LineRule = LineSpacingRuleValues.Auto, Before = "0", After = "0" };
paraPropsHeader.Append(spacing);
var paragraphHeader = new Paragraph();
paragraphHeader.Append(paraPropsHeader);
paragraphHeader.Append(runHeader);
tableCellHeader.Append(paragraphHeader);
var tableCellPropertiesHeader = new TableCellProperties();
var tableCellWidthHeader = new TableCellWidth();
tableCellPropertiesHeader.Append(new Shading() { Val = ShadingPatternValues.Clear, Color = "auto", Fill = "#C0C0C0" });
var textDirectionHeader = new TextDirection();
textDirectionHeader.Val = TextDirectionValues.BottomToTopLeftToRight;
tableCellPropertiesHeader.Append(textDirectionHeader);
tableCellWidthHeader.Type = TableWidthUnitValues.Dxa;
tableCellWidthHeader.Width = "2000";
tableCellPropertiesHeader.Append(tableCellWidthHeader);
tableCellHeader.Append(tableCellPropertiesHeader);
tableRowHeader.Append(tableCellHeader);
}
tableRowHeader.AppendChild(new TableHeader());
table.Append(tableRowHeader);
//Create New Row in Table for Each Record
foreach (var record in tableData)
{
var tableRow = new TableRow();
for (int i = 0; i < tableHeadingCount.Length; i++)
{
//**** This is where I dynamically want to iterate through selected properties and output the value ****
var propertyText = "Test";
var tableCell = new TableCell();
//Assign Font Properties to Run
var runProp = new RunProperties();
runProp.Append(new Bold());
runProp.Append(new Color() { Val = "000000" });
//Create New Run
var run = new Run();
//Assign Font Properties to Run
run.Append(runProp);
//Assign the text to the Run
var text = new Text(propertyText);
run.Append(text);
//Create Properties for Paragraph
var justification = new Justification();
justification.Val = JustificationValues.Left;
var paraProps = new ParagraphProperties(justification);
var paragraph = new Paragraph();
paragraph.Append(paraProps);
paragraph.Append(run);
tableCell.Append(paragraph);
var tableCellProperties = new TableCellProperties();
var tableCellWidth = new TableCellWidth();
tableCellWidth.Type = TableWidthUnitValues.Dxa;
tableCellWidth.Width = "2000";
tableCellProperties.Append(tableCellWidth);
tableCell.Append(tableCellProperties);
tableRow.Append(tableCell);
}
table.Append(tableRow);
}
var res = from bm in docPart.Document.Body.Descendants<BookmarkStart>()
where bm.Name == locationInDocument
select bm;
var bookmark = res.SingleOrDefault();
var parent = bookmark.Parent; // bookmark's parent element
Paragraph newParagraph = new Paragraph();
parent.InsertAfterSelf(newParagraph);
if (bookmark != null)
{
newParagraph.InsertBeforeSelf(table);
}
}
}
I resolved this issue by tackling the problem another way. Essentially rather than passing a List to the Insert Table Method. I decided to Pass a Multi-Dimensional Array with all the data need for the table including the table headings. This essentially meant that the Insert Table method would be more generic and any customization i.e. Generating Data, Specifying Column Headings etc would be done in the Method calling Insert Table.

How to delete data or table within two bookmarks from word document using OpenXml in C#

I am creating word document in C# using OpenXML.
I can insert my Text after specified bookmark, but how can delete data within two bookmarks.
Following is the code to insert text after specified Bookmark.
string fileName = #"C:\Users\sharepointadmin\Desktop\volc.docx";
TableValues ObjTableValues = new TableValues();
List<TableValues> allValues = new System.Collections.Generic.List<TableValues>();
for (int i = 1; i <= 5; i++)
{
ObjTableValues = new TableValues();
ObjTableValues.EmpID = i.ToString();
ObjTableValues.EmpName = "Emp" + i.ToString();
ObjTableValues.EmpDesig = "SE";
ObjTableValues.EmpDept = "Software";
allValues.Add(ObjTableValues);
//ConvertMailMergeEscape(fileName);
}
AddTable(fileName, allValues);
}
using (var document = WordprocessingDocument.Open(fileName, true))
{
IDictionary<String, BookmarkStart> bookmarkMap = new Dictionary<String, BookmarkStart>();
var doc = document.MainDocumentPart.Document;
var mainpart = document.MainDocumentPart;
var res = from ObjTableValues in mainpart.Document.Body.Descendants<BookmarkStart>() where ObjTableValues.Name == "testbookmark" select ObjTableValues;
var bookmark = res.SingleOrDefault();
if (bookmark != null)
{
var parent = bookmark.Parent;
run.Append(text);
Paragraph newParagraph = new Paragraph(run);
// insert after bookmark parent
parent.InsertAfterSelf(newParagraph);
foreach (BookmarkStart bookmarkStart in document.MainDocumentPart.RootElement.Descendants<BookmarkStart>())
{
bookmarkMap[bookmarkStart.Name] = bookmarkStart;
}
MoveToRangeStart ranstart = new MoveToRangeStart();
foreach (BookmarkStart bookmarkStart in bookmarkMap.Values)
{
Run bookmarkText = bookmarkStart.NextSibling<Run>();
if (bookmarkText != null)
{
//bookmarkText.GetFirstChild<Text>().Text = "blah";
}
}
DocumentFormat.OpenXml.Wordprocessing.Table table = new DocumentFormat.OpenXml.Wordprocessing.Table();
TableProperties props = new TableProperties(
new TableBorders(
new TopBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new BottomBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new LeftBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new RightBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new InsideHorizontalBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new InsideVerticalBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
}));
table.AppendChild<TableProperties>(props);
foreach (TableValues Tableitem in allValues)
{
var tr = new DocumentFormat.OpenXml.Wordprocessing.TableRow();
var tc = new DocumentFormat.OpenXml.Wordprocessing.TableCell();
tc.Append(new Paragraph(new Run(new Text(Tableitem.EmpID))));
tr.Append(tc);
tc = new DocumentFormat.OpenXml.Wordprocessing.TableCell();
tc.Append(new Paragraph(new Run(new Text(Tableitem.EmpName))));
tr.Append(tc);
tc = new DocumentFormat.OpenXml.Wordprocessing.TableCell();
tc.Append(new Paragraph(new Run(new Text(Tableitem.EmpDesig))));
tr.Append(tc);
tc = new DocumentFormat.OpenXml.Wordprocessing.TableCell();
tc.Append(new Paragraph(new Run(new Text(Tableitem.EmpDept))));
tr.Append(tc);
table.Append(tr);
}
doc.Body.Append(table);
doc.Save();
}
}
}
}
Can any one help me please.
I assume you want to delete some table or data from word document. If this is the case than I will suggest you to enable the developer tab in Microsoft Word. For Microsoft Word 2007 if you click on Office Button and than go to "Word Options" button which is at the bottom of drop down menue. Now enable Show "Developer tab in The Ribbon".
Once you have activated the developer tab now you can see an additional tab "Developer" in your Microsoft Word. From this tab if you click on the Rich Text icon (Marked with Aa), will insert a tag on your word document. Now, If you right click on the tag, you can give this tag a name and id.
Now you can access this tag by its id or name in C#.
e.g. the tag name you have given is 'Test Tag'
MainDocumentPart mainPart = doc.MainDocumentPart;
List<SdtBlock> taggedContentControls = mainPart.Document.Descendants<SdtBlock>().ToList();
foreach (var tagControl in taggedContentControls)
{
string tagName = tagControl.Descendants<SdtAlias>().First().Val.Value;
if(tagName== "Test Tag")
{
// you can insert any data in this tag over here
}
Now, with similar approach let's suppose you have a table and some other data in this tag that you want to delete
foreach (var tagControl in taggedContentControls)
{
string tagName = tagControl.Descendants<SdtAlias>().First().Val.Value;
if (tagName.Contains("Test Tag"))
{
// If we want to delete table only
//tagControl.SdtContentBlock.Descendants<Table>().First().Remov();
// If you want to remove everything in this tag
tagControl.SdtContentBlock.Remove();
}
}
dont forget to save your document at the end of this operation
i mean mainpart.Document.Save();
for simplicity I have written multiple LINQ statements to fetch tag from the document. You can change them according to your understanding.
I hope it will help you to get your problem sorted.
Regards

how can I change the font open xml

How can I change the font family of the document via OpenXml ?
I tried some ways but, when I open the document, it's always in Calibri
Follow my code, and what I tried.
The Header Builder I think is useless to post
private static void BuildDocument(string fileName, List<string> lista, string tipo)
{
using (var w = WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document))
{
var mp = w.AddMainDocumentPart();
var d = new DocumentFormat.OpenXml.Wordprocessing.Document();
var b = new Body();
var p = new DocumentFormat.OpenXml.Wordprocessing.Paragraph();
var r = new Run();
// Get and format the text.
for (int i = 0; i < lista.Count; i++)
{
Text t = new Text();
t.Text = lista[i];
if (t.Text == " ")
{
r.Append(new CarriageReturn());
}
else
{
r.Append(t);
r.Append(new CarriageReturn());
}
}
// What I tried
var rPr = new RunProperties(new RunFonts() { Ascii = "Arial" });
lista.Clear();
p.Append(r);
b.Append(p);
var hp = mp.AddNewPart<HeaderPart>();
string headerRelationshipID = mp.GetIdOfPart(hp);
var sectPr = new SectionProperties();
var headerReference = new HeaderReference();
headerReference.Id = headerRelationshipID;
headerReference.Type = HeaderFooterValues.Default;
sectPr.Append(headerReference);
b.Append(sectPr);
d.Append(b);
// Customize the header.
if (tipo == "alugar")
{
hp.Header = BuildHeader(hp, "Anúncio Aluguel de Imóvel");
}
else if (tipo == "vender")
{
hp.Header = BuildHeader(hp, "Anúncio Venda de Imóvel");
}
else
{
hp.Header = BuildHeader(hp, "Aluguel/Venda de Imóvel");
}
hp.Header.Save();
mp.Document = d;
mp.Document.Save();
w.Close();
}
}
In order to style your text with a specific font follow the steps listed below:
Create an instance of the RunProperties class.
Create an instance of the RunFont class. Set the Ascii property to the desired font familiy.
Specify the size of your font (half-point font size) using the FontSize class.
Prepend the RunProperties instance to your run containing the text to style.
Here is a small code example illustrating the steps described above:
private static void BuildDocument(string fileName, List<string> text)
{
using (var wordDoc = WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document))
{
var mainPart = wordDoc.AddMainDocumentPart();
mainPart.Document = new Document();
var run = new Run();
foreach (string currText in text)
{
run.AppendChild(new Text(currText));
run.AppendChild(new CarriageReturn());
}
var paragraph = new Paragraph(run);
var body = new Body(paragraph);
mainPart.Document.Append(body);
var runProp = new RunProperties();
var runFont = new RunFonts { Ascii = "Arial" };
// 48 half-point font size
var size = new FontSize { Val = new StringValue("48") };
runProp.Append(runFont);
runProp.Append(size);
run.PrependChild(runProp);
mainPart.Document.Save();
wordDoc.Close();
}
}
Hope, this helps.
If you are using Stylesheet just add an instance of FontName property at appropriate font index during Fonts initilaization.
private Stylesheet GenerateStylesheet()
{
Stylesheet styleSheet = null;
Fonts fonts = new Fonts(
new Font( // Index 0 - default
new FontSize() { Val = 8 },
new FontName() { Val = "Arial"} //i.e. or any other font name as string
);
Fills fills = new Fills( new Fill(new PatternFill() { PatternType = PatternValues.None }));
Borders borders = new Borders( new Border() );
CellFormats cellFormats = new CellFormats( new CellFormat () );
styleSheet = new Stylesheet(fonts, fills, borders, cellFormats);
return styleSheet;
}
Then use it in Workbook style part as below.
WorkbookStylesPart stylePart = workbookPart.AddNewPart<WorkbookStylesPart>();
stylePart.Stylesheet = GenerateStylesheet();
stylePart.Stylesheet.Save();

Categories

Resources