iTextSharp allows you to paint different patterns in a PDF document. A pattern consists of smoothly varying color gradient or repeating graphical figures. To create a pattern you need to create a pattern cell. The cell can be created from the PdfPatternPainter class with the CreatePattern mehtod. This cell will be repeated at fixed horizontal and vertical intervals when it is filled in a path. As you see in the image below, there are three patterns. One pattern is created with lines. Another one is created with squares. And the last one is created with images.
By observing the example code below, two versions of the CreatePattern method are used. The first version accepts two parameters: width and height of the pattern cell. The second version of the CreatePattern method allows to input two more values: X and Y step that are the desired horizontal and vertical spacing between pattern cells. The SetPatternFill method of the PdfContentByte object is used to specify the pattern cell that will be painted at a specified location and size on the PDF document. The location and size is determined by a shape drawing method of the PdfContentByte class (e.g. Rectangle, Ellipse...). For every time you want to show the pattern cell on the PDF document, you need to call the Fill method of the PdfContentByte object.
When you create a pattern with images, you need to specify the position of the image once it is added on the pattern cell. This can be done by using the SetAbsolutePosition method. The ScaleAbsolute method can be used to specify th new width and height of the image.
//create a document object
var doc = new Document();
//get the current directory
string path = Environment.CurrentDirectory;
//get PdfWriter object
PdfWriter writer=PdfWriter.GetInstance(doc, new FileStream(path + "/pdfdoc.pdf", FileMode.Create));
//open the document for writing
PdfContentByte canvas = writer.DirectContent;
PdfPatternPainter line = canvas.CreatePattern(5, 5);
canvas.Ellipse(100, 600, 200, 800);
PdfPatternPainter square = canvas.CreatePattern(15,15);
square.SetColorFill(new BaseColor(0x00, 0xFF, 0xFF));
square.SetColorStroke(new BaseColor(0xFF, 0x00, 0x00));
square.Rectangle(10, 10,10, 10);
canvas.Rectangle(100, 450, 100, 100);
PdfPatternPainter imgpat = canvas.CreatePattern(15, 15,20,20);
Image img = Image.GetInstance("d:/baby1.jpg");
canvas.Ellipse(200, 600, 450, 800);
//close the document
//view the result pdf file
System.Diagnostics.Process.Start(path + "/pdfdoc.pdf");
You can apply transparency to your shapes by using the PdfGState class with the FillOpacity property. This property accepts a opacity floating-point value. You will use the SetGState method of the PdfContentByte object to apply the transparency to the shapes.
PdfContentByte content = writer.DirectContentUnder;
PdfGState state = new PdfGState();
content.Ellipse(72, 530, 136, 630);
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages: