📄 pageformatfactory.java
字号:
/** A standard paper size. */
public static final int[] ISOB5_EXTRA = {570, 782};
/** A standard paper size. */
public static final int[] ISOB6 = {354, 499};
/** A standard paper size. */
public static final int[] ISOB7 = {249, 354};
/** A standard paper size. */
public static final int[] ISOB8 = {176, 249};
/** A standard paper size. */
public static final int[] ISOB9 = {125, 176};
/** A standard paper size. */
public static final int[] ISOB10 = {88, 125};
/** A standard paper size. */
public static final int[] LEDGER = {1224, 792};
/** A standard paper size. */
public static final int[] LEGAL = {612, 1008};
/** A standard paper size. */
public static final int[] LEGAL_EXTRA = {684, 1080};
/** A standard paper size. */
public static final int[] LETTER = {612, 792};
/** A standard paper size. */
public static final int[] LETTER_TRANSVERSE = {612, 792};
/** A standard paper size. */
public static final int[] LETTER_EXTRA = {684, 864};
/** A standard paper size. */
public static final int[] LETTER_EXTRATRANSVERSE = {684, 864};
/** A standard paper size. */
public static final int[] LETTER_PLUS = {612, 914};
/** A standard paper size. */
public static final int[] LETTER_ROTATED = {792, 612};
/** A standard paper size. */
public static final int[] LETTER_SMALL = {612, 792};
/** A standard paper size. */
public static final int[] MONARCH = ENVMONARCH;
/** A standard paper size. */
public static final int[] NOTE = {612, 792};
/** A standard paper size. */
public static final int[] POSTCARD = {284, 419};
/** A standard paper size. */
public static final int[] POSTCARD_ROTATED = {419, 284};
/** A standard paper size. */
public static final int[] PRC16K = {414, 610};
/** A standard paper size. */
public static final int[] PRC16K_ROTATED = {610, 414};
/** A standard paper size. */
public static final int[] PRC32K = {275, 428};
/** A standard paper size. */
public static final int[] PRC32K_ROTATED = {428, 275};
/** A standard paper size. */
public static final int[] PRC32K_BIG = {275, 428};
/** A standard paper size. */
public static final int[] PRC32K_BIGROTATED = {428, 275};
/** A standard paper size. */
public static final int[] QUARTO = {610, 780};
/** A standard paper size. */
public static final int[] STATEMENT = {396, 612};
/** A standard paper size. */
public static final int[] SUPERA = {643, 1009};
/** A standard paper size. */
public static final int[] SUPERB = {864, 1380};
/** A standard paper size. */
public static final int[] TABLOID = {792, 1224};
/** A standard paper size. */
public static final int[] TABLOIDEXTRA = {864, 1296};
/** A single instance of the factory. */
private static PageFormatFactory singleton;
/**
* Default constructor.
*/
private PageFormatFactory()
{
}
/**
* Returns a single instance of the factory.
*
* @return an instance of a PageFormatFactory.
*/
public static PageFormatFactory getInstance()
{
if (singleton == null)
{
singleton = new PageFormatFactory();
}
return singleton;
}
/**
* Creates a paper by using the paper size in points found in the int-array. The array must have
* a length of 2 and the first value of this array has to contain the width and the second the
* height parameter. The created Paper has no ImagableArea defined.
*
* @param papersize the definition of the papersize in a 2-element int-array
* @return the created paper
*/
public Paper createPaper(final int[] papersize)
{
if (papersize.length != 2)
{
throw new IllegalArgumentException("Paper must have a width and a height");
}
return createPaper(papersize[0], papersize[1]);
}
/**
* Creates a paper by using the paper size in points. The created Paper has no ImagableArea
* defined.
*
* @param width the width of the paper in points
* @param height the height of the paper in points
* @return the created paper
*/
public Paper createPaper(final int width, final int height)
{
final Paper p = new Paper();
p.setSize(width, height);
setBorders(p, 0, 0, 0, 0);
return p;
}
/**
* Defines the imageable area of the given paper by adjusting the border around the imagable
* area. The bordersizes are given in points.
*
* @param paper the paper that should be modified
* @param top the bordersize of the top-border
* @param left the border in points in the left
* @param bottom the border in points in the bottom
* @param right the border in points in the right
*/
public void setBorders(final Paper paper, final double top,
final double left, final double bottom, final double right)
{
final double w = paper.getWidth() - (right + left);
final double h = paper.getHeight() - (bottom + top);
paper.setImageableArea(left, top, w, h);
}
/**
* Defines the imageable area of the given paper by adjusting the border around the imagable
* area. The bordersizes are given in inches.
*
* @param paper the paper that should be modified
* @param top the bordersize of the top-border
* @param left the border in points in the left
* @param bottom the border in points in the bottom
* @param right the border in points in the right
*/
public void setBordersInch
(final Paper paper, final double top, final double left,
final double bottom, final double right)
{
setBorders(paper, convertInchToPoints(top), convertInchToPoints(left),
convertInchToPoints(bottom), convertInchToPoints(right));
}
/**
* Defines the imageable area of the given paper by adjusting the border around the imagable area.
* The bordersizes are given in millimeters.
*
* @param paper the paper that should be modified
* @param top the bordersize of the top-border
* @param left the border in points in the left
* @param bottom the border in points in the bottom
* @param right the border in points in the right
*/
public void setBordersMm
(final Paper paper, final double top, final double left,
final double bottom, final double right)
{
setBorders(paper, convertMmToPoints(top), convertMmToPoints(left),
convertMmToPoints(bottom), convertMmToPoints(right));
}
/**
* Converts the given inch value to a valid point-value.
*
* @param inches the size in inch
* @return the size in points
*/
public double convertInchToPoints(final double inches)
{
return inches * 72f;
}
/**
* Converts the given millimeter value to a valid point-value.
*
* @param mm the size in inch
* @return the size in points
*/
public double convertMmToPoints(final double mm)
{
return mm * (72d / 254d) * 10;
}
/**
* Creates a new pageformat using the given paper and the given orientation.
*
* @param paper the paper to use in the new pageformat
* @param orientation one of PageFormat.PORTRAIT, PageFormat.LANDSCAPE or
* PageFormat.REVERSE_LANDSCAPE
* @return the created Pageformat
* @throws NullPointerException if the paper given was null
*/
public PageFormat createPageFormat(final Paper paper, final int orientation)
{
if (paper == null)
{
throw new NullPointerException("Paper given must not be null");
}
final PageFormat pf = new PageFormat();
pf.setPaper(paper);
pf.setOrientation(orientation);
return pf;
}
/**
* Creates a paper by looking up the given Uppercase name in this classes defined constants.
* The value if looked up by introspection, if the value is not defined in this class, null
* is returned.
*
* @param name the name of the constant defining the papersize
* @return the defined paper or null, if the name was invalid.
*/
public Paper createPaper(final String name)
{
try
{
final Field f = this.getClass().getDeclaredField(name);
final Object o = f.get(this);
if (o instanceof int[] == false)
{
// Log.debug ("Is no valid pageformat definition");
return null;
}
final int[] pageformat = (int[]) o;
return createPaper(pageformat);
}
catch (NoSuchFieldException nfe)
{
// Log.debug ("There is no pageformat " + name + " defined.");
return null;
}
catch (IllegalAccessException aie)
{
// Log.debug ("There is no pageformat " + name + " accessible.");
return null;
}
}
/**
* Logs the page format.
*
* @param pf the page format.
*/
public static void logPageFormat(final PageFormat pf)
{
Log.debug("PageFormat: Width: " + pf.getWidth() + " Height: " + pf.getHeight());
Log.debug("PageFormat: Image: X " + pf.getImageableX()
+ " Y " + pf.getImageableY()
+ " W: " + pf.getImageableWidth()
+ " H: " + pf.getImageableHeight());
Log.debug("PageFormat: Margins: X " + pf.getImageableX()
+ " Y " + pf.getImageableY()
+ " X2: " + (pf.getImageableWidth() + pf.getImageableX())
+ " Y2: " + (pf.getImageableHeight() + pf.getImageableY()));
}
/**
* Logs the paper size.
*
* @param pf the paper size.
*/
public static void logPaper(final Paper pf)
{
Log.debug("Paper: Width: " + pf.getWidth() + " Height: " + pf.getHeight());
Log.debug("Paper: Image: X " + pf.getImageableX()
+ " Y " + pf.getImageableY()
+ " H: " + pf.getImageableHeight()
+ " W: " + pf.getImageableWidth());
}
/**
* Tests, whether the given two page format objects are equal.
*
* @param pf1 the first page format that should be compared.
* @param pf2 the second page format that should be compared.
* @return true, if both page formats are equal, false otherwise.
*/
public static boolean isEqual(final PageFormat pf1, final PageFormat pf2)
{
if (pf1 == pf2)
{
return true;
}
if (pf1 == null || pf2 == null)
{
return false;
}
if (pf1.getOrientation() != pf2.getOrientation())
{
return false;
}
final Paper p1 = pf1.getPaper();
final Paper p2 = pf2.getPaper();
if (p1.getWidth() != p2.getWidth())
{
return false;
}
if (p1.getHeight() != p2.getHeight())
{
return false;
}
if (p1.getImageableX() != p2.getImageableX())
{
return false;
}
if (p1.getImageableY() != p2.getImageableY())
{
return false;
}
if (p1.getImageableWidth() != p2.getImageableWidth())
{
return false;
}
if (p1.getImageableHeight() != p2.getImageableHeight())
{
return false;
}
return true;
}
/**
* Returns the left border of the given paper.
*
* @param p the paper that defines the borders.
* @return the left border.
*/
public double getLeftBorder(final Paper p)
{
return p.getImageableX();
}
/**
* Returns the right border of the given paper.
*
* @param p the paper that defines the borders.
* @return the right border.
*/
public double getRightBorder(final Paper p)
{
return p.getWidth() - (p.getImageableX() + p.getImageableWidth());
}
/**
* Returns the top border of the given paper.
*
* @param p the paper that defines the borders.
* @return the top border.
*/
public double getTopBorder(final Paper p)
{
return p.getImageableY();
}
/**
* Returns the bottom border of the given paper.
*
* @param p the paper that defines the borders.
* @return the bottom border.
*/
public double getBottomBorder(final Paper p)
{
return p.getHeight() - (p.getImageableY() + p.getImageableHeight());
}
/**
* Resolves a page format, so that the result can be serialized.
*
* @param format the page format that should be prepared for serialisation.
* @return the prepared page format data.
*/
public Object[] resolvePageFormat(final PageFormat format)
{
final Integer orientation = new Integer(format.getOrientation());
final Paper p = format.getPaper();
final float[] fdim = new float[]{(float) p.getWidth(), (float) p.getHeight()};
final float[] rect = new float[]{(float) p.getImageableX(),
(float) p.getImageableY(),
(float) p.getImageableWidth(),
(float) p.getImageableHeight()};
return new Object[]{orientation, fdim, rect};
}
/**
* Restores a page format after it has been serialized.
*
* @param data the serialized page format data.
* @return the restored page format.
*/
public PageFormat createPageFormat(final Object[] data)
{
final Integer orientation = (Integer) data[0];
final float[] dim = (float[]) data[1];
final float[] rect = (float[]) data[2];
final Paper p = new Paper();
p.setSize(dim[0], dim[1]);
p.setImageableArea(rect[0], rect[1], rect[2], rect[3]);
final PageFormat format = new PageFormat();
format.setPaper(p);
format.setOrientation(orientation.intValue());
return format;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -