[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2009-02-08
2
Medium Priority
?
6,743 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

0
Comment
Question by:DivingDW
  • 2
2 Comments
 

Accepted Solution

by:
DivingDW earned 0 total points
ID: 23595255
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
 

Author Comment

by:DivingDW
ID: 23597498
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Loops Section Overview

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question