Друк документів Програматично або за допомогою ділогів
Ця стаття описує, як друкувати документ обробки слів за допомогою Aspose.Words APIй Також продемонстровано методи друку документа з налаштуваннями, переглядом та діалогами про прогрес друку.
Друк документів з налаштуваннями та друком ділогів Перегляду
При роботі з документами часто потрібно друкувати їх на обраний принтер. Ви можете використовувати діалогове вікно попереднього перегляду друку, щоб візуально зрозуміти, як буде виглядати друкований документ і вибрати відповідні параметри друку.
Про нас Aspose.Words не має вбудованих діалогів або форм, але реалізує AsposeWordsPrintDocument клас перенаречений як java.awt.print. Друковані та java.awt.print. Приват24
Наступний приклад показує, як використовувати ці класи для друку документа з 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й
Приклад даного коду показано нижче:
/