Operation in Asp. Net PDF-iTextSharp-plot Vector

Source: Internet
Author: User

In the content described in the previous article, until now, all content added to the PDF document is dependent on adding the content on the page to the simple iText layout in the typographical stream. the simple iText layout is also responsible for generating a new page if the text content overflows the current page. Another method is required for processing vector images. That is, the metadata contentbyte () object is used. The instance of this object can be obtained from the DirectContent attribute of the metadata writer object. this also means that, unlike the previous method, only the consumer writer is used. the GetInstance method is used. We also need to obtain the example of the writable write class:

 

String pdfpath = Server. MapPath ("PDFs ");

 

Document doc = new Document ();

 

Try

 

{

 

Using writer = Using writer. GetInstance (doc, new FileStream (pdfpath + "/Graphics.pdf", FileMode. Create ));

 

Doc. Open ();

 

Required contentbyte cb = writer. DirectContent;

...

 

 

Now we can use the contentbyte object to draw a vector image:

 

 

 

Cb. MoveTo (doc. PageSize. Width/2, doc. PageSize. Height/2 );

 

Cb. LineTo (doc. PageSize. Width/2, doc. PageSize. Height );

 

Cb. Stroke ();

 

Cb. MoveTo (0, doc. PageSize. Height/2 );

 

Cb. LineTo (doc. PageSize. Width, doc. PageSize. Height/2 );

 

Cb. Stroke ();

 

The first line of code moves the cursor to the input X and Y axis parameters, that is, the center of the document. The next line of code draws a line from the current cursor position to the input coordinate parameter point. Draw a line from the middle of the document to the top of the document. Of course, this method has not yet begun to "Draw" this line. This method only records our intention to draw this line until the Stroke () method is called, this line can be written into PDF documents. The second paragraph of Code draws from the leftmost in the middle of the document to the rightmost in the middle, forming a document outline of the above two points:

 

 

Using the same method, we can add a square to the lattice on the left of the document:

 

Cb. MoveTo (100f, 700f );

 

Cb. LineTo (200f, 700f );

 

Cb. LineTo (200f, 600f );

 

Cb. LineTo (100f, 600f );

 

Cb. ClosePath ();

 

Cb. Stroke ();

 

 

 

 

We did not directly specify the last side of the square to be drawn. The ClosePath () method can be used to directly draw back to the origin coordinate from the current point. in fact, we can also use the convenient method provided by iTextSharp to draw a square. The following code shows how to use this shortcut to draw a square in the upper right corner:

 

 

 

Cb. Rectangle (doc. PageSize. Width-200f, 600f, 100f, 100f );

 

Cb. Stroke ();

 

 

Let's continue adding four squares to the page. Each square uses a different method than Stroke () to write it into the document. If you have used Photoshop Or firework, you should know, in fact, Stroke () is actually the outline of the object, and Fill is actually the Fill color of the object. Here I use the CMYK color to set the border color to blue-green, and the Fill color to Yellow:

 

 

Cb. SetColorStroke (new CMYKColor (1f, 0f, 0f, 0f ));

 

Cb. SetColorFill (new CMYKColor (0f, 0f, 1f, 0f ));

 

 

 

Cb. MoveTo (70,200 );

 

Cb. LineTo (170,200 );

 

Cb. LineTo (170,300 );

 

Cb. LineTo (70,300 );

 

// Path closed and stroked

 

Cb. ClosePathStroke ();

 

 

 

Cb. MoveTo (190,200 );

 

Cb. LineTo (290,200 );

 

Cb. LineTo (290,300 );

 

Cb. LineTo (190,300 );

 

// Filled, but not stroked or closed

 

Cb. Fill ();

 

 

 

Cb. MoveTo (310,200 );

 

Cb. LineTo (410,200 );

 

Cb. LineTo (410,300 );

 

Cb. LineTo (310,300 );

 

// Filled, stroked, but path not closed

 

Cb. FillStroke ();

 

 

 

Cb. MoveTo (430,200 );

 

Cb. LineTo (530,200 );

 

Cb. LineTo (530,300 );

 

Cb. LineTo (430,300 );

 

// Path closed, stroked and filled

 

Cb. ClosePathFillStroke (); www.2cto.com

 

 

 

 

When you use a Rectangle object instead of manually drawing a Rectangle, the first and second parameters are the coordinates in the lower right corner of the Rectangle, and the other two parameters are the width and height of the Rectangle, respectively. iTextSharp also includes other vector graphs, such as circles. The coordinates passed in the circle are the coordinates of the center, and the third parameter is the radius. The preceding four squares show that the side length of the square is 100 pixels, and the center coordinates of the Square are 120 x and 250 y. The following Code adds a circle to the square:

 

 

Cb. setCMYKColorStroke (255,255, 0, 0); cb. setCMYKColorFill (0,255,255, 0); cb. setLineWidth (2f); cb. circle (120f, 250f, 50f); cb. fill (); I guess you are wondering what the four parameters of CMYK used in the Code represent. This is not intuitive. CMYK represents the percentage of the four colors. For example, the warm red color is actually C: 0%, M100 %, Y100 %, K100 %. the CMYK constructor requires four parameters of the float type. You can input a number representing the percentage. I can also input 0 and 1. In fact, the input parameters can be float of any size, and the percentage is actually the ratio of several parameters. So if I want to set it to 100% blue (blue-green), I can use parameters such as 3000f, 0. Note that you should not accidentally input 0.

 

The SetCMYKColorFill () method accepts an int type parameter. I think it may be because this method is used to set CMYK based on percentages. Therefore, for warm red settings, I passed 0,100,100, 0, but I got a pink color. I think this is a bug. It was because the SetCMYKColorFill () method was used to affect the color of this time. So I reset the CMYK color and try again. Finally, I found that the CMYKColor object is derived from the ExtendedColor class, and the maximum int value acceptable is 255, which is similar to RGB. If you do not know how this class works, then you will be confused. So I set according to this and finally got the desired color. for verification, I also introduced a number greater than 255, and finally found that any number greater than 255 will be truncated by iTextSharp. For example, if 256 is equal to 1,510, it will be equal to 255. at least, this is the conclusion in my test.

Let's go back to the question. a circle with a blue border (2px) is placed in the first square below:

 

Next, let's take a look at other predefined shapes, ovans, provided by iTextSharp. First, define a rectangle, and then add an ellipse to the rectangle:

 

// x, y of bottom left corner, width, height cb.Rectangle(100f, 200f, 300f, 100f); cb.Stroke(); //Bottom left and top right coordinates cb.Ellipse(100f, 200f, 400f, 300f); cb.Stroke();

 

 

 

 

If the first and third parameters in the preceding two images are different, the second and fourth parameters are the same, and the final result is a circle.

The following code shows how to use another predefined image: the rounded corner rectangle. The constructor of the rounded corner rectangle is the coordinate, width, height, and degree of the rounded corner respectively in the lower left corner. To demonstrate the degree principle of the rounded corner rectangle, I add a circle in the lower left corner of the rounded corner rectangle:

 

//Bottom left coordinates followed by width, height and radius of corners cb.RoundRectangle(100f, 500f, 200f, 200f, 20f); cb.Stroke();   cb.Circle(120f, 520f, 20f); cb.Stroke();   cb.MoveTo(118f, 520f); cb.LineTo(122f, 520f); cb.Stroke(); cb.MoveTo(120f, 518f); cb.LineTo(120f, 522f); cb.Stroke();

 

The triangle is easier to draw. Just draw three lines. The following code shows how to draw a right triangle and mark the right angle:

 

cb.MoveTo(350f, 450f); cb.LineTo(350f, 600f); cb.LineTo(500f, 450f); cb.ClosePathStroke();   cb.MoveTo(350f, 460f); cb.LineTo(360f, 460f); cb.LineTo(360f, 450f); cb.Stroke();

 

 

Curves and Arcs

 

The besell curve is a very important vector graph. This curve is based on mathematical formulas rather than just point-to-point. The beiser curve is drawn by specifying a start point, an end point, and two control points. The first control point is based on the start point, and the second control point is based on the end point. The beiser curve starts from the start point toward the end point, and the center bend is determined by the control point. However, the curve itself is very close to the control point, but does not pass through the control point. If you have used PhotoShop Or Firework, the control point can be understood as the "handles" used to control radians by dragging )":

 

