简体   繁体   中英

Issues to Set Image as watermark in pdf using itextsharp

I am trying to add image in PDF using itextsharp , but issues is image not set proper in background(watermark).

I want like this:

在此处输入图片说明

But the output is like this:

在此处输入图片说明

Here some code I am posting:

public class PdfWriterEvents : IPdfPageEvent
{
    string watermarkText = string.Empty;

    public PdfWriterEvents(string watermark)
    {
        watermarkText = watermark;
    }
    public void OnStartPage(PdfWriter writer, Document document)
    {
        float fontSize = 80;
        float xPosition = iTextSharp.text.PageSize.A4.Width / 2;
        float yPosition = (iTextSharp.text.PageSize.A4.Height - 140f) / 2;
        float angle = 45;
        try
        {
            PdfContentByte under = writer.DirectContentUnder;
            Image image = Image.GetInstance(watermarkText);
            image.SetAbsolutePosition(55f, 55f);                
            under.AddImage(image);

        }
        catch (Exception ex)
        {
            Console.Error.WriteLine(ex.Message);
        }
    }
    public void OnEndPage(PdfWriter writer, Document document) { }
    public void OnParagraph(PdfWriter writer, Document document, float paragraphPosition) { }
    public void OnParagraphEnd(PdfWriter writer, Document document, float paragraphPosition) { }
    public void OnChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title) { }
    public void OnChapterEnd(PdfWriter writer, Document document, float paragraphPosition) { }
    public void OnSection(PdfWriter writer, Document document, float paragraphPosition, int depth, Paragraph title) { }
    public void OnSectionEnd(PdfWriter writer, Document document, float paragraphPosition) { }
    public void OnGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) { }
    public void OnOpenDocument(PdfWriter writer, Document document) { }
    public void OnCloseDocument(PdfWriter writer, Document document) { }
}

Calling code here :

writer.PageEvent = new PdfWriterEvents(LogoImage);

There's plenty of unnecessary lines in your code. For instance, you define fontSize , xPosition , yPosition , and angle , but you aren't doing anything with those variables. It's as if you copy/pasted some code from the internet, without understanding what that code is supposed to do. That's odd.

Suppose that you want to scale the image so that it fits the page size, then you have to get the width and the height of the page: document.PageSize.Width and document.PageSize.Height .

Then you have to make the decision whether or not you want the image to keep its aspect ratio. If not, you can use img.ScaleAbsolute(width, height) , but be aware that this can distort your image. If you want to avoid this distortion, you should use the ScaleToFit() method:

public void OnStartPage(PdfWriter writer, Document document)
{
    float width = document.PageSize.Width;
    float height = document.PageSize.Height;
    try
    {
        PdfContentByte under = writer.DirectContentUnder;
        Image image = Image.GetInstance(watermarkText);              
        image.ScaleToFit(width, height);
        image.SetAbsolutePosition(0, 0);  
        under.AddImage(image);
    }
    catch (Exception ex)
    {
        Console.Error.WriteLine(ex.Message);
    }
}

In this sample, I used 0 , 0 as offset. I don't know how much margin you want (you'll have to adapt width and height if you want a margin), nor do I know if you want to center the image (that will require some primary school Math).

In any case, this answer solves your main problem: you forgot to scale the image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM