问题
I want to write some content in my PDF using PDFBox. Once the page height is less than the margin I need to create another page. I want to retain the cursor information. I s there a way through which i can get the cursor information like where the cursor is present so i can subtract the margin from cursor position and add another page to it. Right now I have done something like this
PDRectangle rect = page.getMediaBox();
float positionY = rect.getWidth();
positionY = positionY - pdfWriter.defaultBottomMargin;
if(positionY < positionX) {
positionY = rect.getWidth();
PDPage page2 = page;
rect = page2.getMediaBox();
document.addPage(page2);
PDPageContentStream contentStream = new PDPageContentStream(document, page2);
contentStream.appendRawCommands("T*\n");
contentStream.beginText();
// contentStream.setFont(font, 12);
contentStream.moveTextPositionByAmount(positionX, positionY);
contentStream.drawString(tmpText[k]);
contentStream.endText();
contentStream.close();
}
回答1:
You can use some class level variables like below which maintains positionY throught execution of pdf generation.
float PAGE_MARGIN = 20;
float newPagepositionY = page.findMediaBox().getHeight() - PAGE_MARGIN;
float positionY = newPagepositionY;
PDPage currentPage = new PDPage();
Before adding any content on PDF, check whether cursor has reached at end of page or not. i.e. Create funtion as shown below
public boolean isEndOfPage(Row row)
{
float currentY = this.positionY ;
boolean isEndOfPage = currentY <= (PAGE_MARGIN + 10);
return isEndOfPage;
}
Using above funtion, you can create new page as required.
if (isEndOfPage(row))
{
// Reset positionY to newPagepositionY
this.positionY = newPagepositionY;
this.currentPage = new PDPage();
// your code
}
回答2:
This worked for me
package trypdf;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
public class PDF {
public static PDPage blankPage= new PDPage();
public static float PAGE_MARGIN = 5;
public static float newPagepositionY = blankPage.getMediaBox().getHeight() -
PAGE_MARGIN;
public static float positionY = newPagepositionY;
public static void main(String[] args)throws IOException
{
PDDocument document = new PDDocument();
// adding a new page to the document - the first page
document.addPage(blankPage);
// creating the content that will appear on the previously added
//page
PDPageContentStream contentStream = new
PDPageContentStream(document,blankPage);
//the begin of the text for the content
contentStream.beginText();
//formating the text
contentStream.setFont( PDType1Font.TIMES_ROMAN, 12 );
contentStream.setLeading(14.5f);
//setting the coordinates from where the text will be displayed
//on the page (first one is the column, second is for row)
contentStream.newLineAtOffset(15, 750);
//depending on the text formats you can get more or less rows on a
//page, for my formats I gor 49 rows + the document margins and
//the spaces between the rows; this goes for the first for
for(int i=0; i<48;i++) {
//will display a text at the begining of every row in my case
//it will display the row number
contentStream. showText(Integer.toString(i));
//this for will display some text to each column of the page
//in my case it will display the letter "a"+ one space 64
//times; depending on the text formats you can get more or
//less characters
for(int j=0;j<63;j++) {
contentStream. showText("a"+" ");
}
//I wanted to display a "b" at each end of a line so I coud
//see that the line fits to the page
contentStream. showText("b");
//adds another line
contentStream.newLine();
}
//marks the end of the text that will be displayed on the first
//page
contentStream.endText();
//will close the content for the first page
contentStream.close();
//end of the first page
//adding other 3 pages with the same text
int j=0; //the counter for the pages that will be added
while(j<3) {
//adding another blank page to the document with different
//text from the previous one
document.addPage(blankPage=new PDPage());
//taking a content for the new page
contentStream = new PDPageContentStream(document,blankPage);
contentStream.beginText();
contentStream.setFont( PDType1Font.TIMES_ROMAN, 12 );
contentStream.setLeading(14.5f);
contentStream.newLineAtOffset(15, 720);
//display a number for every row on the page
for( int i=50; i<98;i++) {
contentStream. showText(Integer.toString(i));
contentStream.newLine();
}
//closing the text and the content
contentStream.endText();
contentStream.close();
//increasing the counter for the page
j++;
}
System.out.println("Content added");
//Saving the document
document.save("C:/PdfBox_Examples/docc1.pdf");
System.out.println("PDF created");
document.close();
}
}
来源:https://stackoverflow.com/questions/32308964/how-to-add-multiple-pages-in-pdfbox