How to copy a word document body to the header of another document
-
2012年2月27日 下午 04:11
Hi,
I need to copy the content of the word document body to the header of another document, what is the correct way to do this knowing that document can contain images and shapes.
Regrads

所有回覆
-
2012年2月29日 上午 06:08版主
Hi Tunisian_BB,
Thanks for posting in the MSDN Forum.
What's mean of "the header of another document"? Is it the place of the header/footer part or only copy the body content to the beginning point of the document boy?
Have a good day,
Tom
Tom Xu [MSFT]
MSDN Community Support | Feedback to us
-
2012年2月29日 上午 08:52
Hi Tom_Xu,
I need to copy the main document part of a document to the header/footer part of another document.
Regards
-
2012年3月1日 上午 08:16版主
Hi Tunisian_BB,
This snippet will show how to copy the main document part to header. The similar process can copy content to footer when you use FooterPart instead of HeaderPart ect..
using System; using System.Collections.Generic; using System.Linq; using System.Text; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; namespace CopyToHeadFooter { class Program { static void Main(string[] args) { List<Paragraph> sourceP = new List<Paragraph>(); using (WordprocessingDocument doc = WordprocessingDocument.Open(@"******.docx", false)) { sourceP = doc.MainDocumentPart.Document.Body.Descendants<Paragraph>().ToList(); } if (sourceP.Count > 0) { for (int i = 0; i < sourceP.Count; i++) { Console.WriteLine(sourceP[i].InnerXml); } } using (WordprocessingDocument docx = WordprocessingDocument.Open(@"******", true)) { MainDocumentPart mp = docx.MainDocumentPart; Document dom = mp.Document; HeaderPart h1 = AddNewHead(mp,sourceP); HeaderPart h2 = AddNewHead(mp, sourceP); HeaderPart h3 = AddNewHead(mp, sourceP); SectionProperties sps = dom.Descendants<SectionProperties>().FirstOrDefault(); string h1id = mp.GetIdOfPart(h1); string h2id = mp.GetIdOfPart(h2); string h3id = mp.GetIdOfPart(h3); HeaderReference hr1 = new HeaderReference() { Type = HeaderFooterValues.First, Id = h1id }; HeaderReference hr2 = new HeaderReference() { Type = HeaderFooterValues.Default, Id = h2id }; HeaderReference hr3 = new HeaderReference() { Type = HeaderFooterValues.Even, Id = h3id }; sps.Append(hr1); sps.Append(hr2); sps.Append(hr3); dom.Save(); } Console.WriteLine("OK"); Console.ReadKey(); } private static HeaderPart AddNewHead(MainDocumentPart mp,List<Paragraph> sourceP) { HeaderPart result = mp.AddNewPart<HeaderPart>(); Header header = new Header(); SdtBlock block = new SdtBlock(); SdtContentBlock content = new SdtContentBlock(); foreach (Paragraph p in sourceP) { Paragraph cp = p.Clone() as Paragraph; content.Append(cp); } block.Append(content); header.Append(block); result.Header = header; return result; } } }
I hope it can help you.
Have a good day,
Tom
Tom Xu [MSFT]
MSDN Community Support | Feedback to us
- 已標示為解答 Tunisian_BB 2012年3月8日 上午 09:36
-
2012年3月2日 上午 08:22
Thanks Tom for the code snippet, I am doing the same thing, but the important missing work here is copying images, merging styles, numbering, comments...
Regards.
-
2012年3月7日 上午 07:03版主
Hi Tunisian_BB,
You missed those due to those have different part to declare in the docx file. If you have those things in body, you need retrieve additional part of you docx file.
Have a good day,
Tom
Tom Xu [MSFT]
MSDN Community Support | Feedback to us

