我正在编写一个从多个子文档创建Word文档的应用程序。子文档的格式设置使我可以使用标题并创建树形视图以选择要包含在最终文档中的部分。典型的布局是:
HeadingLevel1
文字|表格|绘图
HeadingLevel2
文字|表格|绘图
HeadingLevel2
...
我已经能够复制包含文本的部分,但是我没有复制表格或工程图。
private void processDonor(String donorFileName, WordprocessingDocument destinationDocument)
{
using (WordprocessingDocument donorDoc = WordprocessingDocument.Open(CreateDocumentPath(donorFileName), false))
{
//
// Get a reference to the Body of the document
//
Body donorBody = donorDoc.MainDocumentPart.Document.Body;
Body destinationBody = destinationDocument.MainDocumentPart.Document.Body;
//
// Get a list of paragraphs
//
List<Paragraph> donorParaList = new List<Paragraph>();
donorParaList = donorBody.OfType<Paragraph>().Where(p => p.ParagraphProperties != null).ToList();
bool sectionFound = false;
foreach (Paragraph donorParagraph in donorParaList)
{
if (donorParagraph.ParagraphProperties != null &&
donorParagraph.ParagraphProperties.ParagraphStyleId != null &&
donorParagraph.ParagraphProperties.ParagraphStyleId.Val.Value.Contains("Heading"))
{
sectionFound = false;
List<TreeNode> sections = documentSectionTreeView.FlattenTree().Where(n => n.Text == donorParagraph.InnerText).ToList();
foreach (TreeNode section in sections)
{
if (((ParagraphTreeNode)section).fileName == donorFileName &&
section.StateImageIndex != (int)CheckState.Unchecked)
{
sectionFound = true;
break;
}
}
if (sectionFound)
{
Paragraph p = (Paragraph)donorParagraph.CloneNode(true);
destinationBody.AppendChild(p);
}
}
else
{
if (sectionFound)
{
Paragraph p = (Paragraph)donorParagraph.CloneNode(true);
destinationBody.AppendChild(p);
}
}
}
}
}
这些表不在段落中,因此它们将被忽略,并且工程图在目标文档中具有占位符,但实际的工程图不会被复制。
我想转到一个选定的标题,然后复制该标题到下一个标题的所有内容,以使AltChunk能够在复制整个文档时处理所有内容。