Powershell和iTextSharp的水印

时间:2019-05-02 21:14:36

标签: powershell itext

是否可以通过iTextSharp在Powershell中给PDF加水印?我看到了它的C#版本,但不知道如何将其转换为PowerShell

下面是C#版本

PdfReader PDFReader = new PdfReader("C:\\file.pdf");

FileStream Stream = new FileStream("C:\\new.pdf", FileMode.Create, FileAccess.Write);

PdfStamper PDFStamper = new PdfStamper(PDFReader, Stream);

for (int iCount = 0; iCount < PDFStamper.Reader.NumberOfPages; iCount++)
{
    PdfContentByte PDFData = PDFStamper.GetOverContent(iCount + 1);
    BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
    PDFData.BeginText();
    PDFData.SetColorFill(CMYKColor.LIGHT_GRAY);
    PDFData.SetFontAndSize(baseFont, 80);
    PDFData.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "SAMPLE DOCUMENT", 300, 400, 45);
    PDFData.EndText();
}

Stream.Close();
PDFReader.Close();
PDFStamper.Close();

1 个答案:

答案 0 :(得分:0)

您可以简单地使用添加类型或反射方式导入dll。该代码的其余部分相当容易移植。

[System.Reflection.Assembly]::LoadFile('C:\temp\itextsharp.dll')
$pdfReader = [iTextSharp.text.pdf.PdfReader]::new('C:\temp\testInput.pdf')
$stream = [System.IO.FileStream]::new('C:\temp\testOutput.pdf', [System.IO.FileMode]::Create, [System.IO.FileAccess]::Write)
$pdfStamper = [iTextSharp.text.pdf.PdfStamper]::new($pdfReader, $stream)

for($i = 0; $i -lt $pdfStamper.Reader.NumberOfPages; $i++)
{
    $pdfData = $pdfStamper.GetOverContent($i+1)
    $baseFont = [iTextSharp.text.pdf.BaseFont]::CreateFont([iTextSharp.text.pdf.BaseFont]::HELVETICA, [iTextSharp.text.pdf.BaseFont]::WINANSI, [iTextSharp.text.pdf.BaseFont]::EMBEDDED)
    $pdfData.BeginText();
    $pdfData.SetColorFill([iTextSharp.text.pdf.CMYKColor]::LIGHT_GRAY)
    $pdfData.SetFontAndSize($baseFont, 80)
    $pdfData.ShowTextAligned([iTextSharp.text.pdf.PdfContentByte]::ALIGN_CENTER, "Sample Document", 300, 400, 45)
    $pdfData.EndText();
}

$pdfStamper.Close()
$stream.Close()
$pdfReader.Close()