是否可以将渐变背景设置为pdfcell或段落?或者我必须使用图像?Itextsharp渐变背景
2
A
回答
6
是的,iText和iTextSharp支持渐变颜色。 PdfShading
对象有几个静态方法,可以为您创建不同类型的对象。你可能最感兴趣的两个是SimpleAxial
和SimpleRadial
。还有三个名字叫Type1
,Type2
和Type3
,我还没有探索过。
一旦你有一个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);
}
}
}
}
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();
相关问题
- 1. 背景渐变
- 2. 渐变背景
- 3. 渐变背景UIScrollView
- 4. JPanel渐变背景
- 5. NSOutlineView渐变背景
- 6. 与背景渐变
- 7. Imagemagick背景渐变
- 8. CSS3背景渐变
- 9. 渐变背景iOS
- 10. Xamarin - 渐变背景
- 11. css渐变背景
- 12. CSS3渐变背景
- 13. 渐变背景上的文字渐变
- 14. Android渐变背景渐变为透明
- 15. 背景渐变+单背景图片
- 16. CSS背景过渡 - 渐变
- 17. 背景页脚渐变
- 18. 颜色渐变的背景
- 19. PDF的渐变背景
- 20. 渐变背景重复
- 21. jVectormap渐变背景填充
- 22. css背景图像渐变
- 23. CSS过渡背景渐变
- 24. Android LinearLayout渐变背景
- 25. CSS渐变到纯背景
- 26. 渐变颜色背景?
- 27. tabBar中的渐变背景
- 28. R:ggplot背景渐变着色
- 29. gnuplot trasparent渐变背景
- 30. 背景需要有渐变
很好的答案。 Frick itext文档应该像这样读取。 – Paddy
@ChirsHaas你有什么机会帮我填充一个从中心开始的径向渐变的圆?请相当好 – Ben