i want to print jpanel with two tables . Two problems i faced , the first one is the Accuracy of the printed image is not very good. the second how can i control the size of the jpanel on the printed page??
here is an image of the printed page using xps viewer(the accuracy is not good)
is there away to make the printed image with this accuracy like this
and this is the code:
PrinterJob printjob = PrinterJob.getPrinterJob();
printjob.setJobName(" TESSCO CUSTOMER CARD ");
Printable printable = new Printable() {
public int print(Graphics pg, PageFormat pf, int pageNum) {
if (pageNum > 0) {
return Printable.NO_SUCH_PAGE;
}
Dimension size = jPanel1.getSize();
BufferedImage bufferedImage = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
jPanel1.print(bufferedImage.getGraphics());
Graphics2D g2 = (Graphics2D) pg;
g2.translate(pf.getImageableX(), pf.getImageableY());
g2.drawImage(bufferedImage, 0, 0, (int) pf.getWidth(), (int) pf.getHeight(), null);
return Printable.PAGE_EXISTS;
}
};
Paper paper = new Paper();
paper.setImageableArea(0, 0,700,890);
paper.setSize(700,890);
PageFormat format = new PageFormat();
format.setPaper(paper);
format.setOrientation(PageFormat.LANDSCAPE);
printjob.setPrintable(printable, format);
if (printjob.printDialog() == false)
return;
try {
printjob.print();
} catch (PrinterException ex) {
System.out.println("NO PAGE FOUND." + ex);
}
Firstly, don't use the pane size, you need to act as the layout manager and size the panel to fit the page.
Secondly, don't use a buffered image. This will not share the same properties as the graphics context past to you by the print engine. Also, another print method is re-entrant, meaning that it may called a number of times for each page, creating a buffered image this way is wasteful on resources
You might like to take a look at How to Print Tables
UPDATE
You could do something like...
public int print(Graphics pg, PageFormat pf, int pageNum) {
if (page > 0) {
return NO_SUCH_PAGE;
}
Graphics2D g2d = (Graphics2D)pg;
double pageWidth = pf.getImageableWidth();
double pageHeight = pf.getImageableHeight();
double pageX = pf.getImageableX();
double pageY = pf.getImageableY();
g2d.translate(pageX, pageY);
double tableHeight = pageHeight / 2d;
jPanel1.setBounds(0, 0, (int)Math.floor(pageWidth), (int)Math.floor(pageHeight));
jPanel1.printAll(g2d);
return Printable.PAGE_EXISTS;
}
Just beware, that this could have the potential of truncating your table. Also, you should not do this with a Component that is already on the screen. You should create a new "print" component.
UPDATE with working example
Okay, so the concept is sound, just needed some tweaking to get it to work ;)
public class PrintTableTest {
public static void main(String[] args) {
final JTable table1 = new JTable(new AbstractTableModel() {
#Override
public int getRowCount() {
return 3;
}
#Override
public int getColumnCount() {
return 3;
}
#Override
public String getColumnName(int column) {
String name = null;
switch (column) {
case 0:
name = "Day";
break;
case 1:
name = "FirstName";
break;
case 2:
name = "LastName";
break;
}
return name;
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
Object value = null;
switch (columnIndex) {
case 0:
switch (rowIndex) {
case 0:
value = "First";
break;
case 1:
value = "Second";
break;
case 2:
value = "Final";
break;
}
break;
}
return value;
}
});
int rowHeight = (int) Math.floor(((700f / 2f) - table1.getTableHeader().getPreferredSize().height) / 3f);
table1.setRowHeight(rowHeight);
PrinterJob printjob = PrinterJob.getPrinterJob();
printjob.setJobName(" TESSCO CUSTOMER CARD ");
Printable printable;
printable = new Printable() {
public int print(Graphics pg, PageFormat pf, int pageNum) {
if (pageNum > 0) {
return NO_SUCH_PAGE;
}
Graphics2D g2d = (Graphics2D) pg;
double pageWidth = pf.getImageableWidth();
double pageHeight = pf.getImageableHeight();
double pageX = pf.getImageableX();
double pageY = pf.getImageableY();
g2d.translate(pageX, pageY);
// Each table will take half the page...
double tableHeight = pageHeight / 2d;
// We need to print the header as well...
JTableHeader header = table1.getTableHeader();
int headerHeight = header.getPreferredSize().height;
int yOffset = 0;
for (int index = 0; index < 2; index++) {
// Set the bounds of the components
// The yOffset is actuall irrelevent to us, but for consitency sake
// we'll keep it.
header.setBounds(0, yOffset, (int) Math.floor(pageWidth), headerHeight);
table1.setBounds(0, yOffset + headerHeight, (int) Math.floor(pageWidth), (int) Math.floor(tableHeight));
// Force the components to update there internal layouts to match
// the new size. We need to do this because, technically, we're not
// attached to any peer, nor do we want them to be taking into account
// the dimensions of any parent any way :P
table1.doLayout();
header.doLayout();
// Translate the graphics. Components asume a position of 0x0 when
// painting. This is a side effect of the AWT/Swing painting engine
// (for which we are greatful), but we need to simulate the change
g2d.translate(0, yOffset);
header.printAll(g2d);
// Translations are relative to the last translation...
g2d.translate(0, headerHeight);
table1.printAll(g2d);
// Reset the last translation
g2d.translate(0, -(headerHeight + yOffset));
// Next table...
yOffset += table1.getHeight();
}
return Printable.PAGE_EXISTS;
}
};
Paper paper = new Paper();
paper.setImageableArea(0, 0, 700, 890);
paper.setSize(700, 890);
PageFormat format = new PageFormat();
format.setPaper(paper);
format.setOrientation(PageFormat.LANDSCAPE);
// printjob.setPrintable(printable, format);
BufferedImage img = new BufferedImage(890, 700, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = img.createGraphics();
g2d.setColor(Color.WHITE);
g2d.fill(new Rectangle(0, 0, 890, 700));
try {
printable.print(g2d, format, 0);
} catch (Exception exp) {
exp.printStackTrace();
}
g2d.dispose();
try {
ImageIO.write(img, "png", new File("Print.png"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Related
Hey I'm trying to make an invoice software everything is fine while I'm printing bill then it is printing according to standard printer size but I want to print it through billing printer I'm not using jesper report or any other reporting software I'm just print JFrame by swing code.
Here is my code. . .
final Component comp;
public testing(Component comp){
this.comp=comp;
}
#Override
public int print(Graphics g, PageFormat format, int page_index)
throws PrinterException {
if (page_index > 0) {
return Printable.NO_SUCH_PAGE;
}
// get the bounds of the component
Dimension dim = comp.getSize();
double cHeight = dim.getHeight();
double cWidth = dim.getWidth();
// get the bounds of the printable area
double pHeight = format.getImageableHeight();
double pWidth = format.getImageableWidth();
double pXStart = format.getImageableX();
double pYStart = format.getImageableY();
double xRatio = pWidth / cWidth;
double yRatio = pHeight / cHeight;
Graphics2D g2 = (Graphics2D) g;
g2.translate(pXStart, pYStart);
g2.scale(xRatio, yRatio);
comp.paint(g2);
return Printable.PAGE_EXISTS;
}
//_____________________________________pint Method
public static void printing(){
PrinterJob pjob = PrinterJob.getPrinterJob();
PageFormat preformat = pjob.defaultPage();
preformat.setOrientation(PageFormat.PORTRAIT);
PageFormat postformat = pjob.defaultPage();
//If user does not hit cancel then print.
if (preformat != postformat) {
//Set print component
pjob.setPrintable(new testing(frame), postformat);
// if (pjob.printDialog()) {
try {
pjob.print();
} catch (PrinterException ex) {
Logger.getLogger(testing.class.getName()).log(Level.SEVERE, null, ex);
}
//}
}
}
Is there any Idea how to print it fit the printer roll because there is no any option to print paper fit to printing roll paper.
Thnkx in advance.
I am trying to change color of image. Hence I used the following code
public class Picture {
String img_name;
BufferedImage buf_img;
int width;
int height;
public Picture(String name) {
this.img_name = name;
try {
buf_img = ImageIO.read(new File(img_name));
} catch (IOException ex) {
Logger.getLogger(Picture.class.getName()).log(Level.SEVERE, null, ex);
}
}
public Picture(int w, int h) {
this.width = w;
this.height = h;
buf_img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
}
public int width() {
width = buf_img.getWidth();
return width;
}
public int height() {
height = buf_img.getHeight();
return height;
}
public Color get(int col, int row) {
Color color = new Color(buf_img.getRGB(col, row));
return color;
}
public void set(int col, int row, Color color) {
buf_img.setRGB(col, row, color.getRGB());
}
public void show() {
try {
File saveAs = new File("D:\\temp\\" + new Random().nextInt() + ".png");
ImageIO.write(buf_img, "png", saveAs);
Desktop.getDesktop().open(saveAs);
} catch (IOException ex) {
Logger.getLogger(Picture.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public class ColorSeparation {
public static void main(String[] args) {
// read in the picture specified by command-line argument
Picture picture = new Picture("D:\\qwe1.jpg");
int width = picture.width();
int height = picture.height();
// create three empy pictures of the same dimension
Picture pictureR = new Picture(width, height);
// separate colors
for (int col = 0; col < width; col++) {
for (int row = 0; row < height; row++) {
Color color = picture.get(col, row);
int r = color.getRed();
int g = color.getGreen();
int b = color.getBlue();
pictureR.set(col, row, new Color(r, 0, 0));
}
}
// display picture in its own window
pictureR.show();
}
}
It is working as expected.
.
Now I want to set color of entire image to rgb 255, 153, 51. I tried to set
pictureR.set(col, row, new Color(255, 153, 51)). But the resulted output is the below image.
How can I get correct image? Please help.
Your initial example is misleading you. Your code in the first example is setting varying shades of red (pulled from the original red channel), creating a "redscale" image, not "colorizing" the image like you think it is.
int r = color.getRed();
pictureR.set(col, row, new Color(r, 0, 0));
Your second example is setting a fixed color for each and every pixel, so you get a uniform orange.
pictureR.set(col, row, new Color(255, 153, 51))
You need to colorize the image by varying all three channels, just like you varied the red value initially. See this question for an example using compositing, which is a different angle from what you're using now.
The easiest implementation for you, given your sample code, would be to calculate the relative luminence of each pixel (effectively it's grayscale value) and use that to adjust the "orange" value you are setting. The standard weighting for luminence is
L = 0.2126*R + 0.7152*G + 0.0722*B
so something like
pictureR.set(col, row, new Color(255 * L/255, 153 * L/255, 51 * L/255));
In the script it is going from around the 300x300 mark down to 60x60. Need to improve the overall image quality as it is coming out very poorly at the moment.
public static Boolean resizeImage(String sourceImg, String destImg, Integer Width, Integer Height, Integer whiteSpaceAmount)
{
BufferedImage origImage;
try
{
origImage = ImageIO.read(new File(sourceImg));
int type = origImage.getType() == 0? BufferedImage.TYPE_INT_ARGB : origImage.getType();
int fHeight = Height;
int fWidth = Width;
int whiteSpace = Height + whiteSpaceAmount; //Formatting all to squares so don't need two whiteSpace calcs..
double aspectRatio;
//Work out the resized dimensions
if (origImage.getHeight() > origImage.getWidth()) //If the pictures height is greater than the width then scale appropriately.
{
fHeight = Height; //Set the height to 60 as it is the biggest side.
aspectRatio = (double)origImage.getWidth() / (double)origImage.getHeight(); //Get the aspect ratio of the picture.
fWidth = (int)Math.round(Width * aspectRatio); //Sets the width as created via the aspect ratio.
}
else if (origImage.getHeight() < origImage.getWidth()) //If the pictures width is greater than the height scale appropriately.
{
fWidth = Width; //Set the height to 60 as it is the biggest side.
aspectRatio = (double)origImage.getHeight() / (double)origImage.getWidth(); //Get the aspect ratio of the picture.
fHeight = (int)Math.round(Height * aspectRatio); //Sets the height as created via the aspect ratio.
}
int extraHeight = whiteSpace - fHeight;
int extraWidth = whiteSpace - fWidth;
BufferedImage resizedImage = new BufferedImage(whiteSpace, whiteSpace, type);
Graphics2D g = resizedImage.createGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, whiteSpace, whiteSpace);
g.setComposite(AlphaComposite.Src);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.drawImage(origImage, extraWidth/2, extraHeight/2, fWidth, fHeight, null);
g.dispose();
ImageIO.write(resizedImage, "jpg", new File(destImg));
}
catch (IOException ex)
{
return false;
}
return true;
}
Really just need to know if their is something I can plug in that will bump up the quality or if I need to look at something else entirely.
EDIT: Picture comparison.
Source, just picked a random washing machine from google.
http://www.essexappliances.co.uk/images/categories/washing-machine.jpg
The same picture converted in Photoshop to what I need it to be.
http://imgur.com/78B1p
What it looks like being converted like this.
http://imgur.com/8WlXD
Scaling an image down over a large range is inherently dangerous (from the point of view of quality), especially using a single step.
The recommended method is to use a divide and conquer method. Basically, you scale the image down in steps of 50% until you reach your desired size.
So, I took the original image of 650x748 and scaled it down to fit within a 60x60 region (52x60).
Divide and conquer compared to one step...
public class TestImageResize {
public static void main(String[] args) {
new TestImageResize();
}
public TestImageResize() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new ScalePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ScalePane extends JPanel {
private BufferedImage original;
private BufferedImage scaled;
public ScalePane() {
try {
original = ImageIO.read(new File("path/to/master.jpg"));
scaled = getScaledInstanceToFit(original, new Dimension(60, 60));
ImageIO.write(scaled, "jpg", new File("scaled.jpg"));
BufferedImage image = new BufferedImage(52, 60, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.drawImage(original, 0, 0, 52, 60, this);
g2d.dispose();
ImageIO.write(image, "jpg", new File("test.jpg"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
#Override
public Dimension getPreferredSize() {
Dimension size = super.getPreferredSize();
if (original != null) {
if (scaled != null) {
size.width = original.getWidth() + scaled.getWidth();
size.height = original.getHeight();
} else {
size.width = original.getWidth();
size.height = original.getHeight();
}
}
return size;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if (original != null) {
int x = 0;
int y = (getHeight() - original.getHeight()) / 2;;
if (scaled != null) {
x = (getWidth() - (original.getWidth() + scaled.getWidth())) / 2;
} else {
x = (getWidth() - original.getWidth()) / 2;
}
g2d.drawImage(original, x, y, this);
if (scaled != null) {
x += original.getWidth();
y = (getHeight() - scaled.getHeight()) / 2;
g2d.drawImage(scaled, x, y, this);
}
}
g2d.dispose();
}
public BufferedImage getScaledInstanceToFit(BufferedImage img, Dimension size) {
float scaleFactor = getScaleFactorToFit(img, size);
return getScaledInstance(img, scaleFactor);
}
public float getScaleFactorToFit(BufferedImage img, Dimension size) {
float scale = 1f;
if (img != null) {
int imageWidth = img.getWidth();
int imageHeight = img.getHeight();
scale = getScaleFactorToFit(new Dimension(imageWidth, imageHeight), size);
}
return scale;
}
public float getScaleFactorToFit(Dimension original, Dimension toFit) {
float scale = 1f;
if (original != null && toFit != null) {
float dScaleWidth = getScaleFactor(original.width, toFit.width);
float dScaleHeight = getScaleFactor(original.height, toFit.height);
scale = Math.min(dScaleHeight, dScaleWidth);
}
return scale;
}
public float getScaleFactor(int iMasterSize, int iTargetSize) {
float scale = 1;
if (iMasterSize > iTargetSize) {
scale = (float) iTargetSize / (float) iMasterSize;
} else {
scale = (float) iTargetSize / (float) iMasterSize;
}
return scale;
}
public BufferedImage getScaledInstance(BufferedImage img, double dScaleFactor) {
BufferedImage imgBuffer = null;
imgBuffer = getScaledInstance(img, dScaleFactor, RenderingHints.VALUE_INTERPOLATION_BILINEAR, true);
return imgBuffer;
}
protected BufferedImage getScaledInstance(BufferedImage img, double dScaleFactor, Object hint, boolean higherQuality) {
int targetWidth = (int) Math.round(img.getWidth() * dScaleFactor);
int targetHeight = (int) Math.round(img.getHeight() * dScaleFactor);
int type = (img.getTransparency() == Transparency.OPAQUE)
? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
BufferedImage ret = (BufferedImage) img;
if (targetHeight > 0 || targetWidth > 0) {
int w, h;
if (higherQuality) {
w = img.getWidth();
h = img.getHeight();
} else {
w = targetWidth;
h = targetHeight;
}
do {
if (higherQuality && w > targetWidth) {
w /= 2;
if (w < targetWidth) {
w = targetWidth;
}
}
if (higherQuality && h > targetHeight) {
h /= 2;
if (h < targetHeight) {
h = targetHeight;
}
}
BufferedImage tmp = new BufferedImage(Math.max(w, 1), Math.max(h, 1), type);
Graphics2D g2 = tmp.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
g2.drawImage(ret, 0, 0, w, h, null);
g2.dispose();
ret = tmp;
} while (w != targetWidth || h != targetHeight);
} else {
ret = new BufferedImage(1, 1, type);
}
return ret;
}
}
}
You may, also, find The Perils of Image.getScaledInstance() of interest.
The issue you are seeing is actually related to the resampling filter used for downscaling. Obviously, the one used by your library is a bad one for the situation. Nearest neighbor, bilinear and bicubic are typical bad examples to be used when downscaling. I don't know the exact resampling filter Photoshop uses, but I used 3-lobed lanczos and got the following result:
So, to solve your problem, you need to use a smarter resampling filter.
dutchman, this is why I maintain the imgscalr library -- to make this kind of stuff painfully easy.
In your example, a single method call would do the trick, right after your first ImageIO.read line:
origImage = ImageIO.read(new File(sourceImg));
you can do the following to get what you want (javadoc for this method):
origImage = Scalr.resize(origImage, Method.ULTRA_QUALITY, 60);
and if that still looked a little jagged (because you are removing so much information from the image, you can add the following OP to the command to apply a light anti-aliasing filter to the image so it looks smoother):
origImage = Scalr.resize(origImage, Method.ULTRA_QUALITY, 60, Scalr.OP_ANTIALIAS);
That will replace all the remainder of the code logic you have. The only other thing I would recommend is saving out your really small samples as PNG's so there is no more compression/lossy conversion done on the image OR make sure you use little to none compression on the JPG if you really want it in JPG format. (Here is an article on how to do it; it utilizes the ImageWriteParam class)
imgscalr is licensed under an Apache 2 license and hosted on GitHub so you can do what you want with it; it also includes asynchronous scaling support if you are using the library in a server-side app and queuing up huge numbers of scaling operations and don't want to kill the server.
As already stated, Java's Graphics2D does not provide a very good algorithm for down-scaling. If you don't want to implement a sophisticated algorithm yourself you could try out the current open source libs specialized for this: Thumbnailator, imgscalr and a Java interface for ImageMagick.
While researching for a private project I tried them out (except ImageMagick) and here are the visual results with Photoshop as reference:
A. Thumbnailator 0.4.8 with default settings (no additional internal resizing)
B. imgscalr 4.2 with ULTRA_QUALTY setting
C. Photoshop CS5 bicubic filter (save for web)
D. Graphics2d with all HQ render hints
Here is the used code
Thumbnailator and PS create similar results, while imgscalr seems to be softer. It is subjective which one of the libs creates the preferable results. Another point to consider though is the performance. While Thumbnailator and Graphics2d have similar runtime, imgscalr is considerably slower (with ULTRA_QUALITY) in my benchmarks.
For more info, read this post providing more detail on this matter.
i have an application from which i want to print an image. The image is loaded as a BufferedImage object. The problem is, when i print the image (to the postscript or to the pdf file), the quality is really poor.
When i'm using some other tools (basically any picture viewer application which can print the image) the result is significantly better.
I know there can be some problems with the DPI vs resolution but i'm not exactly sure how to compute the correct values for printing.
I tried to google and tried some methods, but nothing seems to work as i expected.
Basicaly i just want to print an image (in resolution let's say 3000x2000) to a printer (with DPI for example 600x600).
This is how i create the print job:
PrintRequestAttributeSet printAttributes = new HashPrintRequestAttributeSet();
printAttributes.add(PrintQuality.HIGH);
printAttributes.add(new PrinterResolution(600, 600 PrinterResolution.DPI));
printAttributes.add(new Destination(URI.create("file:/tmp/test.ps")));
PageFormat pf = printerJob.defaultPage();
Paper paper = pf.getPaper();
double xMargin = 0.0;
double yMargin = 0.0;
paper.setImageableArea(xMargin, yMargin, paper.getWidth() - 2 * xMargin, paper.getHeight() - 2 * yMargin);
pf.setPaper(paper);
// create new Printable for the specified image
printerJob.setPrintable(PrintableImage.get(image), pf)
if (printerJob.printDialog(printAttributes)) {
printerJob.print(printAttributes);
}
Where image is BufferedImage and PrintableImage.get returns new instance which implements Printable
Then the actual print is doing this way (i let the commented code which i tried but didn't work for me)
#Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
if (image == null)
throw new PrinterException("no image specified to be printed");
// We have only one page, and 'page' is zero-based
if (pageIndex > 0) {
return NO_SUCH_PAGE;
}
// tranlate the coordinates (according to the orientations, margins, etc)
Graphics2D printerGraphics = (Graphics2D) graphics;
//g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
//g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
printerGraphics.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
// THIS IS A TEST - javax.printing api uses 72 DPI, but we have 600DPI set for the printer
//AffineTransform at = printerGraphics.getTransform();
//printerGraphics.scale((double)72 / (double)600, (double)72 / (double)600);
//printerGraphics.drawRenderedImage(image, null);
//printerGraphics.setTransform(at);
//if(printerGraphics != null)
//return PAGE_EXISTS;
double scale = 72.0 / 600.0;
Dimension pictureSize = new Dimension((int)Math.round(image.getWidth() / scale), (int) Math.round(image.getHeight() / scale));
// center the image horizontaly and verticaly on the page
int xMargin = (int) ((pageFormat.getImageableWidth() - image.getWidth()) / 2);
int yMargin = (int) ((pageFormat.getImageableHeight() - image.getHeight()) / 2);
xMargin = yMargin = 0;
System.out.println(String.format("page size [%.2f x %.2f], picture size [%.2f x %.2f], margins [%d x %d]", pageFormat.getImageableWidth(), pageFormat.getImageableHeight(), pictureSize.getWidth(), pictureSize.getHeight(), xMargin, yMargin));
printerGraphics.drawImage(image, xMargin, yMargin, (int)pageFormat.getWidth(), (int)pageFormat.getHeight(), null);
//printerGraphics.drawImage(image, 0, 0, null);
//printerGraphics.drawImage(image, xMargin, yMargin, pictureSize.width, pictureSize.height, null);
//printerGraphics.drawImage(image, xMargin, yMargin, (int) pageFormat.getImageableWidth(), (int) pageFormat.getImageableHeight(), 0, 0, pictureSize.width, pictureSize.height, null);
//printerGraphics.drawImage(image, 0, 0, (int) pageFormat.getWidth() - xMargin, (int) pageFormat.getHeight() - yMargin, 0, 0, pictureSize.width, pictureSize.height, null);
return PAGE_EXISTS;
}
Does anybody solves the same problem?
Any help would be appreciated.
Thanks
Matej
This is how I did it. It works smoothly. Note that this code snippet doesn't center the image.
public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException {
if (pageIndex > 0) {
return (NO_SUCH_PAGE);
} else {
double pageHeight = pageFormat.getImageableHeight(), pageWidth = pageFormat.getImageableWidth();
Graphics2D g2d = (Graphics2D) g;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
if (pageHeight < image.getHeight() || pageWidth < image.getWidth()) {
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.drawImage(image, 0, 0, (int) pageWidth, (int) pageHeight - textSize, null);
} else {
g2d.drawImage(image, 0, 0, null);
}
g2d.dispose();
return (PAGE_EXISTS);
}
}
#Viktor Fonic
Thank you for this, I was searching a lot to do this!
You're solution works perfectly, but has a small error,
textSize was not declared, so:
int textSize = (int) (pageHeight - image.getHeight()*pageWidth/image.getWidth());
I'm trying to print a JPanel with some painted graphics on it (overriding paintComponent). The graphics is so big that they wont fit on a single page and therefor I'm letting it span across multiple pages. My problem lies within the fact that if I let the user choose the pageFormat/Paper type by calling:
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PageFormat pf = printJob.pageDialog(aset);
printJob.setPrintable(canvas, pf);
When I'm writing my print() method (implementing Printable) in my JPanel class I can't seem to get the hold of the margins? I use graphics.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); to make it start drawing in the correct topleft corner (0;0) and it takes the margins into consideration (i.e.,starting more at (80; 100) or so). But then it prints over the bottom and right margin which I don't want it to do since that negates the user's wishes.
Here is the code of my print() method as a reference, which works fine when you don't let the user set the paper (using the default instead):
Rectangle[] pageBreaks;
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
//Calculate how many pages our print will be
if(pageBreaks == null){
double pageWidth = pageFormat.getPaper().getWidth();
double pageHeight = pageFormat.getPaper().getHeight();
//Find out how many pages we need
int numberOfPagesHigh = (int) Math.ceil(size.getHeight()/pageHeight);
int numberOfPagesWide = (int) Math.ceil(size.getWidth()/pageWidth);
pageBreaks = new Rectangle[numberOfPagesHigh*numberOfPagesWide];
double x = 0;
double y = 0;
int curXPage = 0;
//Calculate what we will print on each page
for (int i = 0; i < pageBreaks.length; i++){
double xStart = x;
double yStart = y;
x += pageWidth;
pageBreaks[i] = new Rectangle((int)xStart, (int)yStart, (int)pageWidth, (int)pageHeight);
curXPage++;
if (curXPage > numberOfPagesWide){
curXPage = 0;
x = 0;
y += pageHeight;
}
}
}
if (pageIndex < pageBreaks.length){
//Cast graphics to Graphics2D for richer API
Graphics2D g2d = (Graphics2D) graphics;
//Translate into position of the paper
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
//Setup our current page
Rectangle rect = pageBreaks[pageIndex];
g2d.translate(-rect.x, -rect.y);
g2d.setClip(rect.x, rect.y, rect.width, rect.height);
//Paint the component on the graphics object
Color oldBG = this.getBackground();
this.setBackground(Color.white);
util.PrintUtilities.disableDoubleBuffering(this);
this.paintComponent(g2d);
util.PrintUtilities.enableDoubleBuffering(this);
this.setBackground(oldBG);
//Return
return PAGE_EXISTS;
}
else {
return NO_SUCH_PAGE;
}
}
After posting this question and tabbing back into my IDE I pretty easily found the answer. Instead of using
double pageWidth = pageFormat.getPaper().getWidth();
double pageHeight = pageFormat.getPaper().getHeight();
use
double pageWidth = pageFormat.getImageableWidth();
double pageHeight = pageFormat.getImageableHeight();
The getImageableWidth() returns the totalPaperWidth-totalMargins whereas getWidth() just returns totalPaperWidth. This makes the print() method not draw more that it can on each page!