-
Notifications
You must be signed in to change notification settings - Fork 291
/
Copy pathBookBundle.java
62 lines (49 loc) · 1.73 KB
/
BookBundle.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package qz.printer;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import qz.utils.SystemUtilities;
import javax.print.attribute.standard.OrientationRequested;
import java.awt.*;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.util.ArrayList;
import java.util.List;
/**
* Wrapper of the {@code Book} class as a {@code Printable} type,
* since PrinterJob implementations do not seem to handle the {@code Pageable} interface properly.
*/
public class BookBundle extends Book implements Printable {
private static final Logger log = LoggerFactory.getLogger(BookBundle.class);
private Printable lastPrint;
private int lastStarted;
public BookBundle() {
super();
}
@Override
public int print(Graphics g, PageFormat format, int pageIndex) throws PrinterException {
log.trace("Requested page {} for printing", pageIndex);
if (pageIndex < getNumberOfPages()) {
Printable printable = getPrintable(pageIndex);
if (printable != lastPrint) {
lastPrint = printable;
lastStarted = pageIndex;
}
return printable.print(g, format, pageIndex - lastStarted);
}
return NO_SUCH_PAGE;
}
/**
* Wrapper of the wrapper class so that PrinterJob implementations will handle it as proper pageable
*/
public Book wrapAndPresent() {
Book cover = new Book();
for(int i = 0; i < getNumberOfPages(); i++) {
cover.append(this, getPageFormat(i));
}
return cover;
}
}