Skip to main content
Jump to: navigation, search

Paperclips beginning tutorial

Revision as of 16:31, 2 October 2019 by (Talk | contribs) (Created page with "< Back to Nebula Paperclips page It may be helpful to have the API javadocs open i...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

< Back to Nebula Paperclips page

It may be helpful to have the [javadocs] open in another window during the tutorial.

In PaperClips, any object that implements the Print interface can be printed. In this tutorial and in the javadocs, these objects are referred to simply as "prints." By convention, every print's class name ends in "Print", e.g. TextPrint for text, ImagePrint for images.

Let's start with the obligatory "Hello" example, which prints the words "Hello PaperClips!"

First we must create a Print of our document (a TextPrint in this case). Once we have the main document, we create a PrintJob and send it to the PaperClips class for printing.

// Create the document 
TextPrint text = new TextPrint("Hello PaperClips!");
// Print it to the default printer (no prompt). The print job 
// name in the printer status window will be "TutorialExample1". 
PaperClips.print(new PrintJob("TutorialExample1", text), new PrinterData());

To print more complicated documents, we create all the pieces and put them together. Then we create a PrintJob and pass it to the PaperClips class like before.

Let's print something a little more complicated:


Name: Matthew Hall Occupation: Programmer Eyes: Blue Gender: Male
Spouse: Sexy

We'll use a GridPrint for layout, TextPrint for text, and LinePrint for the horizontal rules. GridPrint is a composite print which arranges its children in a grid. The GridPrint constructor accepts a comma-delimited String which defines how each column in the grid behaves. Those who have used the FormLayout layout manager by Karsten Lentzsch ( will be familiar with the column layout format.

// Create a grid with the following columns: 
// Column 1: default width 
// Column 2: default width, grows to fill excess width 
// The second argument is the grid look. The look is a pluggable grid appearance. 
// Here we use a DefaultGridLook, configured with 5 point horizontal and vertical 
// cell spacing. 72 points = 1".

GridPrint grid = new GridPrint("d, d:g", new DefaultGridLook(5, 5));

// Now populate the grid with the text and lines 
// (GridPrint.REMAINDER is a special column span value 
grid.add(new TextPrint("VITAL STATISTICS"), GridPrint.REMAINDER, SWT.CENTER);
grid.add(new LinePrint(SWT.HORIZONTAL), GridPrint.REMAINDER);
grid.add(new TextPrint("Name:")); 
grid.add(new TextPrint("Matthew Hall")); 
grid.add(new TextPrint("Occupation:")); 
grid.add(new TextPrint("Programmer")); 
grid.add(new TextPrint("Eyes:")); 
grid.add(new TextPrint("Blue")); 
grid.add(new TextPrint("Gender:")); 
grid.add(new TextPrint("Male")); 
grid.add(new TextPrint("Spouse:")); 
grid.add(new TextPrint("Sexy"));
grid.add(new LinePrint(SWT.HORIZONTAL), GridPrint.REMAINDER);
PrintJob job = new PrintJob("", grid); PaperClips.print(job, new PrinterData()); ```

This should give you a general idea of what PaperClips can do for you, and how simple it can be to create and print documents. See the API documentation on the classes below for further information.

  • BorderPrint - wraps a print with a border.
  • See also Border interface.
  • ColumnPrint - splits the wrapped print into columns
  • EmptyPrint - takes up space (these can be handy in GridPrints)
  • GridPrint - arranges its child prints in a grid. You will use this one a lot.
  • LinePrint - for horizontal rules.
  • PagePrint - decorates a print with page headers and footers, with page numbering capability.
  • PageNumberPrint - for printing the page number. Use this class inside the header or footer of a PagePrint.
  • SeriesPrint - prints each of its children on separate pages.
  • TextPrint - for text.

Also, check out the GridPrint for a detailed description of how to use GridPrint. You'll need to learn all of it's features sooner or later!

Don't forget: PaperClips is extensible, so you can write and use your own Print classes. PaperClips can print any object that correctly implements the Print interface. Read the javadocs for the Print, PrintIterator, and PrintPiece interfaces to get started.

Back to the top