Need help to know how to inject label or read only textbox inside PDF


#1

Hello,

We have requirement like inside PDF there is some year data like below
3-Year:
5-Year:
6-Year:
etc…

(sample PDF attached so you can refer it)
sample.pdf (735.3 KB)

So beside each year we have to show some dynamic calculated rates. So our first priority is to use some label if possible so can you let us know how we can do that and we have not only one PDF we have multiple PDFs and all PDF have different position for this year section so we can not use any static position. So let us know that by which software we can insert label or read only textbox beside each year. And using code we can set it’s value. We already have example for textbox in which we can set field values using code like below

var document = GetPdfDocument(_fileSystem.GetFullPath(pdfPath));
foreach (var field in document.Form.Fields)
{
field.Value = “10”;
}
So we have below question

  1. Is Aspose PDF supports inserting label as like textbox?
  2. If it does not support label then can we use read only textbox if yes then which software we can use to edit PDF file to insert read only textbox beside each year.
  3. If it support label then provide C# example for how to fill all labels?

Thanks.


#2

@santosh.g2047

Thank you for contacting support.

The PDF document does not contain any fields so it is like adding a Text Stamp by finding some text and appending the stamp next to found text dynamically. We have devised below code snippet for your kind reference and have attached generated PDF document as well.

Document pdfDocument = new Document(dataDir + "sample.pdf");
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("3-Year:");
TextSearchOptions textSearchOptions = new TextSearchOptions(true);
textFragmentAbsorber.TextSearchOptions = textSearchOptions;

foreach (Page page in pdfDocument.Pages)
{
    page.Accept(textFragmentAbsorber);
    TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
    TextFragment fragment = textFragmentCollection[1];

    // Create text stamp
    TextStamp textStamp = new TextStamp("1234");

    // Set origin
    textStamp.XIndent = fragment.Rectangle.LLX + fragment.Rectangle.Width + 5 ;
    textStamp.YIndent = fragment.Rectangle.LLY;

    // Set text properties
    textStamp.TextState.FontSize = fragment.TextState.FontSize;
    textStamp.TextState.ForegroundColor = fragment.TextState.ForegroundColor;
    // Add stamp to particular page
    pdfDocument.Pages[fragment.Page.Number].AddStamp(textStamp);
}
pdfDocument.Save(dataDir + "Replaced_19.6.pdf");

Replaced_19.6.pdf

We hope this will be helpful. Please feel free to contact us if you need any further assistance.


#3

Thanks we tried above code and it works good but we get problem in one of the PDF file. Here the problem is somehow code is not able to find specified string even if it exist in PDF file. I have attached PDF file sample2.pdf (1.9 MB)

In this PDF file we are trying to find Life (M 65) and put cost beside it but it is not able to find this text even if it exist on the first page. Same way we need to find Life (F 65) as well as Joint Life (M 65 / F 65)

We tried you above code sample so please let us know other code required over here.
Thanks.


#4

@santosh.g2047

Thank you for your kind feedback.

We are afraid shared PDF document looks corrupt as Adobe Reader fails to open it. Please check and share the file once again so that we may proceed.


#5

sample2.pdf (1.9 MB)
Hi Farhan,

Thank you for you quick reply. And I am hereby attaching the pdf again. Along with that I am attaching the snip shot of that pdf. Sample 3.JPG (80.9 KB)


#6

@santosh.g2047

We are afraid it is corrupt once again while other forum attachments are fine. We have attached a screenshot of the dialog box that appears while opening the file with Adobe Acrobat and Adobe Reader Corrupt.PNG. Would you please share it once again as ZIP file via Google Drive, Dropbox etc. so that we may assist you accordingly.


#7

Our above problem is resolved for that PDF. Instead of “Life (M 65)” we gave search text as “M 65” and it is able to find that string in the pdf and and it worked. May be problem was with the “(” character.

However in next pdf SamplePDF.pdf (643.9 KB)
we are having problem. In the pdf we are trying to find
“1-Year Point-to-Point Cap Index Account (S&P 500)”
…etc. and put the rate beside that string. but we are unable to read that string and I am getting exception message as “Invalid index: index should be in the range [1…n] where n equals to the text fragments count.”

We tried the above code sample. Could you please help us with this issue? Is there an alternate code to read the text string of pdf and put the value(that comes from database) beside it? Hoping(643.9 KB)
for the quick reply. Thank you.

Here is the google drive link for the pdf if you cannot open above pdf: https://drive.google.com/file/d/18j5nl6WnpRGH9lgbGNropJsfnNfXQgob/view?usp=sharing


#8

@santosh.g2047

We are glad to know that previous problem has been resolved. About SamplePDF.pdf file, it contains the text on image thus is not searchable. You can convert non-searchable PDF file to searchable PDF document, please try using following code snippet with Tesseract .

C#

Document doc = new Document("D:/Downloads/input.pdf");
doc.Convert(CallBackGetHocr);
doc.Save("E:/Data/pdf_searchable.pdf");
//********************* CallBackGetHocr method ***********************//
static string CallBackGetHocr(System.Drawing.Image img)
{
    string dir = @"E:\Data\";
    img.Save(dir + "ocrtest.jpg");
    ProcessStartInfo info = new ProcessStartInfo(@"C:\Program Files (x86)\Tesseract-OCR\tesseract.exe");
    info.WindowStyle = ProcessWindowStyle.Hidden;
    info.Arguments = @"E:\data\ocrtest.jpg E:\data\out hocr";
    Process p = new Process();
    p.StartInfo = info;
    p.Start();
    p.WaitForExit();
    StreamReader streamReader = new StreamReader(@"E:\data\out.html");
    string text = streamReader.ReadToEnd();
    streamReader.Close();
    return text;
}

#9

Thank you for your reply. It helped me. As you said the problem was with the PDF which was non-searchable.


#10

@santosh.g2047

Thank you for your kind feedback.

We are pleased to know it helped you. Please feel free to create a separate topic if you need any further assistance.