在C中使用ITextSharp将文本框添加到现有pdf的右角#

在C中使用ITextSharp将文本框添加到现有pdf的右角#,itext,sharp,Itext,Sharp,我想用c#在现有pdf的右角添加一个文本框,但我无法完成。我已经写了下面的代码,但它无助于解决问题,有人能建议我吗 using (MemoryStream stream = new MemoryStream()) { PdfReader reader = new PdfReader(bytes); PdfReader.unethicalreading = true; Paragraph p = new Paragrap

我想用c#在现有pdf的右角添加一个文本框,但我无法完成。我已经写了下面的代码,但它无助于解决问题,有人能建议我吗

using (MemoryStream stream = new MemoryStream())
{
        PdfReader reader = new PdfReader(bytes);               
        PdfReader.unethicalreading = true;
        Paragraph p = new Paragraph();
        Document doc = new Document();

        using (PdfStamper stamper = new PdfStamper(reader, stream))
        {
            PdfContentByte canvas = stamper.GetOverContent(1);
            iTextSharp.text.Rectangle size = reader.GetPageSizeWithRotation(1);


            //PdfContentByte cb = null;
            //PdfImportedPage page;
            int pages = reader.NumberOfPages;                    
            for (int i = 1; i <= pages; i++)
            {
                var size1 = reader.GetPageSize(i);
                w = size1.Width;
                h = size1.Height;
                stamper.FormFlattening = true;

                TextField tf = new TextField(stamper.Writer, new iTextSharp.text.Rectangle(0, 0, 300, 100), displaytext);
                //Change the orientation of the text
                tf.Rotation = 0;
                stamper.AddAnnotation(tf.GetTextField(), i);
            }
        }
        bytes = stream.ToArray();
}
File.WriteAllBytes(str, bytes);
使用(MemoryStream stream=new MemoryStream())
{
PdfReader reader=新PdfReader(字节);
PdfReader.unethicalreading=true;
第p段=新的第()段;
单据单据=新单据();
使用(PdfStamper压模=新PdfStamper(读卡器,流))
{
PdfContentByte canvas=stamper.GetOverContent(1);
iTextSharp.text.Rectangle size=reader.GetPageSizeWithRotation(1);
//PdfContentByte cb=null;
//PDF导入页面;
int pages=reader.NumberOfPages;
对于(inti=1;i,正如OP在对问题的评论中所阐明的,他希望

  • 将文本作为页面内容添加到页面右下角,然后
  • 要删除以前存在的页面内容
这方面的一个简单实现将包括

  • 首先用填充矩形覆盖现有页面内容,然后
  • 然后在那里写文章
这些任务可以通过以下助手方法实现:

void EmptyTextBoxSimple(PdfStamper stamper, int pageNumber, Rectangle boxArea, BaseColor fillColor)
{
    PdfContentByte canvas = stamper.GetOverContent(pageNumber);
    canvas.SaveState();
    canvas.SetColorFill(fillColor);
    canvas.Rectangle(boxArea.Left, boxArea.Bottom, boxArea.Width, boxArea.Height);
    canvas.Fill();
    canvas.RestoreState();
}

例如,像这样:

using (PdfReader reader = new PdfReader(source))
using (PdfStamper stamper = new PdfStamper(reader, new FileStream(dest, FileMode.Create)))
{
    Rectangle cropBox = reader.GetCropBox(1);
    Rectangle bottomRight = new Rectangle(cropBox.GetRight(216), cropBox.Bottom, cropBox.Right, cropBox.GetBottom(146));
    EmptyTextBoxSimple(stamper, 1, bottomRight, BaseColor.WHITE);
    ColumnText columnText = GenerateTextBox(stamper, 1, bottomRight);
    columnText.AddText(new Phrase("Some test text to draw into a text box in the lower right corner of the first page"));
    columnText.Go();
}
using (PdfReader reader = new PdfReader(source))
using (PdfStamper stamper = new PdfStamper(reader, new FileStream(dest, FileMode.Create)))
{
    Rectangle cropBox = reader.GetCropBox(1);
    int rotation = reader.GetPageRotation(1);
    while (rotation > 0)
    {
        cropBox = cropBox.Rotate();
        rotation -= 90;
    }
    Rectangle bottomRight = new Rectangle(cropBox.GetRight(216), cropBox.Bottom, cropBox.Right, cropBox.GetBottom(146));
    EmptyTextBoxSimple(stamper, 1, bottomRight, BaseColor.WHITE);
    ColumnText columnText = GenerateTextBox(stamper, 1, bottomRight);
    columnText.AddText(new Phrase("Some test text to draw into a text box in the lower right corner of the first page"));
    columnText.Go();
}
对于此源页面

示例代码生成了这个

补遗 OP在评论中指出

它适用于所有文件,但对于某些pdf文件,它显示在中间

最终,他提供了一个文件,这个问题就发生在这个文件上。事实上,这个文件可以复制这个问题

问题的原因是示例文件中的页面使用了页面旋转,这是iText(仅适用于)的功能部分允许用户忽略。特别是iText在旋转后自动将文本旋转为竖直,并转换坐标,但在检索页面的cropbox时,在使用其坐标之前仍必须应用旋转。因此,更完整的示例如下:

using (PdfReader reader = new PdfReader(source))
using (PdfStamper stamper = new PdfStamper(reader, new FileStream(dest, FileMode.Create)))
{
    Rectangle cropBox = reader.GetCropBox(1);
    Rectangle bottomRight = new Rectangle(cropBox.GetRight(216), cropBox.Bottom, cropBox.Right, cropBox.GetBottom(146));
    EmptyTextBoxSimple(stamper, 1, bottomRight, BaseColor.WHITE);
    ColumnText columnText = GenerateTextBox(stamper, 1, bottomRight);
    columnText.AddText(new Phrase("Some test text to draw into a text box in the lower right corner of the first page"));
    columnText.Go();
}
using (PdfReader reader = new PdfReader(source))
using (PdfStamper stamper = new PdfStamper(reader, new FileStream(dest, FileMode.Create)))
{
    Rectangle cropBox = reader.GetCropBox(1);
    int rotation = reader.GetPageRotation(1);
    while (rotation > 0)
    {
        cropBox = cropBox.Rotate();
        rotation -= 90;
    }
    Rectangle bottomRight = new Rectangle(cropBox.GetRight(216), cropBox.Bottom, cropBox.Right, cropBox.GetBottom(146));
    EmptyTextBoxSimple(stamper, 1, bottomRight, BaseColor.WHITE);
    ColumnText columnText = GenerateTextBox(stamper, 1, bottomRight);
    columnText.AddText(new Phrase("Some test text to draw into a text box in the lower right corner of the first page"));
    columnText.Go();
}
正如OP在对问题的评论中澄清的那样,他希望

  • 将文本作为页面内容添加到页面右下角,然后
  • 要删除以前存在的页面内容
这方面的一个简单实现将包括

  • 首先用填充矩形覆盖现有页面内容,然后
  • 然后在那里写文章
这些任务可以通过以下助手方法实现:

void EmptyTextBoxSimple(PdfStamper stamper, int pageNumber, Rectangle boxArea, BaseColor fillColor)
{
    PdfContentByte canvas = stamper.GetOverContent(pageNumber);
    canvas.SaveState();
    canvas.SetColorFill(fillColor);
    canvas.Rectangle(boxArea.Left, boxArea.Bottom, boxArea.Width, boxArea.Height);
    canvas.Fill();
    canvas.RestoreState();
}

例如,像这样:

using (PdfReader reader = new PdfReader(source))
using (PdfStamper stamper = new PdfStamper(reader, new FileStream(dest, FileMode.Create)))
{
    Rectangle cropBox = reader.GetCropBox(1);
    Rectangle bottomRight = new Rectangle(cropBox.GetRight(216), cropBox.Bottom, cropBox.Right, cropBox.GetBottom(146));
    EmptyTextBoxSimple(stamper, 1, bottomRight, BaseColor.WHITE);
    ColumnText columnText = GenerateTextBox(stamper, 1, bottomRight);
    columnText.AddText(new Phrase("Some test text to draw into a text box in the lower right corner of the first page"));
    columnText.Go();
}
using (PdfReader reader = new PdfReader(source))
using (PdfStamper stamper = new PdfStamper(reader, new FileStream(dest, FileMode.Create)))
{
    Rectangle cropBox = reader.GetCropBox(1);
    int rotation = reader.GetPageRotation(1);
    while (rotation > 0)
    {
        cropBox = cropBox.Rotate();
        rotation -= 90;
    }
    Rectangle bottomRight = new Rectangle(cropBox.GetRight(216), cropBox.Bottom, cropBox.Right, cropBox.GetBottom(146));
    EmptyTextBoxSimple(stamper, 1, bottomRight, BaseColor.WHITE);
    ColumnText columnText = GenerateTextBox(stamper, 1, bottomRight);
    columnText.AddText(new Phrase("Some test text to draw into a text box in the lower right corner of the first page"));
    columnText.Go();
}
对于此源页面

示例代码生成了这个

补遗 OP在评论中指出

它适用于所有文件,但对于某些pdf文件,它显示在中间

最终,他提供了一个文件,这个问题就发生在这个文件上。事实上,这个文件可以复制这个问题

问题的原因是示例文件中的页面使用了页面旋转,这是iText(仅适用于)的功能部分允许用户忽略。特别是iText在旋转后自动将文本旋转为竖直,并转换坐标,但在检索页面的cropbox时,在使用其坐标之前仍必须应用旋转。因此,更完整的示例如下:

using (PdfReader reader = new PdfReader(source))
using (PdfStamper stamper = new PdfStamper(reader, new FileStream(dest, FileMode.Create)))
{
    Rectangle cropBox = reader.GetCropBox(1);
    Rectangle bottomRight = new Rectangle(cropBox.GetRight(216), cropBox.Bottom, cropBox.Right, cropBox.GetBottom(146));
    EmptyTextBoxSimple(stamper, 1, bottomRight, BaseColor.WHITE);
    ColumnText columnText = GenerateTextBox(stamper, 1, bottomRight);
    columnText.AddText(new Phrase("Some test text to draw into a text box in the lower right corner of the first page"));
    columnText.Go();
}
using (PdfReader reader = new PdfReader(source))
using (PdfStamper stamper = new PdfStamper(reader, new FileStream(dest, FileMode.Create)))
{
    Rectangle cropBox = reader.GetCropBox(1);
    int rotation = reader.GetPageRotation(1);
    while (rotation > 0)
    {
        cropBox = cropBox.Rotate();
        rotation -= 90;
    }
    Rectangle bottomRight = new Rectangle(cropBox.GetRight(216), cropBox.Bottom, cropBox.Right, cropBox.GetBottom(146));
    EmptyTextBoxSimple(stamper, 1, bottomRight, BaseColor.WHITE);
    ColumnText columnText = GenerateTextBox(stamper, 1, bottomRight);
    columnText.AddText(new Phrase("Some test text to draw into a text box in the lower right corner of the first page"));
    columnText.Go();
}

“TextBox”是什么意思?你是指多行文本字段?自由文本批注?实际页面内容?实际上,它是我想要显示的字符串(想在页面内容中添加额外的行),当我尝试使用下面的代码时,它只是显示,但并没有擦除下面的textColumnText.ShowTextAligned(stamper.GetOverContent(I),Element.ALIGN_基线,新段落(displaytext.ToString(),blackFont),250f,5f,0);“它只是在显示,但没有擦除下面的文本”-好的,是的,显然不会删除任何文本,毕竟您不做任何事情来导致某些文本被删除。要删除的文本是页面内容的一部分吗?然后您可以用填充矩形覆盖它,或者使用iText编校类(
PdfCleanUp*
在iText xtra中)实际删除它。或者文本顶部在某些批注中被删除了吗?然后你必须相应地更新批注。您好,很高兴听到回复,我尝试了添加矩形并执行此操作的确切方法,但我也无法获得我在顶部尝试的代码。对代码有何建议,您所说的“文本框”是什么意思?你的意思是多行文本字段?自由文本批注?实际页面内容?实际上,它是我想要显示的字符串(想在页面内容中添加额外的行),当我尝试使用下面的代码时,它只是显示,但没有擦除下面的textColumnText.ShowTextAligned(stamper.GetOverContent(I),Element.ALIGN_基线,新段落(displaytext.ToString(),blackFont),250f,5f,0);“它只是在显示,但没有擦除下面的文本”-好的,是的,显然不会删除任何文本,毕竟您不做任何事情来导致某些文本被删除。要删除的文本是页面内容的一部分吗?然后您可以用填充矩形覆盖它,或者使用iText编校类(
PdfCleanUp*
在iText xtra中)实际删除它。或者文本顶部是否在某些注释中被删除?然后您必须相应地更新注释。您好,很高兴听到回音,我尝试了添加矩形的确切方法,但我也无法获得我在顶部尝试过的代码。对代码有何建议,非常感谢,这对我解决此问题有很大帮助。太好了!在c中ase请接受答案(单击左上角的勾号)。您好,mkl,代码工作,我面临矩形框大小减小的问题,如果我减小框大小,则字符串不会显示在框中,我尝试将框大小减小到页面右下角,最小宽度和高度为w