Druk'n Dokument Programmaties of Gebruik Dialoog

Hierdie artikel beskryf hoe om’n teksverwerkingsdokument te druk met behulp van Aspose.Words API. Dit demonstreer ook die metodes van die druk van’n dokument met Instellings, Druk Voorskou, en Druk vordering dialoog.

Druk’n Dokument met Instellings en Druk Voorskou Dialoog

Wanneer jy met dokumente werk, moet jy dit dikwels op’n geselekteerde drukker druk. Dit is nuttig om’n drukvoorskou dialoog te gebruik om visueel te inspekteer hoe die gedrukte dokument sal verskyn en kies relevante druk opsies.

Die Aspose.Words het geen ingeboude dialoog of vorms nie, maar implementeer die AsposeWordsPrintDocument klas oorheers beide java.awt.druk.Drukbaar en java.awt.druk.Bladsy.

Die volgende voorbeeld toon hoe om hierdie klasse te gebruik om’n dokument van Aspose.Words te druk via Die Druk voorskou en Instellings dialoog:

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

Druk Verskeie Bladsye op Een Vel

Aspose.Words implementeer die MultipagePrintDocument klas, om die drukbewerking te verfyn om u persoonlike logika te implementeer deur die manier waarop die dokument op die gedrukte bladsy verskyn, te definieer. Die MultipagePrintDocument klas bied die vermoë om verskeie bladsye op een vel papier te druk.

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

Jy kan’n voorbeeld van die gebruik van die MultipagePrintDocument klas aflaai van Aspose.Words GitHub.

Die resultaat van hierdie kode voorbeeld word hieronder getoon:

print_several_pages_on_one_sheet_aspose_words_java/