In Tài liệu một cách Lập trình hoặc Sử dụng hộp thoại

Bài viết này mô tả cách in một tài liệu xử lý văn bản bằng Aspose.Words API. Nó cũng chứng minh các phương pháp in một tài liệu với Settings, Print Preview và Print tiến trình thoại.

In một tài liệu với các cài đặt và hộp thoại xem trước in

Khi làm việc với tài liệu, thường thì yêu cầu phải in chúng ra máy in đã chọn. Sử dụng một hộp thoại xem trước để kiểm tra trực quan cách thức xuất bản tài liệu sẽ xuất hiện và chọn các tùy chọn in phù hợp.

The Aspose.Words không có các hộp thoại hoặc biểu mẫu được tích hợp nhưng thực hiện lớp AsposeWordsPrintDocument vượt qua cả java.awt.print.Printable và java.awt.print.Pageable.

Ví dụ sau này cho thấy cách sử dụng các lớp này để in một tài liệu từ Aspose.Words thông qua hộp thoại In xem trước và Cài đặt:

// 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);

In ấn nhiều trang trên một tờ giấy

Aspose.Words thực hiện lớp MultipagePrintDocument, để tinh chỉnh hoạt động in ấn nhằm thực hiện logic tùy chỉnh bằng cách xác định cách tài liệu sẽ xuất hiện trên trang in. Lớp MultipagePrintDocument cung cấp khả năng in một số trang trên một tờ giấy.

// 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();

Bạn có thể tải về ví dụ sử dụng lớp MultipagePrintDocument từ Aspose.Words GitHub

Kết quả của ví dụ mã này được hiển thị bên dưới:

print_several_pages_on_one_sheet_aspose_words_java/