Вот мой метод парсинга HTML-файла и создания из него архивного документа PDF / A, также с помощью встраивания шрифтов с использованием таблицы стилей (чтобы избежать ошибки: «Все шрифты должны быть встроены. т: Helvetica ")
Надеюсь, это кому-нибудь поможет ...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text.pdf;
using iTextSharp.text;
using System.IO;
using iTextSharp.text.html.simpleparser;
namespace SaveAsPDF
{
class HtmlPdfConverter
{
public void RendererWebForm2PDFArchive(string fileName)
{
Console.WriteLine("Parsing HTML " + fileName);
Document document = new Document(PageSize.A4);
try
{
// we create a writer that listens to the document and directs a XML-stream to a file
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(fileName + ".pdf", FileMode.Create));
//set document as arhive
writer.PDFXConformance = PdfWriter.PDFA1A;
document.Open();
//apply stylesheet to change font (and embedd it)
StyleSheet styles = new StyleSheet();
FontFactory.Register("c:\\windows\\fonts\\verdana.ttf");
styles.LoadTagStyle("body", "face", "Verdana");
//prepare html
StreamReader sr = new StreamReader(fileName, Encoding.Default);
string html = sr.ReadToEnd();
html = RemoveTag(html, "<title>", "</title>");
//convert string to stream
byte[] byteArray = Encoding.UTF8.GetBytes(html);
MemoryStream ms = new MemoryStream(byteArray);
//parse html
HTMLWorker htmlWorker = new HTMLWorker(document);
System.Collections.Generic.List<IElement> elements;
elements = HTMLWorker.ParseToList(new StreamReader(ms), styles);
foreach (IElement item in elements)
{
document.Add(item);
}
writer.CreateXmpMetadata();
document.Close();
Console.WriteLine("Done");
}
catch (Exception e)
{
Console.Error.WriteLine(e.Message);
Console.Error.WriteLine(e.StackTrace);
}
}**strong text**