创建PDF溢出布局通过iTextSharp

本文关键字:iTextSharp 布局 PDF 溢出 创建 | 更新日期: 2023-09-27 17:50:17

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

虽然不太可能,但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(); 
    }

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

创建PDF溢出布局通过iTextSharp

好的,有两个选项

  1. 用PdfContentByte和ColumnText手工绘制文本布局部分

  2. 说服正常的ittext布局代码,你的页面是足够宽的容纳整个行,然后拆分这些页面成块以后。不漂亮,但可能比另一种选择更容易。

首先你需要定义一个比正常宽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();

该死,我很好。噢哦!和温和的!让我们不要忘记漂亮、勇敢、礼貌、机智……