Powershell itextSharp PDF打中文水印

## PDF Add WaterMark

[void][System.Reflection.Assembly]::LoadFile($itextSharpDllPath);

$pdfReader = [iTextSharp.text.pdf.PdfReader]::new($sOriPdfPath);

$stream = [System.IO.FileStream]::new($sTempWaterMarkPath + "\" + $sOriPdfName, [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);

    # simfang.ttf  STFANGSO.TTF  simkai.ttf

    $bfChinese = [iTextSharp.text.pdf.BaseFont]::CreateFont($sFontTTF, [iTextSharp.text.pdf.BaseFont]::IDENTITY_H, [iTextSharp.text.pdf.BaseFont]::NOT_EMBEDDED);

    $pdfData.BeginText();

    $pdfData.SetFontAndSize($bfChinese, 11);

    $pdfData.SetColorFill([iTextSharp.text.pdf.CMYKColor]::DARK_GRAY);   

    #$pdfData.ShowTextAligned([iTextSharp.text.pdf.PdfContentByte]::ALIGN_CENTER, $sWaterMatkText, $pdfStamper.Reader.GetPageSize(1).Width / 2, $pdfStamper.Reader.GetPageSize(1).Height / 2, $strRotation);   

    $rect = $pdfReader.GetPageSizeWithRotation($i + 1);

    $x = ($rect.Right + $rect.Left) / 2;

    $y = ($rect.Top + $rect.Bottom) / 2;   

    $pdfData.ShowTextAligned([iTextSharp.text.Element]::ALIGN_CENTER, $sWaterMatkText, $x, $y, $strRotation);     

    $pdfData.EndText();

}

$pdfStamper.Close();

$stream.Close();

$pdfReader.Close();


搞了一日,最后MAK哥帮手,原来是要求出GetPageSizeWithRotation,因为PDF的X和Y是调转的。

你可能感兴趣的:(Powershell itextSharp PDF打中文水印)