我无法将HTML表格解析为PDF格式。表正在传播,它已经不在了。我该怎么办呢?
我正在使用HTMLWorker
。它将HTML转换为PDF是好的,但那个表......
这是我的代码(它在方法中):
StyleSheet ST = new StyleSheet();
ST.LoadTagStyle(HtmlTags.BODY, HtmlTags.FACE, "Arial Unicode MS");
ST.LoadTagStyle(HtmlTags.BODY, HtmlTags.ENCODING, BaseFont.IDENTITY_H);
HTMLWorker worker = new HTMLWorker(doc);
worker.SetStyleSheet(ST);
worker.StartDocument();
worker.Parse((TextReader)File.OpenText(xmlNode.ChildNodes[i].Attributes["path"].Value.ToString()));
worker.EndDocument();
worker.Close();
我怎么能告诉HTMLWorker
所以用PDF创建表是好的?我可以给工人的工人大小吗?
或者我应该使用其他“工作人员”,例如XMLWorkerHelper
或XMLWorker
。当我使用XMLWorker
或XMLWorkerHelper
时,我无法设置为“worker”我的字体(我需要西里尔语,而且只是不明白如何添加字体)。当我使用这个“工人”时,我对表有同样的问题。
代码XMLWorker
:
//working with XMLWorker
ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(true);
cssResolver.AddCss(fileCSS,true);
CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
PdfWriterPipeline pdf = new PdfWriterPipeline(doc, writer);
HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);
XMLWorker worker = new XMLWorker(css, true);
XMLParser p = new XMLParser(worker);
p.Parse(new FileStream(fileHTML, FileMode.Open, FileAccess.Read));
代码XMLWorkerHelper
:
string arialuniTff = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIALUNI.TTF");
FontFactory.Register(arialuniTff);
using (var msCss = new MemoryStream(Encoding.UTF8.GetBytes(fileCSS)))
{
using (FileStream fsHtml = new FileStream(fileHTML,FileMode.Open,FileAccess.Read))
{
iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, fsHtml, msCss, FontFactory.FontImp as IFontProvider);
}
}
你能告诉我什么? 谢谢!
更新
我发现了一个article与{Cyrillic XMLWorkerHelper
using (var msCss = new MemoryStream(Encoding.UTF8.GetBytes(fileCSS)))
{
using (FileStream fsHtml = new FileStream(fileHTMLTest,FileMode.Open,FileAccess.Read))
{
XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
fontProvider.Register(arial);
FontFactory.FontImp = fontProvider;
doc.Add(new Paragraph("XMLWorkerHelper"));
doc.Add(new Paragraph("XMLWorkerHelper вот это да!"));
iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, fsHtml, msCss, Encoding.UTF8, fontProvider);
}
}
,但我的代码不起作用。我不明白为什么。
我的代码:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type"content="text/html;charset=utf-8"></meta>
<title></title>
</head>
<body>
<p>Привет Мир!</p>
<table border="0">
<tr>
<td><h2 align="center">Акционерное общество «OOO "Ландыш"»</h2></td>
</tr>
<tr>
<td>
<p align="center">Тема:</p>
<p align="center">[THEME]</p>
</td>
</tr>
<tr>
<td>
<div align="right">
<p>Выполнил:</p>
<p>[AUTHOR]</p>
</div>
</td>
</tr>
<tr>
<td><p align="center">[CITY] [YEAR]г.</p></td>
</tr>
</table>
<table border="1">
<tr>
<td>Row 1, Column 1; Столбец 1, Строка 1 </td>
<td>Row 1, Column 2; Столбец 1, Строка 2</td>
</tr>
<tr>
<td>Row 2, Column 1; Столбец 2, Строка 1</td>
<td>Row 2, Column 2; Столбец 2, Строка 2</td>
</tr>
</table>
<img src="img.jpg" alt="picture"></img>
</body>
</html>
我的HTML代码(测试):
package com.kaizen.sqlitedatabase;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity{
Sqlite mydb;
TextView name,surName,marks;
EditText edtName,edtSurName,edtMarks;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mydb=new Sqlite(this);
name=(TextView)findViewById(R.id.name);
surName=(TextView)findViewById(R.id.surName);
marks=(TextView)findViewById(R.id.marks);
edtName=(EditText)findViewById(R.id.edt_name);
edtSurName=(EditText)findViewById(R.id.edt_surName);
edtMarks=(EditText)findViewById(R.id.edt_marks);
}
}