1
當我生成第二頁時,我得到一個空白頁。我有兩個功能。一個生成一個帶有文本的表格,另一個生成一個PDF。當第一頁結束時,我添加另一頁,並且我想在新頁面中寫入。當我打開生成的PDF文件時,第二頁是空白:pdfbox生成多頁
//global variables
PDPage nowa=null;
PDPageContentStream contentStream1 = null;
//function to generate table
private void print_sumActionPerformed(java.awt.event.ActionEvent evt){
try {
PDDocument doc = null;
PDPage page = null;
int max_row=55;
int suma=0;
int pozycja=0;
final int starty=760;
final int startx=30;
try{
doc = new PDDocument();
page = new PDPage(PDPage.PAGE_SIZE_A4);
doc.addPage(page);
PDFont font = PDType1Font.HELVETICA;
PDPageContentStream content = new PDPageContentStream(doc, page,true,true);
//some code to generate table
drawTable(page, content, starty, startx, content1,doc);
content.close();
doc.save("path");
doc.close();
Thread.sleep(500);
} catch (Exception e){
System.out.println(e);
}
} catch (IOException ex) {
Logger.getLogger(Okno.class.getName()).log(Level.SEVERE, null, ex);
} catch (PrinterException ex) {
Logger.getLogger(Okno.class.getName()).log(Level.SEVERE, null, ex);
}
}
函數生成表PDF
private void drawTable(PDPage page, PDPageContentStream contentStream,float y, float margin, String[][] content,PDDocument doc) throws IOException, InterruptedException {
final int rows = content.length;
final int cols = content[0].length;
final float rowHeight = 13f;
final float marginCell=5;
final float startx=30;
final int tableWidth3=200;
final int tableWidth5=250;
boolean new_kol;
if (margin<230){
new_kol=false;
}else {
new_kol=true;
}
float textx;
final float odst=20;
final float starty = y;
float texty=starty-rowHeight+3;
//width table
int tableWidth;
if(cols==5)tableWidth=tableWidth5;
else tableWidth=tableWidth3;
//start print table in pdf
if(!new_kol){
contentStream.drawLine(startx,starty,tableWidth+startx+marginCell,starty);
textx=startx+marginCell;
}else {
contentStream.drawLine(startx+tableWidth5+odst, starty, 2*tableWidth5+(startx)+odst+marginCell,starty);
textx=startx+marginCell+tableWidth+odst;
}
for(int i = 0; i < content.length; i++){
for(int j = 0 ; j < content[i].length; j++){
//linia pionowa
contentStream.drawLine(textx-marginCell,texty-3,textx-marginCell,texty-3+rowHeight);
String text=content[i][j];
if(text.contains("AB")){
contentStream.setFont(PDType1Font.HELVETICA_BOLD,10);
}else contentStream.setFont(PDType1Font.HELVETICA,8);
contentStream.beginText();
contentStream.moveTextPositionByAmount(textx,texty);
contentStream.drawString(text);
contentStream.endText();
switch(j){
case 0: textx+=30;
break;
case 1:
if (cols==5) textx += 120;
else textx+=150;
break;
case 2: textx+=15;
if(cols==3) contentStream.drawLine(textx+marginCell,texty-3,textx+marginCell,texty-3+rowHeight);
break;
case 3: textx+=40;
break;
case 4: textx+=40;
contentStream.drawLine(textx+marginCell,texty-3,textx+marginCell,texty-3+rowHeight);
break;
}
}
if(new_kol){
textx=tableWidth+startx+odst+marginCell;
contentStream.drawLine(textx-marginCell,texty-3,textx+tableWidth,texty-3);
}
else {
textx=startx+marginCell;
contentStream.drawLine(textx-marginCell,texty-3,textx+tableWidth,texty-3);
}
if((texty-=rowHeight)<50){
if(!new_kol){
new_kol=true;
contentStream.drawLine(startx,texty+10,startx+tableWidth+marginCell,texty+10);
texty=760-10;
textx=tableWidth+startx+odst+marginCell;
contentStream.drawLine(textx-marginCell,texty+10,textx+tableWidth,texty+10);
}else{
new_kol=false;
contentStream.drawLine(startx+tableWidth5+odst,texty+10,startx+2*tableWidth,texty+10);
texty=760-10;
textx=startx+marginCell;
//here i add new page when end page height
//i get blank page
contentStream.close();
page=null;
contentStream=null;
nowa=new PDPage(PDPage.PAGE_SIZE_A4);
page=null;
page=nowa;
doc.addPage(nowa);
PDFont font = PDType1Font.HELVETICA;
contentStream1 = new PDPageContentStream(doc, nowa,false,true);
contentStream=contentStream1;
contentStream1=null;
contentStream.drawLine(textx,texty+10,textx+tableWidth,texty+10);
}
}
}
}
您不關閉在'drawTable'中創建的最後一個'PDPageContentStream'。因此PDF中產生的流對象可能被破壞。請提供一個樣本輸出PDF來驗證。 – mkl
謝謝。這個答案幫助我。但是當我想生成更多的頁面沒有這個功能時,得到空點異常。我想在print_sumActionPerformed(可變內容)中進一步寫入。現在這是不可能的,因爲我有空指針。怎麼做? – user3030832
檢查NullPointerException發生的位置。你假設有一個堆棧跟蹤... – mkl