Добрый день. Появилась проблема с кириллицей при сохранении в PDF с помощью библиотеки iText. Пытаюсь сохранить в pdf html. Не прописываются кириллические символы. В нете куча примеров как это починить, но они у меня почему то не работают.
Все что не через HTMLWorker сохраняется кириллицей. Что парсится через него нет. Версия itextsharp.dll по свойствам файла 10.0
Вот мой код:
Код:
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.html;
using iTextSharp.text.pdf;
using iTextSharp.text.xml;
namespace TestSaveToPDF
{
class Program
{
private const int HorizontalMargin = 40;
private const int VerticalMargin = 40;
private const string htmlText = "<html><body>Уважаемый(ая) % user %" +
"<br/><br/>" +
"База знаний системы IntraService пополнилась новыми документами:<br/><br/>" +
"<a href = '%baseurl%kb.ivp/tree?documentId=83' > 1.Импорт пользователей(Администратор)</a><br/>" +
"</body></html>";
private const string cyrilicText = "Уважаемый(ая)";
static void Main(string[] args)
{
StringBuilder sBuff = new StringBuilder("<html>");
sBuff.Append("<body>Уважаемый(ая)");
sBuff.Append("Привет!");
sBuff.Append("");
sBuff.Append("1.Импорт пользователей(Администратор)");
sBuff.Append("</body></html>");
using (var saveFile = new FileStream("res.pdf", FileMode.Create))
{
using (var pdfDocument = new Document(PageSize.A4, HorizontalMargin, HorizontalMargin, VerticalMargin, VerticalMargin))
{
PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDocument, saveFile);
pdfWriter.CloseStream = false;
pdfWriter.PageEvent = new PrintHeaderFooter { Title = "Название документа" };
pdfDocument.Open();
string ttf = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIAL.TTF");
var baseFont = BaseFont.CreateFont(ttf, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
var font = new Font(baseFont, iTextSharp.text.Font.DEFAULTSIZE, iTextSharp.text.Font.NORMAL);
pdfDocument.Add(new Paragraph("Пример 1", font));
//pdfDocument.NewPage(); // new page
pdfDocument.Add(new Paragraph("Пример 2", font));
using (var htmlViewReader = new StringReader(sBuff.ToString()))
{
using (var htmlWorker = new HTMLWorker(pdfDocument))
{
var ST = new StyleSheet();
ST.LoadTagStyle(HtmlTags.BODY, HtmlTags.FACE, "Arial Unicode MS");
ST.LoadTagStyle(HtmlTags.BODY, HtmlTags.ENCODING, BaseFont.IDENTITY_H);
htmlWorker.SetStyleSheet(ST);
htmlWorker.Parse(htmlViewReader);
}
}
}
}
}
}
}
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.html;
namespace TestSaveToPDF
{
public class PrintHeaderFooter : PdfPageEventHelper
{
private PdfContentByte pdfContent;
private PdfTemplate pageNumberTemplate;
private BaseFont baseFont;
private DateTime printTime;
public string Title { get; set; }
public override void OnOpenDocument(PdfWriter writer, Document document)
{
printTime = DateTime.Now;
string ttf = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIAL.TTF");
var font = new Font(baseFont, iTextSharp.text.Font.DEFAULTSIZE, iTextSharp.text.Font.NORMAL);
baseFont = BaseFont.CreateFont(ttf, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
pdfContent = writer.DirectContent;
pageNumberTemplate = pdfContent.CreateTemplate(50, 50);
}
public override void OnStartPage(PdfWriter writer, Document document)
{
base.OnStartPage(writer, document);
Rectangle pageSize = document.PageSize;
if (Title != string.Empty)
{
pdfContent.BeginText();
pdfContent.SetFontAndSize(baseFont, 11);
pdfContent.SetRGBColorFill(0, 0, 0);
pdfContent.SetTextMatrix(pageSize.GetLeft(40), pageSize.GetTop(40));
pdfContent.ShowText(Title);
pdfContent.EndText();
}
}
public override void OnEndPage(PdfWriter writer, Document document)
{
base.OnEndPage(writer, document);
int pageN = writer.PageNumber;
string text = pageN + " - ";
float len = baseFont.GetWidthPoint(text, 8);
Rectangle pageSize = document.PageSize;
pdfContent = writer.DirectContent;
pdfContent.SetRGBColorFill(100, 100, 100);
pdfContent.BeginText();
pdfContent.SetFontAndSize(baseFont, 8);
pdfContent.SetTextMatrix(pageSize.Width / 2, pageSize.GetBottom(30));
pdfContent.ShowText(text);
pdfContent.EndText();
pdfContent.AddTemplate(pageNumberTemplate, (pageSize.Width / 2) + len, pageSize.GetBottom(30));
pdfContent.BeginText();
pdfContent.SetFontAndSize(baseFont, 8);
pdfContent.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, printTime.ToString(), pageSize.GetRight(40), pageSize.GetBottom(30), 0);
pdfContent.EndText();
}
public override void OnCloseDocument(PdfWriter writer, Document document)
{
base.OnCloseDocument(writer, document);
pageNumberTemplate.BeginText();
pageNumberTemplate.SetFontAndSize(baseFont, 8);
pageNumberTemplate.SetTextMatrix(0, 0);
pageNumberTemplate.ShowText(string.Empty + (writer.PageNumber - 1));
pageNumberTemplate.EndText();
}
}
}
Может что подскажите, или хотя бы проверите что у Вас работает этот код.