2011-07-15 38 views
1
頁面

1列轉換爲3列我在想,如果我能得到一些幫助這個轉換成3列每頁報告(下降到左)。iTextSharp的每

using System; 
using System.IO; 
using iTextSharp.text; 
using iTextSharp.text.pdf; 

namespace ConsoleApp 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 

      Console.WriteLine("-> Creates a PDF file with a block of Text."); 
      Document document = new Document(PageSize.LETTER); 
      try 
      { 
       PdfWriter writer = PdfWriter.GetInstance(
         document, 
         new FileStream(@"c:\\temp\\column_example.pdf", FileMode.Create)); 
       document.Open(); 
       PdfContentByte cb = writer.DirectContent; 

       float pos; 
       PdfPTable table; 
       PdfPCell cell = new PdfPCell(new Phrase(string.Empty)); 
       Phrase phrase; 
       float columnWidth = (PageSize.LETTER.Width - 36); 
       ColumnText ct = GetColumn(cb, columnWidth); 

       int status = 0; 


       string line = "Line{0}"; 

       for(int i=0; i<50; i++) 
       { 
        table = new PdfPTable(1); 
        table.SpacingAfter = 9F; 


        cell = new PdfPCell(new Phrase("Header for table " + i)); 
        table.AddCell(cell); 
        for (int j = 0; j < (i%2 == 0 ? 5 : 7); j++) 
        { 
         phrase = new Phrase(string.Format(line, i)); 
         cell = new PdfPCell(phrase); 
         table.AddCell(cell); 
        } 

        ct.AddElement(table); 
        pos = ct.YLine; 
        status = ct.Go(true); 
        Console.WriteLine("Lines written:" + ct.LinesWritten + " Y-position: " + pos + " - " + ct.YLine); 
        if (!ColumnText.HasMoreText(status)) 
        { 
         ct.AddElement(table); 
         ct.YLine = pos; 
         ct.Go(false); 
        } 
        else 
        { 
         document.NewPage(); 
         ct.SetText(null); 
         ct.AddElement(table); 
         ct.YLine = PageSize.LETTER.Height - 36; 
         ct.Go(); 
        } 
       } 
      } 

      catch (DocumentException de) 
      { 
       Console.Error.WriteLine(de.Message); 
      } 

      catch (IOException ioe) 
      { 
       Console.Error.WriteLine(ioe.Message); 
      } 
      finally 
      { 
       document.Close(); 
      } 
      Console.ReadLine(); 
     } 


     private static ColumnText GetColumn(PdfContentByte cb, float columnWidth) 
     { 
      var ct = new ColumnText(cb); 
      ct.SetSimpleColumn(36, 36, columnWidth, PageSize.LETTER.Height - 36, 18, Element.ALIGN_JUSTIFIED); 
      return ct; 
     } 
    } 
} 

我真的是新的itextsharp,找不到任何有關如何做到這一點的好例子。

感謝所有幫助

回答

2

做到這一點的最簡單的方法是把你的個人的表到主3列的表格。下面是這樣做的代碼。您可能需要調整邊距,寬度和邊框,但至少應該讓您開始。

而且,既然你說你是新來iTextSharp的我會假設你沒有使用DirectContent特定需要。 DC功能非常強大,但大多數您需要使用iTextSharp來完成,您可以通過特定的對象來完成。下面的代碼已刪除所有的DC東西。

//(iText 5.1.1.0) 

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using iTextSharp.text; 
using iTextSharp.text.pdf; 
using System.IO; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "column_example.pdf"); 

      Console.WriteLine("-> Creates a PDF file with a block of Text."); 
      Document document = new Document(PageSize.LETTER); 
      try 
      { 
       PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filePath, FileMode.Create)); 
       document.Open(); 

       //Create a master table with 3 columns 
       PdfPTable masterTable = new PdfPTable(3); 
       //Set the column widths, this should probably be adjusted 
       masterTable.SetWidths(new float[] { 200, 200, 200 }); 

       PdfPTable table; 
       PdfPCell cell; 
       Phrase phrase; 

       string line = "Line{0}"; 

       for (int i = 0; i < 50; i++) 
       { 
        table = new PdfPTable(1); 
        table.SpacingAfter = 9F; 

        cell = new PdfPCell(new Phrase("Header for table " + i)); 
        table.AddCell(cell); 
        for (int j = 0; j < (i % 2 == 0 ? 5 : 7); j++) 
        { 
         phrase = new Phrase(string.Format(line, i)); 
         cell = new PdfPCell(phrase); 
         table.AddCell(cell); 
        } 
        //Add the sub-table to our master table instead of the writer 
        masterTable.AddCell(table); 
       } 

       //Add the master table to our document 
       document.Add(masterTable); 
      } 

      catch (DocumentException de) 
      { 
       Console.Error.WriteLine(de.Message); 
      } 

      catch (IOException ioe) 
      { 
       Console.Error.WriteLine(ioe.Message); 
      } 
      finally 
      { 
       document.Close(); 
      } 
      Console.ReadLine(); 
     } 
    } 
} 

