2012-09-24 3 views
5

나는 매월 수평 목록으로 월의 세로 목록을 작성합니다. 매일 나는 크기가 채워진 사각형을 추가하고 있습니다. 크기 및 색상은 db 쿼리의 값에 따라 다릅니다. 그것은 항상에 위치 :센터 정렬 방법 PdfPCell에서 서식 파일 요소 맞추기

난 괜찮아 (사각형, 사각형의 색상의 크기), 떨어져 사각형의 위치에서 작품을 다른 this answer

다에서 제공 PdfPTable, PdfPCellcbCreateTemplate을 사용하고 있습니다 0,0 비록 내가 V & H 위치를 설정했다고 생각합니다. 코드의 발췌 부분은 아래와 같습니다. 제발 조언. should be aligned center/center

그것은 센터/센터 있어야한다 :

int Severity = args.getPLR().get(i).getItems().get(j).getItems().get(itemIndex).getSeverity(); 
Severity = Severity + 5; //plump up, so that max (10) should fill the cell 
PdfPCell cell = new PdfPCell(); 
cell.setPadding(0); 
template = cb.createTemplate(Severity, Severity); 
template.setLineWidth(0.5f); 
template.rectangle(0, 0, Severity, Severity); 
//we should switch the color 
//based on the Severity 
switch ((Severity-5)) { 
    case 0: 
     template.setColorFill(Color.GREEN); 
     break; 
    case 1: 
     template.setColorFill(Color.GREEN); 
     break; 
    case 2: 
     template.setColorFill(Color.YELLOW); 
     break; 
    case 3: 
     template.setColorFill(Color.YELLOW); 
     break; 
    case 4: 
     template.setColorFill(Color.YELLOW); 
     break; 
    case 5: 
     template.setColorFill(Color.ORANGE); 
     break; 
    case 6: 
     template.setColorFill(Color.ORANGE); 
     break; 
    case 7: 
     template.setColorFill(Color.ORANGE); 
     break; 
    case 8: 
     template.setColorFill(Color.RED); 
     break; 
    case 9: 
     template.setColorFill(Color.RED); 
     break; 
    case 10: 
     template.setColorFill(Color.RED); 
     break; 
} 
template.fill(); 
img = Image.getInstance(template);   
chunk = new Chunk(img, 0f, 0f); 
cell.addElement(chunk); 
cell.setHorizontalAlignment(Element.ALIGN_CENTER); 
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); 
painTable.addCell(cell); 

가 표시되는 내용의 그래픽입니다. 내가 어디로 잘못 갔니?

이 허용 솔루션을 사용하여 업데이트 된 코드의 일부입니다

img = Image.getInstance(template);   
chunk = new Chunk(img, 0f, 0f); 
Phrase severityChunk = new Phrase(chunk); 
PdfPCell cell = new PdfPCell(severityChunk); 
cell.setPadding(0); 
cell.setHorizontalAlignment(Element.ALIGN_CENTER); 
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); 
painTable.addCell(cell); 

답변

10

당신은 text modecomposite mode 혼합하고 있습니다.

정렬 속성은 텍스트 모드에서만 PdfPCell 수준으로 설정됩니다. 복합 모드로 전환하면 (addElement() 메서드 사용) iText는 셀 내용에 대해 정의 된 정렬을 위해 셀에 대해 정의 된 정렬을 무시합니다.

텍스트 모드에서는 모든 콘텐츠의 정렬이 동일합니다. 복합 모드에서는 정렬이 다른 여러 요소를 가질 수 있습니다.

다른 옵션이 있습니다. ChunkParagraph에 넣고 셀 대신 단락의 맞춤을 정의 할 수 있습니다. 청크를 포함하는 Phrase을 사용하여 텍스트 모드에서 셀을 만들 수 있습니다. 청크 등을 사용하지 않고 Image으로 셀을 생성하는 것이 가능할 수도 있습니다.

이것은 "iText in Action"에서 설명한 것입니다.

+0

감사합니다. Bruno. Phrase in Cell 솔루션을 사용하고 위의 업데이트 된 코드를 추가했습니다. – DaveSav