【问题标题】:Problem with table of contents after page resize页面调整大小后的目录问题
【发布时间】:2021-05-26 18:01:30
【问题描述】:

我有文档并更改页面大小

builder.PageSetup.PageWidth = ConvertUtil.MillimeterToPoint(219.1);
builder.PageSetup.PageHeight = ConvertUtil.MillimeterToPoint(285.7);

文档有目录,但更改大小后放置错误 wrong sized TOC

有什么建议如何放置正常的 TOC 全页宽度?

尝试使用不同的文档。同样的问题。

【问题讨论】:

  • tabStops 的问题

标签: c# aspose aspose.words


【解决方案1】:

目录的宽度由目录样式中的制表位定义。因此,在增加页面宽度后,您还相应地增加了 TOC 样式制表位的大小。请看以下代码:

Document doc = new Document(@"C:\Temp\in.docx");
doc.FirstSection.PageSetup.PageWidth = doc.FirstSection.PageSetup.PageWidth + 200;
UpdateTocStyles(doc);
// Update fields to rebuild the TOC.
doc.UpdateFields();
doc.Save(@"C:\Temp\out.docx");

private void UpdateTocStyles(Document doc)
{
    // List of all TOC styles.
    StyleIdentifier[] tocStyles = new StyleIdentifier[]
    {
        StyleIdentifier.Toc1,
        StyleIdentifier.Toc2,
        StyleIdentifier.Toc3,
        StyleIdentifier.Toc4,
        StyleIdentifier.Toc5,
        StyleIdentifier.Toc6,
        StyleIdentifier.Toc7,
        StyleIdentifier.Toc8,
        StyleIdentifier.Toc9
    };

    // Calculate width of the page without left and right margins.
    double pageWidth = doc.FirstSection.PageSetup.PageWidth - doc.FirstSection.PageSetup.RightMargin - doc.FirstSection.PageSetup.LeftMargin;
    // Reset tab stops in TOC styles.
    foreach (StyleIdentifier tocStyleIdentifier in tocStyles)
    {
        Style tocStyle = doc.Styles[tocStyleIdentifier];
        tocStyle.ParagraphFormat.TabStops.Clear();
        tocStyle.ParagraphFormat.TabStops.Add(new TabStop(pageWidth, TabAlignment.Right, TabLeader.Dots));
    }

}

【讨论】:

  • 你是Aspose之神!
猜你喜欢
  • 2012-09-07
  • 2017-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-07
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多