使用itextsharp在pdf文件中画线的问题

发布于 2024-10-19 04:01:55 字数 506 浏览 1 评论 0原文

我正在使用 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.enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

木緿 2024-10-26 04:01:55
Paragraph p = new Paragraph(new Chunk(new iTextSharp.text.pdf.draw.LineSeparator(0.0F, 100.0F, BaseColor.BLACK, Element.ALIGN_LEFT, 1)));
document.Add(p);
Paragraph p = new Paragraph(new Chunk(new iTextSharp.text.pdf.draw.LineSeparator(0.0F, 100.0F, BaseColor.BLACK, Element.ALIGN_LEFT, 1)));
document.Add(p);
人│生佛魔见 2024-10-26 04:01:55

您应该始终确保为正在执行的操作设置颜色,否则您将不知道会得到什么(它将来自之前执行的任何操作)。尝试执行 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.

浅笑轻吟梦一曲 2024-10-26 04:01:55

您确定 pdfDocument.Top 返回一个值吗?
我使用了PageSize.Width和PageSize.Height

iTextSharp.text.Document myDocument = new Document(PageSize.A4);
PdfContentByte contentByte = writer.DirectContent;
contentByte.SetLineWidth(1);
contentByte.MoveTo(0,  14);
contentByte.LineTo(myDocument.PageSize.Width,14);
contentByte.Stroke();

Are you sure that pdfDocument.Top is returning a value?
I used PageSize.Width and PageSize.Height

iTextSharp.text.Document myDocument = new Document(PageSize.A4);
PdfContentByte contentByte = writer.DirectContent;
contentByte.SetLineWidth(1);
contentByte.MoveTo(0,  14);
contentByte.LineTo(myDocument.PageSize.Width,14);
contentByte.Stroke();
江挽川 2024-10-26 04:01:55

iTextsharp 线条绘制:-

Dim line1 As New iTextSharp.text.pdf.draw.LineSeparator(0.0F, 100.0F, BaseColor.Black, Element.ALIGN_LEFT, 1)
pdfDoc.Add(New Chunk(line1))

iTextsharp Line Draw:-

Dim line1 As New iTextSharp.text.pdf.draw.LineSeparator(0.0F, 100.0F, BaseColor.Black, Element.ALIGN_LEFT, 1)
pdfDoc.Add(New Chunk(line1))
爱你不解释 2024-10-26 04:01:55

您知道在 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?

无语# 2024-10-26 04:01:55

我最终使用了 plinth 提供的答案以及上面的较少答案的组合。使用 StringBuilder 函数,您可以将某些内容屏蔽掉,然后手动绘制一条线,除非您的表格单元格占据了 TD 标签的所有宽度以及一个单词。

StringBuilder chistHeader = new StringBuilder();
StringBuilder chistCourses = new StringBuilder();

HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition", "inline;filename=CourseHistory.pdf");
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);

Document pdfDoc = new Document();
PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);

pdfDoc.Open();

chistHeader = CourseHistoryHeader(Convert.ToInt32(hdUserID.Value), hdSystemPath.Value, "CourseHistory");
chistCourses = CourseHistoryCourses(Convert.ToInt32(hdUserID.Value), hdSystemPath.Value, "CourseHistory");



        //write header for the pdf
foreach (IElement element in HTMLWorker.ParseToList(new StringReader(chistHeader.ToString()), new StyleSheet()))
    {
        pdfDoc.Add(element);
    }

//have to manually draw a line this way as ItextSharp doesn't allow a <hr> tag....
iTextSharp.text.pdf.draw.LineSeparator line1 = new    iTextSharp.text.pdf.draw.LineSeparator(1f, 100f, BaseColor.BLACK, Element.ALIGN_LEFT, 1);
pdfDoc.Add(new Chunk(line1));

 //write out the list of courses
 foreach (IElement element in HTMLWorker.ParseToList(new StringReader(chistCourses.ToString()), new StyleSheet()))
    {
        pdfDoc.Add(element);
    }

 pdfDoc.Close();

 HttpContext.Current.Response.Write(pdfDoc);
 HttpContext.Current.Response.End();

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.

StringBuilder chistHeader = new StringBuilder();
StringBuilder chistCourses = new StringBuilder();

HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition", "inline;filename=CourseHistory.pdf");
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);

Document pdfDoc = new Document();
PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);

pdfDoc.Open();

chistHeader = CourseHistoryHeader(Convert.ToInt32(hdUserID.Value), hdSystemPath.Value, "CourseHistory");
chistCourses = CourseHistoryCourses(Convert.ToInt32(hdUserID.Value), hdSystemPath.Value, "CourseHistory");



        //write header for the pdf
foreach (IElement element in HTMLWorker.ParseToList(new StringReader(chistHeader.ToString()), new StyleSheet()))
    {
        pdfDoc.Add(element);
    }

//have to manually draw a line this way as ItextSharp doesn't allow a <hr> tag....
iTextSharp.text.pdf.draw.LineSeparator line1 = new    iTextSharp.text.pdf.draw.LineSeparator(1f, 100f, BaseColor.BLACK, Element.ALIGN_LEFT, 1);
pdfDoc.Add(new Chunk(line1));

 //write out the list of courses
 foreach (IElement element in HTMLWorker.ParseToList(new StringReader(chistCourses.ToString()), new StyleSheet()))
    {
        pdfDoc.Add(element);
    }

 pdfDoc.Close();

 HttpContext.Current.Response.Write(pdfDoc);
 HttpContext.Current.Response.End();
如若梦似彩虹 2024-10-26 04:01:55
Dim line1 As New iTextSharp.text.pdf.draw.LineSeparator(0.0F, 100.0F, BaseColor.Black, Element.ALIGN_LEFT, 1)
pdfDoc.Add(New Chunk(line1))
Dim line1 As New iTextSharp.text.pdf.draw.LineSeparator(0.0F, 100.0F, BaseColor.Black, Element.ALIGN_LEFT, 1)
pdfDoc.Add(New Chunk(line1))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文