【问题标题】:How to add multiple pages in PDFBox如何在 PDFBox 中添加多个页面
【发布时间】:2015-11-25 08:28:44
【问题描述】:

我想用 PDFBox 在我的 PDF 中写一些内容。一旦页面高度小于边距,我需要创建另一个页面。我想保留光标信息。我有一种方法可以获取光标信息,例如光标所在的位置,这样我就可以从光标位置减去边距并添加另一个页面。现在我做了这样的事情

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

【问题讨论】:

  • “Noddy 先生”的“回答”可能是一个评论,但他没有 50 分的声望。他告诉你看这里:stackoverflow.com/questions/18152803/…。顺便说一句,您的问题“有没有一种方法可以让我获得光标所在位置等光标信息”的答案是“否”。
  • 如果答案解决了您的问题,请按绿色复选标记。如果不是,请在 cmets 中说明原因。

标签: java pdfbox


【解决方案1】:

您可以使用一些类级别的变量,如下所示,通过执行 pdf 生成来保持 positionY。

float PAGE_MARGIN = 20;
float newPagepositionY = page.findMediaBox().getHeight() - PAGE_MARGIN;
float positionY = newPagepositionY;
PDPage currentPage = new PDPage();

在 PDF 上添加任何内容之前,请检查光标是否已到达页尾。 即创建如下所示的函数

public boolean isEndOfPage(Row row) 
{
    float currentY = this.positionY ;
    boolean isEndOfPage = currentY  <= (PAGE_MARGIN + 10);

    return isEndOfPage;
}

使用上述功能,您可以根据需要创建新页面。

if (isEndOfPage(row)) 
{
    // Reset positionY  to newPagepositionY
    this.positionY  = newPagepositionY;

    this.currentPage = new PDPage();

    // your code
}

【讨论】:

    【解决方案2】:

    这对我有用

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

    【讨论】:

    • 你能解释一下解决方案吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    • 2021-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多