简体   繁体   中英

How to Convert string (contains html tag) to PDF format passing through Phrase using itextsharp in c#?

My output like this 在此处输入图片说明

i want outputlike this 在此处输入图片说明

I am generating a pdf file. i am passing string contains html tags. but the string binding in pdf as html tags. but i need output as a design what in th html tags. I am passing string notes to phrase ph14...

public string GenerateQuotePdf(int id)
{
    Stream stream = Stream.Null;
    Document pdfDoc = new Document(PageSize.A4, 30F, 30F, 20F, 0F);
    try
        {
            stream = new FileStream(filePath, FileMode.Create);
            PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, stream);
            pdfDoc.Open();

            string notes ="<div style=\"text-align: center\"><b><span style=\"font-size: large\">Terms and Conditions</span></b></div><div>*<span class=\"Apple-tab-span\" style=\"white-space: pre\">\t</span>Prices are in AED</div><div><br /></div><div>*<span class=\"Apple-tab-span\" style=\"white-space: pre\">\t</span>All Credit Card transactions are subject to a 3.25% processing fee</div><div><br /></div><div>*<span class=\"Apple-tab-span\" style=\"white-space: pre\">\t</span>In the event production is required per customer request, 50% of the entire bill will be due prior to start of production, and the <span class=\"Apple-tab-span\" style=\"white-space: pre\">\t</span>balance due upon delivery.</div><div><br /></div><div>*<span class=\"Apple-tab-span\" style=\"white-space: pre\">\t</span>All furniture will be delivered in A+ condition. In the event that the equipment is damaged, the renter shall be liable for all <span class=\"Apple-tab-span\" style=\"white-space: pre\">\t</span>repair costs to restore the equipment to its state at the beginning of the rental period.</div><div><br /></div><div>*<span class=\"Apple-tab-span\" style=\"white-space: pre\">\t</span>Equipment shall be utilized for the stated purpose and at the stated location only.</div>";

            PdfPTable table14 = new PdfPTable(1);
            table14.WidthPercentage = 99;
            table14.DefaultCell.Border = 0;
            table14.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;
            Phrase ph14 = new Phrase(notes, textFont6);
            PdfPCell cell14 = new PdfPCell(ph14);
            cell14.VerticalAlignment = Element.ALIGN_CENTER;
            cell14.Border = 0;
            cell14.HorizontalAlignment = Element.ALIGN_CENTER;
            cell14.PaddingLeft = 10F;
            cell14.PaddingRight = 4F;
            cell14.PaddingTop = 4F;
            cell14.PaddingBottom = 6F;
            table14.AddCell(cell14);

            PdfPTable mainTable = new PdfPTable(1);
            mainTable.WidthPercentage = 100;
            PdfPCell mainCell = new PdfPCell();
            mainCell.PaddingTop = 0F;
            mainCell.PaddingRight = 10F;
            mainCell.PaddingBottom = 25F;
            mainCell.PaddingLeft = 10F;
            mainCell.Border = 0;
            mainCell.AddElement(table14);
            mainTable.AddCell(mainCell);
            pdfDoc.Add(mainTable);
        }
        catch
        {
        }
        finally
        {
            pdfDoc.Close();
            stream.Close();
            stream.Dispose();
        }
    }

I have copied your HTML to a file named list_dirty.html :

<div style="text-align: center"><b><span style="font-size: large">Terms and Conditions</span></b></div>
<div>*<span class="Apple-tab-span" style="white-space: pre"> </span>Prices are in AED</div>
<div><br /></div>
<div>*<span class="Apple-tab-span" style="white-space: pre"> </span>All Credit Card transactions are subject to a 3.25% processing fee</div>
<div><br /></div>
<div>*<span class="Apple-tab-span" style="white-space: pre"> </span>In the event production is required per customer request, 50% of the entire bill will be due prior to start of production, and the <span class="Apple-tab-span" style="white-space: pre"> </span>balance due upon delivery.</div>
<div><br /></div>
<div>*<span class="Apple-tab-span" style="white-space: pre"> </span>All furniture will be delivered in A+ condition. In the event that the equipment is damaged, the renter shall be liable for all <span class="Apple-tab-span" style="white-space: pre"> </span>repair costs to restore the equipment to its state at the beginning of the rental period.</div>
<div><br /></div>
<div>*<span class="Apple-tab-span" style="white-space: pre"> </span>Equipment shall be utilized for the stated purpose and at the stated location only.</div>