編輯

對不起,我沒有從原來的職位瞭解你要找的人,但現在要做的。不幸的是,你正在進入數學和國防部的領域。我沒有時間(或腦力今天上午)經歷這種完全,但希望我可以給你一個開始。

整個編程世界都是從左到右,然後是,然後是從上到下,當你切換它時,你往往需要跳過巨大的箍來做你想做的事情(比如做一個HTML名單成列1與A的按字母順序排列3列,B的第2欄等)

爲了你需要知道表的高度,你想要什麼,這樣就可以計算出有多少垂直,你可以進入頁面。不幸的是桌子高度直到渲染時間才知道。 (至少對我來說)的解決方案是每個表中提取到一個臨時文件,它可以讓我們知道的高度,那麼我們存儲表中的數組和扔掉的文件。現在我們已經得到了一系列已知高度的表格,我們可以通過這些表格。

下面的代碼片段做了這一切。爲了獲得更多的樣本數據,我將行計數規則更改爲從2到9的隨機數。此外,從iTextSharp 5.1開始(我認爲這是正確的版本),許多「大」對象支持IDisposable,所以我是using。如果您使用的是舊版本,你需要刪除using並切換到正常的變量聲明。希望評論有意義。你會看到我也將一些魔術數字轉換爲變量。

 //Our array of tables 
     List<PdfPTable> Tables = new List<PdfPTable>(); 

     //Create a random number of rows to get better sample data 
     int rowCount; 
     Random r = new Random(); 

     string line = "Line {0}"; 
     PdfPTable table; 

     //This is the horizontal padding between tables 
     float hSpace = 5; 
     //Total number of columns that we want 
     int columnCount = 3; 

     //Create a temporary document to write our table to so that their sizes can be calculated 
     using (Document tempDoc = new Document(PageSize.LETTER)) 
     { 
      using (MemoryStream tempMS = new MemoryStream()) 
      { 
       using (PdfWriter tempW = PdfWriter.GetInstance(tempDoc, tempMS)) 
       { 
        tempDoc.Open(); 

        //Calculate the table width which is the usable space minus the padding between tables divided by the column count 
        float documentUseableWidth = tempDoc.PageSize.Width - tempDoc.LeftMargin - tempDoc.RightMargin; 
        float totalTableHPadding = (hSpace * (columnCount - 1)); 
        float tableWidth = (documentUseableWidth - totalTableHPadding)/columnCount; 

        for (int i = 0; i < 50; i++) 
        { 
         table = new PdfPTable(1); 
         table.AddCell(new PdfPCell(new Phrase("Header for table " + i))); 
         rowCount = r.Next(2, 10); 
         for (int j = 0; j < rowCount; j++) 
         { 
          table.AddCell(new PdfPCell(new Phrase(string.Format(line, i)))); 
         } 
         //In order to use WriteSelectedRows you need to set the width of the table 
         table.SetTotalWidth(new float[] { tableWidth }); 
         //Write the table to our temporary document in order to calculate the height 
         table.WriteSelectedRows(1, table.Rows.Count, 0, 0, tempW.DirectContent); 
         //Add the table to our array 
         Tables.Add(table); 
        } 
        tempDoc.Close(); 
       } 
      } 
     } 

一旦你得到了你,你可以循環表的陣列通過這些使用吸引他們:

Tables[i].WriteSelectedRows(1, Tables[i].Rows.Count, curX, curY, writer.DirectContent); 

哪裏i是當前表的索引和curXcurY是你的當前座標。

希望這會讓你朝着正確的方向前進。 WriteSelectedRows在把桌子準確放在你想要的位置方面做得非常好。

要記住的最後一件事情是,Y座標從文檔的底部開始,而不是頂部,所以0是底部,720是「高於」而不是低於底部。

+0

這看起來非常接近我需要的東西,但有沒有辦法讓文字向下,跨越右側,而不是跨越右側,然後下降? – zSynopsis

+0

@zSysop,我上面更新了我的帖子。不是一個完整的答案,但希望它會讓你朝着正確的方向前進。 –