We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Trying to convert HTML to PDF with iTextsharp, need help.

DivingDW
DivingDW asked
on
Medium Priority
6,865 Views
Last Modified: 2013-11-13
Ok, here's my predicament. I'm trying to write to a PDF directly from a Database. In the database, there is a column that has HTML tags in it. When I use the HTMLWorker parser to handle the HTML and spit it back into the document, it works fine when I'm not trying to put it in an iText Table. Unfortunatly I need to put the text in a table so I can format the PDF correctly (it needs to be on the left of another set of information). Am I shooting myself in the foot, and if not, how can I get this to work as intended?

In my snippet, you'll see I tried using the ColumnText approach as well, to no avail. It's commented out but I left it there in case I need to explore that option again (it's there to remind me). Also, the variable 'st' is my stylesheet.
Here is the snippet:
 
 // Bio Section
                    String strBio = SqlDR["szAttorneyBio"].ToString();
                    strBio = RemoveCRLF(strBio);
                    StringReader sr = new StringReader(strBio);
                    //PdfContentByte cb = writer.DirectContent;
                    ArrayList p = HTMLWorker.ParseToList(sr, st);
                    Paragraph para = new Paragraph();  
                    //iTextSharp.text.pdf.ColumnText ct = new iTextSharp.text.pdf.ColumnText(cb);
                    
                    for (int k = 0; k < p.Count; ++k)
                    {
                        para.Add((Phrase)p[k]);
                    }
                    tCell.Add(para);

Open in new window

Comment
Watch Question

Commented:
I solved my issue by putting the Arraylist into a Simple Table instead, and it works!
// Bio Section
                    String strBio = SqlDR["szAttorneyBio"].ToString();
                    strBio = RemoveCRLF(strBio);
                    StringReader sr = new StringReader(strBio);
                    ArrayList p = HTMLWorker.ParseToList(sr, st);
                    Paragraph para = new Paragraph();
                   
                    SimpleTable sTable = new SimpleTable();
                    sTable.Border = 0;
                    sTable.Widthpercentage = 100;
 
                    SimpleCell sRow = new SimpleCell(true);
                    sRow.Widthpercentage = 100;
 
                    SimpleCell sCell = new SimpleCell(false);
                    sCell.Widthpercentage = 75;
                    sCell.Spacing_left = 10;
                    sCell.Border = 0;
 
                    for (int k = 0; k < p.Count; ++k)
                    {
                        sCell.AddElement((IElement)p[k]);
                    }
                    sRow.AddElement(sCell);
 
                    // Side Information
                    sCell = new SimpleCell(false);
                    sCell.BackgroundColor = backColor;
                    sCell.Widthpercentage = 25;
                    sCell.Border = 0;
 
..........
 
                    sCell.AddElement((IElement)para);
                    sRow.AddElement(sCell);
                    sTable.AddElement(sRow);
                    document.Add(sTable);

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Also, just to add a bit more since I dreaded the hours upon hours of searching for what seemed to be a simple fix to other problems I had, I should mention I wrapped my SimpleTable with a MultiColumnText object, which made it so my text went to the next page and was positioned right on the document. Hope this helps anyone else searching for solutions. Sometimes the simple things aren't quite as simple when your brain is wrecked from days of coding....
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.