Main Content

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

PDF 报告中的正式图像居中

此示例显示如何将正式图像(带有题注的图像)放置在 PDF 报 告的横向页面上的中心。

该示例创建一个具有横向布局的报告 API PDF 报告,并使用 centerFormalImage 本地函数在页面中心添加正式图像。

sample_output.JPG

创建报告

导入 DOM 和报告 API 包,这样您就不必使用长而完全限定的类名。

import mlreportgen.dom.*
import mlreportgen.report.*

创建 PDF 报 告。

rpt = Report("myreport","pdf");
open(rpt);

更新报告页面布局

创建页面布局对象。

pageLayoutObj = PDFPageLayout;

指定页面方向、高度和宽度。

pageLayoutObj.PageSize.Orientation = "landscape";
pageLayoutObj.PageSize.Height = "8.5in";
pageLayoutObj.PageSize.Width = "11in";

指定页边距。

pageLayoutObj.PageMargins.Top = "0.5in";
pageLayoutObj.PageMargins.Bottom = "0.5in";
pageLayoutObj.PageMargins.Left = "0.5in";
pageLayoutObj.PageMargins.Right = "0.5in";

pageLayoutObj.PageMargins.Header = "0.3in";
pageLayoutObj.PageMargins.Footer = "0.3in";

将页面布局对象添加到报告中。

add(rpt,pageLayoutObj);

创建并添加正式图像

创建正式图像并调用 centerFormalImage 本地函数将这些正式图像添加到页面的中心。

formalImg1 = FormalImage( ...
    "Image",which("ngc6543a.jpg"), ...
    "Caption","Cat's Eye Nebula or NGC 6543");
centerFormalImage(formalImg1,rpt);

formalImg2 = FormalImage( ...
    "Image",which("peppers.png"), ...
    "Caption","Peppers");
centerFormalImage(formalImg2,rpt);

生成报告

关闭并查看报告。

close(rpt);
rptview(rpt);

centerFormalImage 局部函数

该函数在指定的 PDF 报 告中将指定的正式图像添加到页面的中心。该函数使用不可见的 DOM Table 进行布局。

function centerFormalImage(formalImage,rpt)

导入 DOM API、 报告 API 和报告生成器实用程序包,这样您就不必使用长而完全限定的类名。

    import mlreportgen.dom.*
    import mlreportgen.report.*
    import mlreportgen.utils.*

要确定当前页面大小和页边距,请获取报告的当前页面布局。页面布局信息用于确定后续步骤中创建的表格的大小。

    pageLayout = getReportLayout(rpt);
    pageSize = pageLayout.PageSize;
    pageMargins = pageLayout.PageMargins;

计算页面主体宽度。页面主体宽度表示可用于显示内容的页面宽度。对于 PDF 报 告,页面主体宽度是通过从页面宽度中减去左边距和右边距大小来确定的。

    bodyWidth = units.toInches(pageSize.Width) - ...
        units.toInches(pageMargins.Left) - ...
        units.toInches(pageMargins.Right);
    bodyWidth = sprintf("%0.2fin",bodyWidth);

计算页面主体高度。页面主体高度表示可用于内容的页面高度。对于 PDF 报 告,页面正文高度是通过从页面高度中减去上边距、下边距、页眉和页脚大小来确定的。

    bodyHeight = units.toInches(pageSize.Height) - ...
        units.toInches(pageMargins.Top) - ...
        units.toInches(pageMargins.Bottom) - ...
        units.toInches(pageMargins.Header) - ...
        units.toInches(pageMargins.Footer);
    bodyHeight = sprintf("%0.2fin",bodyHeight);

使用 FormalImage 报告器的 getImageReporter 方法获取图像报告器,使用 getCaptionReporter 方法获取题注报告器。

    imageReporter = getImageReporter(formalImage,rpt);
    captionReporter = getCaptionReporter(formalImage);

使用图像和题注报告器的 getImpl 方法来获取相应的 DOM 实现。

    imageImpl = getImpl(imageReporter,rpt);
    captionImpl = getImpl(captionReporter,rpt);

DOM 实现包含一个 DOM Paragraph,其中包含图像和题注内容。更新段落的样式,以确保段落周围没有空白,并且段落在后续步骤中创建的表格条目中居中。

    paraStyle = { ...
        OuterMargin("0in","0in","0in","0in"), ...
        HAlign("center") ...
        };
    
    imagePara = clone(imageImpl.Children(1));
    imagePara.Style = [imagePara.Style, paraStyle];
    
    captionPara = clone(captionImpl.Children(1));
    captionPara.Style = [captionPara.Style, paraStyle];

创建一个 1×1 的不可见布局表 (lo_table)。如果未定义表格及其表格条目的边框,则该表格被视为不可见。

    lo_table = Table(1);
    row = append(lo_table,TableRow);
    entry = append(row,TableEntry);

将包含图像和题注内容的段落添加到不可见布局表中的唯一表格条目中。

    append(entry,imagePara);
    append(entry,captionPara);

将表格扩展到可用的页面主体宽度。

    lo_table.Width = bodyWidth;

将唯一的表格条目跨越至可用的页体高度。指定垂直和水平对齐格式,以确保图像和题注在表格条目内垂直和水平居中。

    lo_table.TableEntriesStyle = [lo_table.TableEntriesStyle ...
        { ...
        Height(bodyHeight), ...
        HAlign("center"), ...
        VAlign("middle") ...
        }];

将布局表添加到报告中。

    add(rpt,lo_table);
end

另请参阅

| | | | |

相关主题