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

标签 .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 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()

关于.net - 使用 Powershell 将文本文件转换为 PDF 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23892631/

相关文章:

powershell - 如何删除一个目录(包括子目录)中除一个子目录外的所有内容?

C# 类设计 - 我应该考虑将 "Project"类放在哪里

.net - 每个控件都有一个单独的ErrorProvider是否有问题?

c# - 如何向列添加值并更新数据库

c# - 小数位和千位的字符串格式

java - 从方法生成后如何打开PDF文件

java - 在 Android 中阅读 PDF 文档

javascript - 使用javascript在新窗口中打开PDF字符串

powershell - 命令行等待输入,然后处理下一步

powershell - 使用 powershell 对 Microsoft Graph api 进行身份验证