通过 iTextSharp 在 pdf 中插入图像代替便签不正确

Insert image in place of sticky notes in pdf by iTextSharp incorrect

我想插入一张图片来代替 pdf 上的便签。但是,我遇到的问题是,便利贴的位置是正确的,但是当插入图像时,它不在那里。

这是我的代码

  public void LoadData()
    {
        Stream inputPdfStream = new FileStream(txtFilePDF.Text, FileMode.Open, FileAccess.Read, FileShare.Read);
        Stream outputPdfStream = new FileStream("result.pdf", FileMode.Create, FileAccess.Write, FileShare.None);

        reader = new PdfReader(inputPdfStream);
        stamper = new PdfStamper(reader, outputPdfStream);
       

        //loop find sticky notes in page and replace by image
        for (int x = 1; x <= reader.NumberOfPages; x++) // i =1 page 1
        {
            PdfDictionary page = reader.GetPageN(x);
            pdfContentByte = stamper.GetOverContent(x);
            PdfArray annots = page.GetAsArray(PdfName.ANNOTS);

            //replace image in place of sticky notes
            try
            {
                for (int i = 0; i < annots.Count(); i++)
                {
                    PdfDictionary sticky = annots.GetAsDict(i);
                    PdfArray stickyRect = sticky.GetAsArray(PdfName.RECT);
                    //get position sticky notes
                    stickyRectangle = new PdfRectangle(
                        stickyRect.GetAsNumber(0).FloatValue, stickyRect.GetAsNumber(1).FloatValue,
                        stickyRect.GetAsNumber(2).FloatValue, stickyRect.GetAsNumber(3).FloatValue
                    );
                    
                  
                    string Content = "";
                    try
                    {
                        Content = sticky.GetAsString(PdfName.CONTENTS).ToString();
                    }
                    catch
                    {
                        Content = "";
                    }

                    if (Content.ToString().Trim().ToLower() == "trinhky")
                    {
                        //get image
                        GetHinhAnh(txtUserTrinhKy.Text);
                    }
                  




                }

            }
            catch { }

           
        }
        //remove all sticky notes in output pdf
       // reader.RemoveAnnotations();
        //end process
        stamper.Close();
        reader.Close();
        inputPdfStream.Close();
        outputPdfStream.Close();

   
    }

    public void GetHinhAnh(string UserName)
    {
        try
        {
            //get image
            System.Drawing.Image chuky = ((System.Drawing.Image)(Properties.Resources.ResourceManager.GetObject(UserName)));
            //covert to  iTextSharp image
            iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(chuky, iTextSharp.text.BaseColor.WHITE, false);

            image.ScaleAbsolute(100, 80);
            image.SetAbsolutePosition( stickyRectangle.Left,stickyRectangle.Bottom); // set position sticky note to image
            pdfContentByte.AddImage(image);
        }
        catch { }
    }
  

如何在pdf文件的便利贴正确位置插入图片?如果有人能给我一个解决方案,我将不胜感激。

现在,我试试下面的方法

stamper.RotateContents=假;

现在它得到了正确的位置,但添加的图像由于 pdf 页面而旋转(演示文件旋转 270)。我应该怎么做才能不旋转新图像?

这里有一些细节导致位置错误。

旋转页面

页面旋转了270°。在这种情况下,iText 转换内容流的坐标系 additions。另一方面,它们不会针对您的注释位置 retrieval 进行转换。因此,它们根本不匹配。

您自己已经发现可以让 iText 按原样使用坐标,而不是通过执行

来转换它们
stamper.RotateContents=false;

但这也会导致添加的图像在静态内容中不旋转,因此在旋转后的页面上看起来翻倒了。

要同时旋转图像以匹配稍后的页面旋转,您可以这样做

image.RotationDegrees = reader.GetPageRotation(x);

(在 90° 和 270° 旋转的情况下,您可能还必须切换 image.ScaleAbsolute 调用的参数以防止失真。)

注释上的 NoRotate 标志

此外,示例文档中的注释设置了 NoRotate 标志。此标志的这种效果说明为:

(ISO 32000-2 第 12.5.3 节 — 注释标志)

因此,当 PDF 查看器将页面旋转 270° 时,注释不会随文本一起旋转,并且您使用的 Rect 值不会精确映射到静态内容.但是,由于注释非常小,因此产生的偏移量也很小。

左下不再是左下

旋转的另一个影响是页面旋转后注释的左下角(这是未旋转用例中的固定点)不再是左下角。

在您的示例文档中,旋转 270° 会导致注释的 下角成为固定点,并且您的图像会向左和向上移动。 ..