Ishan
Ishan

Reputation: 4028

problem in drawing a line in a pdf file using itextsharp

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.enter image description here

Upvotes: 11

Views: 44066

Answers (7)

gfyhtu
gfyhtu

Reputation: 1

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

Upvotes: -3

ettore ct
ettore ct

Reputation: 199

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

Upvotes: 16

user1143550
user1143550

Reputation: 35

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

Upvotes: 0

lessly
lessly

Reputation: 49

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

Upvotes: 4

plinth
plinth

Reputation: 49189

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.

Upvotes: 5

JoR
JoR

Reputation: 305

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

Upvotes: 4

Paddy
Paddy

Reputation: 33857

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?

Upvotes: 0

Related Questions