Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, September 29, 2011 7:42 PM
I am trying to create a script that will convert text documents in a defined directory to PDF files. I am able to make the conversion but the formatting just does not translate. The entire text document gets printed to one line in the PDF. This is being caused by the chunk command. iTextSharp also has phrase and paragraph commands for formatting but I cannot get them to work. This is what I have so far.
[System.Reflection.Assembly]::LoadFrom("C:\itextsharp\itextsharp.dll")
Get-ChildItem "C:\PDF\" *.txt -R | ForEach-Object {
$object = Get-Content $_.FullName
$Doc = New-Object iTextSharp.text.Document
[void][iTextSharp.text.pdf.PdfWriter]::GetInstance($Doc, [System.IO.File]::Create("C:\PDF\" + $_.BaseName + ".pdf") )
$Doc.Open()
$Chunk = New-Object iTextSharp.text.Chunk
[void]$Chunk.Append($object)
$Doc.Add($Chunk)
$Doc.AddAuthor("PowerShell")
$Doc.Close()
}
All replies (6)
Thursday, September 29, 2011 8:09 PM âś…Answered | 1 vote
[System.Reflection.Assembly]::LoadFrom("C:\itextsharp\itextsharp.dll")
$doc = New-Object itextsharp.text.document
$stream = [IO.File]::OpenWrite("C:\test.pdf")
$writer = [itextsharp.text.pdf.PdfWriter]::GetInstance($doc, $stream)
$doc.Open()
[IO.File]::ReadAllLines("C:\test.txt") | foreach {
$line = New-Object itextsharp.text.Paragraph($_)
$doc.Add($line)
}
$doc.Close()
$stream.Close()
Thursday, September 29, 2011 10:00 PM
This was the key. Thank you very much! I blended the two scripts to accomplish my task:
[System.Reflection.Assembly]::LoadFrom("C:\itextsharp\itextsharp.dll")
Get-ChildItem "C:\PDF\" *.txt -R | ForEach-Object {
$doc = New-Object itextsharp.text.document
$stream = [IO.File]::OpenWrite("C:\PDF\" + $_.BaseName + ".pdf")
$writer = [itextsharp.text.pdf.PdfWriter]::GetInstance($doc, $stream)
$doc.Open()
[IO.File]::ReadAllLines("C:\PDF\" + $_.BaseName + ".txt") | foreach {
$line = New-Object itextsharp.text.Paragraph($_)
$doc.Add($line)
}
$doc.Close()
$stream.Close()
}
Sunday, November 27, 2011 5:33 PM
Hi,
This part does not work very well :
foreach {
$line = New-Object itextsharp.text.Paragraph($_)
$doc.Add($line)
}
because it omits repetitive lines break and repetitive spaces (on the same line) ...
I tried something like that .... (without success ) :
foreach {
$chunk= New-Object itextsharp.text.Chunk($_)
$doc.Add($chunk)
....
$nl = New-Object iTextSharp.text.Paragraph("`n")
$doc.Add($nl) ....
I don't know how to correct that :-(
maybe somebody would have an idea ?
Thursday, February 2, 2012 3:13 PM
The spaces problem caused by the following code in the Chunk.cs for the itextsharp:
public virtual string Content {
get {
return content.ToString().Replace("\t", "");
}
}
The tabs will be removed.
So replace the tabs with the number of spaces you want by modifying the following line:
$line = New-Object itextsharp.text.Paragraph($_)
to
$line = New-Object itextsharp.text.Paragraph($_.Replace("`t", " "))
Friday, March 7, 2014 3:45 PM
Does anyone have sample code for the opposite, converting a PDF to another format like TXT?
Levi Stevens | Technical Consultant Dell Services
Friday, March 7, 2014 3:50 PM | 1 vote
Check out http://social.technet.microsoft.com/Forums/scriptcenter/en-US/086b9a8c-7e47-49ed-8e94-8f5f43f408fe/search-a-pdf-and-return-specific-text?forum=winserverpowershell for a starting point. It shows how to use iTextSharp's PdfTextExtractor class, which could be used to create a text file.