【转] iTextSharp - 描画形状およびグラフィックス

これは、スイッチ:https://www.mikesdotnetting.com/article/88/itextsharp-drawing-shapes-and-graphics

前のiTextSharpの記事は、PDFファイルに画像をもたらし、それらの作業を見ました。しかし、時には、あなたは、PDF内図形や線を描きたいとイメージに依存しないことがあります。iTextSharpは非常に複雑な形状への簡単な描画をカバーしていた機能の多くを含んでいます。この記事では、あなたが開始されます。前の記事は以下の通りです:

iTextSharp入門- ASP.NETでPDFを作成
iTextSharp -フォントの操作
iTextSharp -チャンク、フレーズと段落でテキストを追加する
iTextSharpとリスト
のリンクやブックマーク- iTextSharp
の表に紹介- iTextSharp
iTextSharpを-ワーキングイメージの

今まで、以前の記事でPDF文書に追加されたすべてのコンテンツは、文書の流れの中に配置するコンテンツの世話をするシンプルなiTextの、に依存してきました。私たちは今、明示的PdfContentByte()オブジェクトを使用する必要があるということで、少し異なるアプローチを必要とするグラフィックスでオーバーフローテキストなどの作業を受け入れるために新しいページを作成した後、それはまた見えます。これは、PDFWriterのオブジェクトのDirectContentから取得されます。だけではなく、PDFWriterののgetInstanceメソッドを呼び出すと、私たちは実際にPDFWriterのオブジェクトをインスタンス化する必要があり、この手段も。

 

