Skip to content

Commit

Permalink
Merge pull request #25 from hfs/pdf-custom-page-size
Browse files Browse the repository at this point in the history
PDF: Add support for custom page size
  • Loading branch information
duns committed Sep 2, 2014
2 parents 818b879 + 8e88783 commit 176d281
Showing 1 changed file with 57 additions and 8 deletions.
Expand Up @@ -92,9 +92,31 @@ public class PDFGraphics2D extends AbstractVectorGraphicsIO implements
public static final String BACKGROUND_COLOR = rootKey + "."
+ PageConstants.BACKGROUND_COLOR;

/**
* Property name for setting the size of the pages output by this {@code PDFGraphics2D}.
* <p>
* To set a pre-defined page size, set this property to one of the page size constants defined
* in {@link PageConstants} - for instance, {@linkplain PageConstants#A4},
* {@linkplain PageConstants#LETTER}.
* <p>
* To set a custom page size, set this property to the value {@link CUSTOM_PAGE_SIZE}. Then,
* set the {@link CUSTOM_PAGE_SIZE} property to a {@link Dimension} object.
* @see UserProperties
*/
public static final String PAGE_SIZE = rootKey + "."
+ PageConstants.PAGE_SIZE;

/**
* Property name (and value!) for setting a custom page size.
* <p>
* If the {@link PAGE_SIZE} property is set to THIS VALUE, then the properties are consulted
* for the value of THIS KEY. The value of this key should be a {@link Dimension} object,
* in the "user coordinates" of Java2D.
*
*/
public static final String CUSTOM_PAGE_SIZE = rootKey + "."
+ PageConstants.CUSTOM_PAGE_SIZE;

public static final String PAGE_MARGINS = rootKey + "."
+ PageConstants.PAGE_MARGINS;

Expand Down Expand Up @@ -393,8 +415,7 @@ public void writeTrailer() throws IOException {
for (int i = 1; i <= currentPage; i++) {
pages.addPage("Page" + i);
}
Dimension pageSize = PageConstants.getSize(getProperty(PAGE_SIZE),
getProperty(ORIENTATION));
Dimension pageSize = getPageSize();
pages.setMediaBox(0, 0, pageSize.getWidth(), pageSize.getHeight());
pages.setResources("Resources");
os.close(pages);
Expand Down Expand Up @@ -530,8 +551,7 @@ private void openPage(Dimension size, String title, Component component)
// so that the origin is the upper left corner of the page.
AffineTransform pageTrafo = new AffineTransform();
pageTrafo.scale(1, -1);
Dimension pageSize = PageConstants.getSize(getProperty(PAGE_SIZE),
getProperty(ORIENTATION));
Dimension pageSize = getPageSize();
Insets margins = PageConstants.getMargins(
getPropertyInsets(PAGE_MARGINS), getProperty(ORIENTATION));
pageTrafo
Expand Down Expand Up @@ -972,17 +992,46 @@ private void showCharacterCodes(String str) throws IOException {
"PDFLatin"), this);
}

private double getWidth() {
Dimension pageSize = PageConstants.getSize(getProperty(PAGE_SIZE),
/**
* Reads the PAGE_SIZE Property. If A4 .. A0 is found,
* PageConstants will determine the size. If CUSTOM_PAGE_SIZE
* is found, CUSTOM_PAGE_SIZE is used as a Property key for
* a dimension object.
*
* @return Size of page
*/
protected Dimension getPageSize() {
// A4 ... A0 or PageConstants.CUSTOM_PAGE_SIZE expected
// if PageConstants.CUSTOM_PAGE_SIZE is found,
// PageConstants.CUSTOM_PAGE_SIZE is used as Key too
String pageSizeProperty = getProperty(PAGE_SIZE);

// determine page size
Dimension result;
if (CUSTOM_PAGE_SIZE.equals(pageSizeProperty)) {
result = getPropertyDimension(CUSTOM_PAGE_SIZE);
} else {
result = PageConstants.getSize(getProperty(PAGE_SIZE),
getProperty(ORIENTATION));
}

// set a default value
if (result == null) {
result = PageConstants.getSize(PageConstants.INTERNATIONAL);
}

return result;
}

private double getWidth() {
Dimension pageSize = getPageSize();
Insets margins = PageConstants.getMargins(
getPropertyInsets(PAGE_MARGINS), getProperty(ORIENTATION));
return pageSize.getWidth() - margins.left - margins.right;
}

private double getHeight() {
Dimension pageSize = PageConstants.getSize(getProperty(PAGE_SIZE),
getProperty(ORIENTATION));
Dimension pageSize = getPageSize();
Insets margins = PageConstants.getMargins(
getPropertyInsets(PAGE_MARGINS), getProperty(ORIENTATION));
return pageSize.getHeight() - margins.top - margins.bottom;
Expand Down

0 comments on commit 176d281

Please sign in to comment.