Этот пример иллюстрирует, как установить ширину полей страницы отчета Microsoft Word.
Copyright 2014 MathWorks, Inc.
Следующее является скриптом, используемым, чтобы проиллюстрировать этот пример:
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
Это позволяет скрипту отсылать к классам API DOM их неполными именами, например, Документом вместо mlreportgen.dom. Документ:
import mlreportgen.dom.*;Создайте документ Word на основе шаблона Word API DOM по умолчанию. Шаблон Word по умолчанию содержит раздел макета страницы отдельного слова, который задает 1-дюймовые поля страницы.
workdir = tempdir; docpath = fullfile(workdir, 'myreport'); d = mlreportgen.dom.Document(docpath,'docx'); %#ok<*NOPTS>
Обратите внимание на то, что свойство CurrentDOCXSection документа является пустым.
d.CurrentDOCXSection
ans =
[]
Необходимо открыть документ прежде, чем попытаться изменить его поля страницы. Это вызвано тем, что API DOM не анализирует шаблон документа, пока это не открыто.
open(d);
Свойство CurrentDOCXSection документа является теперь указателем на объект DOCXSection, свойства которого установлены в значения, проанализированные от шаблона API DOM по умолчанию.
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:223449'
Id: '223449'
Граничные свойства объекта DOCXSection установлены в значения, проанализированные от шаблона API DOM по умолчанию.
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:223453'
Id: '223453'
Сначала присвойте указатель на текущий объект DOCXSection к новой переменной. Это не необходимо, но это делает код немного более читаемым.
section = d.CurrentDOCXSection;
Теперь измените левые и правые поля.
section.PageMargins.Left = '0.5in'; section.PageMargins.Right = '0.5in';
Обратите внимание на то, что, потому что объект DOCXSection является объектом указателя (как все Объекты DOM), установка полей через переменную раздела совпадает с установкой полей через d переменную.
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:223456'
Id: '223456'
Добавьте некоторый текст
append(d,'Left and right margins are .5 inch');Создайте изображение PNG L-образной мембраны.
membrane(); imagePath = fullfile(workdir, 'membrane.png'); print('-dpng', imagePath);

Перенесите изображение PNG в изображение DOM.
image = Image(imagePath)
image =
Image with properties:
Path: '/tmp/BR2019ad_1062519_57051/mlx_to_docbook1/membrane.png'
Height: '840px'
Width: '1120px'
Map: []
StyleName: []
Style: {1x2 cell}
CustomAttributes: []
Parent: []
Children: [1x0 mlreportgen.dom.Node]
Tag: 'dom.Image:223459'
Id: '223459'
Масштабируйте изображение, чтобы соответствовать между новыми полями страницы.
image.Style = {ScaleToFit}image =
Image with properties:
Path: '/tmp/BR2019ad_1062519_57051/mlx_to_docbook1/membrane.png'
Height: []
Width: []
Map: []
StyleName: []
Style: {[1x1 mlreportgen.dom.ScaleToFit]}
CustomAttributes: []
Parent: []
Children: [1x0 mlreportgen.dom.Node]
Tag: 'dom.Image:223459'
Id: '223459'
Добавьте изображение к документу
append(d,image);
close(d);
Можно показать документ только о Windows.
if ispc rptview(d.OutputPath); end