When printing a pdf with PdfClown, the print runs off the page without a PdfClown creating a new page, how can this be fixed?
The code is below, it prints text onto a pdf page, and another pdf page should be automatically added when the print starts to run off the bottom of the page, is there a PdfClown setting that will solve this?
Any help would be greatly appreciated.
import org.pdfclown.documents.Document;
import org.pdfclown.documents.Page;
import org.pdfclown.documents.contents.composition.BlockComposer;
import org.pdfclown.documents.contents.composition.PrimitiveComposer;
import org.pdfclown.documents.contents.composition.XAlignmentEnum;
import org.pdfclown.documents.contents.composition.YAlignmentEnum;
import org.pdfclown.documents.contents.fonts.StandardType1Font;
import org.pdfclown.files.File;
import org.pdfclown.files.SerializationModeEnum;
import org.pdfclown.util.math.geom.Dimension;
import java.awt.geom.Dimension2D;
import java.awt.geom.Rectangle2D;
public class PrintWithPdfClown{
private static final int Margin_X = 50;
private static final int Margin_Y = 50;
public void printAuthorInfo() {
// Create a document and add a page to it
try {
for (int j = 0; j < author.size(); j++) {
String name = author.get(j).getFirstName();
String lastName = author.get(j).getLastName();
String addressLine1 = author.get(j).getAddressLine1();
String addressLine2 = author.get(j).getAddressLine2();
String city = author.get(j).getCity();
String state = author.get(j).getState();
String postalCode = author.get(j).getPostalCode();
String country = author.get(j).getCountry();
Document document = new File().getDocument();
Page page = new Page(document);
document.getPages().add(page);
Dimension2D pageSize = page.getSize();
PrimitiveComposer composer = new PrimitiveComposer(page);
BlockComposer blockComposer = new BlockComposer(composer);
composer.beginLocalState();
Rectangle2D frame = new Rectangle2D.Double(Margin_X, Margin_Y, pageSize.getWidth() - Margin_X * 2, pageSize.getHeight() - Margin_Y * 2);
blockComposer.begin(frame, XAlignmentEnum.Left, YAlignmentEnum.Top);
Dimension breakSize = new Dimension(24, 8); // Indentation (24pt)
// and top margin (8pt).
composer.setFont(new StandardType1Font(document, StandardType1Font.FamilyEnum.Courier, true, false), 8);
blockComposer.begin(frame, XAlignmentEnum.Left, YAlignmentEnum.Top);
blockComposer.showText("########## Author Address ##########");
blockComposer.showBreak(breakSize);
blockComposer.showBreak(breakSize);
blockComposer.showText(name + " " + lastName + " " );
blockComposer.showBreak(breakSize);
blockComposer.showText(addressLine1);
blockComposer.showBreak(breakSize);
blockComposer.showText(addressLine2);
blockComposer.showBreak(breakSize);
blockComposer.showText(city + " " + state + " " + postalCode + " " + country);
blockComposer.showBreak(breakSize);
blockComposer.showBreak(breakSize);
for (int i = 0; i < author.get(j).booksOfAuthor.size(); i++) {
String productName = author.get(j).booksOfAuthor.get(i).getBookName();
String pages = author.get(j).booksOfAuthor.get(i).getPages();
blockComposer.showBreak(breakSize);
blockComposer.showText("########## <" + (i + 1) + ". Book > ##########");
blockComposer.showBreak(breakSize);
blockComposer.showText(" <Book Name:> " + bookName + " <Pages:> " + pages);
blockComposer.showBreak(breakSize);
blockComposer.showBreak(breakSize);
}
blockComposer.end();
composer.flush();
document.getFile().save(job.getAuthorFileLocalDir() + "/" + "Author_Info " + j + ".pdf", SerializationModeEnum.Standard);
}
} catch (Exception e) {
e.printStackTrace();
}
}
Currently (before version 0.2.0) PDF Clown requires your code to explicitly create a new page whenever a page is full.
The following code demonstrates how you can do that. As I don't have your book data, the code uses some static text from the
String[] paragraphs
. (Actually the sample is a cut-down version of the PDF Clown cli sample ComplexTypesettingSample and also uses the text used in that sample):As you see you simply check the return code of
blockComposer.showText
to see whether the text actually fitted into the output frame. If it doesn't, you close the current composers, start a new page, open new composers, and continue drawing the cut-off paragragraph from the cut position.You can create more complex layouts, too, with multiple frames per page. For that please do look at the sample ComplexTypesettingSample.
As soon as version 0.2.0 emerges, the new Content composition engine will offer to take that chore from you.