使用itextsharp在pdf文件中绘制一条线的问题

时间:2021-09-06 21:11:23

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.

我使用itextsharp在asp.net c#中生成pdf文件。我无法绘制水平线/垂直线/虚线。

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

我尝试使用以下代码绘制一条线,我没有得到任何错误,但该行也没有显示在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();

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.

代码中的问题是什么。是因为x y坐标的位置?我曾使用粗略点来了解pdf中的大致位置,但该行在pdf文件中从不出现。

The output i am looking out for is as shown in image below.使用itextsharp在pdf文件中绘制一条线的问题

我期待的输出如下图所示。

7 个解决方案

#1


5  

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.

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

#2


13  

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

#3


5  

iTextsharp Line Draw:-

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

#4


3  

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

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

#5


0  

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?

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

#6


0  

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.

我最后使用了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();

#7


-2  

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

#1


5  

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.

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

#2


13  

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

#3


5  

iTextsharp Line Draw:-

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

#4


3  

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

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

#5


0  

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?

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

#6


0  

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.

我最后使用了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();

#7


-2  

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