设置contentbyte itextsharp的背景颜色 [英] Setting the background color of a contentbyte itextsharp

查看:168
本文介绍了设置contentbyte itextsharp的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Itextsharp的MVC3 VB.NET应用程序。我有一段生成pdf文件的代码,一切看起来都不错,但是我希望将该pdf文件中的线条颜色替换为2种颜色,以便查看它的人很容易理解这些值。有没有办法根据字体大小将整行的背景颜色设置为设定颜色?我将使用的函数如下:

MVC3 VB.NET application using Itextsharp. I have a section of code that generates a pdf file everything looks great but I would like to alternate the line colors in that pdf file between 2 color so that the values are easy to follow for the person looking at it. Is there a way to set the background color of a whole line based on font size to a set color? A function I would be using this in is below:

    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来设置线条的背景颜色使用模数来确定颜色应该是灰色还是白色。但我发现在任何地方都没有关于如何设置整行背景颜色的代码示例..任何想法????

I thought about just setting the background color of the line by using the y_line1 and using a modulus to determine if the color should be grey or white. But I have found no code samples anywhere about how to set a whole line background color.. Any ideas????

推荐答案

PDF规范中没有与文本相关的背景颜色概念。任何看起来像背景颜色的东西,甚至是桌子,都只是在矩形(或其他形状)上绘制的文字。

There is no concept of "background color" in the PDF spec in relation to text. Anything that looks like a background color, even a table, is just text drawn on top of a rectangle (or some other shape).

要绘制一个矩形,你只需要调用 PdfContentByte 对象上的 Rectangle 方法。它需要左下角x,y和宽度和高度。颜色取决于先前对其中一种颜色填充的调用,例如 SetColorFill()

To draw a rectangle you just call the Rectangle method on your PdfContentByte object. It takes a lower left x,y and a width and a height. The color is determined by a previous call to one of the color fills such as SetColorFill().

工作时使用原始画布时,建议您还使用 SaveState() RestoreState()。由于填充命令在对象之间共享但意味着不同的东西,这些可以帮助避免混淆。 SaveState()设置一个标志,允许您在调用 RestoreState()时撤消所有图形状态更改。

When working with the raw canvas its recommended that you also use SaveState() and RestoreState(). Since the fill commands are shared between objects but mean different things these can help avoid confusion. SaveState() sets a flag allowing you to undo all graphics state changes when you call RestoreState().

下面的代码是一个完整的VB.Net 2010 WinForms应用程序,针对iTextSharp 5.1.2.0展示了上述内容。它在桌面上创建一个示例文件,其中一行文本重复7次。每条线在两种背景颜色之间来回切换。此外,它会在文本行周围绘制一个笔划来模拟边框。

The code below is a full working VB.Net 2010 WinForms app targeting iTextSharp 5.1.2.0 that shows off the above. It creates a sample file on the desktop with a line of text repeated 7 times. Each line toggles back and forth between two background colors. Additionally it draws a stroke around the line of text to simulate a border.

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

这篇关于设置contentbyte itextsharp的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