2014-03-31 167 views
1

我在我的项目中更改了一些表格,并在所有表格的末尾添加了图像。使用Itextsharp自动分页

我的问题是,如果我添加了太多的表格,图片只是收缩而没有移动到下一页。

此外,我还有更多的报告,我想自动分页,所以如果一个表超出当前页的愤怒,它会自动将此表移动到下一页。

我在C#写于2010年VS

这是我的代码部分:

private void CreateQCTable(Document mdoc, DataRow Headers, DataTable Table, String imgurl, int lb_or_ss) 
{ 
    PdfPTable table; 
    PdfPCell cell; 
    CultureInfo currentCulture = new CultureInfo("he-IL"); 

    Font ArialSmall = FontFactory.GetFont("Arial", 7F, Font.NORMAL, BaseColor.BLACK); 
    Font ArialSmallRowSpan = FontFactory.GetFont("Arial", 7F, Font.NORMAL, BaseColor.BLACK); 
    Font ArialSmallGreen = FontFactory.GetFont("Arial", 7F, Font.NORMAL, new BaseColor(0, 100, 0)); 
    Font ArialSmallRed = FontFactory.GetFont("Arial", 7F, Font.NORMAL, BaseColor.RED); 
    Font ArialSmallBlue = FontFactory.GetFont("Arial", 7F, Font.NORMAL, new BaseColor(0, 68, 136)); 
    Font ArialBold = FontFactory.GetFont("Arial", 8F, Font.BOLD, BaseColor.BLACK); 

    String NewGroupName = Table.Rows[0]["group"].ToString(); 
    String OldGroupName = Table.Rows[0]["group"].ToString(); 
    int i = 0; 
    while (i < Table.Rows.Count) 
    { 
     float[] headersparam = new float[Headers.ItemArray.Length - 1]; 
     for (int j = 0; j < Headers.ItemArray.Length - 1; j++) 
     { 
      headersparam[j] = 0.1f; 
     } 
     table = new PdfPTable(headersparam); 
     table.WidthPercentage = 90; 
     table.SpacingBefore = 0; 


     int internalRowCount = 0; 
     if (internalRowCount == 0) 
     { 
      cell = new PdfPCell(new Phrase("DATE: " + Table.Rows[i]["group"].ToString(), ArialBold)); 
      cell.PaddingTop = 25; 
      cell.Colspan = Headers.ItemArray.Length - 1; 
      cell.HorizontalAlignment = Element.ALIGN_LEFT; 
      cell.VerticalAlignment = Element.ALIGN_MIDDLE; 
      cell.BorderWidth = 0f; 
      cell.BorderWidthBottom = 0.6f; 
      table.AddCell(cell); 
      foreach (object obj in Headers.ItemArray) 
      { 
       if (obj.ToString() != "") 
       { 
        cell = new PdfPCell(new Phrase(obj.ToString(), ArialBold)); 
        cell.PaddingTop = 10; 
        cell.PaddingBottom = 5; 
        cell.Colspan = 1; 
        cell.HorizontalAlignment = Element.ALIGN_LEFT; 
        cell.VerticalAlignment = Element.ALIGN_MIDDLE; 
        cell.BorderWidth = 0f; 
        cell.BorderWidthBottom = 0.6f; 
        table.AddCell(cell); 
       } 
      } 

      internalRowCount++; 
     } 

     while (OldGroupName == NewGroupName && i < Table.Rows.Count) 
     { 
      i++; 
      if (i < Table.Rows.Count) 
       OldGroupName = Table.Rows[i]["group"].ToString(); 

      foreach (object obj in Table.Rows[i - 1].ItemArray) 
      { 
       if (obj.ToString() != Table.Rows[i - 1]["group"].ToString()) 
       { 
        if (obj.ToString() == "PASSED") 
         AddTextCell(table, new PdfPCell(new Phrase(obj.ToString(), ArialSmallGreen)), Element.ALIGN_LEFT, 1, 1, 0, 0.5F, 0, 0, 5, 5, BaseColor.WHITE); 
        else if(obj.ToString() == "FAILED") 
         AddTextCell(table, new PdfPCell(new Phrase(obj.ToString(), ArialSmallRed)), Element.ALIGN_LEFT, 1, 1, 0, 0.5F, 0, 0, 5, 5, BaseColor.WHITE); 
        else 
         AddTextCell(table, new PdfPCell(new Phrase(obj.ToString(), ArialSmall)), Element.ALIGN_LEFT, 1, 1, 0, 0.5F, 0, 0, 5, 5, BaseColor.WHITE); 

       } 
      } 

     } 

     mdoc.Add(table); 
     if (i < Table.Rows.Count) 
     { 
      NewGroupName = Table.Rows[i]["group"].ToString(); 

     } 



    } 
    table = new PdfPTable(new float[] { 0.1f}); 
    table.WidthPercentage = 90; 
    table.SpacingBefore = 0; 

    FileStream fs; 
    fs = File.OpenRead(imgurl); 
    int length = (int)fs.Length; 
    byte[] logo = new byte[length]; 
    fs.Read(logo, 0, length); 
    fs = null; 
    var stream = new System.IO.MemoryStream(); 
    if (logo.Length != 0) 
    { 
     System.Drawing.Image img2 = System.Drawing.Image.FromStream(new MemoryStream(logo)); 
     img2.Save(stream, ImageFormat.Jpeg); 
     stream.Position = 0; 

    } 

    cell = new PdfPCell(iTextSharp.text.Image.GetInstance(stream), true); 
    cell.Rowspan = 1; 
    cell.PaddingTop = 25; 
    cell.PaddingBottom = 50; 
    cell.PaddingLeft = 50; 
    cell.PaddingRight = 50; 
    cell.HorizontalAlignment = Element.ALIGN_CENTER; 
    cell.VerticalAlignment = Element.ALIGN_MIDDLE; 
    cell.BorderWidth = 0f; 
    table.AddCell(cell); 

    mdoc.Add(table); 
    } 

感谢您的帮助!

回答

2

在Java中,应当使用:

image.setScaleToFitHeight(false); 

在iTextSharp的相应的语法是:

Image.ScaleToFitLineWhenOverflow = false; 

(由user2235615见注释)

+0

我发现这一点:** pdfImage.ScaleToFit( fitwidth,fitheight); **但它不起作用。 – user2235615

+0

好吧,我发现在C#中的相应语法 - ** Image.ScaleToFitLineWhenOverflow = false; **谢谢 – user2235615

+0

谢谢,我相应地更新了我的答案。 –