2012-03-05 5 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()과 같은 색상 채우기 중 하나에 대한 이전 호출에 의해 결정됩니다.

원시 캔버스를 사용할 때는 SaveState()RestoreState()도 사용하는 것이 좋습니다. 채우기 명령은 객체간에 공유되기 때문에 다른 것을 의미하므로 혼동을 피할 수 있습니다. SaveState()RestoreState()에 전화 할 때 모든 그래픽 상태 변경을 실행 취소 할 수있는 플래그를 설정합니다.

아래 코드는 위의 내용을 보여주는 iTextSharp 5.1.2.0을 대상으로하는 완전한 VB.Net 2010 WinForms 응용 프로그램입니다. 바탕 화면에 텍스트 줄을 7 번 반복하여 샘플 파일을 만듭니다. 각 선은 두 가지 배경색 사이를왔다 갔다합니다. 또한 테두리를 시뮬레이트하기 위해 텍스트 줄 주위에 선을 그립니다.

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