Knowledgebase (2417)
Children categories
class Program
{
static void Main(string[] args)
{
RemoveHeadersAndFooters("HeadersAndFooters.docx");
}
// Remove all of the headers and footers from a document.
public static void RemoveHeadersAndFooters(string filename)
{
// Given a document name, remove all of the headers and footers
// from the document.
using (WordprocessingDocument doc =
WordprocessingDocument.Open(filename, true))
{
// Get a reference to the main document part.
var docPart = doc.MainDocumentPart;
// Count the header and footer parts and continue if there
// are any.
if (docPart.HeaderParts.Count() > 0 ||
docPart.FooterParts.Count() > 0)
{
// Remove the header and footer parts.
docPart.DeleteParts(docPart.HeaderParts);
docPart.DeleteParts(docPart.FooterParts);
// Get a reference to the root element of the main
// document part.
Document document = docPart.Document;
// Remove all references to the headers and footers.
// First, create a list of all descendants of type
// HeaderReference. Then, navigate the list and call
// Remove on each item to delete the reference.
var headers =
document.Descendants().ToList();
foreach (var header in headers)
{
header.Remove();
}
// First, create a list of all descendants of type
// FooterReference. Then, navigate the list and call
// Remove on each item to delete the reference.
var footers =
document.Descendants().ToList();
foreach (var footer in footers)
{
footer.Remove();
}
// Save the changes.
document.Save();
}
}
}
}
Published in
OpenXML
class Program
{
static void Main(string[] args)
{
string document = "Word9.docx";
string fileName = "test.jpg";
InsertAPicture(document, fileName);
}
public static void InsertAPicture(string document, string fileName)
{
using (WordprocessingDocument wordprocessingDocument =WordprocessingDocument.Open(document, true))
{
MainDocumentPart mainPart = wordprocessingDocument.MainDocumentPart;
ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg);
using (FileStream stream = new FileStream(fileName, FileMode.Open))
{
imagePart.FeedData(stream);
}
AddImageToBody(wordprocessingDocument, mainPart.GetIdOfPart(imagePart));
}
}
private static void AddImageToBody(WordprocessingDocument wordDoc, string relationshipId)
{
// Define the reference of the image.
var element =
new Drawing(
new DW.Inline(
new DW.Extent() { Cx = 990000L, Cy = 792000L },
new DW.EffectExtent()
{
LeftEdge = 0L,
TopEdge = 0L,
RightEdge = 0L,
BottomEdge = 0L
},
new DW.DocProperties()
{
Id = (UInt32Value)1U,
Name = "Picture 1"
},
new DW.NonVisualGraphicFrameDrawingProperties(
new A.GraphicFrameLocks() { NoChangeAspect = true }),
new A.Graphic(
new A.GraphicData(
new PIC.Picture(
new PIC.NonVisualPictureProperties(
new PIC.NonVisualDrawingProperties()
{
Id = (UInt32Value)0U,
Name = "New Bitmap Image.jpg"
},
new PIC.NonVisualPictureDrawingProperties()),
new PIC.BlipFill(
new A.Blip(
new A.BlipExtensionList(
new A.BlipExtension()
{
Uri =
"{28A0092B-C50C-407E-A947-70E740481C1C}"
})
)
{
Embed = relationshipId,
CompressionState =
A.BlipCompressionValues.Print
},
new A.Stretch(
new A.FillRectangle())),
new PIC.ShapeProperties(
new A.Transform2D(
new A.Offset() { X = 0L, Y = 0L },
new A.Extents() { Cx = 990000L, Cy = 792000L }),
new A.PresetGeometry(
new A.AdjustValueList()
)
{ Preset = A.ShapeTypeValues.Rectangle }))
)
{ Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
)
{
DistanceFromTop = (UInt32Value)0U,
DistanceFromBottom = (UInt32Value)0U,
DistanceFromLeft = (UInt32Value)0U,
DistanceFromRight = (UInt32Value)0U,
EditId = "50D07946"
});
// Append the reference to body, the element should be in a Run.
wordDoc.MainDocumentPart.Document.Body.AppendChild(new Paragraph(new Run(element)));
}
}
Published in
OpenXML
class Program
{
static void Main(string[] args)
{
AddCommentOnFirstParagraph("Word8.docx","Open", "test", "This is my comment.");
}
// Insert a comment on the first paragraph.
public static void AddCommentOnFirstParagraph(string fileName,
string author, string initials, string comment)
{
// Use the file name and path passed in as an
// argument to open an existing Wordprocessing document.
using (WordprocessingDocument document =WordprocessingDocument.Open(fileName, true))
{
// Locate the first paragraph in the document.
Paragraph firstParagraph =
document.MainDocumentPart.Document.Descendants().First();
Comments comments = null;
string id = "0";
// Verify that the document contains a
// WordProcessingCommentsPart part; if not, add a new one.
if (document.MainDocumentPart.GetPartsCountOfType() > 0)
{
comments =
document.MainDocumentPart.WordprocessingCommentsPart.Comments;
if (comments.HasChildren)
{
// Obtain an unused ID.
id = comments.Descendants().Select(e => e.Id.Value).Max();
}
}
else
{
// No WordprocessingCommentsPart part exists, so add one to the package.
WordprocessingCommentsPart commentPart =
document.MainDocumentPart.AddNewPart();
commentPart.Comments = new Comments();
comments = commentPart.Comments;
}
// Compose a new Comment and add it to the Comments part.
Paragraph p = new Paragraph(new Run(new Text(comment)));
Comment cmt =
new Comment()
{
Id = id,
Author = author,
Initials = initials,
Date = DateTime.Now
};
cmt.AppendChild(p);
comments.AppendChild(cmt);
comments.Save();
// Specify the text range for the Comment.
// Insert the new CommentRangeStart before the first run of paragraph.
firstParagraph.InsertBefore(new CommentRangeStart()
{ Id = id }, firstParagraph.GetFirstChild());
// Insert the new CommentRangeEnd after last run of paragraph.
var cmtEnd = firstParagraph.InsertAfter(new CommentRangeEnd()
{ Id = id }, firstParagraph.Elements().Last());
// Compose a run with CommentReference and insert it.
firstParagraph.InsertAfter(new Run(new CommentReference() { Id = id }), cmtEnd);
}
}
}
Published in
OpenXML