這是我找到了解決辦法的基礎上,飛碟/ iText的:
// A4 paper size = 297x210 mm
int PAGE_WIDTH = 2100;
int PAGE_LENGTH = 2970;
float MARGIN = 19.05f; // 0.75 inch
int width = PAGE_WIDTH;
// Java2DRenderer.NO_HEIGHT = -1 (it's private)
// if used the required height is computed
Java2DRenderer renderer = new Java2DRenderer(dom, width, -1);
BufferedImage img = renderer.getImage();
// Adjusts width based on required height
int height = img.getHeight();
width = (int) Math.round(height * PAGE_WIDTH/PAGE_LENGTH);
// Render same document again, now with the right dimensions
renderer = new Java2DRenderer(dom, width, height);
img = renderer.getImage();
com.lowagie.text.Document pdf = new com.lowagie.text.Document(PageSize.A4);
pdf.setMargins(MARGIN,MARGIN,MARGIN,MARGIN);
PdfWriter.getInstance(pdf, new FileOutputStream(args[0]+".pdf"));
pdf.open();
com.lowagie.text.Image pdfImage = com.lowagie.text.Image.getInstance(img,Color.WHITE);
Rectangle ps = pdf.getPageSize();
pdfImage.scaleAbsolute(
ps.getWidth()-pdf.leftMargin()-pdf.rightMargin(),
ps.getHeight()-pdf.topMargin()-pdf.bottomMargin()
);
pdf.add(pdfImage);
pdf.close();