Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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>
<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 10-Jul-12 10:46am
Edited 11-Jul-12 3:03am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
 
'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!!
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 195
1 Afzaal Ahmad Zeeshan 132
2 ProgramFOX 130
3 Maciej Los 115
4 Sergey Alexandrovich Kryukov 75
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,657
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 6 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100