我正在尝试自动化为我们公司打印运输标签的过程。我有一个程序接收电子邮件并从中收集我们需要的所有信息并将其放入HTML文件中。我正在使用HTML,因为我们想要包含我们的徽标,在线托管的JPG文件(如果您知道如何在不使用可行的HTML的情况下执行此操作,请分享)。然后我将该文件设置为自动打印到默认打印机而不显示对话框。问题是,打印出来的是文件的文本,这意味着打印出所有html标签。这是我的代码(对不起它有点凌乱,当我开始工作时我会清理它。)
import java.util.*;
import java.io.*;
import java.io.File;
import java.lang.Object;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import javax.print.*;
import javax.print.attribute.*;
import java.awt.print.*;
import java.awt.Desktop;
import javax.print.*;
import javax.print.attribute.*;
import java.io.*;
import java.awt.*;
public class Main2
{
public Main2()
{
Path file = Paths.get("D:\\email\\2016.txt");
ArrayList<String> text = new ArrayList<String>(1000);
boolean stop = false;
int i = 0;
String line1;
String line2;
File myFile = new File("");
try{
File printme = File.createTempFile("printme", ".html", new File("D:\\email\\output\\"));
myFile.deleteOnExit();
String pathName = (myFile.getAbsolutePath());
BufferedWriter bw = new BufferedWriter(new FileWriter(printme));
bw.write("<html> <head> <meta name=vs_targetSchema content=\"http://schemas.microsoft.com/intellisense/ie5\"><style type=\"text/css\"> A { text-decoration: none; } A:link { color: #3366cc; text- decoration: none; } A:visited { color: #663399; text-decoration: none; } A:active { color: #cccccc; text-decoration: none; } A:Hover { text-decoration: underline; } BODY, TD, CENTER, P { font-family: Geneva, Verdana, Arial, Helvetica; font-size: 12px; color: #333333; } .body { font-family: Geneva, Verdana, Arial, Helvetica; font-size: 10px; color: #333333; } .content { font- family: Arial, Helvetica, sans-serif; font-size: 11px; font-weight: normal; color: #000000; } .title { font-family: Helvetica, Arial, sans-serif; font- size: 10px; font-weight: normal; color: #000000; } .headline { font-family: Helvetica, Arial, sans-serif; font-size: 14px; font-weight: bold; color: #000000; } .message { font-family: Geneva, Verdana, Arial, Helvetica; font- size: 9px; } </style> </head><body bgcolor=\"#ffffff\" LINK=\"#3366cc\" VLINK=\"#3366cc\" ALINK=\"#3366cc\" LEFTMARGIN=\"0\" TOPMARGIN=\"0\"> <table cellSpacing=1 cellPadding=3 width=\"100%\" border=\"0\" runat=\"server\"> <tr> <td colSpan=1><IMG src=\"http://www.eshanes.com//Images/eshaneslogo.jpg\" border=0></td> </tr>");
bw.write("<TD class=FormLabel vAlign=top align=left width=\"50%\"><br><h3><b>");
BufferedReader reader = Files.newBufferedReader(file, Charset.defaultCharset());
line1 = reader.readLine();
line1 = reader.readLine();
line1 = reader.readLine();
while(stop == false)
{
line1 = reader.readLine();
line2 = reader.readLine();
if ((line1.length() >= 22) && (line1.substring(0, 21).equals("Special Instructions:")))
{
stop = true;
break;
}
else if ((line2.length() >= 22) && (line2.substring(0, 21).equals("Special Instructions:")))
{
bw.write("<TD class=FormLabel vAlign=top align=left width=\"50%\"><h3><b> <br><br><b>");
bw.write(line1);
bw.write("</h3></b>");
stop = true;
break;
}
else
{
bw.write(line1);
bw.write("<br>");
bw.write(line2);
bw.write("<br>");
}
}
bw.write("</h3></b> </TD>");
reader.close();
bw.close();
print(printme);
}catch(Exception e){
e.printStackTrace();
}
}
public static void print(File file) {
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
// PrintService service = ServiceUI.printDialog(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefau ltScreenDevice().getDefaultConfiguration(), 200, 200,
// printService, defaultService, flavor, pras);
//if (service != null) {
DocPrintJob job = defaultService.createPrintJob();
try{
FileInputStream fis = new FileInputStream(file);
DocAttributeSet das = new HashDocAttributeSet();
Doc document = new SimpleDoc(fis, flavor, das);
job.print(document, pras);
//Thread.sleep(10000);
} catch (IOException ioe) {
ioe.printStackTrace();
}
catch (Exception e) {
}
}
}
请帮我解决这个问题。
答案 0 :(得分:0)
您似乎想要的是解析HTML,然后像浏览器那样进行布局和渲染。从头开始,这是一项相当重要的任务。
您可能想要的是找到一个现有的布局引擎,您可以将HTML传递到该引擎,然后返回渲染的文档/图像/其他内容。
答案 1 :(得分:0)
您需要先呈现HTML。 你可以使用JEditorPane。只需将内容类型设置为“text / html”,即可设置内容。现在,您将获得渲染内容的快照,如下所示
JEditorPane jp = new JEditorPane("text/html", textString);
jp.validate();
int w = jp.getWidth(), h = jp.getHeight();
BufferedImage saveimg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = saveimg.createGraphics();
jp.paint(g2);
现在,使用图形对象进行打印。
答案 2 :(得分:0)
感谢你的帮助,DebD!这并没有解决我的问题,但它使我朝着正确的方向前进。以下是有人需要的代码:
public static void print(File file) {
JFrame frame = new JFrame();
JEditorPane pane= new JEditorPane();
pane.setContentType("text/html");
try{
pane.setPage(file.toURI().toURL());
}catch (IOException ex){
System.out.println("MALFORMED ERROR!");
}
frame.add(pane);
frame.setSize(200,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
try{
pane.print(null, null, false, PrintServiceLookup.lookupDefaultPrintService(), null, false);
} catch (Exception e){
System.out.println("PRINT ERROR!");
}
}