开发者

Adding Image watermark to Pdf while Creating it using iTextSharp

Wonder if this possible. Saw many posts on adding watermark after the pdf is created and saved in开发者_如何学Python disk. But during creation of document how do i add a image watermark. I know how to add a image to document. But how do i position it such that it comes at the end of page.


For C#, use this code...

//new Document

Document DOC = new Document();


// open Document

DOC.Open();


//create New FileStream with image "WM.JPG"

FileStream fs1 = new FileStream("WM.JPG", FileMode.Open);


iTextSharp.text.Image JPG = iTextSharp.text.Image.GetInstance(System.Drawing.Image.FromStream(fs1), ImageFormat.Jpeg);


//Scale image

JPG.ScalePercent(35f);


//Set position

JPG.SetAbsolutePosition(130f,240f);

//Close Stream

fs1.Close();


DOC.Add(JPG);


This is essentially identical to adding a header or footer.

You need to create a class that implements PdfPageEvent, and in the OnPageEnd, grab the page's PdfContentByte, and draw your image there. Use an absolute position.

Note: You probably want to derive from PdfPageEventHelper, it has empty implementations of all the page events, so you just need to write the method you actually care about.

Note: Unless your image is mostly transparent, drawing it on top of your page will cover up Many Things. IIRC ("If I Recall Correctly"), PNG and GIF files added by iText will automatically be properly masked, allowing things under them to show through.

If you want to add an opaque image underneath everything, you should override OnStartPage() instead.

This is Java, but converting it is mostly a matter of capitalizing method names and swapping get/set calls for property access.

Image watermarkImage = new Image(imgPath);
watermarkImage.setAbsolutePosition(x, y);

writer.setPageEvent( new MyPageEvent(watermarkImage) );


public MyPageEvent extends PdfPageEventHelper {
  private Image waterMark;
  public MyPageEvent(Image img) {
    waterMark = img;
  }
  public void OnEndPage/*OnStartPage*/(PdfWriter writer, Document doc) {
    PdfContentByte content = writer.getContent();
    content.addImage( waterMark );
  }
}


This is the accepted answer's port to C#, and what worked for me. I'm using an A4 page size:

Define this BackgroundImagePdfPageEvent class:

public class BackgroundImagePdfPageEvent : PdfPageEventHelper
{
    private readonly Image watermark;

    public BackgroundImagePdfPageEvent(string imagePath)
    {
        using (var fs = new FileStream(imagePath, FileMode.Open))
        {
            watermark = Image.GetInstance(System.Drawing.Image.FromStream(fs), ImageFormat.Jpeg);
            watermark.SetAbsolutePosition(0, 0);
            watermark.ScaleAbsolute(PageSize.A4.Width, PageSize.A4.Height);
            watermark.Alignment = Image.UNDERLYING;
        }
    }

    public override void OnStartPage(PdfWriter writer, Document document)
    {
        document.Add(watermark);
    }
}

Then, when creating your document:

var doc = new Document(PageSize.A4);
doc.SetMargins(60f, 60f, 120f, 60f);
var outputStream = new MemoryStream();
var writer = PdfWriter.GetInstance(doc, outputStream);
var imagePath = "PATH_TO_YOUR_IMAGE";
writer.PageEvent = new BackgroundImagePdfPageEvent(imagePath);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