2017-04-04 5 views
1

画像をセルの右下に揃えようとしています。 私は基本的に各行に2つのセルを持つテーブルを作成しています。セルには、テキストと画像が含まれています。これは、セルの右下に揃えたいものです。 (もちろん、テキストの位置に影響を与えずに)これは私が各セルの右下の画像を配置するにはどうすればよい私のコード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 2で列を作成しますが、

は本当にあなたをthrowを仮定します....ない場合は偶数個の要素は、employeesコレクションを持って検証せずにセルを追加するので

答えて

0

の質問は少しあいまいです

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); 
    } 
} 
:おそらくあなたがしたいレイアウトを取得する最も簡単な方法は、 IPdfPCellEventを実装することで、 単一セル内のテキスト 画像の両方をしたいですか

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パディングを示すために使用されて考慮されています。

関連する問題