I'm trying to use the iTextSharp library to convert an html doc to a pdf and found a useful C# snippet here itextsharp-to-rescue-converting-html:
try
{
// set the file name
string file = "C:/pdf/MyPdf.pdf";
// create a pdf document
Document document = new Document();
// set the page size, set the orientation
document.SetPageSize(PageSize.A4);
// create a writer instance
PdfWriter pdfWriter = PdfWriter.GetInstance(document, new FileStream(file, FileMode.Create));
// open the document
document.Open();
// define fonts
Font titleFont = FontFactory.GetFont("Helvetica", 14, Font.BOLD, new iTextSharp.text.BaseColor(0, 179, 212));
Font textFont = FontFactory.GetFont("Helvetica", 9, Font.NORMAL, BaseColor.BLACK);
// create a paragraph and add it to the document
Paragraph title = new Paragraph("This PDF-document was created using iTextSharp!");
title.Font = titleFont;
title.IndentationLeft = 50f;
document.Add(title);
// add line below title
LineSeparator line = new LineSeparator(1f, 100f, BaseColor.BLACK, Element.ALIGN_CENTER, -1);
document.Add(new Chunk(line));
// html pagina inlezen
string htmlText = File.ReadAllText(Server.MapPath("~/template.html"));
// html pagina parsen in een arraylist van IElements
List< IElement > htmlElements = HTMLWorker.ParseToList(new StringReader(htmlText), null);
// add the IElements to the document
for (int i = 0; i < htmlElements.Count; i++)
{
// cast the element
IElement htmlElement = ((IElement)htmlElements[i]);
document.Add(htmlElement);
}
// close the document
document.Close();
// open the pdf document
Process.Start(file);
}
catch (Exception ex)
{
lblError.Text = "An error ocurred, the PDF-document could not be created.
Exception: " + ex.Message;
}The trouble is that I'm way not a programmer and there's a lot here I can't figure out.
I don't know where LineSeparator is. its not iTextSharp.text.LineSeparator. I don't know how to access it.
I'm not asking for someone to write my script for me, but maybe point me in the right direction?
So far I have this:
[System.Reflection.Assembly]::LoadFrom("C:\itextsharp\itextsharp.dll")
#create doc object
$Doc = New-Object iTextSharp.text.Document
#set page size
$Doc.SetPageSize([iTextSharp.text.PageSize]::A4)
#Get html file
$Stream = [IO.File]::OpenWrite("C:\test\test.html")
#creat pdfwrite object
$Writer = [itextsharp.text.pdf.PdfWriter]::GetInstance($Doc, $Stream)
#open document
$Doc.Open()
#Define fonts
$TitleFont = [iTextSharp.text.FontFactory]::GetFont("Helvetica", 14, [iTextSharp.text.Font]::BOLD, (New-Object iTextSharp.text.BaseColor -ArgumentList @(0, 179, 212)))
$TextFont = [iTextSharp.text.FontFactory]::GetFont("Helvetica", 9, [iTextSharp.text.Font]::NORMAL, [iTextSharp.text.BaseColor]::BLACK)
#create a paragraph and add it to the document
$Title = New-Object iTextSharp.text.Paragraph -ArgumentList @(,"This is the title?")
$Title.Font = $TitleFont
$Title.IndentationLeft = 0x50f
#Add title paragraph to doc object
$doc.Add($Title)
#add line below title
$LineSeparator = New-Object iTextSharp.text.???