2012-03-05 18 views
1

Itextsharpを使用しているMVC3 VB.NETアプリケーション。私はpdfファイルを生成するコードセクションを持っていますが、すべてが素晴らしいようですが、私は2色の間のpdfファイルのラインカラーを交互にして、値を見ている人には簡単に従うことができます。フォントサイズに基づいて、行全体の背景色を設定された色に設定する方法はありますか?私はこの中に使用される関数は、以下である:Contentbyteの背景色を設定するitextsharp

For Each _reg_ In _reg 
       Dim _registrant As reg_info = _reg_ 
       If y_line1 <= 30 Then 
        doc.NewPage() 
        _Page = _Page + 1 
        y_line1 = 670 
       End If 

       If y_line1 = 670 Then 
        cb.BeginText() 
        cb.SetFontAndSize(BF_Times, 6) 
        cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, _datePrinted + " " + _timePrinted, 500, 770, 0) 
        cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, "Page Number" + " " + _Page, 600, 770, 0) 
        cb.SetFontAndSize(BF_Times, 8) 
        cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, _reportHead + " Overrides ", 304, 720, 0) 
        cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "First Name", 20, 700, 0) 
        cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Last Name", 80, 700, 0) 
        cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Last Four", 160, 700, 0) 
        cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Email Address", 300, 700, 0) 

        cb.EndText() 
       End If 

       cb.BeginText() 
       cb.SetFontAndSize(BF_Times, 8) 
       cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, _registrant.first_name, 20, y_line1, 0) 
       cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, _registrant.last_name, 80, y_line1, 0) 
       cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, _registrant.last_four_social, 160, y_line1, 0) 
       cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, _registrant.email, 300, y_line1, 0) 
       _total += 1 
       cb.EndText() 
       y_line1 = y_line1 - 15 
      Next 

私はちょうどy_line1を使用して、色が灰色又は白色であるべきであるかどうかを決定するために係数を使用して行の背景色を設定することについて考えました。しかし、私はどのように行全体の背景色を設定する方法についてはどこにコードサンプルが見つかりません..任意のアイデア????

答えて

4

テキストに関するPDF仕様では「背景色」という概念はありません。背景色のように見えるもの(テーブルさえあれば)は、長方形(または他の形状)の上に描かれたテキストに過ぎません。

長方形を描画するには、PdfContentByteオブジェクトでRectangleメソッドを呼び出します。左下のx、y、幅と高さが必要です。色は、SetColorFill()などのカラー塗りつぶしの1つ前の呼び出しによって決定されます。

生キャンバスで作業する場合は、SaveState()RestoreState()も使用することをお勧めします。塗りつぶしコマンドはオブジェクト間で共有されていますが、異なる意味を持つため、混乱を避けることができます。 SaveState()は、RestoreState()を呼び出すと、すべてのグラフィックステートの変更を元に戻すことができるフラグを設定します。

以下のコードは、上に示したiTextSharp 5.1.2.0をターゲットとしたVB.Net 2010 WinFormsアプリケーションのフルバージョンです。デスクトップ上にテキストの行を7回繰り返してサンプルファイルを作成します。各行は2つの背景色の間を行き来します。さらに、境界線をシミュレートするためにテキスト行の周りにストロークを描画します。

Option Strict On 
Option Explicit On 

Imports System.IO 
Imports iTextSharp.text 
Imports iTextSharp.text.pdf 

Public Class Form1 

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 
     ''//Test file that we'll create 
     Dim TestFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "TestFile.pdf") 
     ''//Test String that we'll repeat 
     Dim TestString = "It was the best of times..." 
     ''//Create an array of our test string 
     Dim TestArray = {TestString, TestString, TestString, TestString, TestString, TestString, TestString} 

     ''//Create our generic font 
     Dim BF_Times = BaseFont.CreateFont(BaseFont.TIMES_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED) 

     ''//Standard PDF setup, change as needed for your stream type 
     Using FS As New FileStream(TestFile, FileMode.Create, FileAccess.Write, FileShare.None) 
      Using Doc As New Document(PageSize.LETTER) 
       Using writer = PdfWriter.GetInstance(Doc, FS) 
        Doc.Open() 

        ''//Grab the raw content object 
        Dim cb = writer.DirectContent 
        ''//Set our starter Y coordinate 
        Dim y = 670 
        ''//Loop through our string collection 
        For I = 0 To (TestArray.Count - 1) 
         ''//Store the current graphics state so that we can unwind it later 
         cb.SaveState() 
         ''//Set the fill color based on eve/odd 
         cb.SetColorFill(If(I Mod 2 = 0, BaseColor.GREEN, BaseColor.BLUE)) 
         ''//Optional, set a border 
         cb.SetColorStroke(BaseColor.BLACK) 
         ''//Draw a rectangle. NOTE: I'm subtracting 5 from the y to account for padding 
         cb.Rectangle(0, y - 5, Doc.PageSize.Width, 15) 
         ''//Draw the rectangle with a border. NOTE: Use cb.Fill() to draw without the border 
         cb.FillStroke() 
         ''//Unwind the graphics state 
         cb.RestoreState() 

         ''//Flag to begin text 
         cb.BeginText() 
         ''//Set the font 
         cb.SetFontAndSize(BF_Times, 6) 
         ''//Write some text 
         cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, TestArray(I), 0, y, 0) 
         ''//Done writing text 
         cb.EndText() 

         ''//Decrease the y accordingly 
         y -= 15 
        Next 


        Doc.Close() 
       End Using 
      End Using 
     End Using 

     Me.Close() 
    End Sub 
End Class 
関連する問題