[System.Reflection.Assembly]::LoadFrom("C:\path\to\itextsharp.dll")
#by default fonts are NOT embedded
#[iTextSharp.text.FontFactory]::DefaultEmbedding -eq false
$doc = New-Object iTextSharp.text.Document
$fileStream = New-Object IO.FileStream("C:\foo\test\allFonts2.pdf", [System.IO.FileMode]::Create)
[iTextSharp.text.pdf.PdfWriter]::GetInstance($doc, $filestream)
#iTextSharp provides a class to work with fonts, but first we have to register them:
[iTextSharp.text.FontFactory]::RegisterDirectories()
#Phrase is the smallest bit of text that will understand a newline if needed. it is a chunk[] and paragraph is a phrase[]
$phrase = new-object iTextSharp.text.Phrase
$paragraph = New-Object iTextSharp.text.Paragraph
#Different fonts for mapping tests
$fN = [iTextSharp.text.Font]::NORMAL
$fB = [iTextSharp.text.Font]::BOLD
$fI = [iTextSharp.text.Font]::ITALIC
$fBI = [iTextSharp.text.Font]::BOLDITALIC
#Sample string
$string = "The quick brown fox jumps over the lazy dog 1234567890 ?.,:;!@#$%^&*()`"'`n"
#something I know maps nicely (I love fixed width fonts!)
$consolas = [iTextSharp.text.FontFactory]::GetFont("consolas", 9)
#create all the nesting needed.
[iTextSharp.text.FontFactory]::RegisteredFamilies | %{
$chunk = new-object iTextSharp.text.Chunk("`n$_`n", $consolas)
$phrase.Add($chunk) | out-null
$a = [iTextSharp.text.FontFactory]::GetFont($_, 9, $fN)
"adding font: $_"
$chunk = new-object iTextSharp.text.Chunk($string, $a)
$phrase.Add($chunk) | out-null
$b = [iTextSharp.text.FontFactory]::GetFont($_, 9, $fB)
$chunk = new-object iTextSharp.text.Chunk($string, $b)
$phrase.Add($chunk) | out-null
$c = [iTextSharp.text.FontFactory]::GetFont($_, 9, $fI)
$chunk = new-object iTextSharp.text.Chunk($string, $c)
$phrase.Add($chunk) | out-null
$d = [iTextSharp.text.FontFactory]::GetFont($_, 9, $fBI)
$chunk = new-object iTextSharp.text.Chunk($string, $d)
$phrase.Add($chunk) | out-null
}
$paragraph.add($phrase) | out-null
$doc.Open()
$doc.add($paragraph) | out-null
$doc.close()
1What are you currently using to create these PDFs? PDFs are basically meant to be printed electronic documents and more or less immutable. Also, what are you reading them with that you don't want the fonts actually printed onto the document? What are you using them for? – Austin T French – 2013-06-15T01:02:41.930
I am testing conversion to other image formats (e.g. tiff), and need to test scenarios where the fonts were NOT embedded to be sure they are mapped to something that doesn't throw off alignment and spacing. It's easy enough to generate a word doc that includes all installed system fonts, but I want to save/export as a pdf without the fonts being embedded. – slipsec – 2013-06-17T12:45:51.030