Hi All.
Rather than duplicating the forum request I made yesterday in the public forum I am simply sending the related link.
<a title="https://forum.aspose.com/t/77001
Please Note: Any resolution can be left public in that it may assist someone else having similar problems.
Thanks in Advance
Hi Michael,
Thanks for the quick reply. Just for your own information as background of what we are doing.
The personalised PDF files containing the accounts data are all initially created via an application which calls a SAP Crystal Report template. (Crystal 2008) This application reads data from approx 100 plus records. As the data for each record is read it populates the report
which is then automatically exported as a pdf adding a group code (Eg AH1_, BP1_, CL1_…)
at the beginning of each filename. (These three letter codes are branch codes which are read in along with the accounts data for that record.) Thus for each record we are in a position to generate files using the following name structure: AH1_BranchAccounts.pdf, BP1_BranchAccounts, etc. Our application then e-mails these files out to the relevant Branch Treasurers by using the treasurer’s Branch Code (AH1) to locate the matching AH1_BranchAccounts.pdf file. We want to run an intermediate process which will allow us to include fillable fields where ever we place specifically named textboxes. We envision that we should be able to proceed by naming each textbox in the format of textbox1,textbox2, textbox3, … textbox20. Then by incorporating ASPOSE.PDF within our application we should be able to create a loop which would be along the line of
for i = 1 to 20
create a formfield for “textbox” & i
next.
When the textboxes have been converted we would simply read in the next file BP1_BranchAccounts.pdf and repeat the process.
Hope this gives you an overview of what we are trying to do.
All the Best
Mick O’Rourke
Hi Mick,
Thanks for your patience.
In order to replace the TextFragment with TextField, we recommend you to try using the Aspose.Pdf.InteractiveFeatures.Forms.TextBoxField object. Please try using the following code snippet which iterates through the whole PDF file and searches for a particular string (based on a regular expression), replaces the string with an empty string, and places TextBoxField over the same location.
For your reference, I have also attached the resultant PDF generated using Aspose.Pdf for .NET 8.8.0.
[VB.NET]
Dim pdfDocument As New Document("c:\pdftest\TestAccountsInput.pdf")
'the input.pdf)
'find text
Dim textFragmentAbsorber As New TextFragmentAbsorber("textbox.", New Aspose.Pdf.Text.TextOptions.TextSearchOptions(True))
'the text extraction options to search using regular expression.
Group = True
Dim textSearchOptions As New TextOptions.TextSearchOptions(True)
textFragmentAbsorber.TextSearchOptions = textSearchOptions
' accept the absorber for all the pages
pdfDocument.Pages.Accept(textFragmentAbsorber)
'get the extracted text fragments
Dim textFragmentCollection As TextFragmentCollection = textFragmentAbsorber.TextFragments
Dim increment As Integer = 1
'loop through the fragments
For Each textFragment As TextFragment In textFragmentCollection
'determine the text position
Dim textRect As Aspose.Pdf.Rectangle = textFragment.Rectangle ' 'Create Object of the rectangle class
'Remove the text
textFragment.Text = String.Empty
'create a field
Dim textBoxField As New Aspose.Pdf.InteractiveFeatures.Forms.TextBoxField(pdfDocument.Pages(1), textRect)
textBoxField.PartialName = "textbox" & increment
textBoxField.Value = "Hello.." & increment
textBoxField.HorizontalAlignment = HorizontalAlignment.Left
textBoxField.DefaultAppearance.FontSize = 12
Dim border As New Aspose.Pdf.InteractiveFeatures.Annotations.Border(textBoxField)
border.Dash = New Aspose.Pdf.InteractiveFeatures.Annotations.Dash(1,1)
textBoxField.Width = 50
textBoxField.Height = 15
'add field to the document
pdfDocument.Form.Add(textBoxField, 1)
increment += 1
Next
pdfDocument.Save("c:\pdftest\TestAccountsOutput.pdf")
'Saves the updated PDF with form field replacement.
“Again, this is our output file with the TextField formatted over the existing Text”(Output file). Thanks & Regards,
Mick Beaupré
Mentor")
Hi Nayyer,
Brilliant! If you ever come to Ireland then I owe you a good dinner.
I probably should have stated in my first post that the final PDF would contain multiple pages, but I did not think that this would become a problem. The total PDF document has three pages. Anyway, I created the ...input.PDF with text boxes appropriately placed on each of the three pages. However, the replacement action places all the formfields on the first page. Please see the accompanying attachments. Is there any way that the replacements can take place on each of the respective pages?
Thanks in advance,
Mick O'Rourke
PS This thread can be placed on the public forum in order to assist others who may have similar issues.
We have managed, though not very elegantly, managed to add the formfields to each of the pages as follows
If increment = 20 Then 'then we have reached the number of textboxes on first page
iPage = 2
ElseIf increment = 29 Then ' and have now reached the number of text boxes on second page
iPage = 3
End If
'add field to the document
pdfDocument.Form.Add(textBoxField, iPage) ' iPage sets page number
increment += 1
. . . . . .
It's not urgent but I would be obliged if you can provide a better solution.
Yours
Michael O'Rourke
morke: Brillaint! If you ever come to Ireland then I owe you a good dinner.
Hi Michael,
Thanks for the invitation and I would love to join you over the dinner, if I ever visit Ireland :).
morke: I probably should have stated in my first post that the final PDF would contain multiple pages, but I did not think that this would become a problem. The total PDF document has three pages. Anyway, I created the […input.PDF…] with textboxes appropriately placed on each of the three pages. However, the replacement action places all the formfields on the first page. Please see the accompanying attachments. Is there any way that the replacements can take place on each of the respective pages?
In order to replace text over all the pages, you need to iterate through individual page of document and search for particular string and then replace it with FormField. Please try using the following code snippet to accomplish your requirements ([notice the text highlighted in yellow]). For your reference, I have also attached the resultant PDF generated over my end.
[VB.NET]
Dim pdfDocument As New Document("c:\pdftest\BrFinanceAsposeTesterInput.pdf") '"input.pdf")
' iterate through all the page of document
For pagecount As Integer = 1 To pdfDocument.Pages.Count
' find text
Dim textFragmentAbsorber As New TextFragmentAbsorber("textbox.", New Aspose.Pdf.Text.TextOptions.TextSearchOptions(True)) '"xtbox2")
' set text search option to specify regular expression usage
Dim textSearchOptions As New TextOptions.TextSearchOptions(True)
textFragmentAbsorber.TextSearchOptions = textSearchOptions
pdfDocument.Pages(pagecount).Accept(textFragmentAbsorber)
' get the extracted text fragments
Dim textFragmentCollection As TextFragmentCollection = textFragmentAbsorber.TextFragments
Dim increment As Integer = 1
' loop through the fragments
For Each textFragment As TextFragment In textFragmentCollection
' determine the text position
Dim textRect As Aspose.Pdf.Rectangle = textFragment.Rectangle 'Create Object of the rectangle class
' Remove the text
textFragment.Text = String.Empty
' create a field
Dim textBoxField As New Aspose.Pdf.InteractiveFeatures.Forms.TextBoxField(pdfDocument.Pages(pagecount), textRect)
textBoxField.PartialName = "textbox" & increment
textBoxField.Value = "Hello.." & increment
textBoxField.HorizontalAlignment = HorizontalAlignment.Left
textBoxField.DefaultAppearance.FontSize = 12
Dim border As New Aspose.Pdf.InteractiveFeatures.Annotations.Border(textBoxField)
border.Dash = New Aspose.Pdf.InteractiveFeatures.Annotations.Dash(1, 1)
textBoxField.Width = 50
textBoxField.Height = 15
' add field to the document
pdfDocument.Form.Add(textBoxField, pagecount)
increment += 1
Next
Next
pdfDocument.Save("c:\pdftest\TestAccountsOutput.pdf")
morke: PS This thread can be placed on the public forum in order to assist others who may have similar issues.
As per your suggestion, I have moved this thread to public forum so that other users can also benefit from it.