使用itextsharp在pdf文件中画线的问题
我正在使用 itextsharp 在 asp.net c# 中生成 pdf 文件。我无法绘制水平线/垂直线/虚线。
我尝试使用以下代码绘制一条线,我没有收到任何错误,但该线也没有显示在 pdf 文件中
PdfContentByte cb = wri.DirectContent;
cb.SetLineWidth(2.0f); // Make a bit thicker than 1.0 default
cb.MoveTo(20, pdfDocument.Top - 40f);
cb.LineTo(400, pdfDocument.Top - 40f);
cb.Stroke();
代码中存在什么问题。是否是因为 xy 坐标的位置?我曾使用粗略的点来了解pdf中的大致位置,但该线从未出现在pdf文件中。
我正在寻找的输出如下图所示。
I am generating a pdf file in asp.net c# using itextsharp. i am not able to draw a horizontal line/verticle line/dotted line.
i tried to draw a line using the following code,i am getting no errors but the line is also not getting displayed in the pdf file
PdfContentByte cb = wri.DirectContent;
cb.SetLineWidth(2.0f); // Make a bit thicker than 1.0 default
cb.MoveTo(20, pdfDocument.Top - 40f);
cb.LineTo(400, pdfDocument.Top - 40f);
cb.Stroke();
What is the problem in the code.Is it because of the position of x y co-ordinates? I had used rough points to know approximate position in pdf,but the line never apears in the pdf file.
The output i am looking out for is as shown in image below.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您应该始终确保为正在执行的操作设置颜色,否则您将不知道会得到什么(它将来自之前执行的任何操作)。尝试执行 cb.setStrokeColor(255, 0, 0) (纯红色),直到您的线条到达您想要的位置。
You should always make sure to set the color for the operation that you're performing, otherwise you won't know what you'll get (it will be from whatever previous operation was performed). Try doing cb.setStrokeColor(255, 0, 0) (pure red) until you get your line where you want it.
您确定 pdfDocument.Top 返回一个值吗?
我使用了PageSize.Width和PageSize.Height
Are you sure that pdfDocument.Top is returning a value?
I used
PageSize.Width and PageSize.Height
iTextsharp 线条绘制:-
iTextsharp Line Draw:-
您知道在 iTextsharp 中,坐标系统从左下角向上工作 - 您确定您的线条不会在页面下方进一步绘制吗?
You know that in iTextsharp, the co-ordinate system works from the bottom left corner upwards - are you sure your line isn't getting drawn further down the page?
我最终使用了 plinth 提供的答案以及上面的较少答案的组合。使用 StringBuilder 函数,您可以将某些内容屏蔽掉,然后手动绘制一条线,除非您的表格单元格占据了 TD 标签的所有宽度以及一个单词。
I ended up using a combination of the answer provided by plinth along with lessly from above. Using StringBuilder functions, you can block things off and then manually draw a line unless you have a table cell that takes up all of the width of the TD tag along with a word.