这是我的itext代码,请帮助我如何设置cell.Width [英] this is my code for itext ,please help me how to set cell.Width
问题描述
#region
私人void M_createPDF()
{
字符串路径= Server.MapPath("PDF");
/*徽标路径*/
字符串strLogoPath = Server.MapPath("images")+"\\ clock.png";
文档doc =新文档(PageSize.LETTER,25F,25F,50F,25F);
PdfWriter.GetInstance(doc,new FileStream(path +"/Font.pdf",FileMode.Create));
字符串imageFilePath = Server.MapPath(.")+"/images/pageheader_bckgr.gif";
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);
jpg.ScaleToFit(3000,770);
//如果要选择图片作为背景,
jpg.Alignment = iTextSharp.text.Image.UNDERLYING;
//如果要给图像绝对/指定的固定位置.
jpg.SetAbsolutePosition(7,69);
doc.Open();
矩形页面= doc.PageSize;
字体Verdana = FontFactory.GetFont("Verdana",10F,Font.NORMAL,BaseColor.BLACK);
PdfPTable表=新的PdfPTable(5);
PdfPCell单元格=新的PdfPCell(新的短语(标题跨越3列"));
//float [] widths = new float [] {1f,1f};
//table.SetWidths(widths);
cell.Colspan = 5;
cell.BackgroundColor = BaseColor.GRAY;
cell.HorizontalAlignment = 1; //0 =左,1 =中心,2 =右
table.AddCell(cell);
table.AddCell("Sr.No");
table.AddCell("Name");
table.AddCell("J1");
table.AddCell("J2");
table.AddCell("J3");
for(int i = 0; i< 10; i ++)
{
如果(i == 5)
{
cell = new PdfPCell(new Phrase(标题跨越3列"));
cell.BackgroundColor = BaseColor.GRAY;
cell.Colspan = 5;
cell.HorizontalAlignment = 1; //0 =左,1 =中心,2 =右
table.AddCell(cell);
}
cell = new PdfPCell(new Phrase(i.ToString()));
矩形_r =新矩形(2,2,2,2,180);
cell.FixedHeight = 20.0f;
//cell.Width = _r.Width;
table.AddCell(cell);
table.AddCell("Name" + i.ToString());
table.AddCell(i.ToString());
table.AddCell((i + 1).ToString());
table.AddCell((i + 2).ToString());
}
doc.Add(jpg);
doc.Add(table);
doc.Close();
}
私有静态无效AddImageInCell(PdfPCell单元格,iTextSharp.text.Image图像,float fitWidth,float fitHight,int Alignment)
{
image.ScaleToFit(fitWidth,fitHight);
image.Alignment =对齐;
cell.AddElement(image);
}
私有void AddtextCell(PdfPTable表,PdfPCell单元格)
{
cell.Colspan = 3;
cell.HorizontalAlignment = 1; //0 =左,1 =中心,2 =右
cell.Border = 0;
table.AddCell(cell);
}
private void AddtextCell(PdfPTable表,PdfPCell单元格,float paddingLeft,float paddingRight)
{
cell.Colspan = 3;
cell.HorizontalAlignment = 0; //0 =左,1 =中心,2 =右
cell.PaddingLeft = paddingLeft;
cell.PaddingRight = paddingRight;
cell.Border = 0;
table.AddCell(cell);
}
private void AddtextCell(PdfPTable表,PdfPCell单元格,float paddingLeft,float paddingRight,int hAlign)
{
cell.Colspan = 3;
cell.HorizontalAlignment = hAlign; //0 =左,1 =中心,2 =右
cell.PaddingLeft = paddingLeft;
cell.PaddingRight = paddingRight;
cell.Border = 0;
table.AddCell(cell);
}
私有静态无效AddtextCell(PdfPTable表,PdfPCell单元格,int Colspan,int HorizontalAlignment,int Border)
{
cell.Colspan = Colspan;
cell.HorizontalAlignment = HorizontalAlignment; //0 =左,1 =中心,2 =右
cell.Border =边框;
table.AddCell(cell);
}
#endregion
#region
private void M_createPDF()
{
string path = Server.MapPath("PDF");
/*Logo Path*/
string strLogoPath = Server.MapPath("images") + "\\clock.png";
Document doc = new Document(PageSize.LETTER, 25F, 25F, 50F, 25F);
PdfWriter.GetInstance(doc, new FileStream(path + "/Font.pdf", FileMode.Create));
string imageFilePath = Server.MapPath(".") + "/images/pageheader_bckgr.gif";
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);
jpg.ScaleToFit(3000, 770);
//If you want to choose image as background then,
jpg.Alignment = iTextSharp.text.Image.UNDERLYING;
//If you want to give absolute/specified fix position to image.
jpg.SetAbsolutePosition(7, 69);
doc.Open();
Rectangle page = doc.PageSize;
Font Verdana = FontFactory.GetFont("Verdana", 10F, Font.NORMAL, BaseColor.BLACK);
PdfPTable table = new PdfPTable(5);
PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns"));
//float[] widths = new float[] { 1f, 1f };
//table.SetWidths(widths);
cell.Colspan = 5;
cell.BackgroundColor = BaseColor.GRAY;
cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
table.AddCell(cell);
table.AddCell("Sr.No");
table.AddCell("Name");
table.AddCell("J1");
table.AddCell("J2");
table.AddCell("J3");
for (int i = 0; i < 10; i++)
{
if (i == 5)
{
cell = new PdfPCell(new Phrase("Header spanning 3 columns"));
cell.BackgroundColor = BaseColor.GRAY;
cell.Colspan = 5;
cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
table.AddCell(cell);
}
cell = new PdfPCell(new Phrase(i.ToString()));
Rectangle _r = new Rectangle(2, 2, 2, 2, 180);
cell.FixedHeight = 20.0f;
//cell.Width = _r.Width;
table.AddCell(cell);
table.AddCell("Name" + i.ToString());
table.AddCell(i.ToString());
table.AddCell((i + 1).ToString());
table.AddCell((i + 2).ToString());
}
doc.Add(jpg);
doc.Add(table);
doc.Close();
}
private static void AddImageInCell(PdfPCell cell, iTextSharp.text.Image image, float fitWidth, float fitHight, int Alignment)
{
image.ScaleToFit(fitWidth, fitHight);
image.Alignment = Alignment;
cell.AddElement(image);
}
private void AddtextCell(PdfPTable table, PdfPCell cell)
{
cell.Colspan = 3;
cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
cell.Border = 0;
table.AddCell(cell);
}
private void AddtextCell(PdfPTable table, PdfPCell cell, float paddingLeft, float paddingRight)
{
cell.Colspan = 3;
cell.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
cell.PaddingLeft = paddingLeft;
cell.PaddingRight = paddingRight;
cell.Border = 0;
table.AddCell(cell);
}
private void AddtextCell(PdfPTable table, PdfPCell cell, float paddingLeft, float paddingRight, int hAlign)
{
cell.Colspan = 3;
cell.HorizontalAlignment = hAlign; //0=Left, 1=Centre, 2=Right
cell.PaddingLeft = paddingLeft;
cell.PaddingRight = paddingRight;
cell.Border = 0;
table.AddCell(cell);
}
private static void AddtextCell(PdfPTable table, PdfPCell cell, int Colspan, int HorizontalAlignment, int Border)
{
cell.Colspan = Colspan;
cell.HorizontalAlignment = HorizontalAlignment; //0=Left, 1=Centre, 2=Right
cell.Border = Border;
table.AddCell(cell);
}
#endregion
推荐答案
尝试一下此朋友:
http://itextpdf.com/examples/iia.php?id=76 [ ^ ]
http://www.kodejava.org/examples/833.html [ http://itext-general.2136553. n4.nabble.com/How-to-set-PDFPtable-cell-s-width-td2153558.html [ http://www.mail-archive.com/itext-questions@lists. sourceforge.net/msg03885.html [ ^ ]
谢谢,
Ambesha
Try this freind:
http://itextpdf.com/examples/iia.php?id=76[^]
http://www.kodejava.org/examples/833.html[^]
http://itext-general.2136553.n4.nabble.com/How-to-set-PDFPtable-cell-s-width-td2153558.html[^]
http://www.mail-archive.com/itext-questions@lists.sourceforge.net/msg03885.html[^]
Thanks,
Ambesha
这篇关于这是我的itext代码,请帮助我如何设置cell.Width的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!