如何使用itextsharp在pdf文件的文本框控件中使文本底部对齐

问题描述 投票:1回答:1

我需要从PDF文件中的TextBox中获得的文本底部对齐。

我正在使用以下代码从pdf中的TextBox获取文本。

 for (int i = 1; i <= reader.NumberOfPages; i++)
        {

            iTextSharp.text.pdf.PdfArray array = reader.GetPageN(i).GetAsArray(iTextSharp.text.pdf.PdfName.ANNOTS);
            if (array == null) continue;
            for (int j = 0; j < array.Size; j++)
            {
                iTextSharp.text.pdf.PdfDictionary annot = array.GetAsDict(j);

                iTextSharp.text.pdf.PdfString text = annot.GetAsString(iTextSharp.text.pdf.PdfName.CONTENTS);

            }
        }

我正在使用ItextSharp库。错误截图

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9PM1FVZC5wbmcifQ==” alt =“在此处输入图像描述”>

c# .net itextsharp itext
1个回答
1
投票
PdfReader reader = new PdfReader(SOURCE); 
PdfStamper stamper = new PdfStamper(reader, TARGET); 

TextField tf = new TextField(stamper.getWriter(), new Rectangle(300, 400, 
500, 420), text);
stamper.addAnnotation(tf.getTextField(), 1);

PdfContentByte overContent = stamper.getOverContent(1);
BaseFont baseFont = BaseFont.createFont();
overContent.setFontAndSize(baseFont, 12);
overContent.beginText();
overContent.showTextAligned(PdfContentByte.ALIGN_BOTTOM, text, 300,
405, 0);
overContent.endText();
stamper.close ();

或也尝试一下

using (PdfStamper stamper = new PdfStamper(new PdfReader(inputFile), File.Create(outputFile)))
{
TextField tf = new TextField(stamper.Writer, new iTextSharp.text.Rectangle(0, 0, 100, 300), "Vertical");
stamper.AddAnnotation(tf.GetTextField(), 1);
}
© www.soinside.com 2019 - 2024. All rights reserved.