此示例展示了如何在PDF和Microsoft®Word报表的横向页面上居中图像。
该示例创建一个具有横向布局的Report API报告,并使用centerImage
在页面中央添加图像的本地函数。
导入DOM和Report API包,这样您就不必使用冗长的、完全限定的类名。
进口mlreportgen.dom。*进口mlreportgen.report。*
创建PDF报告。要创建Microsoft®Word报表,请更改“pdf”
“多克斯”
.
报告()“myreport”,“pdf”);打开(rpt);
创建页面布局对象。
如果strcmpi (rpt。类型,“pdf”) pageLayoutObj = PDFPageLayout;其他的pageLayoutObj = DOCXPageLayout;结束
指定页面方向、高度和宽度。
pageLayoutObj.PageSize.Orientation =“景观”;pageLayoutObj.PageSize.Height =“8.5”;pageLayoutObj.PageSize.Width =“11”;
指定页边距。
pageLayoutObj.PageMargins.Top =“0.5”;pageLayoutObj.PageMargins.Bottom =“0.5”;pageLayoutObj.PageMargins.Left =“0.5”;pageLayoutObj.PageMargins.Right =“0.5”;pageLayoutObj.PageMargins.Header =“0.3”;pageLayoutObj.PageMargins.Footer =“0.3”;
将页面布局对象添加到报表。
add (rpt, pageLayoutObj);
调用centerImage
在页面中央添加图像的本地函数。
centerImage ((“ngc6543a.jpg”), rpt);centerImage ((“peppers.png”), rpt);
关闭并查看报告。
关闭(rpt);rptview (rpt);
centerImage
本地函数此函数将指定的图像添加到指定报表的页面中央。该函数使用DOM图像
来包装图像文件和一个不可见的DOM表格
来做布局。
函数centerImage (imageFile rpt)
导入DOM API和报表生成器实用程序包,这样您就不必使用冗长的、完全限定的类名。
进口mlreportgen.dom。*进口mlreportgen.utils。*
获取报告的当前页面布局以确定当前页面大小和页边距。页面布局信息用于计算页面主体大小,以便调整在后续步骤中创建的布局表的大小。
pageLayout = getReportLayout(rpt);pageSize = pageLayout.PageSize;pagemmargin = pagelayout . pagemmargin;
计算页面主体宽度。页面主体宽度表示内容可用的页面宽度,并通过从页面宽度减去左右边距大小来确定。对于DOCX输出,还需要减去槽大小。
bodyWidth = units. toinch (pageSize.Width…units.toInches (pageMargins.Left)…units.toInches (pageMargins.Right);如果strcmpi (rpt。类型,“多克斯”) bodyWidth = bodyWidth -…units.toInches (pageMargins.Gutter);结束bodyWidth = sprintf“% 0.2鳍”, bodyWidth);
计算页面主体高度。页面主体高度表示内容可用的页面高度,并通过从页面高度减去顶部和底部边距大小来确定。对于PDF输出,还需要减去页眉和页脚的大小,因为正文从页眉的底部延伸到页脚的顶部。
bodyHeight = units. toinch (pageSize.Height…units.toInches (pageMargins.Top)…units.toInches (pageMargins.Bottom);如果strcmpi (rpt。类型,“pdf”) bodyHeight = bodyHeight -…units.toInches (pageMargins.Header)…units.toInches (pageMargins.Footer);结束bodyHeight = sprintf“% 0.2鳍”, bodyHeight);
创建一个图像
对象围绕图像文件进行包装。缩放图像以适合在后续步骤中创建的布局表的条目。
image = image (imageFile);的形象。Style = [image.]风格{ScaleToFit}];
在段落中包装图像,因为PDF要求图像驻留在段落中。更新段落样式,确保图像周围没有空白。
para =段落(图像);帕拉。Style = [para.]风格{OuterMargin (“0”,“0”,“0”,“0”)});
在1 × 1不可见的布局表中添加包含图像的段落(lo_table
)。当表及其表项没有定义边界时,表被认为是不可见的。
lo_table = Table({para});
将表格扩展到可用的页面主体宽度。
lo_table。Width = bodyWidth;
将唯一的表项跨到可用的页面主体高度。另外,指定垂直和水平对齐格式,以确保图像在表项内垂直和水平居中。
lo_table。表格EntriesStyle = [lo_table.TableEntriesStyle…{…高度(bodyHeight),…HAlign (“中心”),…VAlign (“中间”)…});
将布局表添加到报表。
add (rpt, lo_table);结束
mlreportgen.report.Report
|mlreportgen.dom.PDFPageLayout
|mlreportgen.dom.DOCXPageLayout
|getReportLayout
|mlreportgen.utils.units
|mlreportgen.dom.Image
|mlreportgen.dom.Table