ここでは、印刷フレームワークの使用方法を説明する簡単な例を示します。
簡単な例を印刷するには、以下の手順に従います。
-
IlvPrintableDocument オブジェクトを作成します。
-
ページをドキュメントに追加します。
-
ドキュメントにヘッダーとフッターを設定します。
-
IlvPrintableObject オブジェクトを実装します。
-
印刷コントローラーを作成して使用します。
-
最後に、ユーザー・インターフェース・コンポーネントによる、作成ドキュメントのプレビューを行います。
コード例を次に示します。
印刷可能オブジェクトを使用してドキュメントを作成する
IlvPrintableDocument document = new IlvPrintableDocument("A test document"); // Sets a header in the document. document.setHeader(new IlvHeader(null, "Printing Example", IlvHeader.PageKey)); // Creates the first page. IlvPage page1 = new IlvPage(); // Prints an oval on the first page. IlvPrintableObject oval = new IlvPrintableObject() { /** * Overrides the <code>print</code> method to print an oval. * It shows how to implement a <code>PrintableObject</code>. * @param dst The output <code>Graphics</code> object. * @param format The page format, which is ignored here. * @param pageIndex The page index, which is ignored here. */ public int print(Graphics dst, PageFormat format, int pageIndex) throws PrinterException { dst.drawOval(200, 200, 200, 100); return Printable.PAGE_EXISTS; } }; // Adds this new printable to the page. page1.addPrintableObject(oval); // Creates the second page. IlvPage page2 = new IlvPage(); // Prints a rectangle on the second page. IlvUnit.Rectangle area = new IlvUnit.Rectangle(5, 5, 10, 10, IlvUnit.CM); IlvPrintableRectangle rectangle = new IlvPrintableRectangle(area); page2.addPrintableObject(rectangle); // Adds the pages to the document. document.addPage(page1); document.addPage(page2); // Creates the print manager. IlvPrintingController controller = new IlvPrintingController(document); // Previews the document. controller.setPreviewMode(IlvPrintPreviewPanel.CONTINUOUS_MODE); controller.printPreview(JOptionPane.getRootFrame());
画面には以下のように表示されます。
![printovalrectangle.gif](../../JViews_Framework/usradvfwork/_media/printovalrectangle_default.gif)
印刷可能オブジェクトのあるドキュメント