การพิมพ์เอกสารโดยโปรแกรมหรือใช้กล่องโต้ตอบ

บทความนี้อธิบายวิธีการพิมพ์เอกสารการประมวลผลคำโดยใช้Aspose.WordsAPI ารพิมพ์เอกสารที่มีการตั้งค่าตัวอย่างการพิมพ์และไดอะล็อกความคืบหน้าการพิมพ์.

การพิมพ์เอกสารด้วยไดอะล็อกแสดงตัวอย่างการตั้งค่าและพิมพ์

เมื่อทำงานกับเอกสารมักจะต้องพิมพ์เอกสารไปยังเครื่องพิมพ์ที่เลือก ใช้กล่องโต้ตอบแสดงตัวอย่างการพิมพ์เพื่อตรวจสอบว่าเอกสารที่พิมพ์จะปรากฏขึ้นอย่างไรและเลือกตัวเลือกการพิมพ์ที่เกี่ยวข้อง.

Aspose.Wordsไม่มีไดอะล็อกหรือฟอร์มในตัวแต่ใช้คลาสของAsposeWordsPrintDocumentแทนที่ทั้งจาวาโอ้วพิมพ์พิมพ์และจาวาโอ้วพิมพ์หน้าได้.

ตัวอย่างต่อไปนี้แสดงวิธีการใช้คลาสเหล่านี้เพื่อพิมพ์เอกสารจากAspose.Wordsผ่านไดอะล็อกแสดงตัวอ:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// Open the document.
Document doc = new Document(dataDir + "TestFile.doc");
PrinterJob pj = PrinterJob.getPrinterJob();
// Initialize the Print Dialog with the number of pages in the document.
PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
attributes.add(new PageRanges(1, doc.getPageCount()));
if (!pj.printDialog(attributes)) {
return;
}
// This object is responsible for rendering our document for use with the Java
// Print API.
AsposeWordsPrintDocument awPrintDoc = new AsposeWordsPrintDocument(doc);
// Pass our document as pageable to the printer job.
pj.setPageable(awPrintDoc);
// Create an instance of the print preview dialog and pass the print dialog and
// our document.
// Note that AsposeWordsPrintDocument implements both the Pageable and Printable
// interfaces. If the pageable constructor for PrintPreviewDialog
// is used then the formatting of each page is taken from the document. If the
// printable constructor is used then Page Setup dialog becomes enabled
// and the desired page setting for all pages can be chosen there instead.
PrintPreviewDialog previewDlg = new PrintPreviewDialog(awPrintDoc);
// Pass the desired page range attributes to the print preview dialog.
previewDlg.setPrinterAttributes(attributes);
// Proceed with printing if the user accepts the print preview.
if (previewDlg.display())
pj.print(attributes);

พิมพ์หลายหน้าบนแผ่นเดียว

Aspose.Wordsใช้ชั้นเรียนMultipagePrintDocumentเพื่อปรับแต่งการดำเนินการพิมพ์เพื่อใช้ตรรกะที่กำหนดเองของคุณโดยการกำ คลาสMultipagePrintDocumentให้ความสามารถในการพิมพ์หลายหน้าบนกระดาษแผ่นเดียว.

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// Open the document.
Document doc = new Document(dataDir + "TestFile.doc");
// Create a print job to print our document with.
PrinterJob pj = PrinterJob.getPrinterJob();
// Initialize an attribute set with the number of pages in the document.
PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
attributes.add(new PageRanges(1, doc.getPageCount()));
// Pass the printer settings along with the other parameters to the print document.
MultipagePrintDocument awPrintDoc = new MultipagePrintDocument(doc, 4, true, attributes);
// Pass the document to be printed using the print job.
pj.setPrintable(awPrintDoc);
pj.print();

คุณสามารถดาวน์โหลดตัวอย่างของการใช้MultipagePrintDocumentชั้นจาก Aspose.Words GitHub.

ผลลัพธ์ของตัวอย่างรหัสนี้แสดงด้านล่าง:

print_several_pages_on_one_sheet_aspose_words_java/