如何在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();
感谢您提出任何建议。
答案 0 :(得分:0)
最后,我找到了打印多页的工作解决方案。
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();
可打印课程:
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);
}
}