Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have been scouring the web trying to find a method to accomplish the task of replacing text during an HTML to PDF conversion. What exactly I am trying to accomplish is to create HTML files that will be parsed to PDF. But....I need to incorporate Text Fields into this conversion at some point. I've been trying to add them once the parse is completed, but you need exact coordinates to place the text field, and I need it to be more dynamic than that.

I am using iTextSharp to attempt to accomplish this.

For example, let's say I have a short HTML snippet for a 1x1 table(cell), and I have the placeholders in the cell.

HTML
<html>
<body> 
<table>
<tr>
<td>
<p>%SomePlaceholderText%</p>
</td>
</tr>
</table>
</body>
</html>



Question:
Is it possible that before the conversion takes place(or during, or after, really) for me to iterate through the text that will be written to the pdf, and look for placeholders, and replace them with the method to insert a text field? I have tried using TextExtractionStrategy, but I am not very seasoned in c# and it feels like it's above my level of experience. All I want to do is replace the placeholder with a Text Field dynamically. Is this at all possible with iTextSharp, or should I try another library?
Posted
Updated 11-Jul-12 2:03am
v2

1 solution

I found a great VB.Net solution on Stack Overflow for this particular requirement after what felt like days of searching. I am still in the process of adapting it to c#, and I am working on a few things at the moment, but as soon as I convert it to c# I'll post a link here to the source code once it is completed underneath the VB.Net link.

Essentially what this solution does is to parse an existing pdf, searching for specific text values within it. If the extractor encounters matching text, it draws a pink rectangle around it. What it actually does with the extracted text is completely customizable though. I have it Drawing a rectangle around the text, and dropping a text field in it's place. I'll include the piece that needs to be swapped in VB to do this, so if anyone sees this, they can get the general idea of how to adapt the extracted text to fit your needs.

VB Link
http://stackoverflow.com/questions/6523243/how-to-highlight-a-text-or-word-in-a-pdf-file-using-itextsharp[^] It is the last answer, just click on the word HERE that is licked, and the download dialog will open.

c# Link

Coming soon

In Form1.vb under
Public Sub PDFTextGetter(ByVal pSearch As String, ByVal SC As StringComparison, ByVal SourceFile As String, ByVal DestinationFile As String)
And directly underneath the comment I include with the code, you can do what you please with the rectangle drawn around the extracted text.

VB
'MatchesFound contains all text with locations, so do whatever you want with it, this highlights them using PINK color:
Dim AccountFields = 1
Dim MeterFields = 1
For Each rect As iTextSharp.text.Rectangle In MatchesFound
    cb.Rectangle(rect.Left, rect.Bottom, rect.Width, rect.Height + 2)

    Dim field As New TextField(stamper.Writer, New iTextSharp.text.Rectangle(rect.Left, rect.Bottom, rect.Right, rect.Top + 2), "AccountNumber" & AccountFields)
    Dim form = stamper.AcroFields
    Dim fieldKeys = form.Fields.Keys
    stamper.AddAnnotation(field.GetTextField(), page)
    AccountFields += 1



The comment says it will fill in the rectangle, but I just left that in there so you can trace it to the original spot in the solution you download. Obviously this is not what it does in the snippet I provide. Anyone with a background with iTextsharp can piece that together, but I figured I would articulate it as well.

*Edit* Just a side note, the default writing mode for the ContentByte in this program is to GetUnderContent, so if you want to drop anything over that, simply toggle that to GetOverContent, and you will be golden.
Cheers!!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900