This HTML is considered "dirty" because you want to render a list, but you are not using the right tags to do so.

I have parsed this HTML to PDF using the List_Dirty example:

public void createPdf(String file) throws IOException, DocumentException {
    // step 1
    Document document = new Document();
    // step 2
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
    writer.setInitialLeading(12);
    // step 3
    document.open();
    // step 4
    XMLWorkerHelper.getInstance().parseXHtml(writer, document,
            new FileInputStream(HTML));
    // step 5
    document.close();
}

The result is a PDF that looks like this:

在此处输入图片说明

If you respect yourself as a developer (which I assume you do), you should not be happy with this result: the list isn't a real list. You can fix this by making it a real list as I have done in the file list_clean.html :

<div style="text-align: center"><b><span style="font-size: large">Terms and Conditions</span></b></div>
<ul>
<li>Prices are in AED</li>
<li>All Credit Card transactions are subject to a 3.25% processing fee</li>
<li>In the event production is required per customer request, 50% of the entire bill will be due prior to start of production, and the balance due upon delivery.</li>
<li>All furniture will be delivered in A+ condition. In the event that the equipment is damaged, the renter shall be liable for all repair costs to restore the equipment to its state at the beginning of the rental period.</li>
<li>Equipment shall be utilized for the stated purpose and at the stated location only.</li>
</ul>

I have parsed this HTML to PDF using the List_Clean example:

public void createPdf(String file) throws IOException, DocumentException {
    // step 1
    Document document = new Document();
    // step 2
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
    writer.setInitialLeading(12);
    // step 3
    document.open();
    // step 4
    XMLWorkerHelper.getInstance().parseXHtml(writer, document,
            new FileInputStream(HTML)); 
    // some code that will be explained later
    // step 5
    document.close();
}

What is different in this code from what we had before? Nothing! Only the HTML is different resulting in a PDF that looks like this:

在此处输入图片说明

That's already better, but in your code, you have more spacing and you also add the list to a PdfPTable . That's why I added the same HTML a second time in the List_Clean example:

String html = Utilities.readFileToString(HTML);
String css = "ul { list-style: disc } li { padding: 10px }";
PdfPTable table = new PdfPTable(1);
table.setSpacingBefore(20);
PdfPCell cell = new PdfPCell();
for (Element e : XMLWorkerHelper.parseToElementList(html, css)) {
    cell.addElement(e);
}
table.addCell(cell);
document.add(table);

Do you see the difference? I am using CSS to define the list symbol and I am defining a padding for each list item. Instead of rendering the HTML straight to the Document and the PdfWriter , I parse the HTML and the CSS to a list of elements. I then add each element to a PdfPCell .

The result looks like this:

在此处输入图片说明

This looks quite nice, doesn't it? For more info about using iText(Sharp) and XML Worker, take a look at the official documentation : XML Worker examples / XML Worker FAQ

Usually, I convert HTML tags to plain text manually using this method

public static string ConvertHtmlToPlainText(string text)
{
text = HttpUtility.HtmlDecode(text);

text = text.Replace("<br>", "\n");
text = text.Replace("<br >", "\n");
text = text.Replace("<br />", "\n");
text = text.Replace("&nbsp;&nbsp;", "\t");


text = text.Replace("&nbsp;&nbsp;", "  ");

text = ReplaceAnchorTags(text);

return text;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM