【问题标题】:Watermark with Powershell and iTextSharp带有 Powershell 和 iTextSharp 的水印
【发布时间】:2019-05-03 08:15:56
【问题描述】:

有没有办法通过 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();

【问题讨论】:

  • 如果你有dll,你可以用Add-Type将它们导入到PS脚本中,然后你就可以访问库中的方法

标签: powershell itext


【解决方案1】:

您可以简单地使用 add-type 或反射导入 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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-02
    • 2019-12-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-04
    • 2013-09-02
    相关资源
    最近更新 更多