我有以下问题:要写一份权力书,把所有档案都放在一纸上,最后,例如,Cpp .java .xml等。 每一档案都应从新网页开始,档案名称应当从一开始。
Add-Type -Path "C:Program FilesWindowsPowerShellModulesitextsharp5.5.13.3libitextsharp.dll"
function Merge-FilesToPdf {
param(
[Parameter(Mandatory = $true)]
[string]$SourcePath,
[Parameter(Mandatory = $true)]
[string]$DestinationFile
)
# Create a PDF document
$document = New-Object iTextSharp.text.Document
# Initialize the PDF writer
$writer = [iTextSharp.text.pdf.PdfWriter]::GetInstance($document, [System.IO.File]::Create($DestinationFile))
# Open the PDF document
$document.Open()
try {
# Define the font
$font = [iTextSharp.text.pdf.BaseFont]::CreateFont([iTextSharp.text.pdf.BaseFont]::COURIER, [iTextSharp.text.pdf.BaseFont]::CP1252, [iTextSharp.text.pdf.BaseFont]::EMBEDDED)
# List files in the source directory
$files = Get-ChildItem -Path $SourcePath -File
foreach ($file in $files) {
# Check if the file is a text file
if ($file.Extension -match .(txt|c|log|xml|ps1)$ ) {
# Create a new page
$document.NewPage()
# Add the file name as text
$header = New-Object iTextSharp.text.Paragraph($file.Name)
$header.Font = $font # Set font for the file name
$document.Add($header)
# Add the file content to the PDF
$content = Get-Content -Path $file.FullName -Raw
$paragraph = New-Object iTextSharp.text.Paragraph($content)
$paragraph.Font = $font # Set font for the file content
$document.Add($paragraph)
}
}
}
finally {
# Close the PDF document and release memory
$document.Close()
$writer.Close()
$document.Dispose()
$writer.Dispose()
}
}
Merge-FilesToPdf -SourcePath "C:Projekt estkl01erg001" -DestinationFile "C:Output.pdf"
这部法典最终运作,它只是没有像原始档案中那样在pdf档案中格式化。 谁有如何解决这一问题的想法?
Edit: I mean if I have for example a C code as a source, the code will be left justified in the PDF, tabs will not transfer