2013-10-08 1 views
0

iText를 사용하여 PDF 파일을 작성해야합니다. 첫 번째 페이지에는 페이지 상단에 헤더가 있어야하고 나머지 페이지 영역 (가로 및 세로)의 중앙에 정확하게 문서 제목이 있어야합니다.나머지 페이지의 중앙에 단락 (가로 및 세로)

Google 검색에서 가장 좋은 해결책은 표를 만들고 셀 정렬 방법을 사용하는 것입니다. 문제는 : 수직 정렬을 올바르게 사용하려면 셀의 최소 높이를 설정해야합니다 (cell.setMinimumHeight (...);) 그러나 높이가 얼마나 남았는지는 알 수 없습니다! 일부 하드 코딩 된 오프셋과 함께 document.getPageSize() .getHeight()를 사용하면 좋은 옵션처럼 보이지 않습니다. 글꼴 크기 등을 변경할 때마다이 하드 코드를 변경하고 싶지 않습니다.

다음 코드는 페이지의 상단에있는 "헤더", 그것은 중요한 경우 :

Paragraph preface = new Paragraph(); 
Paragraph o = new Paragraph("test", headerFont); 
o.add(new LineSeparator(1, 100, Color.BLACK, Element.ALIGN_CENTER, -5)); 
preface.add(o); 
o.add(new Paragraph(" ")); 
document.add(preface); 

답변

1

좋아, 여기에 내가 지금까지 가지고 무엇을 ... 나를 위해

public static float getAvailableHeight(PdfDocument pdfDocument) { 
    Float indentBottom = pdfDocument.bottomMargin(); 
    try { 
     Method method = pdfDocument.getClass().getDeclaredMethod("indentBottom"); 
     method.setAccessible(true); 
     indentBottom = (Float) method.invoke(pdfDocument); 
    } catch (NoSuchMethodException e) { 
     e.printStackTrace(); 
    } catch (InvocationTargetException e) { 
     e.printStackTrace(); 
    } catch (IllegalAccessException e) { 
     e.printStackTrace(); 
    } 
    float offset = pdfDocument.top() - pdfDocument.getVerticalPosition(false); 
    return pdfDocument.getPageSize().getHeight() - offset - pdfDocument.topMargin() - indentBottom 
      - pdfDocument.bottomMargin(); 
} 

작품. 희망이 다른 사람을 도울 것입니다.

PdfWriter에 캡슐화 된 PdfDocument 객체에 대한 액세스가 필요합니다. 방금 PdfWriter를 확장하는 자체 CustomPdfWriter를 만들었습니다.

indentBottom() 메서드는 PdfDocument 클래스의 패키지 로컬이므로 반성하는 불량한 부분이 필요합니다.