128 lines
4.6 KiB
Java
128 lines
4.6 KiB
Java
|
package com.zcloud.util;
|
||
|
|
||
|
import java.awt.*;
|
||
|
import java.awt.Insets;
|
||
|
import java.awt.image.BufferedImage;
|
||
|
import java.io.ByteArrayInputStream;
|
||
|
import java.io.ByteArrayOutputStream;
|
||
|
import java.io.File;
|
||
|
import java.io.FileInputStream;
|
||
|
import java.io.FileNotFoundException;
|
||
|
import java.io.FileOutputStream;
|
||
|
import java.io.IOException;
|
||
|
import java.io.InputStream;
|
||
|
import java.io.StringReader;
|
||
|
import java.net.URL;
|
||
|
import java.net.URLDecoder;
|
||
|
import java.security.InvalidParameterException;
|
||
|
|
||
|
import javax.imageio.ImageIO;
|
||
|
import javax.imageio.stream.ImageOutputStream;
|
||
|
import javax.print.*;
|
||
|
import javax.print.attribute.DocAttributeSet;
|
||
|
import javax.print.attribute.HashDocAttributeSet;
|
||
|
import javax.print.attribute.HashPrintRequestAttributeSet;
|
||
|
import javax.print.attribute.PrintRequestAttributeSet;
|
||
|
import javax.print.attribute.standard.MediaSizeName;
|
||
|
import javax.print.attribute.standard.OrientationRequested;
|
||
|
|
||
|
/*import com.aspose.words.Document;
|
||
|
import com.aspose.words.SaveFormat;
|
||
|
import com.itextpdf.text.pdf.PdfReader;*/
|
||
|
|
||
|
public class PrintUtil {
|
||
|
// static final int wdDoNotSaveChanges = 0;// 不保存待定的更改。
|
||
|
// static final int wdFormatPDF = 17;// word转PDF 格式
|
||
|
//
|
||
|
public static void print(String filePath,String content) {
|
||
|
try {
|
||
|
File file = new File(filePath);
|
||
|
// File file = new File("C:\\\\Users\\\\Administrator\\\\Desktop\\\\测试用图\\\\QQ图片20201013090855.jpg");
|
||
|
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
|
||
|
if (file != null) {
|
||
|
BufferedImage img = ImageIO.read(file);
|
||
|
BufferedImage DestImage = null;
|
||
|
int w1 = img.getWidth();
|
||
|
int h1 = img.getHeight();
|
||
|
int c = (int)Math.ceil((double)h1/1000);//根据图片大小缩放文字
|
||
|
int[] ImageArrayOne = new int[w1 * h1];
|
||
|
ImageArrayOne = img.getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 逐行扫描图像中各个像素的RGB到数组中
|
||
|
DestImage = new BufferedImage(w1, h1 + (c*20), BufferedImage.TYPE_INT_RGB);
|
||
|
DestImage.setRGB(0, c*20, w1, h1, ImageArrayOne, 0, w1); // 设置下半部分的RGB
|
||
|
Graphics g = DestImage.getGraphics();
|
||
|
Font fTxtBottom = new Font("微软雅黑", Font.PLAIN, c*15);
|
||
|
Color myColorTxtBottom = Color.WHITE; //图片颜色
|
||
|
g.setColor(myColorTxtBottom);
|
||
|
g.setFont(fTxtBottom);
|
||
|
// g.drawString(文字, x 位置, y 位置);
|
||
|
g.drawString(content, 5,c*15);
|
||
|
g.dispose();
|
||
|
// String imgName = "C:\\Users\\Administrator\\Desktop\\测试用图\\1.jpg"; //生成的图片名称
|
||
|
// File newFile = new File(imgName); //生成新的图片
|
||
|
//
|
||
|
//// 此处要写PNG否则可能会出现遮罩层
|
||
|
// ImageIO.write(DestImage, "png", newFile);
|
||
|
|
||
|
ByteArrayOutputStream bs = new ByteArrayOutputStream();
|
||
|
ImageOutputStream imOut = ImageIO.createImageOutputStream(bs);
|
||
|
ImageIO.write(DestImage, "png",imOut);
|
||
|
DocPrintJob jon = printService.createPrintJob();
|
||
|
// 设置纸张大小,也可以新建MediaSize类来自定义大小
|
||
|
// 文件类型
|
||
|
DocFlavor flavor = DocFlavor.INPUT_STREAM.PNG;
|
||
|
|
||
|
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
|
||
|
pras.add(MediaSizeName.ISO_A4);
|
||
|
if(w1>h1) {
|
||
|
pras.add(OrientationRequested.LANDSCAPE);//横向打印
|
||
|
}
|
||
|
DocAttributeSet das = new HashDocAttributeSet();
|
||
|
|
||
|
InputStream input = new ByteArrayInputStream(bs.toByteArray());
|
||
|
Doc doc = new SimpleDoc(input, flavor, das);
|
||
|
jon.print(doc, pras);
|
||
|
}
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
// public static void word2pdf(String source, String target) {
|
||
|
//
|
||
|
// FileOutputStream os = null;
|
||
|
// try {
|
||
|
// File file = new File(target); // 新建一个空白pdf文档
|
||
|
// os = new FileOutputStream(file);
|
||
|
// Document doc = new Document(source); // Address是将要被转化的word文档
|
||
|
// doc.save(os, SaveFormat.PDF);
|
||
|
// System.out.println(doc.getPageCount());
|
||
|
// } catch (Exception e) {
|
||
|
// e.printStackTrace();
|
||
|
// } finally {
|
||
|
// if (os != null) {
|
||
|
// try {
|
||
|
// os.close();
|
||
|
// } catch (IOException e) {
|
||
|
// e.printStackTrace();
|
||
|
// }
|
||
|
// }
|
||
|
// }
|
||
|
// }
|
||
|
|
||
|
// public static int getPdfPage(String filepath){
|
||
|
// int pagecount = 0;
|
||
|
// PdfReader reader;
|
||
|
// try {
|
||
|
// reader = new PdfReader(filepath);
|
||
|
// pagecount= reader.getNumberOfPages();
|
||
|
// } catch (IOException e) {
|
||
|
// e.printStackTrace();
|
||
|
// }
|
||
|
// System.out.println(pagecount);
|
||
|
// return pagecount;
|
||
|
// }
|
||
|
|
||
|
public static void main(String[] args) {
|
||
|
print(null,null);
|
||
|
}
|
||
|
}
|