The following example shows how to draw a curve. Set the start point to (350,150, 10) and the end point ):

 

//Start Point cb.MoveTo(200f, 10f); //Control Point 1, Control Point 2, End Point cb.CurveTo(150f, 30f, 450f, 70f, 350f, 150f); 

Cb. Stroke ();

 

 

This curve is curved from the start point to the first control point and bent toward the second control point. before reaching the end point, the curve is as follows:

 

 

The above picture may not be so easy to understand, so I added some "handles )":

 

 

cb.MoveTo(200f, 10f); cb.LineTo(150f, 30f); cb.Stroke();   cb.MoveTo(450f, 70f); cb.LineTo(350f, 150f); cb.Stroke();   cb.Circle(450f, 70f, 1f); cb.Stroke(); cb.Circle(150f, 30f, 1f); cb.Stroke();

 

 

The "handles" of the first control point is relatively short, so the curve is only bent a little toward the first control point, and the second control point is relatively long, therefore, the curve is much curved towards the second control point under its influence. The best way to understand this is to extend the "handles" of the second control point again:

 

cb.SetColorStroke(Color.GREEN); //start point (x,y) cb.MoveTo(200f, 10f); //control point 1 (x,y), control point 2 (x,y), end point (x,y) cb.CurveTo(150f, 30f, 550f, 100f, 350f, 150f); cb.Stroke();   cb.MoveTo(550f, 100f); cb.LineTo(350f, 150f); cb.Stroke();   cb.Circle(550f, 100f, 1f); cb.Stroke();   cb.SetColorStroke(Color.LIGHT_GRAY);   //Bottom Left(x,y), Top Right(x,y), Start Angle, Extent cb.Arc(350f, 70f, 550f, 130f, 270f, 90f); cb.SetLineDash(3f, 3f); cb.Stroke();   cb.SetLineDash(0f); cb.MoveTo(550f, 100f); cb.LineTo(535f, 95f); cb.Stroke(); cb.MoveTo(550f, 100f); cb.LineTo(552f, 88f); cb.Stroke();

 

The original curve is displayed in black. Then the added curve is displayed in green. Now you can see that the green control point shows its impact. from the start point, the green curve starts slowly lower than the black curve, and then separated. because the second control point is far from the end point of the curve, the green curve is more circular. That is, more deviations from the control point before the end point is reached.

 

For the newly added code shown above, there are other areas to be noted. One of them is the Arc object, which is used to draw a curve from the original control point to the new control point with arrows. An arc is a part of a curve. Here the arc ranges from (550,130) in the lower left corner to () in the upper right corner. The Arc degree is calculated counterclockwise by default. The arc here is 270, which is equivalent to 90 degrees clockwise. In addition, the SetLineDash () method is used to change the drawn arc to a dotted line.

-------------------------------------------

Original article: iTextSharp-Drawing shapes and Graphics

Translate by CareySon

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.