【发布时间】:2012-12-10 05:30:43
【问题描述】:
我正在使用 iTextSharp 生成 pdf。
我的代码是,
public FileStreamResult Export(int ID)
{
MemoryStream stream = new MemoryStream();
Document pdf = new Document();
PdfWriter writer = PdfWriter.GetInstance(pdf, stream);
pdf.Open();
//code for table
PdfPTable table = new PdfPTable(3);
PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns"));
cell.Colspan = 3;
cell.HorizontalAlignment = 1;
table.SpacingBefore = 100; //not working
table.SpacingAfter = 10; //not working
table.AddCell(cell);
table.AddCell("Col 1 Row 1");
table.AddCell("Col 2 Row 1");
table.AddCell("Col 3 Row 1");
table.AddCell("Col 1 Row 2");
table.AddCell("Col 2 Row 2");
table.AddCell("Col 3 Row 2");
pdf.Add(table);
pdf.Close();
//code to download
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename="+_child[0].Child_Name+".pdf");
Response.Buffer = true;
Response.Clear();
Response.OutputStream.Write(stream.GetBuffer(), 0, stream.GetBuffer().Length);
Response.OutputStream.Flush();
Response.End();
return new FileStreamResult(Response.OutputStream, "application/pdf");
}
表格显示在页面顶部。但我想把桌子移下来。我该怎么办?
请帮忙,
谢谢。
【问题讨论】:
-
您想将它向下移动一个给定的距离(例如 72 个用户空间单位),还是您希望表格位于某些其他给定的 PDF 元素下方(例如您在您问题的第一个版本)?
-
是的......我想在一些 pdf 元素下面添加表格......就像我的第一个问题版本中一样
-
您是否有特殊原因通过直接内容操作在第一个版本中添加其他内容,而不是将其作为
Chunks 或Paragraphs 添加到PDF?如果您执行后者,您的表格将自动位于这些段落下方。直接内容操作实际上是为了在自定义位置向页面添加内容,而不更改添加到Document的数据的自动布局。 -
当我尝试使用块时,换行不起作用。 thatz y 在我的第一个版本中尝试了代码。
标签: asp.net-mvc asp.net-mvc-4 itextsharp