C# iTextSharp没有';不显示所有列
我最近开始使用C# iTextSharp没有';不显示所有列,c#,itext,C#,Itext,我最近开始使用itextsharp,并一直使用它在asp.net中使用web服务创建PDF报告。我在web服务中的代码如下所示。我的问题是,它没有显示前3列数据 我认为问题出在dt.Rows中 string[] strFile = Directory.GetFiles(strUploadPath); Array.ForEach(Directory.GetFiles(strUploadPath), File.Delete); Document d
itextsharp
,并一直使用它在asp.net中使用web服务创建PDF报告。我在web服务中的代码如下所示。我的问题是,它没有显示前3列数据
我认为问题出在dt.Rows
中
string[] strFile = Directory.GetFiles(strUploadPath);
Array.ForEach(Directory.GetFiles(strUploadPath), File.Delete);
Document document = new Document();
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(strUploadPath + "/" + strFilename, FileMode.Create));
document.Open();
Font font5 = FontFactory.GetFont(FontFactory.HELVETICA, 5);
PdfPTable table = new PdfPTable(dt.Columns.Count);
float[] widths = new float[] { 4f, 4f, 4f, 4f, 4f, 4f };
table.SetWidths(widths);
table.WidthPercentage = 200;
PdfPCell cell = new PdfPCell(new Phrase());
int j = 1;
foreach (DataColumn c in dt.Columns)
{
if (j <= (dt.Columns.Count))
{
//table.AddCell(new Phrase(c.ToString(),font5));
//table.AddCell(new Phrase(j.ToString(),font5));
table.AddCell(new Phrase(c.ColumnName, font5));
}
j++;
}
int k = 1;
foreach (DataRow r in dt.Rows)
{
if (dt.Rows.Count > 0)
{
table.AddCell(new Phrase(k.ToString(), font5));
table.AddCell(new Phrase(r[1].ToString(), font5));
table.AddCell(new Phrase(r[2].ToString(), font5));
table.AddCell(new Phrase(r[3].ToString(), font5));
table.AddCell(new Phrase(r[4].ToString(), font5));
table.AddCell(new Phrase(r[5].ToString(), font5));
// table.AddCell(new Phrase(r[6].ToString(), font5));
// table.AddCell(new Phrase(r[7].ToString(), font5));
}
k++;
}
document.Add(table);
document.CloseDocument();
document.Close();
return strFilename;
}
else
{
return null;
}
string[]strFile=Directory.GetFiles(strUploadPath);
ForEach(Directory.GetFiles(strUploadPath)、File.Delete);
文档=新文档();
PdfWriter writer=PdfWriter.GetInstance(文档,新文件流(strUploadPath+“/”+strFilename,FileMode.Create));
document.Open();
Font font5=FontFactory.GetFont(FontFactory.HELVETICA,5);
PdfPTable table=新的PdfPTable(dt.Columns.Count);
float[]宽度=新的float[]{4f,4f,4f,4f,4f};
表.设置宽度(宽度);
表1.2所示百分比=200;
PdfPCell cell=新的PdfPCell(新短语());
int j=1;
foreach(dt.列中的数据列c)
{
if(j 0)
{
table.AddCell(新短语(k.ToString(),font5));
table.AddCell(新短语(r[1].ToString(),font5));
table.AddCell(新短语(r[2].ToString(),font5));
table.AddCell(新短语(r[3].ToString(),font5));
table.AddCell(新短语(r[4].ToString(),font5));
table.AddCell(新短语(r[5].ToString(),font5));
//table.AddCell(新短语(r[6].ToString(),font5));
//table.AddCell(新短语(r[7].ToString(),font5));
}
k++;
}
文件。添加(表);
document.CloseDocument();
document.Close();
返回strFilename;
}
其他的
{
返回null;
}
OP的代码包含以下行:
table.WidthPercentage = 200;
这导致表格的宽度是页面减去页边距的两倍(200%)。因此,它是部分隐藏的
使用WidthPercentage
属性设置表格宽度时,通常不应将其设置为100以上
OP同时确认,适当的更改会使代码正常工作。您确定
table.WidthPercentage=200
是个好主意吗?这毕竟意味着表格的宽度将是页面减去页边距后宽度的两倍……感谢您提供的有关其工作的信息,我将给出答案。@NiroseBasnyat请接受mkl的答案(在下面分数附近打勾),因为它帮助您解决了问题。