【问题标题】:Move table down : itextsharp向下移动表格:itextsharp
【发布时间】: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 在我的第一个版本中尝试了代码。
  • 你试过使用Paragraph吗?您看过 iText in Action,第 2 版吗? Chapter 2 作为示例章节提供并解释了基础知识。这本书是为 Java 版本编写的,但它可以很容易地适应 C#。 C# 示例可用here

标签: asp.net-mvc asp.net-mvc-4 itextsharp


【解决方案1】:

您只需在固定值的末尾添加一个“f”。

示例:

table.SpacingBefore = 100f;   //is working 
table.SpacingAfter = 10f;     //is working

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2022-01-20
    • 1970-01-01
    相关资源
    最近更新 更多