【问题标题】:Creating PDF with overflow layout via iTextSharp通过 iTextSharp 创建带有溢出布局的 PDF
【发布时间】:2011-04-14 18:43:22
【问题描述】:

我通过 iTextSharp 从 DataTable 成功创建了一个 PDF 文档,但我无法获得所需格式的布局。

虽然不太可能,但 DataTable 具有数十列的潜力。想象一下下面的 DataTable - 每个数字代表一个页面可以容纳的区域:

|------------|
| 1 : 2 : 3 |
|------------|
| 4 : 5 : 6 |
|------------|

这应该按照我对这些部分进行编号的顺序导出为 6 页的 PDF 文档。相反,它目前生成为一个两页文档,每页上有 40 多列,字体非常小,几乎没有细节。

解释我想要什么的最简单方法是:当生成的 PDF 打印时,我希望它像一个非常宽的 Excel 表一样打印,而不是将所有内容压缩在一起。

我的代码如下:

    public static void ExportAsPDF(DataTable Table, IList<string> Columns, string filename)
    {
        int ColumnCount = Table.Columns.Count;
        int RowCount = Table.Rows.Count;

        iTextSharp.text.Table BodyTable = new iTextSharp.text.Table(ColumnCount, RowCount);
        BodyTable.AutoFillEmptyCells = true;

        foreach (string s in Columns)
        {
            BodyTable.AddCell(s);
        }
        foreach (object o in from DataRow row in Table.Rows from o in row.ItemArray select o)
        {
            BodyTable.AddCell(o.ToString());
        }

        Document doc = new Document();
        PdfWriter.GetInstance(doc, HttpContext.Current.Response.OutputStream);
        doc.Open();
        doc.Add(BodyTable);
        doc.Close();

        HttpContext.Current.Response.ContentType = "application/pdf";
        HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}.pdf", filename));
        HttpContext.Current.Response.End(); 
    }

非常感谢所有输入和帮助。谢谢

【问题讨论】:

    标签: c# asp.net itextsharp


    【解决方案1】:

    好的,两个选项。

    1. 使用 PdfContentByte 和 ColumnText 手动绘制所有内容作为文本布局部分。

    2. 让正常的 iText 布局代码相信您的页面足够宽以容纳一整行,然后稍后将这些页面拆分成几块。不漂亮,但可能比替代方案更容易。

    首先,您需要定义一个比正常页面宽 3 倍的页面。

    Rectangle triplePageRect = new Rectangle(PageSize.LETTER);
    float origWidth = triplePageRect.getWidth();
    triplePageRect.setWidth(origWidth * 3f);
    
    Document doc = new Document(triplePageRect);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfWriter writer = PdfWriter.getInstance(doc, baos);
    

    然后您几乎按照现在的方式绘制表格......但是您必须确保列边缘与您的两个页面边缘对齐,以便您以后可以轻松地分解页面。如果您可以创建一个以分页符所在位置为中心的空行,则可以获得奖励积分。

    //Your Code Here
    

    最后,您需要保存 PDF,再次打开它,然后将其切成丝带。我在想 PdfStamper。

    // write everything out to the baos.
    doc.close(); 
    
    // and suck it right back up again.  Hurray for efficiency.  Or something.
    PdfReader reader = new PdfReader(baos.toByteArrayOrWhateverItsCalled());
    PdfStamper stamper = new PdfStamper( reader, new FileOutputStream(outputPath));
    
    // duplicate the pages.  I'll assume only one page, but you'll get the idea.
    PdfDictionary origPage = reader.getPageN(1);
    for (int i = 0; i < 2; ++i) {
      // initial size is irrelevant, we're going to change it, but !null
      stamper.insertPage(2+i, PageSize.LETTER); 
      PdfDictionary newPageDict = reader.getPage(2 + i);
    
      // copy the original page... note that this is a shallow copy
      newPageDict.putAll(origPageDict);
    
      // duplicate the page rect so each page will have its own copy
      PdfArray pageRect = newPageDict.getAsArray(PdfName.MEDIABOX);
      // also a shallow copy, but changes to this array will be localized to the page.
      PdfArray newRect = new PdfArray(pageRect);
      // page rects are defined as [llx lly urx ury], so we need to change 0 and 2.
      newRect.set(0, new PdfNumber(origWidth * (i+1));
      newRect.set(2, new PdfNumber(origWidth * (i+2));
    }
    
    //Smoke'em if you've got 'em folks, we're done.
    stamper.close();
    

    该死的,我很好。哦哦哦!而且谦虚!别忘了长得好看、勇敢、有礼貌、机智……

    【讨论】:

      猜你喜欢
      • 2016-05-28
      • 1970-01-01
      • 2013-01-20
      • 2011-06-23
      • 2012-03-06
      • 1970-01-01
      • 1970-01-01
      • 2011-06-13
      • 2011-09-02
      相关资源
      最近更新 更多