com.hfg.xml.msofficexml.docx.wordprocessingml.WmlSectionProperties Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.xml.msofficexml.docx.wordprocessingml;
import com.hfg.graphics.PaperSize;
import com.hfg.graphics.units.GfxSize2D;
import com.hfg.graphics.units.GfxUnits;
import com.hfg.graphics.units.Pixels;
import com.hfg.util.Orientation;
import com.hfg.xml.XMLTag;
import com.hfg.xml.msofficexml.docx.Docx;
import com.hfg.xml.msofficexml.docx.DocxException;
import com.hfg.xml.msofficexml.docx.RelationshipXML;
//------------------------------------------------------------------------------
/**
Represents an Office Open XML section properties (<w:sectPr>) tag.
@author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------
// Section 17.6 (pg. 538) of Ecma Office Open XML Part 1
/*
17.6 Sections
WordprocessingML does not natively store the concept of pages, since it is based on paragraphs and runs
(which are laid out on to pages by consumers of this content). However, although there is no concept of
storing pages in the WordprocessingML format, it is often necessary to store information about a page or
group of pages in a document, in order to store information that is to be used to format the pages on which
a set of paragraphs appear. In WordprocessingML, this information is stored via the use of sections.
In WordprocessingML, sections are groupings of paragraphs that have a specific set of properties used to define
the pages on which the text appears, as well as other section-level (applying to all paragraphs' appearance) properties.
[Example: Consider a document with four paragraphs of text that is to be printed on a page in landscape mode,
followed by ten paragraphs of text that are to be printed in portrait mode. This requirement implies information
about the page(s) used to lay out each grouping of text - the first four paragraphs could require one page, or
ten.
Therefore, rather than try to cache knowledge of the number of pages and their properties (which is likely to become
incorrect if the XML is manipulated by a producer that does not understand page layout), this information is stored
by breaking the document into two sections, as follows:
<w:p> ...
</w:p>
<w:p> ...
</w:p>
<w:p> ...
</w:p>
<w:p>
<w:sectPr> ...
(section one properties go here) <w:pgSz ... w:orient="landscape" /> ...
</w:sectPr>
...
</w:p>
...
<w:p>
<w:sectPr> ...
(section two properties go here) <w:pgSz ... w:orient="portrait" /> ...
</w:sectPr>
...
</w:p>
end example]
For all sections except the final section, the sectPr element is stored as a child element of the last paragraph
in the section. For the final section, this information is stored as the last child element of the body element
*/
public class WmlSectionProperties extends WmlXMLTag
{
private WmlPageMargins mPageMargins;
private WmlPageSize mPageSize;
//---------------------------------------------------------------------------
public WmlSectionProperties(Docx inDocx)
{
super(WmlXML.SECT_PROPS, inDocx);
}
//---------------------------------------------------------------------------
/**
* Sets the page dimensions based on the specified paper size and orientation.
*/
public WmlSectionProperties setPageSize(PaperSize inSize, Orientation inOrientation)
{
setPageSize(inSize);
getPageSize().setOrientation(inOrientation);
return this;
}
//---------------------------------------------------------------------------
/**
* Sets the page dimensions based on the specified paper size.
*/
public WmlSectionProperties setPageSize(PaperSize inValue)
{
if (inValue != null)
{
getPageSize().setWidth(inValue.getDimensions().getWidth()).setHeight(inValue.getDimensions().getHeight());
}
else
{
mPageSize = null;
}
return this;
}
//---------------------------------------------------------------------------
/**
* Returns the page size tag if one exists or else instantiates a new one.
* @return the page size tag for this section
*/
public WmlPageSize getPageSize()
{
if (null == mPageSize)
{
// Check if it has been added via addSubtag()...
mPageSize = getOptionalSubtagByName(WmlXML.PAGE_SIZE);
if (null == mPageSize)
{
mPageSize = new WmlPageSize();
addSubtag(mPageSize);
}
}
return mPageSize;
}
//---------------------------------------------------------------------------
/**
* Returns the page margins if one exists or else instantiates a new one.
* @return the page margins for this section
*/
public WmlPageMargins getPageMargins()
{
if (null == mPageMargins)
{
// Check if it has been added via addSubtag()...
mPageMargins = getOptionalSubtagByName(WmlXML.PAGE_MARGINS);
if (null == mPageMargins)
{
mPageMargins = new WmlPageMargins();
addSubtag(mPageMargins);
}
}
return mPageMargins;
}
//---------------------------------------------------------------------------
public GfxSize2D getContentDimensions()
{
// Start with the page dimensions
GfxSize2D dimensions = getPageSize().getDimensions();
float widthPx = dimensions.getWidth().to(GfxUnits.pixels);
float heightPx = dimensions.getHeight().to(GfxUnits.pixels);
// Now adjust for margins and gutter
WmlPageMargins pageMargins = getPageMargins();
if (pageMargins.getLeft() != null)
{
widthPx -= pageMargins.getLeft().to(GfxUnits.pixels);
}
if (pageMargins.getRight() != null)
{
widthPx -= pageMargins.getRight().to(GfxUnits.pixels);
}
if (pageMargins.getGutter() != null)
{
widthPx -= pageMargins.getGutter().to(GfxUnits.pixels);
}
if (pageMargins.getTop() != null)
{
heightPx -= pageMargins.getTop().to(GfxUnits.pixels);
}
if (pageMargins.getBottom() != null)
{
heightPx -= pageMargins.getBottom().to(GfxUnits.pixels);
}
return new GfxSize2D().setWidth(new Pixels(widthPx)).setHeight(new Pixels(heightPx));
}
//---------------------------------------------------------------------------
// The header itself is placed in its own file and a reference is retained.
public void addHeader(WmlHeader inValue, WmlHeaderFooterType inType)
{
Docx docx = getParentDoc();
if (null == docx)
{
throw new DocxException("No reference to the parent doc available!?");
}
String headerId = docx.addHeader(inValue);
XMLTag headerRefTag = new XMLTag(WmlXML.HEADER_REFERENCE);
headerRefTag.setAttribute(RelationshipXML.ID_ATT, headerId);
headerRefTag.setAttribute(WmlXML.TYPE_ATT, inType);
addSubtag(headerRefTag);
}
//---------------------------------------------------------------------------
// The footer itself is placed in its own file and a reference is retained.
public void addFooter(WmlFooter inValue, WmlHeaderFooterType inType)
{
Docx docx = getParentDoc();
if (null == docx)
{
throw new DocxException("No reference to the parent doc available!?");
}
String footerId = docx.addFooter(inValue);
XMLTag footerRefTag = new XMLTag(WmlXML.FOOTER_REFERENCE);
footerRefTag.setAttribute(RelationshipXML.ID_ATT, footerId);
footerRefTag.setAttribute(WmlXML.TYPE_ATT, inType);
addSubtag(footerRefTag);
}
}