iTextSharp - draw rectangle - border width issue

☆樱花仙子☆ 提交于 2019-12-08 12:54:31

问题


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 ?


回答1:


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.)




回答2:


(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.




回答3:


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.




回答4:


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


来源:https://stackoverflow.com/questions/36177432/itextsharp-draw-rectangle-border-width-issue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!