プログラムで文書を印刷するか、ダイアログを使用して文書を印刷する

この記事では、Aspose.WordsAPIを使用してワープロ文書を印刷する方法について説明します。 また、設定、印刷プレビュー、および印刷進行ダイアログを使用してドキュメントを印刷する方法についても説明します。

設定ダイアログと印刷プレビューダイアログを使用した文書の印刷

文書を操作するときは、選択したプリンタに印刷する必要があることがよくあります。 印刷プレビューダイアログを利用して、印刷された文書がどのように表示されるかを視覚的に検査し、関連する印刷オプションを選択すると便利です。

Aspose.Wordsには組み込みのダイアログやフォームはありませんが、AsposeWordsPrintDocumentクラスは両方のjavaをオーバーライドします。awt。印刷します。印刷可能とjava。awt。印刷します。ページング可能。

次の例は、これらのクラスを使用して、印刷プレビューと設定ダイアログを使用して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.

このコード例の結果を以下に示します:

print_several_pages_on_one_sheet_aspose_words_java/