- 追加された行はこの色です。
- 削除された行はこの色です。
* HTMLからPDFファイルを作成 [#f9d79fe1]
- 日本語を使うにはフォントを指定する。
- フォントを指定するにはFontProviderを作る。
static void Main(string[] args)
{
Font fnt = new Font(BaseFont.CreateFont(@"c:\windows\fonts\msgothic.ttc,0", BaseFont.IDENTITY_H, true), 40);
Dictionary< string, object > dicProvider = new Dictionary<string, object>();
dicProvider.Add(HTMLWorker.FONT_PROVIDER, new NewFontProvider());
string html = "<html><body><h1>Test PDF</h1><ul><li>あいうえお</li><li>xyz</li></ul></body></html>";
string outfilepath = @"c:\tmp\test-html.pdf";
FileStream fsoutput = new FileStream(outfilepath, FileMode.Create);
TextReader reader = new StringReader(html);
// step 1: creation of a document-object
Document document = new Document(PageSize.A4, 30, 30, 30, 30);
// step 2:
// we create a writer that listens to the document
// and directs a XML-stream to a file
PdfWriter writer = PdfWriter.GetInstance(document, fsoutput);
// step 3: we create a worker parse the document
HTMLWorker worker = new HTMLWorker(document);
worker.SetProviders(dicProvider);
// step 4: we open document and start the worker on the document
document.Open();
worker.StartDocument();
// step 5: parse the html into the document
worker.Parse(reader);
// step 6: close the document and the worker
worker.EndDocument();
worker.Close();
document.Close();
Console.ReadLine();
}
public class NewFontProvider : FontFactoryImp
{
public override Font GetFont(string fontname, string encoding, bool embedded, float size, int style, BaseColor color, bool cached)
{
if (string.IsNullOrEmpty(fontname)) {
fontname = "c:\\windows\\fonts\\msmincho.ttc,0";
encoding = BaseFont.IDENTITY_H;
embedded = BaseFont.EMBEDDED;
}
return base.GetFont(fontname, encoding, embedded, size, style, color, cached);
}
}
** 参考 [#ye888b31]
- 日本語フォントについて
-- http://catblogger.seesaa.net/article/217675010.html
-- http://www.mosa-architect.com/wp/2011/08/itext-htmlworker%e3%81%a7html%e3%81%ab%e5%90%ab%e3%81%be%e3%82%8c%e3%82%8b%e6%97%a5%e6%9c%ac%e8%aa%9e%e3%82%82pdf%e3%81%ab%e5%87%ba%e5%8a%9b%e3%81%99%e3%82%8b/
- http://codezine.jp/article/detail/462
- http://www.vector.co.jp/soft/winnt/writing/se462217.html
- http://catblogger.seesaa.net/article/217675010.html
- http://www.mosa-architect.com/wp/2011/08/itext-htmlworker%e3%81%a7html%e3%81%ab%e5%90%ab%e3%81%be%e3%82%8c%e3%82%8b%e6%97%a5%e6%9c%ac%e8%aa%9e%e3%82%82pdf%e3%81%ab%e5%87%ba%e5%8a%9b%e3%81%99%e3%82%8b/
- http://jehupc.exblog.jp/12731848/