2017-04-04 1 views
1

이미지를 셀의 오른쪽 하단에 정렬하려고합니다. 기본적으로 각 행에 대해 두 개의 셀이있는 테이블을 만듭니다. 셀에는 텍스트와 이미지가 포함되어 있는데,이 이미지는 셀의 오른쪽 하단에 정렬됩니다. (물론 텍스트의 위치에 영향을주지 않고) 이것은 내가 각 셀의 오른쪽 아래에있는 이미지를 배치하려면 어떻게해야 내 코드iTextSharp PdfPCell 셀 하단에 이미지 정렬

PdfPTable table = new PdfPTable(2); 
table.TotalWidth = 400f; 
table.LockedWidth = true; 

float[] widths = new float[] { 1.5f, 1.5f }; 
table.SetWidths(widths); 
table.HorizontalAlignment = 0; 

table.SpacingBefore = 50f; 
table.SpacingAfter = 30f; 

iTextSharp.text.Image logoImage = iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath("~/Images/MyImage.png")); 
logoImage.ScaleAbsolute(40, 40); 
logoImage.Alignment = iTextSharp.text.Image.ALIGN_BOTTOM; 
logoImage.Alignment = iTextSharp.text.Image.RIGHT_ALIGN; 

foreach (EmployeeModel employee in employees) 
{ 
    PdfPCell cell = new PdfPCell(); 
    cell.FixedHeight = 140f; 

    cell.PaddingLeft = 30f; 
    cell.PaddingRight = 10f; 
    cell.PaddingTop = 20f; 
    cell.PaddingBottom = 5f; 

    Paragraph p = new Paragraph(GetLabelCellText(Employee), NormalFont); 
    p.Alignment = Element.ALIGN_LEFT; 
    p.Alignment = Element.ALIGN_TOP; 
    cell.AddElement(p); 

    cell.AddElement(logoImage); 

    table.AddCell(cell); 
} 

되는 화상

를 참조하십시오. 당신이 Table이와 열을 만들 수 있지만

정말 당신을 throw을 가정합니다 ....하지 않을 경우 요소의 짝수의 employees 수집을했다 확인하지 않고 셀을 추가하기 때문에

답변

0

문제는 조금 모호 당신이 원하는 레이아웃을 얻을 수있는 텍스트 이미지 단일 셀, 아마 가장 간단한 방법 모두를 원하는가하는 것은 IPdfPCellEvent을 구현하는 것입니다 :

public class BottomRightImage : IPdfPCellEvent 
{ 
    public Image Image { get; set; } 

    public void CellLayout(
     PdfPCell cell, 
     Rectangle position, 
     PdfContentByte[] canvases) 
    { 
     if (Image == null) throw new InvalidOperationException("image is null"); 

     PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS]; 
     Image.SetAbsolutePosition(
      position.Right - Image.ScaledWidth - cell.PaddingRight, 
      position.Bottom + cell.PaddingBottom 
     ); 
     canvas.AddImage(Image); 
    } 
} 

PdfPCellCellEvent 속성을 설정합니다. 다음은 간단한 작업 예입니다

using (var stream = new MemoryStream()) 
{ 
    using (var document = new Document()) 
    { 
     PdfWriter.GetInstance(document, stream); 
     document.Open(); 
     var table = new PdfPTable(2) 
     { 
      HorizontalAlignment = Element.ALIGN_LEFT, 
      TotalWidth = 400f, 
      LockedWidth = true       
     }; 

     var image = Image.GetInstance(imagePath); 
     image.ScaleAbsolute(40, 40); 
     var cellEvent = new BottomRightImage() { Image = image }; 

     var testString = 
@"first name: {0} 
last name: {0} 
ID no: {0}"; 
     for (int i = 0; i < 2; ++i) 
     { 
      var cell = new PdfPCell() 
      { 
       FixedHeight = 140f, 
       PaddingLeft = 30f, 
       PaddingRight = 10f, 
       PaddingTop = 20f, 
       PaddingBottom = 5f 
      }; 
      cell.CellEvent = cellEvent; 

      var p = new Paragraph(string.Format(testString, i)) 
      { 
       Alignment = Element.ALIGN_TOP | Element.ALIGN_LEFT 
      }; 
      cell.AddElement(p); 
      table.AddCell(cell); 
     } 
     document.Add(table); 
    } 
    File.WriteAllBytes(outputFile, stream.ToArray()); 
} 

그리고 PDF 출력 :

enter image description here

모든 - 검은 사각형 이미지가 PdfPCell 패딩을 표시하는 데 사용됩니다 고려됩니다.