How i can create Pdf with a certain number of pages.
I need to create a pdf with the exact number of pages, can I do that?
public static void createPdf(int totalpages){ // totalpages количество страниц
PdfDocument pdf = new PdfDocument(new PdfWriter(DEST));
Document document = new Document(pdf);
pdf.setDefaultPageSize(PageSize.A5);
document.add(new Paragraph("hello stackoverflow ... LONG TEXT"));
document.close();
}
not really sure about your scenario, but I suppose something like this could work?
private void limitPages() throws FileNotFoundException {
PdfDocument pdf = new PdfDocument(new PdfWriter(DEST));
EndPageHandler end = new EndPageHandler();
pdf.addEventHandler(PdfDocumentEvent.END_PAGE, end);
Document document = new Document(pdf);
pdf.setDefaultPageSize(PageSize.A5);
while (!end.done) {
document.add(new Paragraph("hello stackoverflow ... LONG TEXT"));
}
document.close();
}
public class EndPageHandler implements IEventHandler {
int pageCount = 0;
int pageLimit = 5;
boolean done = false;
@Override
public void handleEvent(Event event) {
//PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
pageCount++;
if (pageCount == pageLimit) {
done = true;
}
}
}
basically, after a page is done, it checks the page count. if it matches your criteria, it will exit the while look. Quick and dirty, but food for thought.