2009-06-12 8 views
25

iText를 사용하여 테이블이있는 pdf 또는 rtf 파일을 생성하는 프로그램을 작성 중입니다. 더 구체적인 RtfTable 또는 pdfTable보다는 iText 클래스 테이블과 셀을 사용하여 어느 파일이든 끝에 생성 될 수 있도록했습니다. 셀 패딩을 -1 값으로 설정해야하거나 그렇지 않으면 인쇄 된 시트의 각 데이터 행 사이에 너무 많은 공간이있었습니다. 그러나, 나는 지금 국경 (특히 pdf 파일에)을 추가하려고 노력하고있다. 그리고 세포는 텍스트와 일렬로 나란히 서 있지 않다. 각 셀의 아래쪽 경계선은 텍스트를 통해 직접 자릅니다. 셀 패딩이 2 이상으로 설정된 경우 텍스트를 실제로 둘러 쌉니다.iText 셀 테두리 텍스트를 자르다

Document document = new Document(); 
    Paragraph paragraph = new Paragraph(); 
    Font iTextFont = new Font(Font.TIMES_ROMAN, 9, Font.NORMAL); 
    try{ 
    PdfWriter.getInstance(document, new FileOutputStream("C:/datafiles/TestiText.pdf")); 
    document.open(); 

    Table table = new Table(3); 
    table.setPadding(-1); 
    table.setWidth(90); 
    Cell cell1 = new Cell(); 
    cell1.setBorder(Rectangle.BOX); 
    cell1.setVerticalAlignment(ElementTags.ALIGN_TOP); 
    table.setDefaultCell(cell1); 
    paragraph = new Paragraph("header", iTextFont); 
    Cell cell = new Cell(paragraph); 
    cell.setHeader(true); 
    cell.setColspan(3); 
    table.addCell(cell); 
    paragraph = new Paragraph("example cell", iTextFont); 
    table.addCell(paragraph); 
    paragraph = new Paragraph("one", iTextFont); 
      table.addCell(cell); 
    paragraph = new Paragraph("two", iTextFont); 
    cell = new Cell(paragraph); 
    table.addCell(paragraph); 
    paragraph = new Paragraph("Does this start a new row?", iTextFont); 
    table.addCell(paragraph); 
    paragraph = new Paragraph("Four", iTextFont); 
    table.addCell(paragraph); 
    paragraph = new Paragraph("Five", iTextFont); 
    table.addCell(paragraph); 
    document.add(table); 
    } catch (Exception e) { 
    //handle exception 
    } 
    document.close(); 

    } 

중 하나 (텍스트 배치에 영향을주지 않고) 한 방울을 전체 경계를 아래로 이동하여이 문제를 해결하기 위해, 또는 사이의 공간을 제거하는 방법은 무엇입니까 : 아래는 내가 뭐하는 거지의 샘플입니다 셀 패딩을 -1로 설정하지 않고 각 행 (공백은 텍스트 위의 문제 일뿐, 아래가 아님)

+0

해결 방법을 찾으셨습니까? 나는 똑같은 문제를 겪고있다. 나는 패딩에 기인 한 것으로 보이지만, 패딩을 줄이면 텍스트가 아래쪽 경계를 자른다. –

+2

신경 쓰지 마세요, 저는 PdfPTable로 전환했습니다. 이제 괜찮습니다. PDF 만 생성하기 때문에이 솔루션으로 충분합니다. –

+0

가져온 라이브러리를 Java에 추가하십시오. java를 구현하기 위해 "전체 클래스 제공" – shareef

답변

0

당신은 온다 여기에 하나가 같은 문제에 직면 있도록 을 시작하는 곳이 질문에 대한 일반적인 대답하지 않을 수 있습니다 그것이 내가이 답변을 게시 구성 요소 유용하고 잘 감싸 인 많은 방법이있다 PdfPTable 알고 사용하지만해야 ...

Organizing content in tables
The PDF output

import java.io.FileOutputStream; 
import java.io.IOException; 

