Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Set Page Margins of a Microsoft Word Report

This example illustrates how to set the width of the page margins of a Microsoft Word report.

Copyright 2014 MathWorks, Inc.

Example Script

The following is the script used to illustrate this example:

import mlreportgen.dom.*;
workdir = tempdir;
docpath = fullfile(workdir, 'myreport');
d = mlreportgen.dom.Document(docpath,'docx');
open(d);
section = d.CurrentDOCXSection;
section.PageMargins.Left = '0.5in';
section.PageMargins.Right = '0.5in';
append(d,'Left and right margins are .5 inch');
membrane();
imagePath = fullfile(workdir, 'membrane.png');
print('-dpng', imagePath);
image = Image(imagePath);
image.Style = {ScaleToFit}
append(d,image);
close(d);
if ispc
    rptview(d.OutputPath);
end

Import DOM API package

This allows the script to refer to DOM API classes by their unqualified names, for example, Document instead of mlreportgen.dom.Document:

import mlreportgen.dom.*;

Create the Document

Create a Word document based on the DOM API's default Word template. The default Word template contains a single Word page layout section that specifies 1-inch page margins.

workdir = tempdir;
docpath = fullfile(workdir, 'myreport');
d = mlreportgen.dom.Document(docpath,'docx'); %#ok<*NOPTS>

Note that the document's CurrentDOCXSection property is null.

d.CurrentDOCXSection
ans =

     []

Open the Document

You must open a document before attempting to change its page margins. This is because the DOM API does not parse a document's template until it is open.

open(d);

The document's CurrentDOCXSection property is now a handle to a DOCXSection object whose properties are set to values parsed from the DOM API's default template.

d.CurrentDOCXSection
ans = 

  DOCXSection with properties:

         PageHeaders: []
         PageFooters: []
          RawFormats: {1x2 cell}
         PageMargins: [1x1 mlreportgen.dom.PageMargins]
            PageSize: [1x1 mlreportgen.dom.PageSize]
     FirstPageNumber: 0
    PageNumberFormat: []
        SectionBreak: 'Next Page'
           StyleName: []
               Style: {1x4 cell}
    CustomAttributes: []
              Parent: [1x1 mlreportgen.dom.Document]
            Children: [1x0 mlreportgen.dom.Node]
                 Tag: 'dom.DOCXSection:222'
                  Id: '222'

The DOCXSection object's margin properties are set to values parsed from the DOM API's default template.

d.CurrentDOCXSection.PageMargins
ans = 

  PageMargins with properties:

       Top: '1in'
    Bottom: '1in'
      Left: '1in'
     Right: '1in'
    Header: '0.5in'
    Footer: '0.5in'
    Gutter: '0in'
       Tag: 'dom.PageMargins:226'
        Id: '226'

Change the Page Margins

First assign the handle to the current DOCXSection object to a new variable. This is not necessary but it makes the code a little more readable.

section = d.CurrentDOCXSection;

Now change the left and right margins.

section.PageMargins.Left = '0.5in';
section.PageMargins.Right = '0.5in';

Please note that because the DOCXSection object is a handle object (as are all DOM objects), setting the margins via the section variable is the same as setting the margins via the d variable.

d.CurrentDOCXSection.PageMargins
ans = 

  PageMargins with properties:

       Top: '1in'
    Bottom: '1in'
      Left: '0.5in'
     Right: '0.5in'
    Header: '0.5in'
    Footer: '0.5in'
    Gutter: '0in'
       Tag: 'dom.PageMargins:229'
        Id: '229'

Append Some Illustrative Content

Append some text

append(d,'Left and right margins are .5 inch');

Create a PNG image of an L-shaped membrane.

membrane();
imagePath = fullfile(workdir, 'membrane.png');
print('-dpng', imagePath);

Wrap the PNG image in a DOM image.

image = Image(imagePath)
image = 

  Image with properties:

                Path: '/tmp/Bdoc17b_716109_27120/publish_examples2/membrane.png'
              Height: '655px'
               Width: '874px'
                 Map: []
           StyleName: []
               Style: {1x2 cell}
    CustomAttributes: []
              Parent: []
            Children: [1x0 mlreportgen.dom.Node]
                 Tag: 'dom.Image:232'
                  Id: '232'

Scale the image to fit between the new page margins.

image.Style = {ScaleToFit}
image = 

  Image with properties:

                Path: '/tmp/Bdoc17b_716109_27120/publish_examples2/membrane.png'
              Height: []
               Width: []
                 Map: []
           StyleName: []
               Style: {[1x1 mlreportgen.dom.ScaleToFit]}
    CustomAttributes: []
              Parent: []
            Children: [1x0 mlreportgen.dom.Node]
                 Tag: 'dom.Image:232'
                  Id: '232'

Append the image to the document

append(d,image);

Close the document.

close(d);

Show the document

You can show the document only on Windows.

if ispc
    rptview(d.OutputPath);
end
Was this topic helpful?