www.gusucode.com > rptgen 案例源码程序 matlab代码 > rptgen/set_page_margins.m

    %% 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

%% 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

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

%% 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

%% 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)

%% 
% Scale the image to fit between the new page margins.
image.Style = {ScaleToFit}

%% 
% 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
%