Create a Report with Pages in Portrait and Landscape Orientation
R2026bThis example shows how to create a report that contains pages in both
11-by-8.5-in landscape and 8.5-by-11-in portrait orientation. In this example, you generate
three representations of cosine functions that have noise, and a 3-D plot of the
peaks function. You then create a report that includes two single page
sections, a landscape-oriented section and a portrait-oriented section. The landscape oriented
section contains the cosine plots, and the portrait oriented section contains the
peaks function plot.
Create the Report Container
To avoid using the full class names in the code, import the Document
Object Model (DOM) and Report API packages.
import mlreportgen.dom.*; import mlreportgen.report.*;
Create a container to hold the report content, and specify the report as a PDF.
rpt = Report("PortraitAndLandscapeReport","pdf");
Create a chapter to store the contents, and specify a title. By default, the chapter layout is portrait. Set the chapter layout to landscape.
chapter = Chapter("Title","Report Plots"); chapter.Layout.Landscape = true;
This report contains two sections. Create the section reporters and specify the section titles.
sec1 = Section("Cosine Plots with Noise"); sec2 = Section("Surface Plot");
Generate the Figures
In this example, you create cosine waves with noise and a 3-D surface plot of the
peaks function, and add these figures to the empty report. Generate
these figures, capture images of them, and set the images to scale to their container
sizes.
x = linspace(0,3*pi,200);
y = cos(x) + rand(1,200);
imgStyle = {ScaleToFit(true)};
fig1 = Figure(bar(x,y));
fig1Img = Image(getSnapshotImage(fig1,rpt));
fig1Img.Style = imgStyle;
delete(gcf);
fig2 = Figure(scatter(x,y));
fig2Img = Image(getSnapshotImage(fig2,rpt));
fig2Img.Style = imgStyle;
delete(gcf);
fig3 = Figure(plot(x,y));
fig3Img = Image(getSnapshotImage(fig3,rpt));
fig3Img.Style = imgStyle;
delete(gcf);
fig4 = Figure(surf(peaks(20)));
fig4Img = Image(getSnapshotImage(fig4,rpt));
fig4Img.Style = imgStyle;
delete(gcf);Add Content to the Report
Include the cosine wave plots in the first section. Display the sine wave plots in a single row by creating a table with one row and five columns. To add space between the images, assign the plots to the first, third, and fifth entries of the table, and assign the second and fourth entries as empty space.
sec1Table = Table({fig1Img," ",fig2Img," ",fig3Img});
sec1Table.entry(1,1).Style = {Width("3.2in"), Height("3in")};
sec1Table.entry(1,2).Style = {Width(".2in"), Height("3in")};
sec1Table.entry(1,3).Style = {Width("3.2in"), Height("3in")};
sec1Table.entry(1,4).Style = {Width(".2in"), Height("3in")};
sec1Table.entry(1,5).Style = {Width("3in"), Height("3in")};Add the table to the first section, and add the section to the chapter.
append(sec1,sec1Table); append(chapter,sec1);
Create a Chapter Clone
The second section has a different orientation than the first. To change the
orientation, add an mlreportgen.dom.PDFPageLayout object that modifies
the orientation of the chapter immediately before the second section. To modify only the
orientation of the page and not other layout properties, such as the headers and footers,
add an mlreportgen.dom.PDFPageLayout object that has the same properties
as the chapter reporter layout except for the orientation. You can retrieve this object by
using the getImpl function on the chapter reporter, and then retrieving
the CurrentPageLayout property of the getImpl function
output object. However, after you apply getImpl to the reporter, you
cannot add content to the reporter. To work around this limitation, create a clone of the
chapter that has the same properties as the chapter you want to update, and then apply
getImpl on the chapter clone.
In this example, the default chapter template has the same properties as the chapter that you use in this report. Create the chapter clone and set the layout to portrait.
cChapter = Chapter; cChapter.Layout.Landscape = false;
Retrieve the mlreportgen.dom.PDFPageLayout object from the chapter
clone.
impl = getImpl(cChapter,rpt); portraitChapterLayout = impl.CurrentPageLayout; portraitChapterLayout.FirstPageNumber = [];
Add Portrait Oriented Section to Report
To add a portrait-oriented section to the report, add a clone of
mlreportgen.dom.PDFPageLayout object to the chapter, add the figure to
the section, and add the second section to the chapter. By creating the clone, you can reuse
the original mlreportgen.dom.PDFPageLayout object in other sections of
the report. Otherwise, you must recreate the object.
plo = clone(portraitChapterLayout); append(chapter,plo); append(sec2,fig4Img); append(chapter,sec2);
The chapter now includes both sections and the images. Add the chapter to the report.
append(rpt,chapter);
Generate and View the Report
Generate and display the report. The generated report includes the cosine figures side-by-side on a landscape page and the surface plot on a portrait page.
close(rpt); rptview(rpt);

Adjust Orientation of Additional Pages and Sections
If you add additional pages to the report, they use portrait page orientation until you
add another mlreportgen.dom.PDFPageLayout object. If you want to add more
content in the landscape orientation, create a
mlreportgen.dom.PDFPageLayout object with landscape orientation and
append it to the chapter before adding the new content. This example code changes the
orientation of the chapter to landscape and adds additional text content.
cChapter = Chapter;
cChapter.Layout.Landscape = true;
impl = getImpl(cChapter,rpt);
landscapeChapterLayout = impl.CurrentPageLayout;
landscapeChapterLayout.FirstPageNumber = [];
plo = clone(landscapeChapterLayout);
append(chapter,plo);
append(chapter,"More content");See Also
mlreportgen.dom.PDFPageLayout | getImpl