我正在使用itextsharp将html转换为pdf 我遇到的问题是任何阿拉伯语句子都喜欢 يسعىبرنامجالشيخخليفةللتميزالحكوميإلىتحقيق 导出时,每个单词中的每个字符都会像http://easycaptures.com/6222949368
一样被扰乱检查我的代码
protected void btnExporttoPdf_Click(object sender, EventArgs e)
{
int assessorId = Convert.ToInt32(Session["assessorId"]);
var ds = AssessorProvider.GetDataSetForTransformation(assessorId);
using (var sw = new StringWriter())
{
var stringReader = new StringReader(ds.GetXml());
XmlReader reader = XmlReader.Create(stringReader);
var xpathDoc = new XPathDocument(reader);
var transform = new XslCompiledTransform();
transform.Load(Server.MapPath(Settings.AssessorFormTemplatePath));
var argsList = new XsltArgumentList();
argsList.AddParam("SiteRoot", "", Settings.DomainUrl);
transform.Transform(xpathDoc, argsList, sw);
byte[] data;
using (var sr = new StringReader(sw.ToString()))
{
var st = new StyleSheet();
FontFactory.Register(Path.Combine(Server.MapPath("~/Common/Fonts/"), "ARIALUNI.TTF"));
st.LoadTagStyle(HtmlTags.BODY, HtmlTags.FACE, "Arial Unicode MS");
st.LoadTagStyle(HtmlTags.BODY, HtmlTags.ENCODING, BaseFont.IDENTITY_H);
using (var ms = new MemoryStream())
{
using (var pdfDoc = new Document(PageSize.A4, 0.5f, 0.5f, 0.5f, 0.5f))
{
using (var w = PdfWriter.GetInstance(pdfDoc, ms))
{
pdfDoc.Open();
var parsedHtmlElements = HTMLWorker.ParseToList(sr, st);
foreach (var htmlElement in parsedHtmlElements)
{
pdfDoc.Add(htmlElement as IElement);
}
pdfDoc.Close();
data = ms.ToArray();
}
}
}
}
Response.Buffer = false;
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=AssessorRegistration.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(data);
Response.End();
}
}
编辑:
我尝试过上面代码中的更改 如
foreach (var htmlElement in parsedHtmlElements)
{
if (htmlElement is PdfPTable)
{
SetDirection(htmlElement as PdfPTable);
}
pdfDoc.Add(htmlElement);
}
和我的递归方法
private static void SetDirection(PdfPTable tbl)
{
tbl.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
foreach (PdfPRow pr in tbl.Rows)
{
foreach (PdfPCell pc in pr.GetCells())
{
if (pc != null)
{
pc.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
if (pc.CompositeElements != null)
{
foreach (var element in pc.CompositeElements)
{
if (element is PdfPTable)
{
SetDirection((PdfPTable) element);
}
}
}
}
}
}
}
字体是连接的,但方向不正确。 请检查屏幕截图http://easycaptures.com/2213738973
答案 0 :(得分:1)
不是一个好的解决方案,但可以通过以下方式完成
foreach (var htmlElement in parsedHtmlElements)
{
if (htmlElement is PdfPTable)
{
SetDirection(htmlElement as PdfPTable);
}
pdfDoc.Add(htmlElement);
}
private static void SetDirection(PdfPTable tbl)
{
tbl.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
tbl.HorizontalAlignment = Element.ALIGN_LEFT;
foreach (PdfPRow pr in tbl.Rows)
{
foreach (PdfPCell pc in pr.GetCells())
{
if (pc != null)
{
pc.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
pc.HorizontalAlignment = Element.ALIGN_LEFT;
if (pc.CompositeElements != null)
{
foreach (var element in pc.CompositeElements)
{
if (element is PdfPTable)
{
SetDirection((PdfPTable)element);
}
}
}
}
}
}
}
另外在我的html更改中有一些单元格命令