【问题标题】:How to insert a page break without DocumentBuilder?如何在没有 DocumentBuilder 的情况下插入分页符?
【发布时间】:2016-08-26 06:20:14
【问题描述】:

我有一个包含页眉/页脚的模板文档,我必须插入多个文档。在每个文档之后,我需要插入一个分页符。

com.aspose.words.DocumentBuilder 中有一个insertDocument() 和一个insertBreak()

我更喜欢使用com.aspose.words.NodeImporter,因为它更灵活。这工作正常,但我找不到事后插入分页符的方法。

除了使用DocumentBuilder之外,还有其他插入内容(例如段落、中断)的方法吗?

【问题讨论】:

    标签: java aspose aspose.words


    【解决方案1】:

    请使用以下方法:

    doc.FirstSection.Body.FirstParagraph.Runs.Add(new Run(doc, ControlChar.PageBreak));
    

    我与 Aspose 合作,担任开发人员宣传员。

    【讨论】:

    • 所以我使用这个代码:doc.getLastSection().getBody().getLastParagraph().getRuns().add(new Run(doc, ControlChar.PAGE_BREAK));,但是如果文档的最后一个元素不是Paragraph,而是Table,我该如何在Table?
    【解决方案2】:

    请尝试使用以下代码:

    Document doc = new Document(filePath);
    NodeCollection col = doc.GetChildNodes(NodeType.Paragraph, true);
    Paragraph para = (Paragraph)col[col.Count - 2];
    Table tab = (Table)para.GetAncestor(NodeType.Table);
    if (tab != null)
    {
        // it means last para is inside table
        Paragraph newPara = new Paragraph(doc);
        newPara.Runs.Add(new Run(doc, ControlChar.PageBreak));
        newPara.ParagraphBreakFont.Size = 1;
        tab.ParentNode.InsertAfter(newPara, tab);
    }else
    {
        // normal case
    }
    doc.Save(MyDir + @"16.7.0.docx");
    

    我与 Aspose 合作,担任开发人员宣传员。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-08
      • 2012-11-08
      • 1970-01-01
      • 2014-04-21
      • 2021-12-05
      • 1970-01-01
      • 2016-02-19
      相关资源
      最近更新 更多