文字列pdfpath = Server.MapPathの("PDFファイル");

  文書ドキュメント=  新しい  ドキュメント();

  試します

  {

    PDFWriterのライター=  PdfWriter.GetInstance(DOC、  新しい  FileStreamを(pdfpath +  "/Graphics.pdf"、  FileMode.Create));

    doc.Open();

    PdfContentByte CB = writer.DirectContent。
    ...

 

今、私たちが取り組んでPdfContentByteオブジェクトを持っていることを、私たちは、描画を開始するためにそれを使用することができます。

 

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

 

Xの最初の行に移動し、y座標は、この場合、半文書を横切っており、アップ途中(または中心点)は、渡されたパラメータで指定されました。次の行は、途中文書を横切ってまだあるが、それの上部にLINETO()メソッドで指定された位置にこの点から線を描画します。実際には、それを「描く」しませんが、実際には我々の意図を説明しています。ラインだけ実際に脳卒中()が呼び出された文書に取り組んでいます。我々は概説文書の先頭2四半期で終わるように、 - 第二線が途中同じ高さの右側エッジに文書上の位置に、左側の縁部から引き出されます。

同じ方法論を使用して、我々は、左上の四半期に四角形を追加することができます。

 

cb.MoveTo(100F、700F)。

cb.LineTo(200F、700F)。

cb.LineTo(200F、600F)。

cb.LineTo(100F、600F)。

cb.ClosePath();

cb.Stroke();

 

私たちは、明示的に、正方形の最終面の座標を指定する必要はありませんでした。ClosePath()は、自動的に、我々は、元の座標であることを現在の位置からのラインを提供します。しかし、iTextSharpによって提供される便利な形状のいずれかを使用して、正方形(または長方形)を送達する迅速な方法があります。アクションで次のビットを示し、文書の右上四半期に四角を配置します:

 

cb.Rectangle(doc.PageSize.Width-200F、600F、100F、100F)。

cb.Stroke();

 

さらに4つの四角は、ストローク以外の異なる方法の結果を示すそれぞれの()は、文書にそれらを委託するために使用される、ページに追加されます。しかし、最初に、ストローク色と塗りつぶしの色が設定されています。あなたは、このようなPhotoshopや花火など、任意のグラフィックスパッケージで働いている場合は、おそらく脳卒中は、オブジェクトの輪郭で、塗りつぶしが脳卒中で囲まれ、その内部を表していることを知っているだろう。私は黄色にシアンのストローク色と塗りつぶしの色を設定し、色空間としてCMYKを使用しています:

 

cb.SetColorStroke(新しい  CMYKColor(1F、0F、0F、0F));

cb.SetColorFill(新しい  CMYKColor(0F、0F、1F、0F));

 

cb.MoveTo(70、200)。

cb.LineTo(170、200)。

cb.LineTo(170、300)。

cb.LineTo(70、300)。

//パスは閉じられ、撫で

cb.ClosePathStroke();

 

cb.MoveTo(190、200)。

cb.LineTo(290、200)。

cb.LineTo(290、300)。

cb.LineTo(190、300)。

//塗りつぶしが、撫でたり閉じていません

cb.Fill();

 

cb.MoveTo(310、200)。

cb.LineTo(410、200)。

cb.LineTo(410、300)。

cb.LineTo(310、300)。

//、充填ストロークが、パスが閉じていません

cb.FillStroke();

 

cb.MoveTo(430、200)。

cb.LineTo(530、200)。

cb.LineTo(530、300)。

cb.LineTo(430、300)。

//パスは、閉じたストロークといっぱい

cb.ClosePathFillStroke();

 

代わりに描画する正方形又は長方形を表すためにRectangleオブジェクトを使用する場合、最初の2つのパラメータは、右下隅のx座標とy座標を表します。最終の2つのパラメータは、幅と高さです。他の予め設定された形状は、円を含むが、に渡され、xおよびy座標は半径を表す値が続く形の中心点を表します。上記の4乗の最初のを見て、それが幅と高さが100ポイントであることを決定することは比較的容易であり、中心点120のx、yの250であること。それがうまく収まるように、正方形に円を追加するには、次のコードが実行します。

 

cb.SetCMYKColorStroke(255、255、0、0);

cb.SetCMYKColorFill(0、255、255、0);
cb.SetLineWidth(2F)。

cb.Circle(120F、250F、50F)。

cb.Fill();

 

Rather irritatingly, I found you have to guess at the actual values required by the two methods concerning the use of CMYK colour I have used so far. Neither was intuitive, given that generally, CMYK colours are represented as a series of percentages. For example, Warm Red is referenced as C: 0%, M: 100%, Y: 100%, K: 100%. The constructor for CMYKColor() requires four floats. Providing values that represent the percentages works fine. But I have provided values of 1f and 0f. In fact, I can provide any value I want, so long as it is a valid float. The values are treated as being relative to eachother, so for 100% blue (Cyan) i could just have validly provided 3000f, 0f, 0f, 0f. Strange, but workable if you stick to the usual CMYK percentages, and don't accidentally type in an extra zero.

The SetCMYKColorFill() method used just above accepts ints, rather than floats. I thought maybe this method was working on percentages as I would expect with CMYK, so I originally supplied the value 0, 100, 100, 0 to represent Warm Red. All I got was a washy pinky colour. I thought perhaps there was a bug in there somewhere, and that something was being affected by the previous setting of the Fill colour, so I deployed the ResetCMYKColorFill() method in the hope that it did something. Nope. All it did was reset to the default colour of black. Eventually, I found out CMYKColor is built on the ExtendedColor class, and can accept ints up to 255 - which is reminiscent of the RGB colour values and really confusing unless you know. I applied the values you see above, and got my desired result. To build on the oddity of this approach, I also discovered that there is nothing to prevent you entering values above 255. If you do, it seems that iTextSharp subtracts 255 from whatever you supply, so 256 is the equivalent of 1, and 510 is the equivalent of 255 - or at least, that's the results I got when I tried it.

Anyway, enough of the digression. Here's my red circle with a mauve border (2 points in width) in the first square:

Moving on, here's an example of using another preset shape, the ellipse. First, a rectangle is drawn, then an ellipse is added to fit neatly within it to illustrate how the positioning works through the parameters supplied to the constructor:

 

// 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 differences between the first and third parameter, and the second and fourth parameter are the same, you end up with a circle.

The next example shows another preset shape, the rounded rectangle. When one of these are drawn, the values passed in are the x and y coordinates for the bottom left hand corner, followed by the width and height, and finally the radius of the rounded corners. I have also added a circle in the corner with the same radius as the corner itself to show how the radius of the corner works. I've placed a cross at the center of the circle:

 

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

 

Triangles are relatively simple to do, just by drawing the three lines they require. Here's an example of a right-angled triangle with the right angle shown:

 

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

Bezier curves are important in Vector Graphics, where they are based on mathematical equations rather than each individual point being specified along the path of the curve. They work by specifying a start point, an end point, and two control points. The first control point is specified in reference to the start point, and the second one is related to the end point. The curve will begin at the start point, and head towards the end point in the direction of the 1st control point. How far it gets near the control point will depend on the distance between the start point and its control point, and the end point and its control point, and the distance between the start and end point themselves. The control points are used for directional purposes, and are rarely hit by the path. If you have ever used a Vector graphics package, like Photoshop or Fireworks, you will have seen these control points, usually with little "handles" on the end of them which you can move to bend the curve.

Here's an example of a curve, that starts at (200, 10), and ends at (350, 150).

 

//Start Point

cb.MoveTo(200f, 10f);

//Control Point 1, Control Point 2, End Point

cb.CurveTo(150f, 30f, 450f, 70f, 350f, 150f);

cb.Stroke();

 

The curve heads towards the first control point at (150, 30), then bends away towards the second control point at (450, 70), before arriving at the end point. It looks like this:

I'm not really sure that shows a lot, so I will add in some "handles" that show the control points:

 

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 first control point has a relatively short handle, so the curve only begins to move towards it. The second control point has a longer handle, and this starts to exert its influence on the curve along with the ultimate destination at a relatively early point in its journey, which is reflected in the way the line is "pulled" towards it. This effect is probably best illustrated by changing the second control point so that it is further away, and the angle is altered:

 

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 shown in black, while the new one is shown in green. Now you can see that the increased length of the second "handle" has exerted its influence right from the start point, in that the green line starts just slightly beneath the original black one. Since the new second control point is further away from the end point, and is at a slightly more obtuse angle, the green line moves much further towards the second control point before finishing up at the end point.

There are a couple of other points to note about the code for the second curve. One is the Arc object, which has been used in this instance to create the curved arrow illustrating the movement of the original and new second control points. An Arc is part of an ellipse. In this case, the ellipse will fit into a rectangle that has a bottom left corner at (350, 70) and a top right corner at (550, 130). The default direction that an ellipse is drawn is counter-clockwise. The angle at which the arc is started is 270°, and only 90° of it are drawn. Secondly, the SetLineDash() method is shown for the first time in this series of articles, which allows me to draw the arrow as a dashed line.

おすすめ

転載: www.cnblogs.com/freeliver54/p/12173492.html