开发者

problem in drawing a line in a pdf file using itextsharp

开发者 https://www.devze.com 2023-02-12 22:47 出处:网络
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 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.

problem in drawing a line in a pdf file using itextsharp


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


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.


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


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


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?


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


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

精彩评论

暂无评论...
验证码 换一张
取 消