Main Content

Set Page Margins in a Word Report

This example shows how to define page margins in a Word(DOCX) report. You can define top, bottom, left, right margins for a DOCX page, plus its header, footer and gutter sizes.

Create Report

Import the DOM API packages so you do not have to use long, fully-qualified class names.

importmlreportgen.dom.*;

创建和打开一个报告。

d = Document('myreport','docx'); open(d);

Create DOCX Page Header

Get the current page layout object.

currentLayout = d.CurrentPageLayout;

Create a page header definition for DOCX document.

docxheader = DOCXPageHeader();

Create a DOMParagraph对象和马ke it center aligned and bold. Set its font size to 12pt. Append it to theDOCXPageHeaderobject.

p = Paragraph('Sample Traffic Data in Austin'); p.Style = [p.Style, {HAlign('center'), Bold(true), FontSize('12pt')}]; append(docxheader, p);

Assign the createddocxheaderobject to thePageHeadersof the current page layout.

currentLayout.PageHeaders = docxheader;

Create Body Content

Create cell arrays for the styles to be used by the formal table and its table entries.

dataTableStyle = {Border('solid'), ColSep('solid'), RowSep('solid'),宽度('100%')...OuterMargin('0pt','0pt','0pt','0pt')};

Create some sample data from the Austin traffic camera to include in the table. Then create aFormalTableobject and include the sample data in the Header and Body sections.

dataHeader = {'Camera ID','Status',“制造商”,'Signal Engineer Area'};dataBody = {'1','TURNED_ON','Spectra','NORTHEAST';'2','TURNED_ON','Sarix','NORTHWEST';'3','TURNED_OFF','Spectura','SOUTHWEST';'3','TURNED_ON','Spectura','NORTHEAST';'4','TURNED_ON','Sarix','SOUTHEAST';'5','TURNED_ON','Spectra','NORTHEAST';'6','TURNED_ON','Sarix','NORTHWEST';'7','TURNED_OFF','Spectura','SOUTHWEST';'8','TURNED_ON','Spectura','NORTHEAST';'9','TURNED_ON','Sarix','SOUTHEAST'};dataTable = FormalTable(dataHeader, dataBody); dataTable.Header.Style = [dataTable.Header.Style {Bold}]; dataTable.Style = [dataTable.Style dataTableStyle]; append(d, dataTable);

Set Top Margin and Header Size

TheTopproperty of thePageMarginsobject specifies the height of the margin. TheHeaderproperty specifies the distance from the top of the page to the start of the header. The distance from the top of the page to the page body depends on theTopproperty,Headerproperty and the height of the header content. For example, if theHeaderproperty is less than theTopproperty, the header starts in the top margin and expands downward to accommodate the header content. The body begins at the bottom of the margin or the header, which ever is greater.

Use the following settings to ensure that the header fits into theTopmargin. Set theTopproperty to 1 inch. 1 inch equals 72 points, so 0.25 inches equals 18 pts. Set theHeadervalue to 0.75 inches as remaining 0.25 inches is enough to accommodate the 12 ptsParagraphcreated in the DOCX Header.

currentLayout.PageMargins.Top ='1in'; currentLayout.PageMargins.Header ='0.75in';

Create DOCX Page Footer

Create a page footer definition for the DOCX document.

docxfooter= DOCXPageFooter();

Append a horizontal rule to thedocxfooterobject.

append(docxfooter, HorizontalRule());

Append an image to thedocxfooterobject. Use the DOMScaleToFitformat to scale the image to fit in the page. Assign the createddocxfooterobject to thePageFootersof the current page layout..

imgStyle = {ScaleToFit(true), HAlign('right'), Height('0.30in')}; img = Image('Logo_footer.Png'); img.Style = imgStyle; append(docxfooter, img); currentLayout.PageFooters = docxfooter;

Set Bottom Margin and Footer Size

TheBottomproperty of thePageMarginsobject specifies the height of the page margin. TheFooterproperty specifies the distance from the bottom of the page to the bottom of the footer. The distance from the bottom of the page to the page body depends on the settings of theBottomandFooterproperties and the height of the footer content. For example, if theFooterproperty is less than theBottomproperty, the footer starts in the bottom margin and expands upward to expand the footer content. The body starts at the top of the margin or the footer, whichever is greater.

Set theBottomproperty value to 1 inch. To accommodate the 0.30 highImageand the horizontal rule created in DOCX Footer, set theFooterproperty value to 0.5 inches.

currentLayout.PageMargins.Bottom ='1in'; currentLayout.PageMargins.Footer ='0.5in';

Set Left Margin, Right Margin and Gutter Size

This example uses theGuttersetting to leave room on the left side of the page for binding the report. The Guttersize is set to 0.25 inches andmargin is set to 0.5 inches. So, the content starts from 0.75 inches(left margin + gutter) from the left side of the page. TheRightmargin is set to 0.5 inches.

currentLayout.PageMargins.Gutter ='0.25in'; currentLayout.PageMargins.Left ='0.5in'; currentLayout.PageMargins.Right ='0.5in';

Generate and display the report.

close(d); rptview(d.OutputPath);