• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6794
  • Last Modified:

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

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

0
DivingDW
Asked:
DivingDW
  • 2
1 Solution
 
DivingDWAuthor 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

0
 
DivingDWAuthor 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....
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now