iTextSharp的渐变背景 [英] Itextsharp gradient background

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

问题描述

有没有办法设置渐变背景pdfcell或段落?还是我使用的图像?

Is there way to set gradient background to pdfcell or paragraph? Or do I have to use image?

推荐答案

是的,iText的和iTextSharp的支持渐变颜色。该 PdfShading 对象有几个用于创建不同类型的 PdfShading 的对象为你的静态方法。这两个,你可能最感兴趣的是 SimpleAxial SimpleRadial 。还有其他三个名为类型1 2型的Type3 我还没有探索呢。

Yes, iText and iTextSharp support gradient colors. The PdfShading object has several static methods that create different types of PdfShading objects for you. The two that you are probably most interested in are SimpleAxial and SimpleRadial. There's three others named Type1, Type2 and Type3 that I haven't explored yet.

一旦你有一个 PdfShading 对象,你可以创建一个 PdfShadingPattern 直接从它,一旦你有,你可以从它创建一个 ShadingColor ShadingColor 最终从 BaseColor 派生,所以你应该能够使用它的地方多数民众赞成使用。你的情况,你想将其分配到一个 BackgroundColor中

Once you have a PdfShading object you can create a PdfShadingPattern directly from it and once you have that you can create a ShadingColor from it. ShadingColor is ultimately derived from BaseColor so you should be able to use it wherever that's used. In your case you want to assign it to a BackgroundColor.

下面是一个完整的工作WinForms应用程序针对iTextSharp的5.1.1.0这显示了创建的表有两列,每个都有自己的渐变背景颜色。

Below is a complete working WinForms app targeting iTextSharp 5.1.1.0 that shows created a table with two columns, each with their own gradient background colors.

注意:将(X,Y)的 PdfShading 静态方法,坐标为文档级别,而不是单元级别。这意味着,你可能无法再使用 PdfShading ojbects根据坡度的大小。这个样本后,下面我将向你展示如何利用细胞的活动来克服这一局限。

NOTE: The (x,y) coordinates of the PdfShading static methods are document-level and not cell-level. What this means is that you might not be able to re-use PdfShading ojbects depending on the gradient's size. After this sample below I'll show you how to overcome this limitation using cell events.

using System;
using System.Text;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Test file name
            string TestFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf");

            //Standard iTextSharp setup
            using (FileStream fs = new FileStream(TestFile, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                using (Document doc = new Document(PageSize.LETTER))
                {
                    using (PdfWriter w = PdfWriter.GetInstance(doc, fs))
                    {
                        //Open the document for writing
                        doc.Open();

                        //Create a shading object. The (x,y)'s appear to be document-level instead of cell-level so they need to be played with
                        PdfShading shading = PdfShading.SimpleAxial(w, 0, 700, 300, 700, BaseColor.BLUE, BaseColor.RED);

                        //Create a pattern from our shading object
                        PdfShadingPattern pattern = new PdfShadingPattern(shading);

                        //Create a color from our patter
                        ShadingColor color = new ShadingColor(pattern);

                        //Create a standard two column table
                        PdfPTable t = new PdfPTable(2);

                        //Add a text cell setting the background color through object initialization
                        t.AddCell(new PdfPCell(new Phrase("Hello")) { BackgroundColor = color });

                        //Add another cell with everything inline. Notice that the (x,y)'s have been updated
                        t.AddCell(new PdfPCell(new Phrase("World")) { BackgroundColor = new ShadingColor(new PdfShadingPattern(PdfShading.SimpleAxial(w, 400, 700, 600, 700, BaseColor.PINK, BaseColor.CYAN))) });



                        //Add the table to the document
                        doc.Add(t);

                        //Close the document
                        doc.Close();
                    }
                }
            }

            this.Close();
        }

    }
}

例2

正如上面提到的,上面的方法使用文档级职位,该职位往往是不够的。为了克服这一点,你需要使用电池级定位要做到这一点,你需要使用电池事件,因为小区位置不知道,直到表本身的呈现方式。要使用单元格的事件,你需要创建一个新的类,它实现 IPdfPCellEvent 并办理 CellLayout 方法。下面是更新code,做这一切的:

As noted above, the method above uses document-level position which often isn't good enough. To overcome this you need to use cell-level positioning and to do that you need to use cell events because cell positions aren't known until the table itself is rendered. To use a cell event you need to create a new class that implements IPdfPCellEvent and handle the CellLayout method. Below is updated code that does all of this:

using System;
using System.Text;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Test file name
            string TestFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf");

            //Standard iTextSharp setup
            using (FileStream fs = new FileStream(TestFile, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                using (Document doc = new Document(PageSize.LETTER))
                {
                    using (PdfWriter w = PdfWriter.GetInstance(doc, fs))
                    {
                        //Open the document for writing
                        doc.Open();

                        //Create a standard two column table
                        PdfPTable t = new PdfPTable(2);

                        //Create an instance of our custom cell event class, passing in our main writer which is needed by the PdfShading object
                        var CE = new GradientBackgroundEvent(w);

                        //Set the default cell's event to our handler
                        t.DefaultCell.CellEvent = CE;

                        //Add cells normally
                        t.AddCell("Hello");
                        t.AddCell("World");


                        //Add the table to the document
                        doc.Add(t);

                        //Close the document
                        doc.Close();
                    }
                }
            }

            this.Close();
        }

        public class GradientBackgroundEvent : IPdfPCellEvent
        {
            //Holds pointer to main PdfWriter object
            private PdfWriter w;

            //Constructor
            public GradientBackgroundEvent(PdfWriter w)
            {
                this.w = w;
            }

            public void CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases)
            {
                //Create a shading object with cell-specific coords
                PdfShading shading = PdfShading.SimpleAxial(w, position.Left, position.Bottom, position.Right, position.Top, BaseColor.BLUE, BaseColor.RED);

                //Create a pattern from our shading object
                PdfShadingPattern pattern = new PdfShadingPattern(shading);

                //Create a color from our patter
                ShadingColor color = new ShadingColor(pattern);

                //Get the background canvas. NOTE, If using an older version of iTextSharp (4.x) you might need to get the canvas in a different way
                PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS];

                //Set the background color of the given rectable to our shading pattern
                position.BackgroundColor = color;

                //Fill the rectangle
                cb.Rectangle(position);
            }
        }
    }
}

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

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