English 中文(简体)
与强权者一起向国防军提交档案
原标题:Files to PDF with Powershell

我有以下问题:要写一份权力书,把所有档案都放在一纸上,最后,例如,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

问题回答

PDF传统上不支持电话线/电话型塔

因此,使用固定空间和NON比例F,如Cier/Consolas

我曾建议,在你先前的提问中,用Nenscript作为改写,然而,在测试方面,“Tabs Support”也“Lacking”。

因此,我提议使用支持ASCII/ANSI表格的Windows传统格式。

A typical batch file for drag and drop or looping through files could look like
enter image description here

撰稿人

set "enscript=%~dp0enscriptinenscript.exe"
set "gs=C:Program Filesgsgs10.01.1ingswin64c.exe"
set "tab=4"

REM -t title here is set as filename without extension, and other switches are available

"%enscript%" -B -t"%~n1" -T%tab% "%~dpn1.txt" -p"%temp%	mp.ps"
"%gs%" -sDEVICE=pdfwrite -o"%~dpn1.pdf" -f "%temp%	mp.ps"

仅举一例说明“Tabs”的情况,NOT自己就把一个“Cier Font”放在了总部指挥系统内,但这是一个范围更广的问题,有几个答案。

If you do not have the 3 critical files (1 exe & 2 dll s) use this install/unpacker.
instEnscript.cmd

md installer
cd installer
curl -o innounp.zip https://packages.chocolatey.org/innounp.0.50.nupkg
if not exist innounp.zip timeout 3
tar -xf innounp.zip tools/innounp.exe
if not exist toolsinnounp.exe timeout 2
move toolsinnounp.exe innounp.exe
timeout 2 &&if not exist innounp.exe echo FAILED Phase 1 && pause
rd tools
curl -O "https://master.dl.sourceforge.net/project/gnuwin32/enscript/1.6.3-9/enscript-1.6.3-9-bin.exe?viasf=1"
timeout 3&&if not exist enscript-1.6.3-9-bin.exe echo FAILED Phase 2 && pause
innounp -x enscript-1.6.3-9-bin.exe
timeout 4 &&if not exist {app}inenscript.exe echo FAILED Phase 3 && pause
if not exist {app}inlibiconv-2.dll echo FAILED Phase 3 && pause
if not exist {app}inlibintl-2.dll echo FAILED Phase 3 && pause
move {app} .. &&cd ..{app}
curl -O "https://master.dl.sourceforge.net/project/gnuwin32/enscript/1.6.3-9/enscript-1.6.3-9-doc.zip?viasf=1"
timeout 2 &&tar -xf enscript-1.6.3-9-doc.zip
timeout 2 &&cd ..
move {app}enscript-1.6.3-9-doc.zip installer
move {app} enscript





相关问题
Mutually exclusive powershell parameters

SCENARIO I m writing a cmdlet for Powershell 2.0 using Visual Studio 2008 and .NET 3.5 the cmdlet requires 3 arguments. my intended grammar of the cmdlet is something like this: cmdletname [foo|...

Run a program from PowerShell with timeout

I ll write a script that runs a program and wait for it finished. But if the program is not finished within a specified time I want that the program is killed.

How to transpose data in powershell

I have a file that looks like this: a,1 b,2 c,3 a,4 b,5 c,6 (...repeat 1,000s of lines) How can I transpose it into this? a,b,c 1,2,3 4,5,6 Thanks

Powershell v2 remoting and delegation

I have installed Powershell V2 on 2 machines and run Enable-PsRemoting on both of them. Both machines are Win 2003 R2 and are joined to the same active directory domain and I can successfully run ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

热门标签