import com.itextpdf.text.Document; 
import com.itextpdf.text.DocumentException; 
import com.itextpdf.text.Phrase; 
import com.itextpdf.text.pdf.PdfPCell; 
import com.itextpdf.text.pdf.PdfPTable; 
import com.itextpdf.text.pdf.PdfWriter; 

public class Spacing { 

    /** The resulting PDF file. */ 
    public static final String RESULT = "results/part1/chapter04/spacing.pdf"; 

    /** 
    * Main method. 
    * @param args no arguments needed 
    * @throws DocumentException 
    * @throws IOException 
    */ 
    public static void main(String[] args) 
     throws DocumentException, IOException { 
     // step 1 
     Document document = new Document(); 
     // step 2 
     PdfWriter.getInstance(document, new FileOutputStream(RESULT)); 
     // step 3 
     document.open(); 
     // step 4 
     PdfPTable table = new PdfPTable(2); 
     table.setWidthPercentage(100); 
     Phrase p = new Phrase(
      "Dr. iText or: How I Learned to Stop Worrying " + 
      "and Love the Portable Document Format."); 
     PdfPCell cell = new PdfPCell(p); 
     table.addCell("default leading/spacing"); 
     table.addCell(cell); 
     table.addCell("absolute leading: 20"); 
     cell.setLeading(20f, 0f); 
     table.addCell(cell); 
     table.addCell("absolute leading: 3; relative leading: 1.2"); 
     cell.setLeading(3f, 1.2f); 
     table.addCell(cell); 
     table.addCell("absolute leading: 0; relative leading: 1.2"); 
     cell.setLeading(0f, 1.2f); 
     table.addCell(cell); 
     table.addCell("no leading at all"); 
     cell.setLeading(0f, 0f); 
     table.addCell(cell); 
     cell = new PdfPCell(new Phrase(
      "Dr. iText or: How I Learned to Stop Worrying and Love PDF")); 
     table.addCell("padding 10"); 
     cell.setPadding(10); 
     table.addCell(cell); 
     table.addCell("padding 0"); 
     cell.setPadding(0); 
     table.addCell(cell); 
     table.addCell("different padding for left, right, top and bottom"); 
     cell.setPaddingLeft(20); 
     cell.setPaddingRight(50); 
     cell.setPaddingTop(0); 
     cell.setPaddingBottom(5); 
     table.addCell(cell); 
     p = new Phrase("iText in Action Second Edition"); 
     table.getDefaultCell().setPadding(2); 
     table.getDefaultCell().setUseAscender(false); 
     table.getDefaultCell().setUseDescender(false); 
     table.addCell("padding 2; no ascender, no descender"); 
     table.addCell(p); 
     table.getDefaultCell().setUseAscender(true); 
     table.getDefaultCell().setUseDescender(false); 
     table.addCell("padding 2; ascender, no descender"); 
     table.addCell(p); 
     table.getDefaultCell().setUseAscender(false); 
     table.getDefaultCell().setUseDescender(true); 
     table.addCell("padding 2; descender, no ascender"); 
     table.addCell(p); 
     table.getDefaultCell().setUseAscender(true); 
     table.getDefaultCell().setUseDescender(true); 
     table.addCell("padding 2; ascender and descender"); 
     cell.setPadding(2); 
     cell.setUseAscender(true); 
     cell.setUseDescender(true); 
     table.addCell(p); 
     document.add(table); 
     // step 5 
     document.close(); 
    } 
} 
1

테이블을 구축하는 클래스 또는 일반적인 방법을 쓰기 - 당신은 테이블 또는 P를 사용하고 있는지 여부 dfPTable.

이러한 방법은 표준 정렬, 오름차순/내림차순 등을 기반으로 한 측정을 처리합니다. 또한 "3pt 빈 단락"또는 다른 표준 서식을 추가 할 수있는 일반적인 위치를 제공합니다.

OO 소프트웨어는 반복적 인 코드 섹션과 일관성이없는 코드 섹션에 대한 의미가 아닙니다.

희망이 도움이됩니다.