2011-07-15 8 views
1

これをページのレポートごとに3列(左に行く)に変換する助けを得ることができたらどうかと思っていました。itextsharpページごとに1列から3列に変換する

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

EDIT

申し訳ありませんが、私はあなたが探していたものを、あなたのオリジナルのポストからの理解が、今やりませんでした。残念ながら、あなたはMathとModの領域に入っています。私はこれを完全に通過する時間を(または今朝の脳力)必要はありませんが、うまくいけば私はあなたにスタートを与えることができます。

全体のプログラミング世界は左から右に基づいており、から上から下に向かって切り替えると、巨大なフープを飛び越えて自分の望むことをする傾向があります(HTML列1のAのアルファベット順、列2のBのようにアルファベット順に並べ替え)

テーブルの高さを知る必要があるため、できるだけ垂直方向に数ページに乗る。残念ながら、テーブルの高さはレンダリング時間までわかりません。 (少なくとも私のための)解決策は、各テーブルを一時的なドキュメントに描画して、高さを知ることができるようにすることです。次に、テーブルを配列に格納してドキュメントを破棄します。今、私たちは歩くことができる高さのテーブルを用意しています。

以下のスニペットでこれをすべて行います。私はサンプルデータをより多様化するために、行数ルールを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(); 
       } 
      } 
     } 

あなたがテーブルのあなたの配列を持ったらあなたがそれらと使用してそれらを描くをループすることができます

iが現在のテーブルインデックスと curXcurYある
Tables[i].WriteSelectedRows(1, Tables[i].Rows.Count, curX, curY, writer.DirectContent); 

があなたの現在の座標です。

これが正しい方向に進むことを望みます。 WriteSelectedRowsは、あなたが望む場所に正確にテーブルを置く素晴らしい仕事をします。

最後に覚えておいていただきたいことは、ドキュメントの最下部から始めて0を底にして720を上にし、下にはなく上にして、座標はYです。

+0

これは、私が必要とするところにはかなり近いようですが、テキストを右から左に向かって右から左に移動させる方法がありますか? – zSynopsis

+0

@zSysop、私は上記の私のポストを更新しました。完全な答えではありませんが、うまくいけば、正しい方向に進むことができます。 –

関連する問題