Knowledgebase (2328)
Children categories
class Program
{
static void Main(string[] args)
{
string fileName = "Word10.docx";
CreateTable(fileName);
}
// Insert a table into a word processing document.
public static void CreateTable(string fileName)
{
using (WordprocessingDocument doc = WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document))
{
MainDocumentPart mainPart = doc.AddMainDocumentPart();
mainPart.Document = new Document();
Body body = mainPart.Document.AppendChild(new Body());
Table tb = new Table();
TableRow row = new TableRow();
TableCell cel = new TableCell(new Paragraph(new Run(new Text("OpenXML"))));
row.AppendChild(cel);
tb.AppendChild(row);
body.Append(tb);
}
}
Published in
OpenXML
class Program
{
static void Main(string[] args)
{
string docName = "OpenXML.docx";
string authorName = "Gary zhang";
AcceptRevisions(docName, authorName);
}
public static void AcceptRevisions(string fileName, string authorName)
{
// Given a document name and an author name, accept revisions.
using (WordprocessingDocument wdDoc = WordprocessingDocument.Open(fileName, true))
{
Body body = wdDoc.MainDocumentPart.Document.Body;
// Handle the formatting changes.
List changes =
body.Descendants()
.Where(c => c.Author.Value == authorName).Cast().ToList();
foreach (OpenXmlElement change in changes)
{
change.Remove();
}
// Handle the deletions.
List deletions =
body.Descendants()
.Where(c => c.Author.Value == authorName).Cast().ToList();
deletions.AddRange(body.Descendants()
.Where(c => c.Author.Value == authorName).Cast().ToList());
deletions.AddRange(body.Descendants()
.Where(c => c.Author.Value == authorName).Cast().ToList());
foreach (OpenXmlElement deletion in deletions)
{
deletion.Remove();
}
// Handle the insertions.
List insertions =
body.Descendants()
.Where(c => c.Author.Value == authorName).Cast().ToList();
insertions.AddRange(body.Descendants()
.Where(c => c.Author.Value == authorName).Cast().ToList());
insertions.AddRange(body.Descendants()
.Where(c => c.Author.Value == authorName).Cast().ToList());
foreach (OpenXmlElement insertion in insertions)
{
// Found new content.
// Promote them to the same level as node, and then delete the node.
foreach (var run in insertion.Elements())
{
if (run == insertion.FirstChild)
{
insertion.InsertAfterSelf(new Run(run.OuterXml));
}
else
{
insertion.NextSibling().InsertAfterSelf(new Run(run.OuterXml));
}
}
insertion.RemoveAttribute("rsidR",
"http://schemas.openxmlformats.org/wordprocessingml/2006/main");
insertion.RemoveAttribute("rsidRPr",
"http://schemas.openxmlformats.org/wordprocessingml/2006/main");
insertion.Remove();
}
}
}
}
Published in
OpenXML
class Program
{
static void Main(string[] args)
{
CreateWordprocessingDocument("Create a word processing document.doc");
}
public static void CreateWordprocessingDocument(string filepath)
{
// Create a document by supplying the filepath.
using (WordprocessingDocument wordDocument =
WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document))
{
// Add a main document part.
MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
// Create the document structure and add some text.
mainPart.Document = new Document();
Body body = mainPart.Document.AppendChild(new Body());
Paragraph para = body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Text("Create text in body - CreateWordprocessingDocument"));
}
}
}
Published in
OpenXML