C# 为什么将非英语文本导出为pdf时会显示css标记?
我正在使用itextsharp将我的乌尔都语文本加载到pdf文件,它可以工作,但它也可以传输css标记。我正在从数据库nvarchar列加载一个编码文本并对其进行解码,但它仍然显示css标记。为什么? 从db加载到html变量的字符串C# 为什么将非英语文本导出为pdf时会显示css标记?,c#,asp.net,c#-4.0,webforms,pdf-generation,C#,Asp.net,C# 4.0,Webforms,Pdf Generation,我正在使用itextsharp将我的乌尔都语文本加载到pdf文件,它可以工作,但它也可以传输css标记。我正在从数据库nvarchar列加载一个编码文本并对其进行解码,但它仍然显示css标记。为什么? 从db加载到html变量的字符串 <p> 1. <span dir="RTL">میں اس ادارے کی پالیسیوں کی خلاف ورزی نہیں کرے گا</span>.</p> <p> 2. &l
<p>
1. <span dir="RTL">میں اس ادارے کی پالیسیوں کی خلاف ورزی نہیں کرے گا</span>.</p>
<p>
2. <span dir="RTL">میرے فرائض نشان تک ہو گا</span>.</p>
<p>
3. <span dir="RTL">کوئی خلاف ورزی ادنڈت جانا چاہیئے</span></p>
<p>
4. <span dir="RTL">میں نے اپنے ملک کے لئے اپنی جان دیتا ہوں گے</span>.</p>
<p>
5. <span dir="RTL">میں ایک غیر معمولی انداز میں رد عمل کا اظہار تو میری زبان معاف کرنا</span>.</p>
<p>
6. <span dir="RTL">میں ایسا میرا سب کچھ میرا ادارے اور ملک کی خاطر ہے</span>.</p>
<p>
7. <span dir="RTL">پاکستان ایک خود مختار ملک ہے اور رہے گی</span>.</p>
<p>
8. <span dir="RTL">میں مکمل اپنے فرائض کو بھر دوں گا</span></p>
<p>
9. <span dir="RTL">میں پکڑے گئے تو؛ میں نے آپ کو نہیں پتہ</span>.</p>
<p>
10. <span dir="RTL">تمام ہے</span>.</p>
1.ۑ
2.م
3.(小标题)(小标题)(小标题)(小标题)(小标题)(小标题)(小标题)(小标题)(小标题)
4.م
5.在2014 5 5月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日نا
6.第五章:我国农村劳动力市场的发展现状
7.پ
8.م
9م
10تااہے
代码:
Document Document=新文档();
尝试
{
MemoryStream msOutput=新的MemoryStream();
PdfWriter writer=PdfWriter.GetInstance(文档,msOutput);
document.Open();
string fontpath=Environment.GetEnvironmentVariable(“SystemRoot”)+“\\fonts\\arial.ttf”;
BaseFont BaseFont=BaseFont.CreateFont(fontpath,BaseFont.IDENTITY_H,true);
iTextSharp.text.Font arabicFont=新的iTextSharp.text.Font(basefont,24,iTextSharp.text.Font.NORMAL,iTextSharp.text.Color.BLUE);
var el=新块();
iTextSharp.text.Font f2=新的iTextSharp.text.Font(basefont,el.Font.Size,
el.Font.Style、el.Font.Color);
el.Font=f2;
PdfPTable table=新的PdfPTable(1);
table.RunDirection=PdfWriter.RUN\u DIRECTION\u RTL;
var str=Server.HtmlDecode(html);//html是从db加载的字符串
str=str.Replace(“,”);
PdfPCell=新的PdfPCell(新短语(10,str,el.Font));
单元格边框=0;
表2.AddCell(cell);
文件。添加(表);
document.Close();
MemoryStream memStream=msOutput;//返回内存流对象
字节[]ByteInstream=memStream.ToArray();
Response.Clear();
字符串FileName=“TermsAndConditionsUrdu.pdf”;
FileName=FileName.Replace(“,”,“”);
Response.ContentType=“application/pdf”;
Response.Headers.Remove(“内容处置”);
AddHeader(“内容处置”、“附件;文件名=\”“+文件名+”\”);
响应。二进制写入(字节流内);
Response.End();
Response.Flush();
}
catch(DocumentException de)
{
//this.Message=de.Message;
}
捕获(ioe异常ioe)
{
//this.Message=ioe.Message;
}
//步骤5:我们关闭文档
document.Close();
因为str.Replace(“,”)
不是一种从HTML中提取文本的明智方法…@Lucastzesniewski我确实想用空格替换所有HTML标记,但我知道这不是一种方法,每个文件中都有不同的HTML标记,所以不可能全部替换,这就是我来这里寻找解决方案的原因。帮助。永远不要在html上使用正则表达式或字符串替换,请始终解析它。尝试使用html Agility Pack而不是正则表达式,因为str.Replace(“,”)
不是一种从HTML中提取文本的明智方法…@Lucastzesniewski我想用空格替换所有HTML标记,但我知道不是这样,每个文件中都有不同的HTML标记,所以不可能全部替换,这就是我来这里寻找解决方案的原因。帮助。永远不要在HTML上使用正则表达式或字符串替换尝试使用Html Agility Pack而不是正则表达式
Document document = new Document();
try
{
MemoryStream msOutput = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(document, msOutput);
document.Open();
string fontpath = Environment.GetEnvironmentVariable("SystemRoot") + "\\fonts\\arial.ttf";
BaseFont basefont = BaseFont.CreateFont(fontpath, BaseFont.IDENTITY_H, true);
iTextSharp.text.Font arabicFont = new iTextSharp.text.Font(basefont, 24, iTextSharp.text.Font.NORMAL, iTextSharp.text.Color.BLUE);
var el = new Chunk();
iTextSharp.text.Font f2 = new iTextSharp.text.Font(basefont, el.Font.Size,
el.Font.Style, el.Font.Color);
el.Font = f2;
PdfPTable table = new PdfPTable(1);
table.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
var str = Server.HtmlDecode(html); //html is a string loaded from db
str = str.Replace("<span dir=", "").Replace("RTL", "").Replace('"', ' ').Replace("</span>", "").Replace("</p>", "").Replace("<p>", "").Replace('>', ' ');
PdfPCell cell = new PdfPCell(new Phrase(10, str, el.Font));
cell.Border = 0;
table.AddCell(cell);
document.Add(table);
document.Close();
MemoryStream memStream = msOutput; // returns a memory stream object
byte[] bytesInStream = memStream.ToArray();
Response.Clear();
string FileName = "TermsAndConditionsUrdu.pdf";
FileName = FileName.Replace(",", "");
Response.ContentType = "application/pdf";
Response.Headers.Remove("content-disposition");
Response.AddHeader("content-disposition", "attachment; filename=\"" + FileName + "\"");
Response.BinaryWrite(bytesInStream);
Response.End();
Response.Flush();
}
catch (DocumentException de)
{
// this.Message = de.Message;
}
catch (IOException ioe)
{
// this.Message = ioe.Message;
}
// step 5: we close the document
document.Close();