2011-10-07 234 views

回答

6

是的,iText和iTextSharp支持漸變顏色。 PdfShading對象有幾個靜態方法,可以爲您創建不同類型的對象。你可能最感興趣的兩個是SimpleAxialSimpleRadial。還有三個名字叫Type1,Type2Type3,我還沒有探索過。

一旦你有一個PdfShading對象,你可以直接創建一個PdfShadingPattern,一旦你有,你可以從它創建一個ShadingColorShadingColor最終來自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); 
      } 
     } 
    } 
} 
+0

很好的答案。 Frick itext文檔應該像這樣讀取。 – Paddy

+0

@ChirsHaas你有什麼機會幫我填充一個從中心開始的徑向漸變的圓?請相當好 – Ben

3

如果其他人仍有意 我一直在尋找,找出如何着色整個背景漸變 你可以做這樣的....

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