如何找到在当前iTextSharp的(X,Y)的位置? [英] How to find current (X, Y) position in iTextSharp?

查看:875
本文介绍了如何找到在当前iTextSharp的(X,Y)的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建几个部分的PDF,以及每个部分后需要添加一条线,但我不知道在哪里画这条线。

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.

我要找到确切的坐标[X,Y]在文档中的下一个元素会写。

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

推荐答案

像@Olaf说,使用 GetVerticalPosition 来获得 。在 X 仅仅是文档的 LEFTMARGIN 。下面是一个完整的工作的WinForms应用定位iTextSharp的5.1.1.0是希望做你在找什么:

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

这篇关于如何找到在当前iTextSharp的(X,Y)的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