我创建了PDF文档,使用c#asp.net中的iTextSharp显示文本框值。 pdf创建成功。但问题是,我以表格格式显示价值。我想做行跨度和跨度。请任何人帮助我...
PdfWriter.GetInstance(pdfDoc, System.Web.HttpContext.Current.Response.OutputStream);
pdfDoc.Open();
//Set Font Properties for PDF File
Font fnt = FontFactory.GetFont("Times New Roman", 14);
PdfPTable PdfTable = new PdfPTable(2);
PdfPCell Cell = new PdfPCell();
PdfTable.TotalWidth = 600f;
float[] widths = new float[] { 4f, 8f };
PdfTable.SetWidths(widths);
PdfPCell cell=new PdfPCell(new Phrase("MOM TABLE HEADER"));
cell.Rowspan=2; // this not working
PdfTable.AddCell("Meeting By");
PdfTable.AddCell(txtheld.Text);
答案 0 :(得分:1)
如果您需要创建带有 rowspan 和 colspan 的表格,您可以使用我们的 PDFFlow 库,我们有许多功能可以让您自定义表格。也许这对你来说会容易得多。这就是它的工作原理:
DocumentBuilder.New()
.AddSection()
.AddParagraph("Table with colspan and rowspan:")
.SetBold().SetMarginBottom(5)
.ToSection()
.AddTable()
.SetWidth(300)
.AddColumnToTable().AddColumnToTable()
.AddColumnToTable().AddColumnToTable()
.AddRow()
.SetBackColor(Color.FromRgba(0, 0.69, 0.94, 1)).SetBold()
.AddCell("Product")
.SetRowSpan(2)
.SetVerticalAlignment(VerticalAlignment.Center)
.ToRow()
.AddCell("Month")
.SetColSpan(3)
.SetHorizontalAlignment(HorizontalAlignment.Center)
.ToTable()
.AddRow()
.SetBackColor(Color.FromRgba(0, 0.69, 0.94, 1)).SetBold()
.SetHorizontalAlignment(HorizontalAlignment.Center)
.AddCellToRow().AddCellToRow("January")
.AddCellToRow("February").AddCellToRow("March")
.ToTable()
.AddRow()
.AddCell()
.SetHorizontalAlignment(HorizontalAlignment.Left)
.AddParagraphToCell("Product 1")
.ToRow()
.SetHorizontalAlignment(HorizontalAlignment.Center)
.AddCellToRow("100").AddCellToRow("115").AddCellToRow("103")
.ToTable()
.AddRow()
.AddCell()
.SetHorizontalAlignment(HorizontalAlignment.Left)
.AddParagraphToCell("Product 2")
.ToRow()
.SetHorizontalAlignment(HorizontalAlignment.Center)
.AddCellToRow("200").AddCellToRow("204").AddCellToRow("207")
.ToTable()
.AddRow()
.SetBackColor(Color.FromRgba(0, 0.69, 0.94, 1)).SetBold()
.AddCellToRow("Total")
.AddCell("929")
.SetColSpan(3)
.SetHorizontalAlignment(HorizontalAlignment.Right)
.ToDocument().Build("Table.pdf");
上面的代码片段生成包含以下 table
的 Table.pdf 文档如您所见,您可以设置单元格跨越的列数或行数。您还可以分别自定义每一行和每一列,例如,通过定义单元格的字体和文本对齐方式或设置您需要的背景颜色。库中方法的名称很简单,因此很容易找到您要实现的功能。
要查看更多示例,请浏览我们的真实文档 samples。您可以按照随附文章中描述的步骤重现这些示例或创建类似的文档。我们的图书馆对非商业和开源项目免费。
答案 1 :(得分:0)
我认为您还没有添加rowpan单元格表格,这就是您遇到问题的原因。我尝试了以下代码并按预期工作:
protected void btnExport_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream("C://test1.pdf", FileMode.Create, FileAccess.Write, FileShare.None);
try
{
Document pdfDoc = new Document();
PdfWriter.GetInstance(pdfDoc, fs);
pdfDoc.Open();
//Set Font Properties for PDF File
Font fnt = FontFactory.GetFont("Times New Roman", 14);
PdfPTable PdfTable = new PdfPTable(2);
PdfPCell Cell = new PdfPCell();
PdfTable.TotalWidth = 600f;
float[] widths = new float[] { 4f, 8f };
PdfTable.SetWidths(widths);
PdfPCell cell = new PdfPCell(new Phrase("MOM TABLE HEADER"));
cell.Rowspan = 2; // this not working
PdfTable.AddCell(cell);
PdfTable.AddCell("Meeting By");
PdfTable.AddCell("test1");
pdfDoc.Add(PdfTable);
pdfDoc.Close();
}
catch
{
}
finally
{
fs.Close();
}
}
答案 2 :(得分:0)
使用带有行间距&amp ;;的c#在asp.net中创建PDF列跨度
1)请安装Install-Package iTextSharp-LGPL nuget
2)对于Rowspan
private static void addCellWithRowSpan(PdfPTable table, string text, int rowspan, bool colorStatus, bool fontStatus)
{
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
iTextSharp.text.Font times;
if (fontStatus == false)
{
times = new iTextSharp.text.Font(bfTimes, 8, iTextSharp.text.Font.NORMAL, Color.BLACK);
}
else
{
times = new iTextSharp.text.Font(bfTimes, 8, iTextSharp.text.Font.BOLD, Color.BLACK);
}
PdfPCell cell = new PdfPCell(new Phrase(text, times));
cell.Rowspan = rowspan;
cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
if (colorStatus == true)
{
cell.BackgroundColor = new Color(179, 179, 179);
}
table.AddCell(cell);
}
3)对于Colspan
private static void addCellWithColSpan(PdfPTable table, string text, int colspan, bool colorStatus, bool alignmentStatus, bool fontStatus)
{
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
iTextSharp.text.Font times;
if (fontStatus == false)
{
times = new iTextSharp.text.Font(bfTimes, 8, iTextSharp.text.Font.NORMAL, Color.BLACK);
}
else
{
times = new iTextSharp.text.Font(bfTimes, 8, iTextSharp.text.Font.BOLD, Color.BLACK);
}
PdfPCell cell = new PdfPCell(new Phrase(text, times));
cell.Colspan = colspan;
if (alignmentStatus == false)
{
cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
}
else
{
cell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
}
if (colorStatus == true)
{
cell.BackgroundColor = new Color(179, 179, 179);
}
table.AddCell(cell);
}
4)下面的代码将让您了解如何使用addCellWithRowSpan和addCellWithColSpan
public void GenerateInvoice()
{
var doc = new Document(PageSize.A4);
PdfWriter.GetInstance(doc, new FileStream(@"D:\" + "/Doc19.pdf", FileMode.Create));
doc.Open();
PdfPTable headerTable = new PdfPTable(new float[] { 30f, 40f });
// add a image
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
Font fontH1 = new Font(bfTimes, 9, Font.BOLD);
PdfPCell imageCell;
iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance("ImagePath");
logo.ScaleToFit(90f, 90f);
imageCell = new PdfPCell(logo);
imageCell.Colspan = 1; // either 1 if you need to insert one cell
imageCell.Border = 0;
// add a image
PdfPCell rowCell;
float[] headerWidths = new float[] { 250f, 250f };
headerTable.AddCell(imageCell);
rowCell = new PdfPCell(new Phrase("Invoice No : INV10001\nInvoices Type : ", fontH1));
rowCell.Border = 0;
rowCell.PaddingTop = 30f;
headerTable.AddCell(rowCell);
headerTable.HorizontalAlignment = 0;
headerTable.TotalWidth = 700f;
headerTable.LockedWidth = true;
headerTable.SetWidths(headerWidths);
headerTable.DefaultCell.Border = Rectangle.NO_BORDER;
doc.Add(headerTable);
Paragraph p = new Paragraph(new Chunk(new iTextSharp.text.pdf.draw.LineSeparator(0.0F, 100.0F, new Color(65, 105, 225), Element.ALIGN_LEFT, 1)));
doc.Add(p);
Paragraph p1 = new Paragraph("\n");
doc.Add(p1);
PdfPTable ContentTable = new PdfPTable(7);
ContentTable.HorizontalAlignment = 0;
ContentTable.TotalWidth = 523f;
ContentTable.LockedWidth = true;
float[] widths = new float[] { 20f, 60f, 60f, 60f, 60f, 60f, 60f };
ContentTable.SetWidths(widths);
addCellWithRowSpan(ContentTable, "#", 1, true, true);
addCellWithRowSpan(ContentTable, "Request NO", 1, true, true);
addCellWithRowSpan(ContentTable, "Shipment Date", 1, true, true);
addCellWithRowSpan(ContentTable, "VIN", 1, true, true);
addCellWithColSpan(ContentTable, "Vehicle Name", 2, true, false, true);
addCellWithRowSpan(ContentTable, "Amount", 1, true, true);
addCellWithRowSpan(ContentTable, "1", 3, false, false);
addCellWithRowSpan(ContentTable, "SHC!10002", 1, false, false);
addCellWithRowSpan(ContentTable, "1-1-1", 1, false, false);
addCellWithRowSpan(ContentTable, "VIEEEIEWWNdfssssssssssssssssssssssssssssssssssssssssss", 1, false, false);
addCellWithColSpan(ContentTable, "Vehicle Name", 2, false, false, false);
addCellWithRowSpan(ContentTable, "254567576", 3, false, false);
addCellWithRowSpan(ContentTable, "Consignee Name", 1, true, true);
addCellWithRowSpan(ContentTable, "Notify Name", 1, true, true);
addCellWithRowSpan(ContentTable, "Port Of Loading", 1, true, true);
addCellWithRowSpan(ContentTable, "Port Of Destination", 1, true, true);
addCellWithRowSpan(ContentTable, "Millage", 1, true, true);
addCellWithRowSpan(ContentTable, "Tata Birla", 1, false, false);
addCellWithRowSpan(ContentTable, "Dipanki Jadav", 1, false, false);
addCellWithRowSpan(ContentTable, "POL", 1, false, false);
addCellWithRowSpan(ContentTable, "POD@", 1, false, false);
addCellWithRowSpan(ContentTable, "Millagekhefjkhjhf", 1, false, false);
addCellWithRowSpan(ContentTable, "2", 3, false, false);
addCellWithRowSpan(ContentTable, "SHC!10002", 1, false, false);
addCellWithRowSpan(ContentTable, "1-1-1", 1, false, false);
addCellWithRowSpan(ContentTable, "VIEEEIEWWNdfssssssssssssssssssssssssssssssssssssssssss", 1, false, false);
addCellWithColSpan(ContentTable, "Vehicle Name", 2, false, false, false);
addCellWithRowSpan(ContentTable, "254567576", 3, false, false);
addCellWithRowSpan(ContentTable, "Consignee Name", 1, true, true);
addCellWithRowSpan(ContentTable, "Notify Name", 1, true, true);
addCellWithRowSpan(ContentTable, "Port Of Loading", 1, true, true);
addCellWithRowSpan(ContentTable, "Port Of Destination", 1, true, true);
addCellWithRowSpan(ContentTable, "Millage", 1, true, true);
addCellWithRowSpan(ContentTable, "Tata Birla", 1, false, false);
addCellWithRowSpan(ContentTable, "Dipanki Jadav", 1, false, false);
addCellWithRowSpan(ContentTable, "POL", 1, false, false);
addCellWithRowSpan(ContentTable, "POD@", 1, false, false);
addCellWithRowSpan(ContentTable, "Millagekhefjkhjhf", 1, false, false);
addCellWithColSpan(ContentTable, "20i083408", 6, true, true, true);
addCellWithRowSpan(ContentTable, "-----", 1, true, true);
doc.Add(ContentTable);
doc.Add(p1);
Font fontH2 = new Font(bfTimes, 9, Font.BOLD);
Paragraph p2 = new Paragraph("Company Name \n -----------\nPhone No: ----------------", fontH2);
doc.Add(p2);
doc.Close();
}