C# IText单元格数据的背景色

C# IText单元格数据的背景色,c#,asp.net,itext,C#,Asp.net,Itext,我正在使用itextsharp创建PDF,我有三行数据。我可以设置文本的背景色,但我需要的是根据不同行中的文本为整个区域提供背景色。 我通过添加表找到了同样的解决方案,但是当我添加表时,我的表在文档中不可见 任何人都可以共享完整的代码来添加表格,然后添加带有内容和背景颜色的单元格吗? 这是我的密码: float width = 85.6063f; iTextSharp.text.Rectangle pageSize = new iTextSharp.text.Rectangle(width, t

我正在使用itextsharp创建PDF,我有三行数据。我可以设置文本的背景色,但我需要的是根据不同行中的文本为整个区域提供背景色。 我通过添加表找到了同样的解决方案,但是当我添加表时,我的表在文档中不可见

任何人都可以共享完整的代码来添加表格,然后添加带有内容和背景颜色的单元格吗? 这是我的密码:

float width = 85.6063f;
iTextSharp.text.Rectangle pageSize = new iTextSharp.text.Rectangle(width, tempHeight);
if (System.IO.File.Exists(path))
{
    System.IO.File.Delete(path);
}
Document document = null;
PdfWriter writer = null;
document = new Document(pageSize, 4, 4, 0, 5);

writer = PdfWriter.GetInstance(document, new FileStream(path, FileMode.OpenOrCreate));
document.Open();
PdfPCell myCell;
iTextSharp.text.Font font1 = FontFactory.GetFont(FontFactory.HELVETICA, 12, BaseColor.BLACK);
myCell = new PdfPCell(new Phrase("hi", font1));
PdfPTable myTable = new PdfPTable(2);

PdfPCell cellTest = new PdfPCell(new Phrase("This is a test document"));
cellTest.BackgroundColor=new BaseColor(255,0,0);
myTable.AddCell(cellTest);

document.Add(myTable);
Paragraph welcomeParagraph = new Paragraph("Hello, World");

document.Add(welcomeParagraph);
string BinPath = Server.MapPath("~");
String strAppPath = BinPath;
float fontSize = 5;

BaseFont bfnimodmt = BaseFont.CreateFont(BinPath + @"Fonts\mangalb.ttf", BaseFont.CP1252, BaseFont.EMBEDDED);
iTextSharp.text.Font font = new iTextSharp.text.Font(bfnimodmt, 12);
Chunk c1 = new Chunk(HTML, font);
Phrase p1 = new Phrase(c1);
Paragraph ph = new Paragraph();
ph.Add(p1);
iTextSharp.text.Font fonts = new iTextSharp.text.Font(bfnimodmt, (float)fontSize);
iTextSharp.text.Font fontsbold = new iTextSharp.text.Font(bfnimodmt, (float)fontSize);

BaseFont bfnimodmta = BaseFont.CreateFont(BinPath + @"Fonts\mangal.ttf", BaseFont.CP1252, BaseFont.EMBEDDED);
iTextSharp.text.Font fonts1 = new iTextSharp.text.Font(bfnimodmt, (float)fontSize);
iTextSharp.text.Font fontsbold1 = new iTextSharp.text.Font(bfnimodmt, (float)fontSize);
//For Image-------------------------------------------------Image------------------------------
#region For image

string imageURL = imageDynamicPath;
string FrameImgPath = Server.MapPath("~") + @"Images\big.jpg";

System.Drawing.Image img1 = System.Drawing.Image.FromFile(imageURL);
System.Drawing.Image img2 = System.Drawing.Image.FromFile(FrameImgPath);

