【发布时间】:2018-06-30 17:40:52
【问题描述】:
我正在使用 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;
在上面的代码中,“myTable”在我的 PDF“文档”中不可见
【问题讨论】:
-
你试过什么?您没有包含任何代码。您也没有包含 iText 版本。
-
我使用的是 IText 5.5.1 版
-
HTMLWorker相关部分是否相关?特别是,您是否打算在 HTML 中创建需要背景颜色的表格?还是只是一些通用内容?