简体   繁体   English

如何在PDFBox中添加多个页面

[英]How to add multiple pages in PDFBox

I want to write some content in my PDF using PDFBox. 我想使用PDFBox在我的PDF中写一些内容。 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();
               }

You can use some class level variables like below which maintains positionY throught execution of pdf generation. 您可以使用一些像下面这样的类级别变量,这些变量在执行pdf生成过程中始终保持positionY。

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. 在PDF上添加任何内容之前,请检查光标是否到达页面末尾。 ie 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
}

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();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM