Main Content

本页采用了机器翻译。点击此处可查看英文原文。

通过编程方式对章节标题、表格标题和图窗题注进行编号

此示例显示如何以编程方式创建章节的编号标题以及章节中小节的分层编号标题。该示例还演示了如何创建位于编号章节或章节小节中的分层编号表格标题和图窗题注。

导入 DOM API 命名空间,这样您就不必使用长而完全限定的类名。

import mlreportgen.dom.*

创建并打开一个文档。要创建 Word 文档,请将输出类型从 pdf 更改为 docx。要创建 HTML 文档,请分别将 pdf 更改为 htmlhtml-file,以用于多文件或单文件文档。

d = Document("mydoc","pdf");
open(d);

将目录追加到文档。

append(d,TOC);

为章节标题、小节标题、图窗题注和表格标题创建编号流。默认情况下,流使用初始值为 0 的阿拉伯数字创建。

chapterStream = createAutoNumberStream(d,"chapter");
sectionStream = createAutoNumberStream(d,"section");
figureStream = createAutoNumberStream(d,"figure");
tableStream = createAutoNumberStream(d,"table");

定义一些图像、表格和表格条目样式,将在后面的部分中使用。

imageStyle = ...
    { ...
    Height("5in"), ...
    Width("5in") ...
    };

tableStyle = ...
    { ...
    Width("100%"), ...
    Border("solid"), ...
    RowSep("solid"), ...
    ColSep("solid") ...
    };

tableEntriesStyle = ...
    { ...
    HAlign("center"), ...
    VAlign("middle") ...
    };

以下代码创建文档中的第一章。章标题是使用 getChapterTitle 函数创建的。此函数使用 chapter 编号流创建编号章标题。该章由两个小节组成,其标题是使用 getSectionTitle 函数创建的。此函数使用 chaptersection 编号流来创建分层编号的节标题。小节由多个带有分层编号题注的图窗组成,这些图窗是使用 getFigureCaption 函数创建的。此函数使用 chapterfigure 编号流来创建分层编号的图窗题注。用于创建本章的 getChapterTitlegetSectionTitlegetFigureCaption 函数将在本示例后面描述。

% Chapter 1.
chapterTitle = getChapterTitle("Figures with numbered captions");
append(d,chapterTitle);

% Section 1.1.
sectionTitle = getSectionTitle("Figure for Land Ocean");
append(d,sectionTitle);

% Figure 1.1.
image1 =  Image(which("landOcean.jpg"));
image1.Style = imageStyle;
append(d,image1);
append(d,getFigureCaption("Land Ocean"));

% Section 1.2.
sectionTitle = getSectionTitle("Figures for Peppers and Cat's Eye Nebula");
append(d,sectionTitle);

% Figure 1.2.
image2 = Image(which("peppers.png"));
image2.Style = imageStyle;
append(d,image2);
append(d,getFigureCaption("Peppers"));

% Figure 1.3.
image3 = Image(which("ngc6543a.jpg"));
image3.Style = imageStyle;
append(d,image3);
append(d,getFigureCaption("Cat's Eye Nebula or NGC 6543"));

以下代码创建文档中的第二章。该章有一个编号的标题,其中有两个按层次编号的子节。这里,小节由具有分层编号标题的多个表组成,这些表是使用本示例后面定义的 getTableTitle 函数创建的。此函数使用 chaptertable 编号流来创建分层编号表标题。

% Chapter 2.
chapterTitle = getChapterTitle("Tables with numbered titles");
append(d,chapterTitle);

% Section 2.1.
sectionTitle = getSectionTitle("Table for Magic(5)");
append(d,sectionTitle);

% Table 2.1.
append(d,getTableTitle("Magic(5)"));
table1 = Table(magic(5));
table1.Style = tableStyle;
table1.TableEntriesStyle = tableEntriesStyle;
append(d,table1);

% Section 2.2.
sectionTitle = getSectionTitle("Tables for Magic(8) and Magic(10)");
append(d,sectionTitle);

% Table 2.2.
append(d,getTableTitle("Magic(8)"));
table2 = Table(magic(8));
table2.Style = tableStyle;
table2.TableEntriesStyle = tableEntriesStyle;
append(d,table2);

% Table 2.3.
append(d,getTableTitle("Magic(10)"));
table3 = Table(magic(10));
table3.Style = tableStyle;
table3.TableEntriesStyle = tableEntriesStyle;
append(d,table3);

关闭并查看文档。

close(d);
rptview(d);

以下函数返回章的编号标题。编号标题使用 DOM Heading1 对象创建,其中标题内容以字符串 Chapter N 和句点为前缀,其中 Nchapter 流计数器。例如,第一章的标题包含“第 1 章。”作为前缀。Style 属性中的 CounterInc 格式会导致在将此章节标题追加到文档时 chapter 流计数器递增。Style 属性中的 CounterReset 格式会导致在将此章节标题追加到文档时将其他相关流计数器(如 sectionfiguretable)重置为其初始值。

function chapterTitle = getChapterTitle(content)
    import mlreportgen.dom.*
    
    chapterTitle = Heading1();
    append(chapterTitle,Text("Chapter "));
    append(chapterTitle,AutoNumber("chapter"));
    append(chapterTitle,Text(". "));
    append(chapterTitle,Text(content));
    
    chapterTitle.Style = ...
        { ...
        CounterInc("chapter"), ...
        CounterReset("section figure table"), ...
        WhiteSpace("preserve"), ...
        PageBreakBefore(true), ...
        KeepWithNext(true) ...
        };
end

以下函数返回章中某一节的分层编号标题。使用 DOM Heading2 对象创建分层编号标题,其中标题内容以字符串“N.M.”为前缀,其中 N 和 M 分别是 chaptersection 流计数器。例如,第二章第一节的标题包含“2.1”作为前缀。Style 属性中的 CounterInc 格式会导致在将此节标题追加到文档时 section 流计数器递增。

function sectionTitle = getSectionTitle(content)
    import mlreportgen.dom.*
    
    sectionTitle = Heading2();
    append(sectionTitle,AutoNumber("chapter"));
    append(sectionTitle,Text("."));
    append(sectionTitle,AutoNumber("section"));
    append(sectionTitle,Text(". "));
    append(sectionTitle,Text(content));
    
    sectionTitle.Style = ...
        { ...
        CounterInc("section"), ...
        WhiteSpace("preserve"), ...
        KeepWithNext(true) ...
        };
end

以下函数返回添加到章节或章节中小节的图窗的分层编号题注。使用 DOM Paragraph 对象创建分层编号题注,其中题注内容以字符串“Figure N.F.”为前缀,其中 N 和 F 分别是 chapterfigure 流计数器。例如,第二章中第三张图的题注包含“图窗 2.3。”作为前缀。Style 属性中的 CounterInc 格式会导致在将此图窗题注追加到文档时 figure 流计数器递增。

function figureCaption = getFigureCaption(content)
    import mlreportgen.dom.*
    
    figureCaption = Paragraph();
    append(figureCaption,Text("Figure "));
    append(figureCaption,AutoNumber("chapter"));
    append(figureCaption,Text("."));
    append(figureCaption,AutoNumber("figure"));
    append(figureCaption,Text(". "));
    append(figureCaption,Text(content));
    
    keepWithPrevious = FOProperty("keep-with-previous.within-page","always");
    figureCaption.Style = ...
        { ...
        CounterInc("figure"), ...
        WhiteSpace("preserve"), ...
        FOProperties(keepWithPrevious) ...
        };
end

以下函数返回添加到章节或章节中小节的表格的分层编号标题。使用 DOM Paragraph 对象创建分层编号标题,其中标题内容以字符串“Table N.T.”为前缀,其中 NT 分别是 chaptertable 流计数器。例如,第二章中第三个表格的标题包含“表 2.3.”作为前缀。Style 属性中的 CounterInc 格式会导致在将此表标题追加到文档时 table 流计数器递增。

function tableTitle = getTableTitle(content)
    import mlreportgen.dom.*
    
    tableTitle = Paragraph();
    append(tableTitle,Text("Table "));
    append(tableTitle,AutoNumber("chapter"));
    append(tableTitle,Text("."));
    append(tableTitle,AutoNumber("table"));
    append(tableTitle,Text(". "));
    append(tableTitle,Text(content));
    
    tableTitle.Style = ...
        { ...
        CounterInc("table"), ...
        WhiteSpace("preserve"), ...
        KeepWithNext(true), ...
        Bold(true), ...
        OuterMargin("0pt", "0pt", "10pt", "5pt") ...
        };
end