Java 응용 프로그램에서 HTML 파일을 인쇄해야합니다. 몇 가지 방법을 시도했습니다. 그 중 두 명은 일하고 있지만 예상대로는 아닙니다.Java에서 HTML 파일 인쇄
방법 1 :
문제 : 인쇄는 시트의 3 분기 이후의 절단한다.
try {
PrinterJob printJob = PrinterJob.getPrinterJob();
PageFormat pageFormat = new PageFormat();
pageFormat.setOrientation(PageFormat.LANDSCAPE);
Paper paper = new Paper(); // Set to A4 size.
paper.setSize(594.936, 841.536); // Set the margins.
paper.setImageableArea(0, 0, 594.936, 841.536);
pageFormat.setPaper(paper);
XHTMLPanel panel = new XHTMLPanel();
panel.setDocument(new File("./documents/" + "Personalstamm"
+ ".html"));
printJob.setPrintable(new XHTMLPrintable(panel), pageFormat);
if (printJob.printDialog()) {
printJob.print();
}
} catch (Exception x) {
x.printStackTrace();
}
방법 2 :
문제 : 출력은 HTML 파일에 설정된 스타일 시트없이.
PageFormat pageFormat = new PageFormat();
Paper a4paper = new Paper();
double paperWidth = 8.26;
double paperHeight = 11.69;
a4paper.setSize(paperWidth * 72.0, paperHeight * 72.0);
/*
* set the margins respectively the imageable area
*/
double leftMargin = 0.78; /* should be about 2cm */
double rightMargin = 0.78;
double topMargin = 0.78;
double bottomMargin = 0.78;
a4paper.setImageableArea(leftMargin * 72.0, topMargin * 72.0,
(paperWidth - leftMargin - rightMargin) * 72.0, (paperHeight
- topMargin - bottomMargin) * 72.0);
pageFormat.setPaper(a4paper);
pageFormat.setOrientation(PageFormat.LANDSCAPE);
DocumentRenderer documentRenderer = new DocumentRenderer(pageFormat,
"Report");
try {
FileInputStream stringReader = new FileInputStream(new File(
"./documents/" + "Personalstamm" + ".html"));
HTMLEditorKit htmlKit = new HTMLEditorKit();
HTMLDocument htmlDoc = (HTMLDocument) htmlKit
.createDefaultDocument();
htmlKit.read(stringReader, htmlDoc, 0);
documentRenderer.print(htmlDoc);
} catch (Exception x) {
x.printStackTrace();
}
아무도이 방법 중 하나에서 문제를 해결하는 방법을 알고 있습니까? 아니면 누구든지 Java에서 파일을 인쇄하는 더 좋은 방법이 있습니까?
인쇄'HTML'이 가능합니다. 하지만 인쇄물은 잘려나 간다. 이미'PDF로 변환하려고 시도했지만 끔찍한보고있다. – Stone
첫 번째 방법은 'panel'을'JScrollPane' 안에 넣고 출력 해보는 것입니다. 'JScrollPane scroll = new JScrollPane (panel);을 실행 해, scrollPane를 출력합니다. 전체 내용을 인쇄하도록 코드를 작성해야하지만 스크롤 팬 – ItachiUchiha