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,
Dim pdfDocument As New Document(“c:\pdftest\TestAccountsInput.pdf”) '“input.pdf”)<o:p></o:p>
' 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.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") 'And this is our output file with the text replace by a formfield
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:Brilliant! If you ever come to Ireland then I owe you a good dinner.
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 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?
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”)<o:p></o:p>
’ iterate through all the page of
document<o:p></o:p>
For pagecount As Integer = 1 To
pdfDocument.Pages.Count<o:p></o:p>
’ find text<o:p></o:p>
Dim textFragmentAbsorber As New TextFragmentAbsorber(“textbox.”, New
Aspose.Pdf.Text.TextOptions.TextSearchOptions(True)) 'xtbox2")<o:p></o:p>
'set text search option to specify
regular expression usage<o:p></o:p>
Dim textSearchOptions As New TextOptions.TextSearchOptions(True)<o:p></o:p>
textFragmentAbsorber.TextSearchOptions =
textSearchOptions<o:p></o:p>
pdfDocument.Pages(pagecount).Accept(textFragmentAbsorber)<o:p></o:p>
'get the extracted text fragments<o:p></o:p>
Dim textFragmentCollection As
TextFragmentCollection =
textFragmentAbsorber.TextFragments<o:p></o:p>
Dim increment As Integer = 1<o:p></o:p>
‘loop through the fragments<o:p></o:p>
For Each textFragment As TextFragment In textFragmentCollection<o:p></o:p>
’ determine the text position<o:p></o:p>
Dim textRect As Aspose.Pdf.Rectangle = textFragment.Rectangle ‘Create Object of the rectangle class<o:p></o:p>
’ Remove the text<o:p></o:p>
textFragment.Text = String.Empty<o:p></o:p>
'create a field<o:p></o:p>
Dim textBoxField As New Aspose.Pdf.InteractiveFeatures.Forms.TextBoxField(pdfDocument.Pages(pagecount),
textRect)<o:p></o:p>
textBoxField.PartialName = “textbox”
& increment<o:p></o:p>
textBoxField.Value = “Hello…”
& increment<o:p></o:p>
textBoxField.HorizontalAlignment = HorizontalAlignment.Left<o:p></o:p>
textBoxField.DefaultAppearance.FontSize = 12<o:p></o:p>
Dim border As New Aspose.Pdf.InteractiveFeatures.Annotations.Border(textBoxField)<o:p></o:p>
border.Dash = New
Aspose.Pdf.InteractiveFeatures.Annotations.Dash(1,
1)<o:p></o:p>
textBoxField.Width = 50<o:p></o:p>
textBoxField.Height = 15<o:p></o:p>
'add field to the document<o:p></o:p>
pdfDocument.Form.Add(textBoxField, pagecount)<o:p></o:p>
increment += 1<o:p></o:p>
Next<o:p></o:p>
Next<o:p></o:p>
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.