如何在c#中使用itextsharp将字符串(包含html标记)转换为通过短语传递的PDF格式?

如何在c#中使用itextsharp将字符串(包含html标记)转换为通过短语传递的PDF格式?,c#,html,pdf,itextsharp,C#,Html,Pdf,Itextsharp,我的输出是这样的 我想要这样的输出 我正在生成一个pdf文件。我正在传递包含html标记的字符串。但是pdf中的字符串绑定为html标记。但我需要输出作为一个设计什么在html标签。我把弦乐传给短语ph14 public string GenerateQuotePdf(int id) { Stream stream = Stream.Null; Document pdfDoc = new Document(PageSize.A4, 30F, 30F, 20F, 0F);

我的输出是这样的

我想要这样的输出

我正在生成一个pdf文件。我正在传递包含html标记的字符串。但是pdf中的字符串绑定为html标记。但我需要输出作为一个设计什么在html标签。我把弦乐传给短语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();
        }
    }
公共字符串生成器PDF(int-id)
{
Stream=Stream.Null;
文档pdfDoc=新文档(PageSize.A4、30F、30F、20F、0F);
尝试
{
stream=新文件流(filePath,FileMode.Create);
PdfWriter PdfWriter=PdfWriter.GetInstance(pdfDoc,stream);
pdfDoc.Open();
弦乐="条款和条件*\t价格为AED
*\t托尔信用卡交易需支付3.25%的手续费
*\t如果根据客户要求进行活动生产,则整个账单的50%将在开始生产前到期,余额将在交付时到期。
*\t托尔家具将在a+条件下交付如果设备损坏,承租人应承担将设备恢复至租赁期开始时的状态所需的所有修理费用。
*\tEquipment应仅用于指定用途和指定位置。“; PDFPTable14=新的PdfPTable(1); 表14.1所示百分比=99; table14.DefaultCell.Border=0; 表14.DefaultCell.VerticalAlignment=Element.ALIGN\u MIDDLE; 短语ph14=新短语(注释,textFont6); PdfPCell cell14=新的PdfPCell(ph14); cell14.VerticalAlignment=Element.ALIGN_CENTER; 单元格14.边框=0; cell14.HorizontalAlignment=Element.ALIGN_CENTER; 单元格14.PaddingLeft=10F; 单元格14.PaddingRight=4F; 单元14.PaddingTop=4F; 单元14.PaddingBottom=6F; 表14.AddCell(cell14); PdfPTable mainTable=新的PdfPTable(1); mainTable.WidthPercentage=100; PdfPCell mainCell=新的PdfPCell(); mainCell.PaddingTop=0F; mainCell.PaddingRight=10F; mainCell.PaddingBottom=25F; mainCell.PaddingLeft=10F; mainCell.Border=0; 主单元格.附录(表14); mainTable.AddCell(mainCell); pdfDoc.Add(主表); } 抓住 { } 最后 { pdfDoc.Close(); stream.Close(); stream.Dispose(); } }
通常,我使用此方法手动将HTML标记转换为纯文本

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;
}
public静态字符串转换HTML纯文本(字符串文本)
{
text=HttpUtility.HtmlDecode(text);
text=文本。替换(“
”,“\n”); text=文本。替换(“
”,“\n”); text=text.Replace(“
”,即“\n”); text=text.Replace(“,”\t”); text=text.Replace(“,”); text=ReplaceAnchorTags(text); 返回文本; }
我已将您的HTML复制到一个名为:

结果是一个PDF,如下所示:

如果您尊重自己作为一名开发人员(我认为您是这样做的),那么您不应该对这个结果感到满意:列表不是一个真实的列表。您可以通过将其设置为一个真实的列表来解决此问题,就像我在文件中所做的那样:

这段代码与我们之前的代码有什么不同?没有什么!只有HTML不同,生成的PDF如下所示:

这已经更好了,但是在代码中,您有更多的间距,并且还将列表添加到
PdfPTable
。这就是我在示例中第二次添加相同HTML的原因:

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);
你看到区别了吗?我正在使用CSS定义列表符号,并为每个列表项定义一个填充。我没有将HTML直接呈现到
文档和
PdfWriter
,而是将HTML和CSS解析到元素列表中。然后将每个元素添加到
PdfPCell

结果如下所示:


这看起来很不错,不是吗?有关使用iText(夏普)的更多信息还有XML Worker,看看://

你的代码看起来脏兮兮的。例如:很明显,你想呈现一个列表,但你没有使用
标记,而是使用
*
和普通的
跨度。你也没有使用你的常识:你只是将HTML倒入一个
段落
中,并期望HTML能够显示出来被解析。如果发生这种情况,这将是iTextSharp中的一个巨大错误。你永远无法在PDF文档中显示原始HTML代码。如果你想将HTML呈现为PDF,你需要。仔细看看你的HTML,我还看到了
class=“Apple tab span“
,但我没有看到任何定义苹果标签span
的CSS。现在还不清楚为什么要使用HTML(以及为什么HTML如此糟糕)。还不清楚为什么要引入
PdfPTable
。如果你想得到答案,你应该改进你的问题。
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();
}
<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>
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();
}
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);