簡體   English   中英

Itextsharp漸變背景

[英]Itextsharp gradient background

有沒有辦法將漸變背景設置為pdfcell或段落? 或者我必須使用圖像?

是的,iText和iTextSharp支持漸變色。 PdfShading對象有幾個靜態方法,可以為您創建不同類型的PdfShading對象。 您可能最感興趣的兩個是SimpleAxialSimpleRadial 還有其他三個名為Type1Type2Type3我尚未探索過。

一旦你有了PdfShading對象,就可以直接從它創建一個PdfShadingPattern ,一旦你有了它,就可以從中創建一個ShadingColor ShadingColor最終來自BaseColor因此您應該能夠在任何使用過的地方使用它。 在您的情況下,您想將其分配給BackgroundColor

下面是一個完整的WinForms應用程序,目標是iTextSharp 5.1.1.0,該應用程序顯示創建了一個包含兩列的表,每列都有自己的漸變背景顏色。

注意PdfShading靜態方法的(x,y)坐標是文檔級而不是單元級。 這意味着您可能無法重復使用PdfShading ojbects,具體取決於漸變的大小。 在下面的示例之后,我將向您展示如何使用單元格事件克服此限制。

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方法的新類。 以下是執行所有這些操作的更新代碼:

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

如果其他人仍然感興趣我想找到如何用漸變顏色整個背景,你可以這樣做....

    PdfShading shading = PdfShading.simpleAxial(writer, 0, pageH, pageW, 0,
            BaseColor.WHITE, BaseColor.LIGHT_GRAY);
    PdfShadingPattern pattern = new PdfShadingPattern(shading);
    cb.setShadingFill(pattern);
    // cb.circle(500, 500, 500);
    cb.rectangle(0, 0, pageW, pageH);
    cb.fill();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM