C# tutorial: painting patterns in PDF document


Painting patterns in PDF document

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.

pdf pattern colors

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
doc.Open();
PdfContentByte canvas = writer.DirectContent;
//line pattern
PdfPatternPainter line = canvas.CreatePattern(5, 5);
line.SetLineWidth(1);
line.SetColorStroke(BaseColor.YELLOW);
line.MoveTo(3, 0);
line.LineTo(3, 10);
line.Stroke();
canvas.SetPatternFill(line);
canvas.Ellipse(100, 600, 200, 800);
canvas.Fill();
//square pattern
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);
square.FillStroke();
canvas.SetPatternFill(square);
canvas.Rectangle(100, 450, 100, 100);
canvas.SetColorStroke(BaseColor.RED);
canvas.FillStroke();
//image pattern
PdfPatternPainter imgpat = canvas.CreatePattern(15, 15,20,20);
Image img = Image.GetInstance("d:/baby1.jpg");
img.ScaleAbsolute(20, 10);
img.SetAbsolutePosition(0, 0);
canvas.Ellipse(200, 600, 450, 800);
imgpat.AddImage(img);
canvas.SetPatternFill(imgpat);
canvas.Fill();
//close the document
doc.Close();
//view the result pdf file
System.Diagnostics.Process.Start(path + "/pdfdoc.pdf");

Transparency

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.

pdf transparency

PdfContentByte content = writer.DirectContentUnder;
content.SetColorFill(new BaseColor(200,50,50));
content.Ellipse(36,500,100,600);
content.Fill();
content.SaveState();
PdfGState state = new PdfGState();
state.FillOpacity=0.5f;
content.SetGState(state);
content.SetColorFill(BaseColor.YELLOW);
content.Ellipse(72,550,136,650);
content.Fill();
content.SetColorFill(BaseColor.PINK);
content.Ellipse(72, 530, 136, 630);
content.Fill();
content.RestoreState();


Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.