2011-04-14 13 views
1

我成功地通過iTextSharp從DataTable創建了PDF文檔,但是我無法以理想的格式獲得佈局。通過iTextSharp創建帶溢出佈局的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(); 
    } 

所有輸入和幫助深表感謝。謝謝

回答

3

好的,有兩個選項。

  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(); 

該死的我很好。哦,哦!謙虛!讓我們不要忘記好看,勇敢,有禮貌,機智......