2017-11-20 55 views
0

PDFBox을 사용하여 Java에서 PDF 파일을 만들려고합니다. 이 파일은 2 개의 열이있는 큰 테이블을 포함합니다. 테이블을 PDF로 렌더링하려면 다른 라이브러리 인 boxable을 사용하고 있습니다.Boxable 및 Java PDFBox를 사용하여 큰 테이블을 PDF 파일로 렌더링

파일이 성공적으로 만들어지고 테이블도 렌더링됩니다. 그러나 행 중 하나에 큰 데이터가 포함될 때 문제가 발생합니다. 이 경우, 테이블은 분할과 데이터를 제대로 표시되지 않습니다 : 첫 번째 페이지가 비어 남아있는 동안

enter image description hereenter image description hereenter image description hereenter image description here

큰 행은 새 페이지로 자동으로 이동됩니다. 두 번째 페이지에서 행은 갑자기 종료됩니다. 셀에 전체 데이터를 표시하지 않고 테스트를 위해 새 작업을 입력했습니다. 이것은 반복되는 문제이며이 사례에만 해당되는 것은 아닙니다.

Row<PDPage> headerRow; 
Row<PDPage> row; 
Cell<PDPage> cell; 
int i; 

headerRow = table.createRow(15f); 

cell = headerRow.createCell(30, "Goal Category"); 
cell.setFont(PDType1Font.HELVETICA); 
cell.setFontSize(11); 
cell = headerRow.createCell(70, "My Goal"); 
cell.setFont(PDType1Font.HELVETICA); 
cell.setFontSize(11); 
table.addHeaderRow(headerRow); 

for(i=0;i<goals.size();i++) 
{ 
    ArrayList<String> goal=goals.get(i); 
    row = table.createRow(12); 
    cell = row.createCell(30, goal.get(0)); 
    cell.setFont(PDType1Font.HELVETICA); 
    cell.setFontSize(11); 
    System.out.println("My Goal="+goal.get(1)); 
    cell = row.createCell(70, goal.get(1)); 
    cell.setFont(PDType1Font.HELVETICA); 
    cell.setFontSize(11); 

} 
table.draw(); 

나는 해결책 또는 PDF에 테이블을 렌더링하는 다른 방법을 찾고 있어요.

+0

독서에서부터 볼 때, 이것은 박스형면에 대한 조사가 필요한 버그입니다. 먼저 페이지 셀에 들어갈 수있는 선을 최대한 많이 차지하도록하십시오. 행의 "캐리 오버"셀을 도입함으로써 행에 셀 전송이있는 한 새 행을 추가하십시오. –

+0

@JoopEggen 셀의 최대 라인을 어떻게 수정합니까 ?? 가변 셀이 많은 셀이있을 수 있으며 그 경우 최대 라인도 달라질 수 있습니다 – HBK

+0

아래 패치 코드를 추가했습니다. –

답변

0

라이브러리가 페이지 나누기를 만족스럽게 처리하지 못하는 것 같습니다. 상용 작업 말 itext은이 경우 언급할만한 가치가 있습니다.

페이지 나누기를 위해 직접 테이블을 준비 할 때 여러 행의 행을 선제 분할하면 실용적인 솔루션이 될 수 있습니다.

아래 행은 대부분 페이지 당 줄 수로 제한되며 페이지 나누기가 올바르게 작동합니다. 여러 줄의

public int lineno; 
public int linesPerPage = 50; // Not counting header lines. 

public void splitRow(List<String> multilineRow) { 

분할 단일 행의 셀 값 (워드 랩 여기 무시) :

List<List<String>> sublines = new ArrayList<>(multilineRow.size()); 
    int sublineCount = 0; 
    for (String cellValue : multilineRow) { 
     List<String> column = Arrays.asList(cellValue.split("\r?\n")); 
     sublines.add(column); 
     sublineCount = Math.max(sublineCount, column.size()); 
    } 

그런 다음 우리는 열당 서브 라인이 있고, 한 행에 여러 개의 서브 라인을 번들 :

for (int sublineI = 0; sublineI < sublineCount;) { 
     int linesPerRow = Math.min(sublineCount, linesPerPage - lineno); 
     if (linesPerRow <= 0) { 
      break; 
     } 
     int sublineI2 = sublineI + linesPerRow; 

     List<String> cellValues = new ArrayList<>(multilineRow.size()); 
     for (int j = 0; j < multilineRow.size(); ++j) { 
      List<String> column = sublines.get(j); 
      String value = ""; 
      if (sublineI < column.size()) { 
       List<String> vs = column.subList(sublineI, 
             Math.min(sublineI2, column.size())); 
       value = vs.stream().collect(Collectors.joining("\n")); 
      } 
      cellValues.add(value); 
     } 
     createRow(); 
     ++lineno; 
     .... fill with cellValues.get(0) and get(1) 
     sublineI = sublineI2; 
     if (lineno >= linesPerPage) { 
      lineno = 0 
     } 
     //if (sublineI < sublineCount) { 
     // Page break/createHeader; 
     //} 
    } 
} 

이것은 매우 견고한 해결책은 아닙니다. 그러나 라이브러리 버그가 제거 될 때까지 패치를 제공 할 수 있습니다.