通过iTextSharp创建溢出布局的PDF

| 我已经通过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(); 
    }
非常感谢所有的输入和帮助。谢谢     
已邀请:
好的,有两个选择。 使用PdfContentByte和ColumnText手工绘制所有内容,以用作文本布局部分。 让普通的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();
该死的我很好。哦,哦!又谦虚!让我们不要忘记好看,勇敢,有礼貌,机智...     

要回复问题请先登录注册