Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 使用Powershell将文本文件转换为PDF文件_.net_Pdf_Powershell_File Conversion - Fatal编程技术网

.net 使用Powershell将文本文件转换为PDF文件

.net 使用Powershell将文本文件转换为PDF文件,.net,pdf,powershell,file-conversion,.net,Pdf,Powershell,File Conversion,我有一个文本文件,上面有简单的固件版本。我需要使用powershell脚本将文本文件转换为安全的pdf文件 还有,当文件被转换时,有没有办法在文件上加上日期和时间戳 我不太知道怎么做,我刚刚使用powershell,谢谢 我在Word 2010中使用了这个功能,但它也应该在2007中使用: # File paths $txtPath = "C:\Users\Public\test.txt" $pdfPath = "C:\Users\Public\test.pdf" # Required Wor

我有一个文本文件,上面有简单的固件版本。我需要使用powershell脚本将文本文件转换为安全的pdf文件

还有,当文件被转换时,有没有办法在文件上加上日期和时间戳


我不太知道怎么做,我刚刚使用powershell,谢谢

我在Word 2010中使用了这个功能,但它也应该在2007中使用:

# File paths
$txtPath = "C:\Users\Public\test.txt"
$pdfPath = "C:\Users\Public\test.pdf"

# Required Word Variables
$wdExportFormatPDF = 17
$wdDoNotSaveChanges = 0

# Create a hidden Word window
$word = New-Object -ComObject word.application
$word.visible = $false

# Add a Word document
$doc = $word.documents.add()

# Put the text into the Word document
$txt = Get-Content $txtPath
$selection = $word.selection
$selection.typeText($txt)

# Set the page orientation to landscape
$doc.PageSetup.Orientation = 1

# Export the PDF file and close without saving a Word document
$doc.ExportAsFixedFormat($pdfPath,$wdExportFormatPDF)
$doc.close([ref]$wdDoNotSaveChanges)
$word.Quit()

我用Word 2010实现了这一点,但它也应该适用于2007:

# File paths
$txtPath = "C:\Users\Public\test.txt"
$pdfPath = "C:\Users\Public\test.pdf"

# Required Word Variables
$wdExportFormatPDF = 17
$wdDoNotSaveChanges = 0

# Create a hidden Word window
$word = New-Object -ComObject word.application
$word.visible = $false

# Add a Word document
$doc = $word.documents.add()

# Put the text into the Word document
$txt = Get-Content $txtPath
$selection = $word.selection
$selection.typeText($txt)

# Set the page orientation to landscape
$doc.PageSetup.Orientation = 1

# Export the PDF file and close without saving a Word document
$doc.ExportAsFixedFormat($pdfPath,$wdExportFormatPDF)
$doc.close([ref]$wdDoNotSaveChanges)
$word.Quit()

我用Word 2010实现了这一点,但它也应该适用于2007:

# File paths
$txtPath = "C:\Users\Public\test.txt"
$pdfPath = "C:\Users\Public\test.pdf"

# Required Word Variables
$wdExportFormatPDF = 17
$wdDoNotSaveChanges = 0

# Create a hidden Word window
$word = New-Object -ComObject word.application
$word.visible = $false

# Add a Word document
$doc = $word.documents.add()

# Put the text into the Word document
$txt = Get-Content $txtPath
$selection = $word.selection
$selection.typeText($txt)

# Set the page orientation to landscape
$doc.PageSetup.Orientation = 1

# Export the PDF file and close without saving a Word document
$doc.ExportAsFixedFormat($pdfPath,$wdExportFormatPDF)
$doc.close([ref]$wdDoNotSaveChanges)
$word.Quit()

我用Word 2010实现了这一点,但它也应该适用于2007:

# File paths
$txtPath = "C:\Users\Public\test.txt"
$pdfPath = "C:\Users\Public\test.pdf"

# Required Word Variables
$wdExportFormatPDF = 17
$wdDoNotSaveChanges = 0

# Create a hidden Word window
$word = New-Object -ComObject word.application
$word.visible = $false

# Add a Word document
$doc = $word.documents.add()

# Put the text into the Word document
$txt = Get-Content $txtPath
$selection = $word.selection
$selection.typeText($txt)

# Set the page orientation to landscape
$doc.PageSetup.Orientation = 1

# Export the PDF file and close without saving a Word document
$doc.ExportAsFixedFormat($pdfPath,$wdExportFormatPDF)
$doc.close([ref]$wdDoNotSaveChanges)
$word.Quit()

假设用户安装了Word或其他类似应用程序,另一种选择是在脚本中包含一个库,该库将支持将文本转换为PDF。iTextSharp专门设计用于与.Net一起创建和维护PDF文档。你可以得到它

唯一的规定是,它是使用AGPL(免费!)许可证结构的许可软件,除非您计划将其打包到您自己的软件或其他类似软件中,并且不计划将其开源。听起来你只是在写剧本,这不应该是个问题。如果你选择了iTextSharp,你实际上可以逃脱,而不必把所有事情都公开为开源,所以你可能想走这条路

