Bir Belgeyi Programatik Olarak Yazdırma veya Diyaloglar Kullanarak

Bu makale bir kelime işlemci belgesi yazdırma Aspose.Words API’den nasıl yazdırılacağını anlatır. Ayrıca ayarlar, yazdırma önizlemesi ve yazdırma ilerleme iletişim kutularını kullanarak bir belgeyi yazdırma yöntemlerini gösterir.

Ayarlarla ve Yazdırma Önizleme Diyalogları ile Bir Belgeyi Yazdırma

Bir belge üzerinde çalışırken, bunları seçili yazıcıya yazdırabilmek önemlidir. Yazdırma önizleme iletişim kutusunu kullanarak yazdırılmış belgeyi nasıl görüneceğini görsel olarak incelemek ve ilgili yazdırma seçeneklerini seçmek faydalıdır.

The Aspose.Words yerleşik diyalogları veya formları yok ancak hem java.awt.print.Printable hem de java.awt.print.Pageable’ı geçersiz kılan AsposeWordsPrintDocument sınıfını uygular.

Aşağıdaki örnek, Aspose.Words via Yazdırma Önizleme ve Ayarlar iletişim kutularını kullanarak bir belge yazdırmak için bu sınıfları nasıl kullanacağınızı göstermektedir:

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

Bir Sayfada Birden Fazla Sayfa Yazdırma

Aspose.Words MultipagePrintDocument sınıfını uygular, böylece basma işlemini özelleştirilmiş mantığınızı uygulayacak şekilde ayarlayabilirsiniz, bunun için belgenin basılan sayfada nasıl görüneceğini tanımlarsınız. MultipagePrintDocument sınıfı bir seferde bir kağıda birden fazla sayfa yazdırma kabiliyetini sağlar.

// 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 sınıfını nasıl kullanacağınızın bir örneğini Aspose.Words GitHub‘den indirebilirsiniz.

Bu kod örneğinin sonucu aşağıda gösterilmiştir:

print_several_pages_on_one_sheet_aspose_words_java/