Java Mailing List Archive

http://www.junlu.com/

Home » Post all your questions about iText here »

[iText-questions] iTextSharp HTML to PDF via powershell

red 888

2013-06-21


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:

[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.pdf")

#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.???

#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 SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
iText-questions mailing list
iText-questions@(protected)
https://lists.sourceforge.net/lists/listinfo/itext-questions

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: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php
©2008 junlu.com - Jax Systems, LLC, U.S.A.