Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用itextsharp在pdf文件中绘制直线时出现问题_C#_Asp.net_Pdf Generation_Itextsharp - Fatal编程技术网

C# 使用itextsharp在pdf文件中绘制直线时出现问题

C# 使用itextsharp在pdf文件中绘制直线时出现问题,c#,asp.net,pdf-generation,itextsharp,C#,Asp.net,Pdf Generation,Itextsharp,我正在asp.net c#中使用itextsharp生成一个pdf文件。我不会画水平线/垂直线/虚线 我试图用下面的代码画一条线,我没有得到任何错误,但这条线也没有显示在pdf文件中 PdfContentByte cb = wri.DirectContent; cb.SetLineWidth(2.0f); // Make a bit thicker than 1.0 default cb.MoveTo(20, pdfDocument.Top - 40f); c

我正在asp.net c#中使用itextsharp生成一个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();
代码中的问题是什么?是因为x y坐标的位置吗?我曾使用粗糙点来了解pdf中的大致位置,但该行在pdf文件中从未出现过


我要查看的输出如下图所示。

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

您确定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();

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

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

我最终使用了普林斯提供的答案和上面的答案的组合。使用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();
StringBuilder ChisHeader=new StringBuilder();
StringBuilder chistCourses=新建StringBuilder();
HttpContext.Current.Response.ContentType=“application/pdf”;
HttpContext.Current.Response.AddHeader(“内容处置”,“内联;文件名=CourseHistory.pdf”);
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
文档pdfDoc=新文档();
GetInstance(pdfDoc,HttpContext.Current.Response.OutputStream);
pdfDoc.Open();
ChisHeader=CourseHistoryHeader(将.ToInt32(hdUserID.Value),hdSystemPath.Value,“CourseHistory”);
chistCourses=CourseHistoryCourses(Convert.ToInt32(hdUserID.Value),hdSystemPath.Value,“CourseHistory”);
//为pdf编写标题
foreach(HTMLWorker.parsetList中的IEElement元素(新StringReader(chisHeader.ToString()),新样式表())
{
pdfDoc.Add(元素);
}
//必须以这种方式手动绘制一条线,因为ItextSharp不允许使用
标记。。。。 iTextSharp.text.pdf.draw.LineSeparator line1=新的iTextSharp.text.pdf.draw.LineSeparator(1f,100f,BaseColor.BLACK,Element.ALIGN_LEFT,1); 添加(新块(第1行)); //写出课程表 foreach(HTMLWorker.parsetList中的IElement元素(新StringReader(chistCourses.ToString()),新样式表()) { pdfDoc.Add(元素); } pdfDoc.Close(); HttpContext.Current.Response.Write(pdfDoc); HttpContext.Current.Response.End();
以防万一:您不是在试图将行写入PDF来编辑信息吗?你需要实际删除行下的文本内容,否则人们仍然可以从PDF中提取。是的,我知道在iTextsharp中,坐标系统从左下角向上运行,是的,我检查了整个页面。对我来说很有用。错误:没有方法“SetColorStroke”的重载接受5.3.2.0版的“3”参数,您可以使用cb.SetColorStroke(新的BaseColor(255,0,0));这不是我的反对票,但这不就是我的反对票吗?为什么这样更好?
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);
Dim line1 As New iTextSharp.text.pdf.draw.LineSeparator(0.0F, 100.0F, BaseColor.Black, Element.ALIGN_LEFT, 1)
pdfDoc.Add(New Chunk(line1))