4

I need to create a PDF with several sections, and after each section need to add a line, but I don't know where to draw this line.

I need to find the exact coordinates [x, y] where the next element in the document will be write.

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
Emanuel
  • 6,622
  • 20
  • 58
  • 78

4 Answers4

9

Like @Olaf said, use GetVerticalPosition to get the Y. The X is just the document's LeftMargin. Below is a full working WinForms app targeting iTextSharp 5.1.1.0 that hopefully does what you are looking for:

using System;
using System.Text;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Test file name
            string TestFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf");

            //Standard iTextSharp setup
            using (FileStream fs = new FileStream(TestFile, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                using (Document doc = new Document(PageSize.LETTER))
                {
                    using (PdfWriter w = PdfWriter.GetInstance(doc, fs))
                    {
                        //Open the document for writing
                        doc.Open();

                        //Will hold our current x,y coordinates;
                        float curY;
                        float curX;

                        //Add a paragraph
                        doc.Add(new Paragraph("It was the best of times"));

                        //Get the current Y value
                        curY = w.GetVerticalPosition(true);

                        //The current X is just the left margin
                        curX = doc.LeftMargin;

                        //Set a color fill
                        w.DirectContent.SetRGBColorStroke(0, 0, 0);
                        //Set the x,y of where to start drawing
                        w.DirectContent.MoveTo(curX, curY);
                        //Draw a line
                        w.DirectContent.LineTo(doc.PageSize.Width - doc.RightMargin, curY);
                        //Fill the line in
                        w.DirectContent.Stroke();

                        //Add another paragraph
                        doc.Add(new Paragraph("It was the word of times"));

                        //Repeat the above. curX never really changes unless you modify the document's margins
                        curY = w.GetVerticalPosition(true);

                        w.DirectContent.SetRGBColorStroke(0, 0, 0);
                        w.DirectContent.MoveTo(curX, curY);
                        w.DirectContent.LineTo(doc.PageSize.Width - doc.RightMargin, curY);
                        w.DirectContent.Stroke();


                        //Close the document
                        doc.Close();
                    }
                }
            }

            this.Close();
        }
    }
}
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
8

I believe there is only the y-position available: try

PdfWriter.getVerticalPosition()
Olaf
  • 10,049
  • 8
  • 38
  • 54
2

There is indeed only the y-position.

But if one needs to render some simple text and after that put a picture or draw a line, he can always count the size of the text rendered:

var chunk = new Chunk(String.Format("Sample text {0}", ));                                                             
document.Add(new Paragraph(t));
float curY = writer.GetVerticalPosition(false);
float x = document.Left + chunk.GetWidthPoint();
Trakhan
  • 463
  • 1
  • 6
  • 15
1

If you just need to draw a line after the current section, maybe you do not need to know current x and y. Try this:

        iTextSharp.text.pdf.draw.DottedLineSeparator sepLINE = new iTextSharp.text.pdf.draw.DottedLineSeparator();

        sepLINE.LineWidth = 1;
        sepLINE.Gap = 2;
        sepLINE.Percentage = 50;
        sepLINE.LineColor = new iTextSharp.text.BaseColor(System.Drawing.Color.Blue);

        Chunk chnkLINE = new Chunk(sepLINE);
        pdfDoc.Add(chnkLINE);
Pmarloz
  • 11
  • 1