简体   繁体   中英

Printing Multiple pages in JAVA

So I was tinkering with this code that I found I altered the page size to 300, 400 when I tried printing to XPS the first page is always default and the following is 300,400, I want to is set all printed pages to 300,400 找到了这个代码,当我第一次尝试打印到XPS时,将页面大小更改为300、400页面始终是默认页面,以下是300,400,我想将所有打印页面设置为300,400

below is the code

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);
}
}          

Try to use the Book class which is used in this thread .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM