我正在嘗試使用 powershell 生成 PDF,但我不知道如何繼續。我已經嘗試過使用 Itext 7,但我不知道如何讓它作業。
當我嘗試在 powershell 上安裝 Itext7 時,出現以下錯誤訊息:
No match found for the specified search criteria and the itext7 package name. Use
Get-PackageSource to display for all available registered package sources.
我可以幫忙嗎?
提前致謝
uj5u.com熱心網友回復:
下面是 PowerShell 腳本的代碼,它輸出帶有“Hello World!”的 PDF。寫在上面。它反映了 iText 7 基本 Hello World 示例的功能。您可以根據您的要求更改它。
Add-Type -Path "C:\temp\Common.Logging.Core.dll"
Add-Type -Path "C:\temp\Common.Logging.dll"
Add-Type -Path "C:\temp\BouncyCastle.Crypto.dll"
Add-Type -Path "C:\temp\itext.io.dll"
Add-Type -Path "C:\temp\itext.layout.dll"
Add-Type -Path "C:\temp\itext.kernel.dll"
[string] $DEST = "C:\files\HelloWorldPowerShell.pdf"
$pdfWriter = [iText.Kernel.Pdf.PdfWriter]::new($DEST)
$pdf = [iText.Kernel.Pdf.PdfDocument]::new($pdfWriter)
$document = [iText.Layout.Document]::new($pdf)
$document.Add([iText.Layout.Element.Paragraph]::new("Hello World!"))
$pdf.Close()
uj5u.com熱心網友回復:
PowerShell 依賴項的組合可能會出現問題,因為它們需要及時屬于已知的作業組,并且 7.1.14 被吹捧為輕量級解決方案,因此請參閱后面的 TL;DR 編輯或其他評論,并以管理員身份運行,可能與普通用戶。因此,請仔細遵循這些步驟,因為有些可能會降級您當前的設定。
最重要的是使用專案目錄并注意您的提示符是否位于該檔案夾中,以確保您沒有在默認的 PowerShell 目錄中運行。我使用目標目錄為“空白/空”的快捷方式,因此默認為當前作業檔案夾。
第一次檢查:-
project folder>[Net.ServicePointManager]::SecurityProtocol
應該回傳 Tls12 或 Tls13,我們需要它為 1.2,所以如果你的設定為 Tls13 并運行此行,請記下。
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
我們可能需要更改包提供程式,因此首先檢查 nuget 是否包含
請注意腳本的順序和位置對于正確加載至關重要
Add-Type -Path (Join-Path $PSScriptRoot ".\Common.Logging.Core.3.4.1\lib\net40\Common.Logging.Core.dll")
Add-Type -Path (Join-Path $PSScriptRoot ".\Common.Logging.3.4.1\lib\net40\Common.Logging.dll")
Add-Type -Path (Join-Path $PSScriptRoot ".\Portable.BouncyCastle.1.8.9\lib\net40\BouncyCastle.Crypto.dll")
Add-Type -Path (Join-Path $PSScriptRoot ".\itext7.7.1.14\lib\net40\itext.io.dll")
Add-Type -Path (Join-Path $PSScriptRoot ".\itext7.7.1.14\lib\net40\itext.layout.dll")
Add-Type -Path (Join-Path $PSScriptRoot ".\itext7.7.1.14\lib\net40\itext.kernel.dll")
$pdfDocuFilename = (join-path $PSScriptRoot "My1st.pdf")
$pdfWriter = [iText.Kernel.Pdf.PdfWriter]::new($pdfDocuFilename)
$pdfdocument = [iText.Kernel.Pdf.PdfDocument]::new($pdfWriter)
$pdfdocument.AddNewPage()
$pdfdocument.Close()
這將生成一個空檔案,但證明一切正常,您可以開始運行其他示例,例如 S_G 建議的示例,因此在加載 Add-Type 塊后,將我的空白示例替換為
[string] $DEST = "HelloWorld.pdf"
$pdfWriter = [iText.Kernel.Pdf.PdfWriter]::new($DEST)
$pdf = [iText.Kernel.Pdf.PdfDocument]::new($pdfWriter)
$document = [iText.Layout.Document]::new($pdf)
$document.Add([iText.Layout.Element.Paragraph]::new("Hello World! from Powershell"))
$pdf.Close()
... 祝你好運。
- 上面的版本是針對一個固定的時間點,用戶博客已經驗證了 7.1 混合作業沒有太多沖突,目的是生成一組在Net40環境中作業的獨立檔案,但時間會繼續,你應該確保你使用的是較新的組合。然而,7.1.15中的一切都發生了變化,因為 Net 4.5 和現在的 4.6.1 現在需要一個非常多的依賴項串列,但 packages/itext7/7.2.1 本身仍然適用于 packages/Portable.BouncyCastle/1.8.9 和常見的日志記錄仍然是 3.4.1
uj5u.com熱心網友回復:
只是我的 2 美分,但上面的代碼不適用于 Itext7 7.2.1(在修改正確路徑后)。
希望我上周看到這篇文章 - 浪費了幾天的大部分時間在 7.2.1 上拔頭發而不表現自己。:(
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/444142.html
