Comment trouver la position actuelle (X, Y) dans iTextSharp?

Je dois créer un PDF avec plusieurs sections, et après chaque section, il faut append une ligne, mais je ne sais pas où dessiner cette ligne.

Je dois trouver les coordonnées exactes [x, y] où le prochain élément du document sera écrit.

Comme @Olaf a dit, utilisez GetVerticalPosition pour obtenir le Y Le X n’est que la LeftMargin du document. Vous trouverez ci-dessous une application WinForms complète qui cible iTextSharp 5.1.1.0 et qui, espérons-le, répond à vos attentes:

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

Je crois qu’il n’ya que la position y disponible: essayez

 PdfWriter.getVerticalPosition() 

Il n’y a en effet que la position y.

Mais si vous avez besoin de restituer du texte simple, puis de mettre une image ou de tracer une ligne, vous pouvez toujours compter la taille du texte rendu:

 var chunk = new Chunk(Ssortingng.Format("Sample text {0}", )); document.Add(new Paragraph(t)); float curY = writer.GetVerticalPosition(false); float x = document.Left + chunk.GetWidthPoint(); 

Si vous avez juste besoin de tracer une ligne après la section actuelle, vous n’avez peut-être pas besoin de connaître les x et y actuels. Essaye ça:

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