Insert an Image in PDF using ITextSharp

天涯浪子 提交于 2020-01-30 05:50:06

问题


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.

Appreciate ur help!

This is the working code:

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

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

Any input..? :) Jaleel


回答1:


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.




回答2:


That was perfect Chris. I am able to find the text position and insert the signature. What I understood is , there is a list List<TextChunk> LocationalResult in the LocationTextExtractionStrategy class. The RenderText() method in LocationTextExtractionStrategy will add each text to the LocationalResult list.

Actually the list LocationalResult is a private list, I made it public to access it from outside.
I loop through each page of PDF document and call PdfTextExtractor.GetTextFromPage(reader, i, locationStrat); where i is the pagenumber. At this time all text in the page will be added to the LocationalResult with all the position information. This is what I done . And it works perfect.



来源:https://stackoverflow.com/questions/7115242/insert-an-image-in-pdf-using-itextsharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!