[英]c# openxml : copy paragraph with font and alignment
我想將單詞 *.docx 的一部分復制到另一個 *.docx 文件中。 為此,我從原始文件中獲得了一個段落列表:
private static List<Paragraph> getTextItems( string origFile )
{
List<Paragraph> paragraphItems = new List<Paragraph>();
var parser = new LineTextParser();
using (var doc = WordprocessingDocument.Open( origFile, false))
{
foreach (var el in doc.MainDocumentPart.Document.Body.Elements().OfType<Paragraph>())
{
if (parser.isHorizontalTableLine(el.InnerText))
{
if (true == el.InnerText.EndsWith("|"))
{
break;
}
}
paragraphItems.Add(el);
}
}
return paragraphItems;
}
然后我嘗試將這些項目應用於新文件:
using (WordprocessingDocument wordDocument =
WordprocessingDocument.Create(resultFile, 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());
foreach (var item in paragraphItems)
{
Paragraph para = body.AppendChild(new Paragraph() );
para.Append(item.ParagraphProperties.CloneNode(true));
Run run = para.AppendChild(new Run());
run.AppendChild(new Text(item.InnerText));
}
}
但是原始格式丟失了 - 我的意思是字體已更改並且對齊。 這里的解決方案是什么?
首先,代替item.InnerText
使用item.InnerXml
並將新段落 Xml 設置為源段落 Xml。
您只需要重新排列段落附加到文檔的方式。 下面的方法應該使用從原始文檔復制的段落創建文件。
public void CreateFile(string resultFile, List<Paragraph> paragraphItems)
{
using (WordprocessingDocument wordDocument =
WordprocessingDocument.Create(resultFile, WordprocessingDocumentType.Document))
{
MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
mainPart.Document = new Document();
Body body = mainPart.Document.AppendChild(new Body());
foreach (var item in paragraphItems)
{
Paragraph para = new Paragraph();
// set the inner Xml of the new paragraph
para.InnerXml = item.InnerXml;
// append paragraph to body here
body.AppendChild(para);
}
}
}
現在我們需要將doc.MainDocumentPart.StyleDefinitionsPart
的樣式doc.MainDocumentPart.StyleDefinitionsPart
到新文檔。
下面的方法是從這個 MSDN 指南中修改的。 它從StyleDefinitionsPart
提取樣式作為XDocument
。
public XDocument ExtractStylesPart(string sourceFile)
{
XDocument styles = null;
// open readonly
using (var document = WordprocessingDocument.Open(sourceFile, false))
{
var docPart = document.MainDocumentPart;
StylesPart stylesPart = docPart.StyleDefinitionsPart;
if (stylesPart != null)
{
using (var reader = XmlNodeReader.Create(
stylesPart.GetStream(FileMode.Open, FileAccess.Read)))
{
// Create the XDocument.
styles = XDocument.Load(reader);
}
}
}
// Return the XDocument instance.
return styles;
}
然后您需要將樣式保存到新文檔中。 以下方法應該適合您:
public void SetStyleToTarget(string targetFile, XDocument newStyles)
{
// open with write permissions
using (var doc = WordprocessingDocument.Open(targetFile, true))
{
// add or get the style definition part
StyleDefinitionsPart styleDefn = null;
if (doc.MainDocumentPart.StyleDefinitionsPart == null)
{
styleDefn = doc.MainDocumentPart.AddNewPart<StyleDefinitionsPart>();
}
else
{
styleDefn = doc.MainDocumentPart.StyleDefinitionsPart;
}
// populate part with the new styles
if (styleDefn != null)
{
// write the newStyle xDoc to the StyleDefinitionsPart using a streamwriter
newStyles.Save(new StreamWriter(
styleDefn.GetStream(FileMode.Create, FileAccess.Write)));
}
// probably not needed (works for me with or without this save)
doc.Save();
}
}
上述方法的靈感來自之前鏈接的指南。 不同之處在於這為新文檔創建了一個新的樣式元素(因為它沒有 - 我們剛剛創建了文檔)。
我只使用了StyleDefinitionsPart
但還有另一個部分StylesWithEffectsPart
。 如果您的文檔使用它,您可能需要為StylesWithEffectsPart
實現類似的方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.