使用ITextSharp在PDF中插入图像 [英] Insert an Image in PDF using ITextSharp

查看:143
本文介绍了使用ITextSharp在PDF中插入图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在pdf中插入图像。也就是说,无论我在哪里看到文字'签名',我都必须在那里插入签名图像。我可以说绝对的立场。
但是,我正在寻找如何在PDF格式中找到签名一词的位置并插入图像。

I have to insert a an image in a pdf. That is, wherever I see a text 'Signature', I have to insert an signature image there . I can do by saying absolute positions . But, I am looking for how to find the position of the word 'Signature' in the pdf and insert the image.

感谢您的帮助!

这是工作代码:

 using (Stream inputImageStream = new FileStream(@"C:\signature.jpeg", FileMode.Open, FileAccess.Read, FileShare.Read))
    using (Stream outputPdfStream = new FileStream(@"C:\test\1282011\Result.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
    {

        var reader = new PdfReader(@"C:\Test\1282011\Input.pdf");
        var stamper = new PdfStamper(reader, outputPdfStream);
        var count = reader.NumberOfPages;



        iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(inputImageStream);
        image.SetAbsolutePosition(300, 200); // Absolute position
        image.ScaleToFit(200, 30);

        PRTokeniser pkt = null;
        string strpages = string.Empty;
        System.Text.StringBuilder build = new System.Text.StringBuilder();


        for (int i = 1; i <= count; i++)
        {
            var pdfContentByte = stamper.GetOverContent(i);
            if (pdfContentByte != null)
            {
                pkt = new PRTokeniser(stamper.Reader.GetPageContent(i));
                while (pkt.NextToken())
                {
                    if (pkt.TokenType == PRTokeniser.TokType.STRING)
                    {
                        if (pkt.StringValue == "Signature")
                        {
                            pdfContentByte.AddImage(image);
                        }

                    }

                }
            }
        }
        stamper.Close();


    }
}

经过一些谷歌搜索,我发现文本的绝对位置如下:

After some googling, I found out that I could absolute position of text as follows:

extSharp.text.pdf.AcroFields fields = stamper.AcroFields;
               IList<iTextSharp.text.pdf.AcroFields.FieldPosition> signatureArea = fields.GetFieldPositions("Signature");
                iTextSharp.text.Rectangle rect=  signatureArea.First().position;
                iTextSharp.text.Rectangle logoRect = new iTextSharp.text.Rectangle(rect);
                image.SetAbsolutePosition(logoRect.Width ,logoRect .Height );

但即使pdf包含单词'Signature',变量signatureArea也始终为null。

But the variable , signatureArea is null all the time even when the pdf contains the word 'Signature'.

任何输入..? :)
Jaleel

Any input..? :) Jaleel

推荐答案

查看 PdfTextExtractor ,特别是 LocationTextExtractionStrategy 。使用 LocationTextExtractionStrategy 的确切代码在项目中创建一个类,并在行上放置一个断点 return sb.ToString(); (SVN第131行)并查看变量 locationalResult 的内容。你会看到你正在寻找的东西,一组包含起点和终点位置的文字。如果您的搜索词本身不在一条线上,您可能需要深入挖掘一下,但这应该指向正确的方向。

Check out PdfTextExtractor and specifically the LocationTextExtractionStrategy. Create a class in your project with the exact code for the LocationTextExtractionStrategy and put a breakpoint on the line return sb.ToString(); (line 131 in SVN) and take a look at the contents of the variable locationalResult. You'll see pretty much exactly what you're looking for, a collection of text with start and end locations. If your search word isn't on a line by itself you might have to dig a little deeper but this should point you in the right direction.

这篇关于使用ITextSharp在PDF中插入图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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