Stampa di un documento a livello di codice o tramite finestre di dialogo
In questo articolo viene descritto come stampare un documento di elaborazione testi utilizzando Aspose.Words API. Vengono inoltre illustrati i metodi di stampa di un documento con Impostazioni, Anteprima di stampa e finestre di dialogo Avanzamento stampa.
Stampa di un documento con impostazioni e finestre di dialogo Anteprima di stampa
Quando si lavora con i documenti, è spesso necessario stamparli su una stampante selezionata. È utile utilizzare una finestra di anteprima di stampa per esaminare visivamente come apparirà il documento stampato e scegliere le opzioni di stampa pertinenti.
Il Aspose.Words non ha finestre di dialogo o moduli incorporati ma implementa la classe AsposeWordsPrintDocument che sovrascrive entrambi java.awt.stampa.Stampabile e java.awt.stampa.Pagabile.
L’esempio seguente mostra come utilizzare queste classi per stampare un documento da Aspose.Words tramite le finestre di dialogo Anteprima di stampa e Impostazioni:
// 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); |
Stampa di più pagine su un foglio
Aspose.Words implementa la classe MultipagePrintDocument, per ottimizzare l’operazione di stampa per implementare la logica personalizzata definendo il modo in cui il documento apparirà sulla pagina stampata. La classe MultipagePrintDocument offre la possibilità di stampare più pagine su un foglio di carta.
// 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(); |
È possibile scaricare un esempio di utilizzo della classe MultipagePrintDocument da Aspose.Words GitHub.
Il risultato di questo esempio di codice è mostrato di seguito:
/