简体   繁体   中英

iTextSharp - draw rectangle - border width issue

here's a simple code:

var w = Utilities.MillimetersToPoints(420);
var h = Utilities.MillimetersToPoints(210);

var doc1 = new Document(new Rectangle(w, h));

PdfWriter writer = PdfWriter.GetInstance(doc1, new FileStream("Doc1.pdf", FileMode.Create));

doc1.Open();

PdfContentByte cb = writer.DirectContent;

var rect = new Rectangle(200, 200, 100, 100);

and now, if I do the following:

cb.Rectangle(200, 200, 100, 100);
cb.Stroke();

then I see the rectangle. But I need to set its border width, so I do

 rect.BorderWidth = 5;
 rect.BorderColor = new BaseColor(0,0,0);

 cb.Rectangle(rect);
 cb.Stroke();

and now the rectangle is not visible. Why ?

The Rectangle() method on PdfContentByte has a couple of overloads and they behave quite differently depending on what you pass in.

Your first example is using the very simple overload that just takes 4 floats. If you look at the source for that you'll see that beyond some sanity checking it just writes those coordinates directly to the PDF stream and no actual Rectangle objects are created in the process. Later when you call Stroke() iText writes the stroke command to the stream and that's it. When a PDF renderer (like Adobe's) actually parses the stroke command it looks backwards in the buffer and sees the coordinates that it needs to stroke and performs the action.

Your second example uses the much more complex overload that you can see here which takes an actual Rectangle object. Besides representing four points in space a Rectangle has concepts like background colors and borders and, most importantly for you, these borders can be drawn per side and you need to tell it which sides to draw on .

For instance, for just left and right you'd do:

var rect = new iTextSharp.text.Rectangle(200, 200, 100, 100);
rect.Border = iTextSharp.text.Rectangle.LEFT_BORDER | iTextSharp.text.Rectangle.RIGHT_BORDER; 
rect.BorderWidth = 5;
rect.BorderColor = new BaseColor(0, 0, 0);
cb.Rectangle(rect);

And for all borders you'd change it to:

rect.Border = iTextSharp.text.Rectangle.BOX;

Also, when calling this overload it is actually incorrect to call Stroke() immediately after because this overload takes care of that for you (and might have done it more than once, actually.)

(an Addendum to @Chris' answer)

If you want to implement your task ( to set its border width ) using the simple means of the first example, you can explicitly set the width of lines to stroke:

cb.SetLineWidth(5);
cb.Rectangle(200, 200, 100, 100);
cb.Stroke();

You may want to envelope these lines in cb.SaveState() ... cb.RestoreState() to prevent the changed line width to influence later operations.

Document document = new Document(PageSize.A4, 25, 25, 30, 30);
PdfContentByte cb = writer.DirectContent;
        cb.Rectangle(30,660, 280,80);
 cb.Stroke();

the function writer is start to write in pdf and rectangle function is create rectangle and stroke is to draw above rectangle specification. so you have to write the stroke() function.

 PdfContentByte cb = pdfwrite.DirectContent;
 var Rectangular = new Rectangle(55, 620, 540,375);
 Rectangular.BorderWidthLeft = 0.1f;
 Rectangular.BorderWidthRight = 0.1f;
 Rectangular.BorderWidthTop = 0.1f;
 Rectangular.BorderWidthBottom = 0.1f;
 cb.Rectangle(Rectangular);
 cb.Stroke();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM