我有一个 JSP 页面,它只是获取参数、查询数据库、生成发票 PDF 并以静默方式发送到默认打印机。我使用 itext 库。
发票必须在带有连续纸的点阵打印机上打印。
每个发票页面大小为 a5 横向大小。
如果我选择页面大小为 a5,代码会生成一个 PDF,如下所示
打印时,它会打印一页并将另一页留空。用户必须手动向后滚动纸张。
如果我将页面大小选择为 a5 横向 (a5.rotate()),代码会生成一个 PDF,就像它在此处看到的那样,这更好。
但是当它打印在纸上时,它开始垂直打印页面,因为打印机有一个 a4 纸盘。
在我看来,我需要将我的打印机定义为带有连续纸的点阵打印机。
我正在使用的代码是:
Document document = new Document(PageSize.A5,0,0,0,0);
try {
PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
writer.addViewerPreference(PdfName.PRINTSCALING, PdfName.NONE);
document.open();
StringBuffer javascript = new StringBuffer();
javascript.append("this.print({bUI: false, bSilent: true, bShrinkToFit: true});");
PdfAction pdfAction= PdfAction.javaScript(javascript.toString(), writer);
writer.addJavaScript(pdfAction);
writer.addViewerPreference(PdfName.PRINTSCALING, PdfName.NONE);
PdfPTable table = new PdfPTable(3); // 3 columns.
table.setWidthPercentage(100);
PdfPCell cell1 = new PdfPCell(new Paragraph(""));
PdfPCell cell2 = new PdfPCell(new Paragraph(""));
PdfPCell cell3 = new PdfPCell(new Paragraph(MakbuzNo,FontFactory.getFont(FontFactory.COURIER,9)));
cell3.setLeading(16f, 0f);
cell1.setBorder(Rectangle.NO_BORDER);
cell2.setBorder(Rectangle.NO_BORDER);
cell3.setBorder(Rectangle.NO_BORDER);
cell3.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
cell1 = new PdfPCell(new Paragraph(""));
cell2 = new PdfPCell(new Paragraph(""));
cell3 = new PdfPCell(new Paragraph(Duzenleyen,FontFactory.getFont(FontFactory.COURIER,9)));
cell3.setLeading(16f, 0f);
cell3.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell1.setBorder(Rectangle.NO_BORDER);
cell2.setBorder(Rectangle.NO_BORDER);
cell3.setBorder(Rectangle.NO_BORDER);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
cell1 = new PdfPCell(new Paragraph(MSISDN,FontFactory.getFont(FontFactory.COURIER,9)));
cell2 = new PdfPCell(new Paragraph(""));
cell3 = new PdfPCell(new Paragraph(DuzenlemeSaati,FontFactory.getFont(FontFactory.COURIER,9)));
cell1.setLeading(16f, 0f);
cell3.setLeading(16f, 0f);
cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
cell3.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell1.setBorder(Rectangle.NO_BORDER);
cell2.setBorder(Rectangle.NO_BORDER);
cell3.setBorder(Rectangle.NO_BORDER);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
cell1 = new PdfPCell(new Paragraph(""));
cell2 = new PdfPCell(new Paragraph(""));
cell3 = new PdfPCell(new Paragraph(DuzenlemeTarihi,FontFactory.getFont(FontFactory.COURIER,9)));
cell3.setLeading(16f, 0f);
cell3.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell1.setBorder(Rectangle.NO_BORDER);
cell2.setBorder(Rectangle.NO_BORDER);
cell3.setBorder(Rectangle.NO_BORDER);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
cell1 = new PdfPCell(new Paragraph(" "));
cell2 = new PdfPCell(new Paragraph(" "));
cell3 = new PdfPCell(new Paragraph(" "));
cell1.setLeading(45f, 0f);
cell1.setBorder(Rectangle.NO_BORDER);
cell2.setBorder(Rectangle.NO_BORDER);
cell3.setBorder(Rectangle.NO_BORDER);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
cell1 = new PdfPCell(new Paragraph(izahat,FontFactory.getFont(FontFactory.COURIER,9)));
cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
cell1.setColspan(3);
cell1.setBorder(Rectangle.NO_BORDER);
table.addCell(cell1);
cell1 = new PdfPCell(new Paragraph(" "));
cell2 = new PdfPCell(new Paragraph(" "));
cell3 = new PdfPCell(new Paragraph(" "));
cell1.setLeading(75f, 0f);
cell1.setBorder(Rectangle.NO_BORDER);
cell2.setBorder(Rectangle.NO_BORDER);
cell3.setBorder(Rectangle.NO_BORDER);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
cell1 = new PdfPCell(new Paragraph(kopyayazi,FontFactory.getFont(FontFactory.COURIER,9)));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
cell1.setColspan(3);
cell1.setBorder(Rectangle.NO_BORDER);
table.addCell(cell1);
cell1 = new PdfPCell(new Paragraph(TutarYazi,FontFactory.getFont(FontFactory.COURIER,9)));
cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
cell1.setColspan(2);
cell3 = new PdfPCell(new Paragraph(ToplamTutar,FontFactory.getFont(FontFactory.COURIER,9)));
cell3.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell1.setLeading(16f, 0f);
cell3.setLeading(16f, 0f);
cell1.setBorder(Rectangle.NO_BORDER);
cell3.setBorder(Rectangle.NO_BORDER);
table.addCell(cell1);
table.addCell(cell3);
document.add(table);
document.newPage();
} catch (DocumentException de) {
de.printStackTrace();
System.err.println("document: " + de.getMessage());
}
document.close();
那么如何在连续纸上使用点阵打印机并在页面上的字符已经打印时停止纸卷轴?