在JAVA中打印多个页面

时间:2013-08-25 09:27:50

标签: java swing printing

所以我正在修补我发现 http://docs.oracle.com/javase/tutorial/2d/printing/examples/PaginationExample.java 的代码 当我尝试打印到XPS时,我将页面大小更改为300,400,第一页始终是默认值,以下是300,400,我想将所有打印页面设置为300,400

下面是代码

package app;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.print.*;

public class PaginationExample implements Printable, ActionListener {

int[] pageBreaks;  // array of page break line positions.

/* Synthesise some sample lines of text */
String[] textLines;

private void initTextLines() {
    if (textLines == null) {
        int numLines = 200;
        textLines = new String[numLines];
        for (int i = 0; i < numLines; i++) {
            textLines[i] = "This is line number " + i;
        }
    }
}

public int print(Graphics g, PageFormat pf, int pageIndex)
        throws PrinterException {
    pf.setPaper(paper);


    Font font = new Font("Serif", Font.PLAIN, 10);
    FontMetrics metrics = g.getFontMetrics(font);
    int lineHeight = metrics.getHeight();

    if (pageBreaks == null) {
        initTextLines();
        int linesPerPage = (int) (pf.getImageableHeight() / lineHeight);
        int numBreaks = (textLines.length - 1) / linesPerPage;
        pageBreaks = new int[numBreaks];

        for (int b = 0; b < numBreaks; b++) {
            pageBreaks[b] = (b + 1) * linesPerPage;
        }
    }

    if (pageIndex > pageBreaks.length) {
        return NO_SUCH_PAGE;
    }

    /* User (0,0) is typically outside the imageable area, so we must
     * translate by the X and Y values in the PageFormat to avoid clipping
     * Since we are drawing text we
     */

    Graphics2D g2d = (Graphics2D) g;
    g2d.translate(pf.getImageableX(), pf.getImageableY());

    /* Draw each line that is on this page.
     * Increment 'y' position by lineHeight for each line.
     */
    int y = 0;
    int start = (pageIndex == 0) ? 0 : pageBreaks[pageIndex - 1];
    int end = (pageIndex == pageBreaks.length)
            ? textLines.length : pageBreaks[pageIndex];
    for (int line = start; line < end; line++) {
        y += lineHeight;
        g.drawString(textLines[line], 0, y);
    }

    /* tell the caller that this page is part of the printed document */
    return PAGE_EXISTS;
}

public void actionPerformed(ActionEvent e) {
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(this);
    boolean ok = job.printDialog();
    if (ok) {
        try {
            job.print();
        } catch (PrinterException ex) {
            /* The job did not successfully complete */
        }
    }
}
private static Paper paper;
public static void main(String args[]) {

    paper = new Paper();
    paper.setSize(300, 400);


    try {
        String cn = UIManager.getSystemLookAndFeelClassName();
        UIManager.setLookAndFeel(cn); // Use the native L&F
    } catch (Exception cnf) {
    }
    JFrame f = new JFrame("Printing Pagination Example");
    f.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });
    JButton printButton = new JButton("Print Pages");
    printButton.addActionListener(new PaginationExample());
    f.add("Center", printButton);
    f.pack();
    f.setVisible(true);
}
}          

1 个答案:

答案 0 :(得分:1)

尝试使用此Book中使用的thread类。

Book pBook = new Book();
pBook.append(this, pPageFormat);
pPrinterJob.setPageable(pBook);
pPrinterJob.print();