我有单页输入docx模板文件,用户可以设计使用某些变量,例如contact_name。在使用OpenXml SDK + Open-Xml-PowerTools进行处理期间,我基于此模板创建了许多docx文件实例,并用实数值替换变量。最后我需要一个docx输出,所以我使用Open-Xml-PowerTools DocumentBuilder合并到一个docx中。
这似乎有效,直到用户将编号列表放入模板中。我原来的问题是编号列表继续合并后文档实例的编号,即列表中第二页上的数字是11-20而不是1-10,因为该文档认为它们都引用了相同的列表ID。
我设法通过确保文件正文中的num id是唯一的来解决这个问题,但现在列表的格式在第一页之外丢失,例如在第一页上,编号列表项是缩进的,但在第二页上,它们在页面上很难保留,就像它们不是正确的编号列表一样。似乎我需要更新样式和编号部分以使这些匹配的新数字匹配,但我无法使其正常工作。
我在ericwhite.com的论坛上发布了这个帖子,但我们没有听说过最新的问题(http://ericwhite.com/blog/forums/topic/list-numbering-on-merged-docs/)。
我最近解决此问题的尝试是在OpenXml-Power-Tools中抛出一个异常,所以我想我不想用新列表ID更新某些部分。有谁知道如何做到这一点?代码尝试如下,例外情况如下。
public bool Merge(List<InterchangeableWordProcessingDocument> inputFiles, string outputFilePath)
{
if (inputFiles == null)
{
logger.LogDebug("No files to merge.");
return true;
}
try
{
List<OpenXmlPowerTools.Source> sources = new List<OpenXmlPowerTools.Source>();
int highestListNumbering = 0;
int highestAbstractListNumbering = 0;
foreach (var inputFile in inputFiles)
{
//Sometimes merge puts start of next page onto end of previous one so prevent
//Seems to cause extra blank page when there are labels so don't do on labels pages
if (inputFile.DocType == DocType.Letter)
{
using (var wordDoc = inputFile.GetAsWordProcessingDocument())
{
var para = wordDoc.MainDocumentPart.Document.Body.ChildElements.First<Paragraph>();
if (para.ParagraphProperties == null)
{
para.ParagraphProperties = new ParagraphProperties();
}
para.ParagraphProperties.PageBreakBefore = new PageBreakBefore();
//http://ericwhite.com/blog/forums/topic/list-numbering-on-merged-docs/
//Numberings should be unique to each page otherwise they continue from the previous
//Keep track of how many we have so we can add on to always have a unique number
var numIds = wordDoc.MainDocumentPart.Document.Body.Descendants<NumberingId>().ToList();
logger.LogDebug("Found " + numIds.Count + " num ids.");
foreach (var numId in numIds)
numId.Val += highestListNumbering;
var styleNumIds = wordDoc.MainDocumentPart.StyleDefinitionsPart.RootElement.Descendants<NumberingId>().ToList();
if (wordDoc.MainDocumentPart.StyleDefinitionsPart != null)
{
logger.LogDebug("Found " + styleNumIds.Count + " stlye num ids.");
foreach (var styleNumId in styleNumIds)
styleNumId.Val += highestListNumbering;
}
if (wordDoc.MainDocumentPart.NumberingDefinitionsPart != null)
{
var numberingNumIds = wordDoc.MainDocumentPart.NumberingDefinitionsPart.RootElement.Descendants<NumberingInstance>().ToList();
logger.LogDebug("Found " + numberingNumIds.Count + " numbering num ids.");
foreach (var numberingNumId in numberingNumIds)
{
numberingNumId.NumberID += highestListNumbering;
numberingNumId.AbstractNumId.Val += highestAbstractListNumbering;
}
var abstractNumberingNumIds = wordDoc.MainDocumentPart.NumberingDefinitionsPart.RootElement.Descendants<AbstractNumId>().ToList();
logger.LogDebug("Found " + abstractNumberingNumIds.Count + " abstract num ids." + wordDoc.MainDocumentPart.NumberingDefinitionsPart.RootElement.XName.LocalName);
foreach (var abstractNumberingNumId in abstractNumberingNumIds)
abstractNumberingNumId.Val += highestAbstractListNumbering;
//Keep the max nums up to date
if (abstractNumberingNumIds.Count > 0)
highestAbstractListNumbering = Math.Max(highestAbstractListNumbering, abstractNumberingNumIds.Max(ln => (ln.Val.HasValue ? ln.Val.Value : 0)));
}
if (numIds.Count > 0)
highestListNumbering = Math.Max(highestListNumbering, numIds.Max(ln => (ln.Val.HasValue ? ln.Val.Value : 0)));
wordDoc.MainDocumentPart.Document.Save();
}
}
sources.Add(new OpenXmlPowerTools.Source(inputFile.GetAsWmlDocument(), true));
}
DocumentBuilder.BuildDocument(sources, outputFilePath);
return true;
}
catch (SystemException ex)
{
logger.LogError("Error occured while generating bereavement letters. ", ex);
return false;
}
finally
{
foreach (var inputFile in inputFiles)
{
inputFile.Dispose();
}
}
}
例外:
System.InvalidOperationException: Sequence contains no elements
at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)
at OpenXmlPowerTools.DocumentBuilder.CopyNumbering(WordprocessingDocument sourceDocument, WordprocessingDocument newDocument, IEnumerable1 newContent, List1 images)
at OpenXmlPowerTools.DocumentBuilder.AppendDocument(WordprocessingDocument sourceDocument, WordprocessingDocument newDocument, List1 newContent, Boolean keepSection, String insertId, List1 images)
at OpenXmlPowerTools.DocumentBuilder.BuildDocument(List`1 sources, WordprocessingDocument output)
at OpenXmlPowerTools.DocumentBuilder.BuildDocument(List`1 sources, String fileName)
at BereavementMailing.TemplateEngine.Merge(List`1 inputFiles, String outputFilePath) in C:\caw\Underdog\Apps\Services\BereavementMailingEngine\BM_RequestProcessor\TemplateEngine.cs:line 508
答案 0 :(得分:1)
看起来您在每次传递期间两次更新相同的AbstractNumId参考值。您需要更新AbstractNum定义id值。
NumberingPart XML中的参考值如下所示:
<w:num w:numId="58">
<w:abstractNumId w:val="2"/>
</w:num>
你要两次更新。
abstractNumber定义如下所示:
<w:abstractNum w:abstractNumId="0"
w15:restartNumberingAfterBreak="0">
<w:nsid w:val="FFFFFF88"/>
<w:multiLevelType w:val="singleLevel"/>
<w:tmpl w:val="8EE6963C"/>
<w:lvl w:ilvl="0">
<w:start w:val="1"/>
<w:numFmt w:val="decimal"/>
<w:pStyle w:val="ListNumber"/>
<w:lvlText w:val="%1."/>
<w:lvlJc w:val="left"/>
<w:pPr>
<w:tabs>
<w:tab w:val="num"
w:pos="360"/>
</w:tabs>
<w:ind w:left="360"
w:hanging="360"/>
</w:pPr>
</w:lvl>
</w:abstractNum>
尝试更改此部分:
<强>原始强>
var abstractNumberingNumIds = wordDoc.MainDocumentPart.NumberingDefinitionsPart.RootElement.Descendants<AbstractNumId>().ToList();
logger.LogDebug("Found " + abstractNumberingNumIds.Count + " abstract num ids." + wordDoc.MainDocumentPart.NumberingDefinitionsPart.RootElement.XName.LocalName);
foreach (var abstractNumberingNumId in abstractNumberingNumIds)
abstractNumberingNumId.Val += highestAbstractListNumbering;
//Keep the max nums up to date
if (abstractNumberingNumIds.Count > 0)
highestAbstractListNumbering = Math.Max(highestAbstractListNumbering, abstractNumberingNumIds.Max(ln => (ln.Val.HasValue ? ln.Val.Value : 0)));
新强>
var abstractNums = wordDoc.MainDocumentPart.NumberingDefinitionsPart.RootElement.Descendants<AbstractNum>().ToList();
logger.LogDebug("Found " + abstractNums.Count + " abstract nums." + wordDoc.MainDocumentPart.NumberingDefinitionsPart.RootElement.XName.LocalName);
foreach (var abstractNum in abstractNums)
abstractNum.AbstractNumberId += highestAbstractListNumbering;
//Keep the max nums up to date
if (abstractNums.Count > 0)
highestAbstractListNumbering = Math.Max(highestAbstractListNumbering, abstractNums.Select(a => a.AbstractNumberId).Max(n => n.HasValue ? n.Value : 0));