我的问题是我无法正确打印页面:打印机总是在页面的左右边缘空白。我尝试设置不同的MediaPrintableArea,但结果始终相同。例如,我尝试过: attribute.add(new MediaPrintableArea(159,246,width-20,height-20,MediaPrintableArea.MM));但我无法正常工作。这是我的代码:
import java.awt.Graphics;
import java.awt.List;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.IOException;
import javax.print.attribute.Attribute;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.Media;
import javax.print.attribute.standard.MediaPrintableArea;
import javax.print.attribute.standard.MediaSize;
import javax.print.attribute.standard.MediaTray;
import javax.print.attribute.standard.OrientationRequested;
import javax.print.attribute.standard.Sides;
import org.apache.commons.io.FileUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.PDPageContentStream.AppendMode;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.printing.Orientation;
import org.apache.pdfbox.printing.PDFPageable;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.tools.imageio.ImageIOUtil;
import org.ghost4j.document.Document;
import org.ghost4j.renderer.SimpleRenderer;
public class STAMPA {
public static void main (String[] args) throws InvalidPasswordException, IOException {
PrinterJob job = PrinterJob.getPrinterJob();
PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
boolean ok = job.printDialog(attributes);
System.setProperty("sun.java2d.cmm", "sun.java2d.cmm.kcms.KcmsServiceProvider");
PDDocument documentAllegato = PDDocument.load(new File("C:\\19001925_20190225_405583658.pdf"));
job.setPageable(new PDFPageable(documentAllegato, Orientation.AUTO, false, 300));
Attribute[] attributeArray2 = attributes.toArray();
Attribute copies2 = attributes.get(Copies.class);
Attribute media2 = attributes.get(Media.class);
Attribute mediaPrintableArea2 = attributes.get(MediaPrintableArea.class);
Attribute mediaTray2 = attributes.get(MediaTray.class);
Attribute orientationRequested2 = attributes.get(OrientationRequested.class);
Attribute sides2 = attributes.get(Sides.class);
attributes.remove(Sides.class);
attributes.add(Sides.DUPLEX);
try {
job.print();
} catch (PrinterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
如何为A4 ISO格式设置合适的页边距?