简体   繁体   English

JavaFX:使用DocPrintJob打印时更改字体大小吗?

[英]JavaFX: change font size on printing with DocPrintJob?

How can I change font size for printing job in JAVA? 如何在JAVA中更改打印作业的字体大小?

TXTFile file = mainApp.getFilesData().get(i);

InputStream stream = new ByteArrayInputStream(file.getModifiedContent().getBytes(StandardCharsets.UTF_8));
PrintService service = PrintServiceLookup.lookupDefaultPrintService();

PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(1));
pras.add(MediaSizeName.ISO_A4);
pras.add(OrientationRequested.PORTRAIT);

DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc doc = new SimpleDoc(stream, flavor, null);
DocPrintJob job = service.createPrintJob();

PrintJobWatcher pjw = new PrintJobWatcher(job);
job.print(doc, pras);
pjw.waitForDone();
stream.close();

Thank you for any suggestions. 感谢您的任何建议。

Finally I found the working solution with printing multiple pages. 最后,我找到了打印多页的可行解决方案。

TXTFile file = mainApp.getFilesData().get(i);

PrintService service = PrintServiceLookup.lookupDefaultPrintService();

PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(MediaSizeName.ISO_A4);

Doc doc = new SimpleDoc(new PrintableLog(file.getModifiedContent(), lineSeparator), DocFlavor.SERVICE_FORMATTED.PRINTABLE, null);
DocPrintJob job = service.createPrintJob();

PrintJobWatcher pjw = new PrintJobWatcher(job);
job.print(doc, pras);
pjw.waitForDone();

Printable class: 可打印类:

package com.txtlogger.logger.libraries;

import java.awt.*;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;

public class PrintableLog implements Printable {

    private int[] pageBreaks;
    private String strContent;
    private String lineSeparator;
    private int pageVerticalMargin = 40;

    public PrintableLog(String strContent, String lineSeparator) {
        this.strContent = strContent;
        this.lineSeparator = lineSeparator;
    }

    @Override
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {

        String[] content = getLinesFromContent(strContent);

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

        if (pageBreaks == null) {
            int linesPerPage = (int)((pageFormat.getImageableHeight()-pageVerticalMargin*2)/lineHeight);
            int numBreaks = (content.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)graphics;
        g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

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

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

    }

    private String[] getLinesFromContent(String content) {
        return content.split(this.lineSeparator);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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