2626import javax .imageio .ImageIO ;
2727import javax .print .attribute .PrintRequestAttributeSet ;
2828import javax .print .attribute .ResolutionSyntax ;
29+ import javax .print .attribute .standard .MediaPrintableArea ;
2930import javax .print .attribute .standard .OrientationRequested ;
3031import javax .print .attribute .standard .PrinterResolution ;
3132import java .awt .*;
3233import java .awt .geom .Rectangle2D ;
3334import java .awt .image .BufferedImage ;
34- import java .awt .print .PageFormat ;
35- import java .awt .print .Printable ;
36- import java .awt .print .PrinterException ;
37- import java .awt .print .PrinterJob ;
35+ import java .awt .print .*;
3836import java .io .ByteArrayInputStream ;
3937import java .io .FileNotFoundException ;
4038import java .io .IOException ;
@@ -51,6 +49,9 @@ public class PrintImage extends PrintPixel implements PrintProcessor, Printable
5149
5250 protected List <BufferedImage > images ;
5351
52+ private double docWidth = 0 ;
53+ private double docHeight = 0 ;
54+
5455 protected double dpiScale = 1 ;
5556 protected boolean scaleImage = false ;
5657 protected Object dithering = RenderingHints .VALUE_DITHER_DEFAULT ;
@@ -69,11 +70,24 @@ public PrintingUtilities.Format getFormat() {
6970
7071 @ Override
7172 public void parseData (JSONArray printData , PrintOptions options ) throws JSONException , UnsupportedOperationException {
73+ PrintOptions .Pixel pxlOpts = options .getPixelOptions ();
74+ double convert = 72.0 / pxlOpts .getUnits ().as1Inch ();
7275 dpiScale = (options .getPixelOptions ().getDensity () * options .getPixelOptions ().getUnits ().as1Inch ()) / 72.0 ;
7376
7477 for (int i = 0 ; i < printData .length (); i ++) {
7578 JSONObject data = printData .getJSONObject (i );
7679
80+ if (!data .isNull ("options" )) {
81+ JSONObject dataOpt = data .getJSONObject ("options" );
82+
83+ if (!dataOpt .isNull ("pageWidth" ) && dataOpt .optDouble ("pageWidth" ) > 0 ) {
84+ docWidth = dataOpt .optDouble ("pageWidth" ) * convert ;
85+ }
86+ if (!dataOpt .isNull ("pageHeight" ) && dataOpt .optDouble ("pageHeight" ) > 0 ) {
87+ docHeight = dataOpt .optDouble ("pageHeight" ) * convert ;
88+ }
89+ }
90+
7791 PrintingUtilities .Flavor flavor = PrintingUtilities .Flavor .parse (data , PrintingUtilities .Flavor .FILE );
7892
7993 try {
@@ -166,6 +180,19 @@ public void print(PrintOutput output, PrintOptions options) throws PrinterExcept
166180 PrintOptions .Pixel pxlOpts = options .getPixelOptions ();
167181 PrintRequestAttributeSet attributes = applyDefaultSettings (pxlOpts , page , output .getSupportedMedia ());
168182
183+ if (docWidth > 0 || docHeight > 0 ) {
184+ Paper paper = page .getPaper ();
185+
186+ if (docWidth <= 0 ) { docWidth = page .getImageableWidth (); }
187+ if (docHeight <= 0 ) { docHeight = page .getImageableHeight (); }
188+
189+ paper .setImageableArea (paper .getImageableX (), paper .getImageableY (), docWidth , docHeight );
190+ page .setPaper (paper );
191+
192+ //pdf uses imageable area from Paper, so this can be safely removed
193+ attributes .remove (MediaPrintableArea .class );
194+ }
195+
169196 scaleImage = pxlOpts .isScaleContent ();
170197 dithering = pxlOpts .getDithering ();
171198 interpolation = pxlOpts .getInterpolation ();
0 commit comments