指定段落之间的间距
这些示例展示了如何指定文档中 DOM Paragraph
对象之间的空间。要设置间距,请修改 Paragraph
对象的 Style
属性,或在文档模板中创建段落样式。
通过编程设置样式
在此示例中,通过在每个段落的 OuterMargin
属性中包含 Style
对象,以编程方式设置段落间距。将创建以下输出:
导入 DOM 命名空间,这样您就不必使用长而完全限定的类名。
import mlreportgen.dom.*
创建并打开一个文档。要创建 Word 文档,请将输出类型从 "pdf"
更改为 "docx"
。要创建 HTML 文档,请分别将 "pdf"
更改为 "html"
或 "html-file"
,以创建多文件文档或单文件文档。追加标题来描述示例。
d = Document("myDoc1", "pdf"); open(d); append(d, Heading1("Spacing Using OuterMargin"));
创建一个段落并将其 Style
设置为包含 OuterMargin
对象。将左、右和上外边距设置为 0 磅,将下边距设置为 50 磅。将该段落追加到文档。
p1 = Paragraph(... "This is a paragraph with a bottom outer margin of 50pt."); p1.Style = {OuterMargin("0pt", "0pt","0pt","50pt")}; append(d, p1);
再创建两个具有不同底部边距的段落。将段落与最终文本对象一起追加到文档中,以便可以看到最后一段的底部边距。
p2 = Paragraph(... "This is a paragraph with a bottom outer margin of 25pt."); p2.Style = {OuterMargin("0pt", "0pt","0pt","25pt")}; append(d, p2); p3 = Paragraph(... "This is a paragraph with a bottom outer margin of 5pt."); p3.Style = {OuterMargin("0pt", "0pt","0pt","5pt")}; append(d, p3); append(d, Text("Text to show spacing"));
关闭并查看文档。
close(d); rptview(d);
在模板中使用样式
在此示例中,段落使用了文档所使用的自定义模板文件中定义的样式。将创建以下输出:
导入 DOM 命名空间,这样您就不必使用长而完全限定的类名。
import mlreportgen.dom.*
创建并打开 PDF 文档。在创建 example_template
对象时指定名为 Document
的自定义文档模板。要制作 Word 或 HTML 文档,首先创建一个 Word 或 HTML 模板。然后根据创建的模板将 "pdf"
更改为 "docx"
、"html"
或 "html-file"
。
有关如何创建模板的详细信息,请参阅创建 HTML 和 PDF 模板或创建 Microsoft Word 模板。
d = Document("myDoc2", "pdf", "example_template");
自定义 PDF 模板 example_template
已被修改为包含在 exampleParagraphStyle
中定义的名为 example_template\stylesheets\root.css
的段落样式,如下所示:
margin-bottom
值为段落提供 50 磅的底部边距,类似于前面的示例。或者,您可以修改模板中的默认段落样式,以便文档中的段落自动具有自定义样式。example_template
中的默认段落样式已被修改为底部边距为 25 磅:
打开文档并追加标题来描述示例。
open(d);
append(d, Heading1("Spacing Using Template-defined Paragraph Styles"));
使用 exampleParagraphStyle
作为样式名称创建一个段落。创建另一个未指定样式名称的段落,以便它使用模板中的默认段落样式。
p1 = Paragraph("This is a paragraph with" + ... " a custom style defined in a template.", ... "exampleParagraphStyle"); p2 = Paragraph("This is a paragraph with the" + ... "default style defined in a template.");
使用默认段落样式创建另一个段落,但将其 Style
属性设置为具有不同的底部边距。这将覆盖模板的默认样式。
p3 = Paragraph(... "This is a paragraph with a bottom outer margin of 5pt."); p3.Style = {OuterMargin("0pt", "0pt","0pt","5pt")};
将段落与最终文本对象一起追加到文档中,以便可以看到最后一段的底部边距。
append(d, p1);
append(d, p2);
append(d, p3);
append(d, Text("Text to show spacing."));
关闭并查看文档。
close(d); rptview(d);