package com.yumu.airshow.Controller;
import com.yumu.airshow.Model.PrintItem;
import com.yumu.airshow.Model.PrintModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.imageio.ImageIO;
import javax.print.DocFlavor;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.HashPrintRequestAttributeSet;
import java.awt.image.BufferedImage;
import java.awt.print.*;
import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
/*
A0=1189*841
A1=841*594
A2=594*420
A3=420*297
A4=210*297 长宽是8.27英寸*11.69英寸
* */
/*
* paper.setSize(width,height)
* 纸张高宽=纸张英寸*72 = 厘米/2.54*72
* 比如A4=21/2.54*72=595
* */
/*实现套打,把套打的格式当成一张图片,将要填入的数据按其在图片在坐标来定位*/
@Controller
@ResponseBody
@RequestMapping("/print")
@ApiOperation("打印票证信息")
public class PrintController implements Printable {
private PrintItem value = null;
/**
* implements Printable
* PageFormat类描述了页面的定位信息、它的尺寸以及每英尺可绘制的区域(单位1/72nd)。
*/
public int print(Graphics g, PageFormat pf, int pageIndex) {
// 设置一次打印任务总打印页数
if (pageIndex > 9999) {
return Printable.NO_SUCH_PAGE;
}
Graphics2D g2d = (Graphics2D) g;
for (PrintModel printModel : value.getParams()) {
float x = printModel.getX();
float y = printModel.getY();
int type = printModel.getType();
String content = printModel.getContent();//数据或者链接
/*数据类型为普通string*/
if (type == 0) {
String strFont = printModel.getFont();
Color color = printModel.getColor();
int size = 24;//设置默认字体大小
size = printModel.getSize();
if(strFont == null) strFont = "新宋体";//设置默认字体
Font font = new Font(strFont, Font.PLAIN, size);
g2d.setFont(font);
g2d.setColor(Color.blue);
g2d.drawString(content,x,y);
}
/*图片类型的参数*/
if (type == 1) {
float width = printModel.getWidth();
float height = printModel.getHeight();
BufferedImage image2 = null;
try {
image2 = ImageIO.read(new FileInputStream(new File(content)));
} catch (Exception e) {
e.printStackTrace();
}
g2d.drawImage(image2, (int)x, (int)y, (int)width, (int)height, null);
}
}
return Printable.PAGE_EXISTS;
}
@PostMapping("/detail")
public void main(@Validated @RequestBody List<PrintItem> printItems, BindingResult bindingResult) {
printReport(printItems);
}
@PostMapping("/printServices")
public PrintService[] PrintService(){
HashPrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
//查找所有的可用的打印服务
PrintService[] printService = PrintServiceLookup.lookupPrintServices(flavor, pras);
System.out.println("this is name;"+printService[0].getName());
return printService;
}
public void printReport(List<PrintItem> printItems) {
PrinterJob printerJob = PrinterJob.getPrinterJob();//创建一个打印任务
PageFormat pageFormat = PrinterJob.getPrinterJob().defaultPage();
Paper paper = pageFormat.getPaper();
// 设置页面高和宽,A4纸为 595,842 长宽是8.27英寸*11.69英寸 单位为英寸 *72
double pageWidth = 595;
double pageHeight = 842;
paper.setSize(pageWidth, pageHeight);
paper.setImageableArea(0, 0, pageWidth, pageHeight);//成像大小
pageFormat.setOrientation(PageFormat.PORTRAIT); //设置打印方向,LANDSCAPE为横向,PORTRAIT打印方向默认为纵向
pageFormat.setPaper(paper);
Book book = new Book();
for (PrintItem printItem : printItems) {
PrintController printController = new PrintController();
printController.setValue(printItem);
book.append(printController, pageFormat);//一个打印任务一般打印一张图,如果需要打印多张图,则使用book相关方法。打印文档添加多页
}
printerJob.setPageable(book);
if (printerJob.printDialog()) { //弹出打印对话框,打印对话框,用户可以通过它改变各种选项,例如:设置打印副本数目,页面方向,或者目标打印机。
try {
printerJob.print();
} catch (PrinterException e) {
e.printStackTrace();
}
}
}
/**
* @return 返回 value。
*/
public PrintItem getValue() {
return value;
}
/**
* @param value 要设置的 value。
*/
public void setValue(PrintItem value) {
this.value = value;
}
}
package com.yumu.airshow.Controller;
import com.yumu.airshow.Model.PrintItem;
import com.yumu.airshow.Model.PrintModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.imageio.ImageIO;
import javax.print.DocFlavor;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.HashPrintRequestAttributeSet;
import java.awt.image.BufferedImage;
import java.awt.print.*;
import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
/*
A0=1189*841
A1=841*594
A2=594*420
A3=420*297
A4=210*297 长宽是8.27英寸*11.69英寸
* */
/*
* paper.setSize(width,height)
* 纸张高宽=纸张英寸*72 = 厘米/2.54*72
* 比如A4=21/2.54*72=595
* */
/*实现套打,把套打的格式当成一张图片,将要填入的数据按其在图片在坐标来定位*/
@Controller
@ResponseBody
@RequestMapping("/print")
@ApiOperation("打印票证信息")
public class PrintController implements Printable {
private PrintItem value = null;
/**
* implements Printable
* PageFormat类描述了页面的定位信息、它的尺寸以及每英尺可绘制的区域(单位1/72nd)。
*/
public int print(Graphics g, PageFormat pf, int pageIndex) {
// 设置一次打印任务总打印页数
if (pageIndex > 9999) {
return Printable.NO_SUCH_PAGE;
}
Graphics2D g2d = (Graphics2D) g;
for (PrintModel printModel : value.getParams()) {
float x = printModel.getX();
float y = printModel.getY();
int type = printModel.getType();
String content = printModel.getContent();//数据或者链接
/*数据类型为普通string*/
if (type == 0) {
String strFont = printModel.getFont();
Color color = printModel.getColor();
int size = 24;//设置默认字体大小
size = printModel.getSize();
if(strFont == null) strFont = "新宋体";//设置默认字体
Font font = new Font(strFont, Font.PLAIN, size);
g2d.setFont(font);
g2d.setColor(Color.blue);
g2d.drawString(content,x,y);
}
/*图片类型的参数*/
if (type == 1) {
float width = printModel.getWidth();
float height = printModel.getHeight();
BufferedImage image2 = null;
try {
image2 = ImageIO.read(new FileInputStream(new File(content)));
} catch (Exception e) {
e.printStackTrace();
}
g2d.drawImage(image2, (int)x, (int)y, (int)width, (int)height, null);
}
}
return Printable.PAGE_EXISTS;
}
@PostMapping("/detail")
public void main(@Validated @RequestBody List<PrintItem> printItems, BindingResult bindingResult) {
printReport(printItems);
}
@PostMapping("/printServices")
public PrintService[] PrintService(){
HashPrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
//查找所有的可用的打印服务
PrintService[] printService = PrintServiceLookup.lookupPrintServices(flavor, pras);
System.out.println("this is name;"+printService[0].getName());
return printService;
}
public void printReport(List<PrintItem> printItems) {
PrinterJob printerJob = PrinterJob.getPrinterJob();//创建一个打印任务
PageFormat pageFormat = PrinterJob.getPrinterJob().defaultPage();
Paper paper = pageFormat.getPaper();
// 设置页面高和宽,A4纸为 595,842 长宽是8.27英寸*11.69英寸 单位为英寸 *72
double pageWidth = 595;
double pageHeight = 842;
paper.setSize(pageWidth, pageHeight);
paper.setImageableArea(0, 0, pageWidth, pageHeight);//成像大小
pageFormat.setOrientation(PageFormat.PORTRAIT); //设置打印方向,LANDSCAPE为横向,PORTRAIT打印方向默认为纵向
pageFormat.setPaper(paper);
Book book = new Book();
for (PrintItem printItem : printItems) {
PrintController printController = new PrintController();
printController.setValue(printItem);
book.append(printController, pageFormat);//打印文档添加多页
}
printerJob.setPageable(book);
if (printerJob.printDialog()) { //弹出打印对话框,打印对话框,用户可以通过它改变各种选项,例如:设置打印副本数目,页面方向,或者目标打印机。
try {
printerJob.print();
} catch (PrinterException e) {
e.printStackTrace();
}
}
}
/**
* @return 返回 value。
*/
public PrintItem getValue() {
return value;
}
/**
* @param value 要设置的 value。
*/
public void setValue(PrintItem value) {
this.value = value;
}
}