0

I am trying to print rich text content in pdf using itextSharp library with version 5.5.8.0. But I am I am printing that rich text, it will print only normal string without any style effect or html tags.

Here is my web view that contain rich text

enter image description here

But while printing in pdf using iTexhSharp, it will print like rich text but each html element it will start printing with new line, as like below image

enter image description here

Here is the code, that I am using to print rich text in pdf:

ElementList elements = XMLWorkerHelper.ParseToElementList(descriptionData, "");

                PdfPCell cell = new PdfPCell();
                foreach(var ele in elements) {
                    cellDescriptionData.AddElement(ele);
                }

                tableThirdBlock.AddCell(cellDescriptionData);

Here the "descriptionData" field will contain the html string.

I want to be print the same in pdf as available in web view.

Here is the actual HTML string, which is generated dynamically. So the html string will be dynamic with dynamic css and text.

" <b style="font-family: Arial, Verdana; font-size: 10pt; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal; color: rgb(204, 255, 255); background-color: rgb(51, 102, 255);">zzz&nbsp;</b><div style="font-family: Arial, Verdana; font-size: 10pt; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal;"><b style="color: rgb(204, 255, 255); background-color: rgb(51, 102, 255);"><br /></b></div><div><b style="font-family: Arial, Verdana; font-size: 10pt; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal; color: rgb(204, 255, 255); background-color: rgb(51, 102, 255);">Test &nbsp;</b> &nbsp; <span style="color: rgb(255, 0, 0);">&nbsp;<span style="font-weight: bold;">Description</span> &nbsp;</span><span style="color: rgb(153, 51, 153); font-style: italic;">Other Color</span></div> ".

Is anything wrong, that I am missing ?

Please help me to print rich text with all effect and styles in pdf.

Thanks

Herin
  • 704
  • 3
  • 18
  • 34
  • 1
    You're using `HTMLWorker`. You should use XML Worker instead. XML Worker is an add-on for iTextSharp ([a separate DLL](http://developers.itextpdf.com/question/generate-pdf-html-using-itextsharpdll)). Read more about it on [the official iText website](http://developers.itextpdf.com/faq/category/parsing-xml-and-xhtml). See for instance [How to convert HTML to PDF?](http://developers.itextpdf.com/question/how-convert-html-pdf) – Bruno Lowagie Feb 16 '16 at 14:14
  • I have an important question: How come you didn't visit the official documentation? I've spent a lot of time and money on building that web site and on providing all that content, but somehow nobody seems to (want to) use it. Can I do something to improve the documentation? Is something missing? Right now, it feels like I've thrown away a lot of money and wasted plenty of time, because I still see people like you who use `HTMLWorker` instead of following the advice given on the official web site. I hope that you understand that this frustrates me. – Bruno Lowagie Feb 16 '16 at 14:16
  • Your above link is right, but how can I use XML Worker to print rich text in particular "PdfPCell" ? I have gone through your website, but didn't find any such example to print rich html text in Cell with all effect and styles. The Styles for such rich text is included in html string only and its dynamic. Can you please provide such example ? – Herin Feb 17 '16 at 05:05
  • @Bruno, people like the OP probably either use some ancient tutorial that still lives on some unknown third party website, or they re-use existing ancient production code they or their predecessor wrote decades ago. There is nothing we can do about that except consistently telling people what the right thing to do is. I'll check if "HTMLWorker" is a keyword we already monitor on StackOverflow. Perhaps you can check with our SEO expert how we can drive Google searches for "HTMLWorker" to our website. (if we're not already doing that) – Amedee Van Gasse Feb 17 '16 at 07:18
  • Hi, can anyone please look at my above question again, as I have updated my question with Image. Please provide your suggestion or help. Thanks – Herin Mar 01 '16 at 10:17

1 Answers1

2

I your comment, you are asking for an example on how to use XML Worker to parse HTML to a list of Element objects. Such an example can be found on the official iText web site in the answers to question such as:

In 1, you'll discover that you can parse an HTML and CSS file to an ElementList object:

ElementList elements = XMLWorkerHelper.parseToElementList(HTML, CSS);

In 2, you'll learn how to add the elements in an ElementList to a PdfPCell:

PdfPTable table = new PdfPTable(1);
PdfPCell cell = new PdfPCell();
for (Element e : elements) {
    cell.addElement(e);
}
table.addCell(cell);
document.add(table);

Note that I simplified the examples from the original questions. You don't need RTL because your text probably isn't Arabic. You can probably use the convenience method parseToElementList() instead of using the full code that was used in 2.

I have created a proof of concept that results in the file test-herin.pdf:

enter image description here

The HTML to get this result looks like this:

<div><span class="bluetextwhitebackground">zzz</span></div>
<div>
<span class="bluetextwhitebackground">Test</span>
<span class="redtext">Description</span>
<span class="italicpurple">Other Color</span>
</div>

The CSS to get the desired styles looks like this:

.bluetextwhitebackground
    { font-family: times; color: white; background: blue}
.redtext
    { font-family: times; color: red; }
.italicpurple
    { font-family: times; font-style: italic; color: purple }

As you can see, I used <div> when I want a block of text that causes a new line after the text is rendered. I used <span> in cases where I don't want a new line to appear.

It is hard to answer your question because you don't show your HTML. New lines are triggered when using <p> or <div> tags, or by defining something as a div in the CSS.

By the way, the code to generate the PDF can be found here: ParseHtml13

Update:

After you shared the HTML, I created another proof of concept:

The resulting PDF file is test-herin2.pdf and it looks like this:

enter image description here

This looks exactly the way I expect it. In other words: I can't reproduce the problem you're describing. If I can't reproduce a problem, I can't fix it.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • In your answer line as: " ElementList elements = XMLWorkerHelper.parseToElementList(HTML, CSS); " Here in this line , there is error says : "iTextSharp.tool.xml.XMLWorkerHelper does not contain definition for 'parseToElementList' " Can you please tell where it is available ? I am using itextsharp.xmlworker DLL with version 5.4.5.0 – Herin Feb 17 '16 at 10:48
  • You have to upgrade to a more recent version! iTextSharp 5.4.5 dates from 2013 and we're already 2016! – Bruno Lowagie Feb 17 '16 at 10:58
  • Hi, can you please look at my above question again, as I have updated my question with Image. Please provide your suggestion or help. Thanks. – Herin Mar 01 '16 at 09:43
  • Please look at my question again, I have updated with my actual HTML string data. That html string will be generated dynamically, So the CSS will also be dynamic for it. How can I achieve it ? Thanks – Herin Mar 01 '16 at 13:23
  • @Herin I checked. It works as expected. Did you post this question to the person you've purchased your iText license from? Maybe you're not using the correct iTextSharp. – Bruno Lowagie Mar 01 '16 at 15:17
  • for that fix css, it will work. But how can I assign dynamic css to XMLWorkerHelper.parseToElementList(HTML, CSS) ? So here in 'CSS' parameter, its string, So how can I assign specify dynamic css for that my Html string ? Thanks. – Herin Mar 04 '16 at 04:24
  • Hi Bruno, can you please look at my this question. I am stuck with it. Can you please help me. link :- http://stackoverflow.com/questions/35909753/need-to-give-page-break-in-inner-pdfptable-of-itextsharp – Herin Mar 14 '16 at 10:03