String jpg3 = Server.MapPath("~") + @"Images\big1.jpg";
Bitmap template = new Bitmap(img2);
Bitmap Picture = new Bitmap(img1);
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageURL);
jpg.ScaleAbsolute(80f, 80f);
//Framejpg.ScaleAbsolute(85f, 85f);
PdfPCell cell = new PdfPCell(jpg);
cell.HorizontalAlignment = Element.ALIGN_CENTER;
#endregion
string strFirst, strSecond;
string HTMLinput = HTML;
int FirstEnter = HTMLinput.IndexOf("\r\n");
int FirstSpace = HTMLinput.IndexOf(" ");
int firstLINE = HTMLinput.IndexOf("</p>");
if (FirstSpace == -1 && FirstEnter == -1)
{
    strFirst = HTMLinput;
    strSecond = "";
}
else
{
    if (FirstSpace == -1)
    {
        strFirst = HTMLinput.Substring(0, HTMLinput.IndexOf("\r\n"));
        strSecond = HTMLinput.Substring(HTMLinput.IndexOf("\r\n"));
    }
    else if (FirstEnter == -1)
    {
        strFirst = HTMLinput.Substring(0, HTMLinput.IndexOf(" "));
        strSecond = HTMLinput.Substring(HTMLinput.IndexOf(" "));
    }
    else if (FirstSpace > FirstEnter)
    {
        strFirst = HTMLinput.Substring(0, HTMLinput.IndexOf("\r\n"));
        strSecond = HTMLinput.Substring(HTMLinput.IndexOf("\r\n"));
    }
    else //if (FirstSpace < FirstEnter )
    {
        strFirst = HTMLinput.Substring(0, HTMLinput.IndexOf(" "));
        strSecond = HTMLinput.Substring(HTMLinput.IndexOf(" "));
    }
}
Chunk ch = new Chunk(strFirst.ToUpper());
string seperator = "", strThid = "";
Chunk ch1 = new Chunk(strSecond);
Chunk ch2 = new Chunk(strThid);
Chunk ch3 = new Chunk(seperator);
iTextSharp.text.Paragraph prText = new iTextSharp.text.Paragraph(" ");
prText.Leading = 1;
List<IElement> objects = HTMLWorker.ParseToList(new StringReader(HTMLinput), null);
foreach (IElement element in objects)
{
    pageSize.BackgroundColor = new BaseColor(255, 222, 173);
    document.Add(element);
    break;
}
int remove = Math.Min(objects.Count, 1);
objects.RemoveRange(0, remove);
foreach (IElement element in objects)
{
    document.Add(element);
}
document.Close();

