Java Mailing List Archive

Home » Post all your questions about iText here »

[iText-questions] iTextSharp HTML to PDF via powershell

red 888


Author LoginPost Reply

 have been trying to translate a C# code snippet I found here to convert an html file to a PDF with itextsharp into powershell, but I'm having some issues.

This is what I have so far:


#create doc object
$Doc = New-Object iTextSharp.text.Document

#set page size

#Get html file
$Stream = [IO.File]::OpenWrite("C:\test\test.pdf")

#creat pdfwrite object
$Writer = [itextsharp.text.pdf.PdfWriter]::GetInstance($Doc, $Stream)

#open document

#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

#add line below title
#$LineSeparator = New-Object iTextSharp.text.???

#read in html file
$htmlfile = "C:\test\test.html"
$HTMLText = [IO.File]::ReadAllText($htmlfile)

#create a new generic list to hold parsed html data
$HTMLElements = New-Object 'System.Collections.Generic.List[string]'
$HTMLElements = [iTextSharp.text.html.simpleparser.HTMLWorker]::ParseToList((new-Object System.IO.StringReader($HTMLText)),$null)

The problem now is that I don't think $HTMLElements = [iTextSharp.text.html.simpleparser.HTMLWorker]::ParseToList((new-Object System.IO.StringReader($HTMLText)),$null) is doing what I want it to.

After doing this if I look at $HTMLElements.Count it returns only 2, but looking at the C# code snippet it looks like it should contain every line of the html document as I am supposed to loop through it and cast each item as an [iTextSharp.text.IElement]

Ideally I'd like to do this without having to use add-type to compile actual C# code.

Any help much appreciated!

This email is sponsored by Windows:

Build for Windows Store.
iText-questions mailing list

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference to the iText book:
Please check the keywords list before you ask for examples:
©2008 - Jax Systems, LLC, U.S.A.