Печат на документ Програмиране или използване на диалогови файлове
Тази статия описва как да отпечатате документ за обработка на думи, използвайки Aspose.Words API. Той също така демонстрира методите за отпечатване на документ със Настройки, Print Preview и Print progress диалози.
Отпечатване на документ със настройки и печат Диалози за преглед
При работа с документи често се изисква да бъдат отпечатани на избран принтер. Полезно е да използвате прозорец за преглед на печат, за да проверите визуално как ще се появи отпечатаният документ и да изберете съответните опции за печат.
На Aspose.Words няма вградени диалози или форми, но прилага AsposeWordsPrintDocument Класът отменя и двата Java.awt.print. Печатаем и разпечатан. Изумително.
Следният пример показва как да използвате тези класове, за да отпечатате документ от 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
Резултатът от този пример с код е показан по-долу:
/