与powershell一起使用它的好参考是,当我必须自己使用该库时,我通常会在这里引用借阅代码(两次,但我认为这既乏味又烦人,所以我不经常使用它,但我很少需要批量或以自动方式生成PDF文档)

所以,本质。。。这将加载DLL,创建生成PDF文件所需的对象(使用Ariel字体,字体大小为10)

然后它读取源文件(以C:\temp\source.txt为例),并将内容添加到
$paragration
对象中,为每行添加一个新行字符,否则它会将所有文本粉碎成一个丑陋的大团块

然后它打开文件对象(有效地创建文件),将
$段落
对象添加到文件,然后关闭文件。无需保存,添加
$段落
对象将数据直接写入驱动器

[System.Reflection.Assembly]::LoadFrom("C:\path\to\itextsharp\itextsharp.dll")

$doc = New-Object iTextSharp.text.Document
$fileStream = New-Object IO.FileStream("C:\temp\output.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()
$arial = [iTextSharp.text.FontFactory]::GetFont("arial", 10)

$paragraph = New-Object iTextSharp.text.Paragraph

$paragraph.add((gc C:\temp\source.txt|%{"$_`n"})) | Out-Null
$doc.open()
$doc.add($paragraph) | Out-Null
$doc.close()

假设用户安装了Word或其他类似应用程序,另一种选择是在脚本中包含一个库,该库将支持将文本转换为PDF。iTextSharp专门设计用于与.Net一起创建和维护PDF文档。你可以得到它

唯一的规定是,它是使用AGPL(免费!)许可证结构的许可软件,除非您计划将其打包到您自己的软件或其他类似软件中,并且不计划将其开源。听起来你只是在写剧本,这不应该是个问题。如果你选择了iTextSharp,你实际上可以逃脱,而不必把所有事情都公开为开源,所以你可能想走这条路

与powershell一起使用它的好参考是,当我必须自己使用该库时,我通常会在这里引用借阅代码(两次,但我认为这既乏味又烦人,所以我不经常使用它,但我很少需要批量或以自动方式生成PDF文档)

所以,本质。。。这将加载DLL,创建生成PDF文件所需的对象(使用Ariel字体,字体大小为10)

然后它读取源文件(以C:\temp\source.txt为例),并将内容添加到
$paragration
对象中,为每行添加一个新行字符,否则它会将所有文本粉碎成一个丑陋的大团块

然后它打开文件对象(有效地创建文件),将
$段落
对象添加到文件,然后关闭文件。无需保存,添加
$段落
对象将数据直接写入驱动器

[System.Reflection.Assembly]::LoadFrom("C:\path\to\itextsharp\itextsharp.dll")

$doc = New-Object iTextSharp.text.Document
$fileStream = New-Object IO.FileStream("C:\temp\output.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()
$arial = [iTextSharp.text.FontFactory]::GetFont("arial", 10)

$paragraph = New-Object iTextSharp.text.Paragraph

$paragraph.add((gc C:\temp\source.txt|%{"$_`n"})) | Out-Null
$doc.open()
$doc.add($paragraph) | Out-Null
$doc.close()

假设用户安装了Word或其他类似应用程序,另一种选择是在脚本中包含一个库,该库将支持将文本转换为PDF。iTextSharp专门设计用于与.Net一起创建和维护PDF文档。你可以得到它

唯一的规定是,它是使用AGPL(免费!)许可证结构的许可软件,除非您计划将其打包到您自己的软件或其他类似软件中,并且不计划将其开源。听起来你只是在写剧本,这不应该是个问题。如果你选择了iTextSharp,你实际上可以逃脱,而不必把所有事情都公开为开源,所以你可能想走这条路

与powershell一起使用它的好参考是,当我必须自己使用该库时,我通常会在这里引用借阅代码(两次,但我认为这既乏味又烦人,所以我不经常使用它,但我很少需要批量或以自动方式生成PDF文档)

所以,本质。。。这将加载DLL,创建生成PDF文件所需的对象(使用Ariel字体,字体大小为10)

然后它读取源文件(以C:\temp\source.txt为例),并将内容添加到
$paragration
对象中,为每行添加一个新行字符,否则它会将所有文本粉碎成一个丑陋的大团块

然后它打开文件对象(有效地创建文件),将
$段落
对象添加到文件,然后关闭文件。无需保存,添加
$段落
对象将数据直接写入驱动器

[System.Reflection.Assembly]::LoadFrom("C:\path\to\itextsharp\itextsharp.dll")

$doc = New-Object iTextSharp.text.Document
$fileStream = New-Object IO.FileStream("C:\temp\output.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()
$arial = [iTextSharp.text.FontFactory]::GetFont("arial", 10)

$paragraph = New-Object iTextSharp.text.Paragraph

$paragraph.add((gc C:\temp\source.txt|%{"$_`n"})) | Out-Null
$doc.open()
$doc.add($paragraph) | Out-Null
$doc.close()

假设用户安装了Word或其他类似应用程序,另一种选择是在脚本中包含一个库,该库将支持将文本转换为PDF。iTextSharp专门设计用于与.Net一起创建和维护TAI