C# “转换为阿拉伯语”;unicode“;使用itextsharp将html或xml内容转换为pdf
我正在尝试在我的asp.net MVC3应用程序中创建报告。经过大量搜索,我发现许多博客帖子都在谈论C# “转换为阿拉伯语”;unicode“;使用itextsharp将html或xml内容转换为pdf,c#,asp.net-mvc-3,itextsharp,C#,Asp.net Mvc 3,Itextsharp,我正在尝试在我的asp.net MVC3应用程序中创建报告。经过大量搜索,我发现许多博客帖子都在谈论ITextSharp将我的Html/Razor转换为Pdf我正在尝试解析Razor视图以获得Pdf,如下所示 public void Render(ViewContext viewContext, TextWriter writer) { var doc = new Document(); // associate output with respons
ITextSharp
将我的Html/Razor
转换为Pdf
我正在尝试解析Razor视图以获得Pdf,如下所示
public void Render(ViewContext viewContext, TextWriter writer)
{
var doc = new Document();
// associate output with response stream
var pdfWriter = PdfWriter.GetInstance(doc, viewContext.HttpContext.Response.OutputStream);
pdfWriter.CloseStream = false;
viewContext.HttpContext.Response.ContentType = "application/pdf";
viewContext.HttpContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
// generate view into string
var sb = new System.Text.StringBuilder();
TextWriter tw = new System.IO.StringWriter(sb);
_result.View.Render(viewContext, tw);
var resultCache = sb.ToString();
//Path to our font
string arialuniTff = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIALUNI.TTF");
//Register the font with iTextSharp
iTextSharp.text.FontFactory.Register(arialuniTff);
//Create a new stylesheet
iTextSharp.text.html.simpleparser.StyleSheet ST = new iTextSharp.text.html.simpleparser.StyleSheet();
//Set the default body font to our registered font's internal name
ST.LoadTagStyle(HtmlTags.BODY, HtmlTags.FACE, "Arial Unicode MS");
//Set the default encoding to support Unicode characters
ST.LoadTagStyle(HtmlTags.BODY, HtmlTags.ENCODING, BaseFont.IDENTITY_H);
//Parse our HTML using the stylesheet created above
List<IElement> list = HTMLWorker.ParseToList(new StringReader(resultCache), ST);
doc.Open();
//Loop through each element, don't bother wrapping in P tags
foreach (var element in list)
{
doc.Add(element);
}
doc.Close();
pdfWriter.Close();
}
这对我来说很好,谢谢:)您需要使用支持运行方向的容器元素,例如ColumnText或PdfPCell,然后设置它们的元素。运行方向=PdfWriter.RUN\u方向\u RTL
List<IElement> list = HTMLWorker.ParseToList(new StringReader(resultCache), ST);
doc.Open();
//Use a table so that we can set the text direction
PdfPTable table = new PdfPTable(1);
//Ensure that wrapping is on, otherwise Right to Left text will not display
table.DefaultCell.NoWrap = false;
table.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
//Loop through each element, don't bother wrapping in P tags
foreach (var element in list)
{
//Create a cell and add text to it
PdfPCell text = new PdfPCell(new Phrase(element, font));
//Ensure that wrapping is on, otherwise Right to Left text will not display
text.NoWrap = false;
//Add the cell to the table
table.AddCell(text);
}
//Add the table to the document
document.Add(table);
doc.Close();
pdfWriter.Close();
List List=HTMLWorker.parsetList(新的StringReader(resultCache),ST);
doc.Open();
//使用表格以便我们可以设置文本方向
PdfPTable table=新的PdfPTable(1);
//确保“换行”处于启用状态,否则将不会显示从右向左的文本
table.DefaultCell.NoWrap=false;
table.RunDirection=PdfWriter.RUN\u DIRECTION\u RTL;
//循环遍历每个元素,不用费心用P标记包装
foreach(列表中的var元素)
{
//创建单元格并向其添加文本
PdfPCell text=新的PdfPCell(新短语(元素、字体));
//确保“换行”处于启用状态,否则将不会显示从右向左的文本
text.NoWrap=false;
//将单元格添加到表中
表.AddCell(文本);
}
//将表添加到文档中
文件。添加(表);
doc.Close();
pdfWriter.Close();
作为补充参考,请看一下。谢谢@Romulus,它工作得很好,但我做了一些更改,因为我的剃须刀就像一张桌子,我将添加更改作为问题的编辑Hello Mohamed,如果有帮助,请您也投赞成票,除非您需要任何进一步的帮助?
List<IElement> list = HTMLWorker.ParseToList(new StringReader(resultCache), ST);
doc.Open();
//Use a table so that we can set the text direction
PdfPTable table = new PdfPTable(1);
//Ensure that wrapping is on, otherwise Right to Left text will not display
table.DefaultCell.NoWrap = false;
table.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
//Loop through each element, don't bother wrapping in P tags
foreach (var element in list)
{
//Create a cell and add text to it
PdfPCell text = new PdfPCell(new Phrase(element, font));
//Ensure that wrapping is on, otherwise Right to Left text will not display
text.NoWrap = false;
//Add the cell to the table
table.AddCell(text);
}
//Add the table to the document
document.Add(table);
doc.Close();
pdfWriter.Close();