如何在 iTextSharp 中找到当前(X,Y)位置?

发布于 2024-12-08 18:20:59 字数 96 浏览 0 评论 0原文

我需要创建一个包含多个部分的 PDF,并且在每个部分之后需要添加一条线,但我不知道在哪里绘制这条线。

我需要找到文档中下一个元素将被写入的确切坐标 [x, y]。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

撩发小公举 2024-12-15 18:20:59

就像@Olaf所说,使用GetVerticalPosition来获取YX 只是文档的LeftMargin。下面是一个针对 iTextSharp 5.1.1.0 的完整工作 WinForms 应用程序,希望能够满足您的需求:

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

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();
        }
    }
}
雪化雨蝶 2024-12-15 18:20:59

我相信只有 y 位置可用:尝试

PdfWriter.getVerticalPosition()

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

PdfWriter.getVerticalPosition()
南冥有猫 2024-12-15 18:20:59

确实只有 y 位置。

但是,如果需要渲染一些简单的文本,然后放置一张图片或画一条线,他总是可以计算渲染的文本的大小:

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

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();
所谓喜欢 2024-12-15 18:20:59

如果你只需要在当前部分之后画一条线,也许你不需要知道当前的x和y。
试试这个:

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文