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

Posted

技术标签:

【中文标题】使用 itextsharp 在 pdf 文件中画线的问题【英文标题】:problem in drawing a line in a pdf file using itextsharp 【发布时间】:2011-07-03 11:16:08 【问题描述】:

我正在使用 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 文件中。

我正在寻找的输出如下图所示。

【问题讨论】:

以防万一:您不想在 PDF 中写入行来编辑信息?您实际上需要删除行下方的文本内容,否则人们仍然可以从 PDF 中提取它。 【参考方案1】:
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);

【讨论】:

【参考方案2】:

您应该始终确保为您正在执行的操作设置颜色,否则您将不知道会得到什么(它将来自之前执行的任何操作)。尝试做 cb.setStrokeColor(255, 0, 0) (纯红色),直到你得到你想要的线条。

【讨论】:

为我工作。你打败了我。 错误:方法“SetColorStroke”没有重载需要“3”个参数 从 5.3.2.0 版本开始,您可以使用 cb.SetColorStroke(new BaseColor(255,0,0));【参考方案3】:

您确定 pdfDocument.Top 正在返回一个值吗? 我用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();

【讨论】:

【参考方案4】:

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

【讨论】:

【参考方案5】:

您知道在 iTextsharp 中,坐标系统从左下角向上工作 - 您确定您的线没有被画到页面下方吗?

【讨论】:

是的,我知道在 iTextsharp 中,坐标系统从左下角向上工作,是的,我检查了整个页面。【参考方案6】:

我最终将 plinth 提供的答案与上面的 less 结合使用。使用 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();

【讨论】:

【参考方案7】:
Dim line1 As New iTextSharp.text.pdf.draw.LineSeparator(0.0F, 100.0F, BaseColor.Black, Element.ALIGN_LEFT, 1)
pdfDoc.Add(New Chunk(line1))

【讨论】:

不是我的反对票,但这与lessly's answer 不一样吗?为什么这样更好?

以上是关于使用 itextsharp 在 pdf 文件中画线的问题的主要内容,如果未能解决你的问题,请参考以下文章

如何在OpenGL中画线?

在 iPhone / iPad 中画线

在 Phaser 3 中画线

iTextSharp:创建 PDF 文件时显示“打开/保存”对话框

C#工具类:使用iTextSharp操作PDF文档

使用 ITextSharp 将 HTML 文件转换为 PDF 文件