Imprimarea unui Document programatic sau folosind Dialoguri
Acest articol descrie modul de imprimare a unui document de procesare a textului folosind Aspose.Words API. De asemenea, demonstrează metodele de imprimare a unui document cu setări, previzualizare imprimare și dialoguri de progres imprimare.
Imprimarea unui Document cu setări și dialoguri de previzualizare a imprimării
Când lucrați cu documente, este adesea necesar să le imprimați pe o imprimantă selectată. Este util să utilizați un dialog de previzualizare a imprimării pentru a inspecta vizual modul în care va apărea documentul tipărit și pentru a alege opțiunile de imprimare relevante.
Aspose.Words nu are dialoguri sau formulare încorporate, dar implementează clasa AsposeWordsPrintDocument suprascrie ambele java.awt.imprimă.Imprimabil și java.awt.imprimă.Pageable.
Următorul exemplu arată cum să utilizați aceste clase pentru a imprima un document din Aspose.Words prin intermediul dialogurilor Print preview și Settings:
// 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); |
Imprimarea mai multor pagini pe o singură coală
Aspose.Words implementează clasa MultipagePrintDocument, pentru a regla fin operația de imprimare pentru a implementa logica personalizată, definind modul în care documentul va apărea pe pagina tipărită. Clasa MultipagePrintDocument oferă posibilitatea de a imprima mai multe pagini pe o coală de hârtie.
// 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(); |
Puteți descărca un exemplu de utilizare a clasei MultipagePrintDocument din Aspose.Words GitHub.
Rezultatul acestui exemplu de cod este prezentat mai jos:
/