[ACCEPTED]-problem in drawing a line in a pdf file using itextsharp-itextsharp

Accepted answer
Score: 16
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);

0

Score: 5

You should always make sure to set the color 6 for the operation that you're performing, otherwise 5 you won't know what you'll get (it will 4 be from whatever previous operation was 3 performed). Try doing cb.setStrokeColor(255, 0, 0) (pure 2 red) until you get your line where you want 1 it.

Score: 4

Are you sure that pdfDocument.Top is returning 1 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();
Score: 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))

0

Score: 0

You know that in iTextsharp, the co-ordinate 3 system works from the bottom left corner 2 upwards - are you sure your line isn't getting 1 drawn further down the page?

Score: 0

I ended up using a combination of the answer 6 provided by plinth along with lessly from 5 above. Using StringBuilder functions, you 4 can block things off and then manually draw 3 a line unless you have a table cell that 2 takes up all of the width of the TD tag 1 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();

More Related questions