[英]Add Watermark to PDF using iTextSharp Not working for Image PDF C#
我正在使用下面的代碼在 PDF 中添加水印。 它適用於普通文本 PDF。 如果它包含圖像水印是不可見的。
public void AddWatermarkPdf()
{
PdfReader pdfReader = new PdfReader("SimpleArabic.pdf");
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(DateTime.Now.ToFileTime()
+ "Out.pdf", FileMode.Create, FileAccess.Write, FileShare.None));
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance("WaterMarkDoc.png");
img.SetAbsolutePosition(-200, -50);
PdfContentByte waterMark;
for (int pageIndex = 1; pageIndex <= pdfReader.NumberOfPages; pageIndex++)
{
Rectangle pagesize = pdfReader.GetCropBox(pageIndex);
waterMark = pdfStamper.GetUnderContent(pageIndex);
waterMark.AddImage(img);
}
pdfStamper.FormFlattening = true;
pdfStamper.Close();
}
即使圖像在 PDF 中,也必須應用水印。
添加水印的方法有兩種:在現有內容下或在現有內容之上。 出於您的預期目的,最好的方法是在 PDF 內容的頂部添加水印,並將水印設置為較低的不透明度。
這是一個 Java 代碼示例,但可以很容易地轉錄:
protected void manipulatePdf(String dest) throws Exception {
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
PdfFont font = PdfFontFactory.createFont(FontProgramFactory.createFont(StandardFonts.HELVETICA));
PdfCanvas over = new PdfCanvas(pdfDoc.getFirstPage());
over.setFillColor(ColorConstants.BLACK);
Paragraph p = new Paragraph("This watermark is added ON TOP OF the existing content")
.setFont(font).setFontSize(15);
new Canvas(over, pdfDoc, pdfDoc.getDefaultPageSize())
.showTextAligned(p, 297, 500, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
p = new Paragraph("This TRANSPARENT watermark is added ON TOP OF the existing content")
.setFont(font).setFontSize(15);
over.saveState();
PdfExtGState gs1 = new PdfExtGState();
gs1.setFillOpacity(0.5f);
over.setExtGState(gs1);
new Canvas(over, pdfDoc, pdfDoc.getDefaultPageSize())
.showTextAligned(p, 297, 450, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
over.restoreState();
pdfDoc.close();
}
資料來源:這在 iText 知識庫中得到了非常徹底的解釋: https://kb.itextpdf.com/home/it7kb/faq/how-to-add-a-watermark-to-a-page-with-an-opaque -圖片
編輯:
這是您正在使用的 iText 5 版本的 C# 示例:
public class TransparentWatermark
{
public static readonly String DEST = "results/sandbox/stamper/transparent_watermark.pdf";
public static readonly String SRC = "../../../resources/pdfs/hero.pdf";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new TransparentWatermark().ManipulatePdf(DEST);
}
protected void ManipulatePdf(String dest)
{
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
PdfCanvas under = new PdfCanvas(pdfDoc.GetFirstPage().NewContentStreamBefore(), new PdfResources(), pdfDoc);
PdfFont font = PdfFontFactory.CreateFont(FontProgramFactory.CreateFont(StandardFonts.HELVETICA));
Paragraph paragraph = new Paragraph("This watermark is added UNDER the existing content")
.SetFont(font)
.SetFontSize(15);
Canvas canvasWatermark1 = new Canvas(under, pdfDoc.GetDefaultPageSize())
.ShowTextAligned(paragraph, 297, 550, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
canvasWatermark1.Close();
PdfCanvas over = new PdfCanvas(pdfDoc.GetFirstPage());
over.SetFillColor(ColorConstants.BLACK);
paragraph = new Paragraph("This watermark is added ON TOP OF the existing content")
.SetFont(font)
.SetFontSize(15);
Canvas canvasWatermark2 = new Canvas(over, pdfDoc.GetDefaultPageSize())
.ShowTextAligned(paragraph, 297, 500, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
canvasWatermark2.Close();
paragraph = new Paragraph("This TRANSPARENT watermark is added ON TOP OF the existing content")
.SetFont(font)
.SetFontSize(15);
over.SaveState();
// Creating a dictionary that maps resource names to graphics state parameter dictionaries
PdfExtGState gs1 = new PdfExtGState();
gs1.SetFillOpacity(0.5f);
over.SetExtGState(gs1);
Canvas canvasWatermark3 = new Canvas(over, pdfDoc.GetDefaultPageSize())
.ShowTextAligned(paragraph, 297, 450, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
canvasWatermark3.Close();
over.RestoreState();
pdfDoc.Close();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.