Java Language Java Print Service Building the Doc that will be printed

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Doc is an interface and the Java Print Service API provide a simple implementation called SimpleDoc.

Every Doc instance is basically made of two aspects:

  • the print data content itself (an E-mail, an image, a document etc)
  • the print data format, called DocFlavor (MIME type + Representation class).

Before creating the Doc object, we need to load our document from somewhere. In the example, we will load an specific file from the disk:

FileInputStream pdfFileInputStream = new FileInputStream("something.pdf");

So now, we have to choose a DocFlavor that matches our content. The DocFlavor class has a bunch of constants to represent the most usual types of data. Let's pick the INPUT_STREAM.PDF one:

DocFlavor pdfDocFlavor = DocFlavor.INPUT_STREAM.PDF;

Now, we can create a new instance of SimpleDoc:

Doc doc = new SimpleDoc(pdfFileInputStream, pdfDocFlavor , null);

The doc object now can be sent to the print job request (see Creating a print job from a print service).



Got any Java Language Question?