Spire.Doc for .NET
Spire.Doc for .NET (337)
Children categories
class Program
{
static void Main(string[] args)
{
DeleteComments("DeleteComments.docx","Gary zhang");
}
public static void DeleteComments(string fileName, string author = "")
{
// Get an existing Wordprocessing document.
using (WordprocessingDocument document =
WordprocessingDocument.Open(fileName, true))
{
// Set commentPart to the document WordprocessingCommentsPart,
// if it exists.
WordprocessingCommentsPart commentPart =
document.MainDocumentPart.WordprocessingCommentsPart;
// If no WordprocessingCommentsPart exists, there can be no
// comments. Stop execution and return from the method.
if (commentPart == null)
{
return;
}
// Create a list of comments by the specified author, or
// if the author name is empty, all authors.
List commentsToDelete =
commentPart.Comments.Elements().ToList();
if (!String.IsNullOrEmpty(author))
{
commentsToDelete = commentsToDelete.
Where(c => c.Author == author).ToList();
}
IEnumerable commentIds =
commentsToDelete.Select(r => r.Id.Value);
// Delete each comment in commentToDelete from the
// Comments collection.
foreach (Comment c in commentsToDelete)
{
c.Remove();
}
// Save the comment part change.
commentPart.Comments.Save();
Document doc = document.MainDocumentPart.Document;
// Delete CommentRangeStart for each
// deleted comment in the main document.
List commentRangeStartToDelete =
doc.Descendants().
Where(c => commentIds.Contains(c.Id.Value)).ToList();
foreach (CommentRangeStart c in commentRangeStartToDelete)
{
c.Remove();
}
// Delete CommentRangeEnd for each deleted comment in the main document.
List commentRangeEndToDelete =
doc.Descendants().
Where(c => commentIds.Contains(c.Id.Value)).ToList();
foreach (CommentRangeEnd c in commentRangeEndToDelete)
{
c.Remove();
}
// Delete CommentReference for each deleted comment in the main document.
List commentRangeReferenceToDelete =
doc.Descendants().
Where(c => commentIds.Contains(c.Id.Value)).ToList();
foreach (CommentReference c in commentRangeReferenceToDelete)
{
c.Remove();
}
// Save changes back to the MainDocumentPart part.
doc.Save();
}
}
}
Published in
OpenXML
How to Convert a word processing document from the DOCM to the DOCX file format
2016-01-13 07:50:45 Written by Koohji
class Program
{
static void Main(string[] args)
{
string filename = "WithMacros.docm";
ConvertDOCMtoDOCX(filename);
}
// Given a .docm file (with macro storage), remove the VBA
// project, reset the document type, and save the document with a new name.
public static void ConvertDOCMtoDOCX(string fileName)
{
bool fileChanged = false;
using (WordprocessingDocument document =
WordprocessingDocument.Open(fileName, true))
{
// Access the main document part.
var docPart = document.MainDocumentPart;
// Look for the vbaProject part. If it is there, delete it.
var vbaPart = docPart.VbaProjectPart;
if (vbaPart != null)
{
// Delete the vbaProject part and then save the document.
docPart.DeletePart(vbaPart);
docPart.Document.Save();
// Change the document type to
// not macro-enabled.
document.ChangeDocumentType(
WordprocessingDocumentType.Document);
// Track that the document has been changed.
fileChanged = true;
}
}
// If anything goes wrong in this file handling,
// the code will raise an exception back to the caller.
if (fileChanged)
{
// Create the new .docx filename.
var newFileName = Path.ChangeExtension(fileName, ".docx");
// If it already exists, it will be deleted!
if (File.Exists(newFileName))
{
File.Delete(newFileName);
}
// Rename the file.
File.Move(fileName, newFileName);
}
}
}
Published in
OpenXML
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