return true;
浮动宽度=85.6063f;
iTextSharp.text.Rectangle pageSize=新的iTextSharp.text.Rectangle(宽度、高度);
if(System.IO.File.Exists(path))
{
System.IO.File.Delete(路径);
}
单据=空;
PdfWriter writer=null;
文档=新文档(页面大小,4,4,0,5);
writer=PdfWriter.GetInstance(文档,新文件流(路径,FileMode.OpenOrCreate));
document.Open();
PDFP细胞菌丝体;
iTextSharp.text.Font font1=FontFactory.GetFont(FontFactory.HELVETICA,12,BaseColor.BLACK);
迈塞尔=新的PdfPCell(新短语(“hi”,font1));
PdfPTable myTable=新的PdfPTable(2);
PdfPCell cellTest=新的PdfPCell(新短语(“这是一份测试文件”);
cellTest.BackgroundColor=新基色(255,0,0);
myTable.AddCell(cellTest);
文件。添加(myTable);
段落Welcome段落=新段落(“你好,世界”);
文件.添加(第段);
字符串BinPath=Server.MapPath(“~”);
字符串路径=BinPath;
浮点数=5;
BaseFont bfnimodmt=BaseFont.CreateFont(BinPath+@“Fonts\mangalb.ttf”,BaseFont.CP1252,BaseFont.EMBEDDED);
iTextSharp.text.Font Font=新的iTextSharp.text.Font(bfnimodmt,12);
Chunk c1=新块(HTML,字体);
短语p1=新短语(c1);
段落ph=新段落();
ph.Add(p1);
iTextSharp.text.Font字体=新的iTextSharp.text.Font(bfnimodmt,(float)fontSize);
iTextSharp.text.Font fontsbold=新的iTextSharp.text.Font(bfnimodmt,(float)fontSize);
BaseFont bfnimodmta=BaseFont.CreateFont(BinPath+@“Fonts\mangal.ttf”,BaseFont.CP1252,BaseFont.EMBEDDED);
iTextSharp.text.Font fonts1=新的iTextSharp.text.Font(bfnimodmt,(float)fontSize);
iTextSharp.text.Font fontsbold1=新的iTextSharp.text.Font(bfnimodmt,(float)fontSize);
//对于图像------------------------------------------图像------------------------------
#图像区域
字符串imageURL=imageDynamicPath;
字符串FrameImgPath=Server.MapPath(“~”+@“Images\big.jpg”;
System.Drawing.Image img1=System.Drawing.Image.FromFile(imageURL);
System.Drawing.Image img2=System.Drawing.Image.FromFile(FrameImgPath);
字符串jpg3=Server.MapPath(“~”+@“Images\big1.jpg”;
位图模板=新位图(img2);
位图图片=新位图(img1);
iTextSharp.text.Image jpg=iTextSharp.text.Image.GetInstance(imageURL);
jpg.可标度溶质(80f,80f);
//可标度溶质(85f,85f);
PdfPCell=新的PdfPCell(jpg);
cell.HorizontalAlignment=Element.ALIGN_CENTER;
#端区
字符串strFirst,strSecond;
字符串HTMLinput=HTML;
int FirstEnter=HTMLinput.IndexOf(“\r\n”);
int FirstSpace=HTMLinput.IndexOf(“”);
int firstLINE=HTMLinput.IndexOf(“

”); if(FirstSpace==-1&&FirstEnter=-1) { strFirst=HTMLinput; strSecond=“”; } 其他的 { if(FirstSpace==-1) { strFirst=HTMLinput.Substring(0,HTMLinput.IndexOf(“\r\n”); strSecond=HTMLinput.Substring(HTMLinput.IndexOf(“\r\n”)); } else if(FirstEnter==-1) { strFirst=HTMLinput.Substring(0,HTMLinput.IndexOf(“”); strSecond=HTMLinput.Substring(HTMLinput.IndexOf(“”); } else if(FirstSpace>FirstEnter) { strFirst=HTMLinput.Substring(0,HTMLinput.IndexOf(“\r\n”); strSecond=HTMLinput.Substring(HTMLinput.IndexOf(“\r\n”)); } else//if(FirstSpace

在上述代码中,“myTable”在我的PDF“document”中不可见。

如果您使用的是iText7,您应该能够通过以下代码解决此问题:

    Table table = new Table(5);

    Cell sn = new Cell(2, 1).add("S/N");
    sn.setBackgroundColor(Color.YELLOW);
    table.addCell(sn);

    Cell name = new Cell(1, 3).add("Name");
    name.setBackgroundColor(Color.CYAN);
    ...
您还可以查看有关表格的教程

如果您正在使用iText5,请查看


<>如果使用iTXT启动一个新项目,请考虑使用ITXTE7。由于iText5即将不再受支持。

要使用iText 5.x for.Net创建具有单个单元格背景的表,可以使用
BackgroundColor
单元格属性:

PdfPTable table = new PdfPTable(2);
PdfPCell cell1 = new PdfPCell(new Phrase("Cell A"));
cell1.BackgroundColor = BaseColor.YELLOW;
table.AddCell(cell1);
PdfPCell cell2 = new PdfPCell(new Phrase("Cell B"));
cell2.BackgroundColor = BaseColor.GREEN;
table.AddCell(cell2);
显示了iText for Java的类似方法


要获得更多控制,例如,对于同一单元格中的多种颜色,应使用单元格事件,例如,使用此事件类

public class ColorizeBackgroundEvent : IPdfPCellEvent
{
    BaseColor color;
    public ColorizeBackgroundEvent(BaseColor color)
    {
        this.color = color;
    }

    public void CellLayout(PdfPCell cell, iTextSharp.text.Rectangle position, PdfContentByte[] canvases)
    {
        PdfContentByte canvas = canvases[PdfPTable.BACKGROUNDCANVAS];
        canvas.SaveState();
        canvas.SetColorFill(color);
        canvas.Rectangle(position.Left, position.Bottom, position.Width, position.Height);
        canvas.Fill();
        canvas.RestoreState();
    }
}
对于上述与本规范相同的效果:

using (Stream output = new FileStream(@"TableWithColoredCellBackgrounds.pdf", FileMode.Create))
using (Document document = new Document(PageSize.A4, 50, 50, 25, 25))
{
    PdfWriter writer = PdfWriter.GetInstance(document, output);
    document.Open();

    PdfPTable table = new PdfPTable(2);
    PdfPCell cell1 = new PdfPCell(new Phrase("Cell A"));
    cell1.CellEvent = new ColorizeBackgroundEvent(BaseColor.YELLOW);
    table.AddCell(cell1);
    PdfPCell cell2 = new PdfPCell(new Phrase("Cell B"));
    cell2.CellEvent = new ColorizeBackgroundEvent(BaseColor.GREEN);
    table.AddCell(cell2);
    document.Add(table);

    document.Close();
}
两种情况下的结果如下所示:


你试过什么?您没有包含任何代码。您也没有包含iText版本。我正在使用iText版本5.5.1与
HTMLWorker
相关的部分相关吗?特别是,您是否打算创建希望其背景颜色也在HTML中的表?或者它只是一些通用的内容?此代码不起作用,我使用的是IText版本5.5.1。您的问题最初没有包含这些信息。我猜不出你用的是哪个版本。如果您提供了代码,我就能够告诉您