[英]How to Convert an Html to a PDF file and save it Using iTextSharp in C#
[英]How to Convert string (contains html tag) to PDF format passing through Phrase using itextsharp in c#?
我正在生成一個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();
}
}
我已將您的HTML復制到名為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>
該HTML被認為是“臟”的,因為您要呈現一個列表,但是您沒有使用正確的標簽來這樣做。
我已經使用List_Dirty示例將此HTML解析為PDF:
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();
}
結果是一個如下所示的PDF:
如果您尊重自己作為開發人員的能力(我假設您是這樣做的),那么您對這個結果將不滿意:列表不是真實的列表。 您可以像在文件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>
我已經使用List_Clean示例將此HTML解析為PDF:
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();
}
此代碼與我們之前的代碼有何不同? 沒有! 只有HTML與眾不同,導致PDF看起來像這樣:
那已經更好了,但是在您的代碼中,您有更多的間距,並且還將列表添加到PdfPTable
。 這就是為什么我在List_Clean示例中第二次添加相同的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呈現給Document
和PdfWriter
,而是將HTML和CSS解析為元素列表。 然后,我將每個元素添加到PdfPCell
。
結果看起來像這樣:
這看起來很不錯,不是嗎? 有關使用iText(Sharp)和XML Worker的更多信息,請查看官方文檔 : XML Worker示例 / XML Worker FAQ
通常,我使用此方法將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(" ", "\t");
text = text.Replace(" ", " ");
text = ReplaceAnchorTags(text);
return text;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.