Creating an IlvDocument with printable objects
This is a simple example to explain how to use the printing framework.
To print the simple example:
2. Add pages to the document.
3. Set header and footer to the document.
5. Create and use the printing controller.
6. Use the user interface components to preview the document you created.
The example code is as follows.
Creating a document with printable objects
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());
Here is what you get on the screen:
Document with printable objects
Copyright © 2018, Rogue Wave Software, Inc. All Rights Reserved.