简体   繁体   English

使用 itextsharp 在 pdf 中将图像设置为水印的问题

[英]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).我正在尝试使用 itextsharp 在 PDF 中添加图像,但问题是图像在背景(水印)中设置不正确。

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.例如,您定义了fontSizexPositionyPositionangle ,但您没有对这些变量做任何事情。 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 .假设您想缩放图像以使其适合页面大小,那么您必须获取页面的宽度和高度: document.PageSize.Widthdocument.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.如果没有,您可以使用img.ScaleAbsolute(width, height) ,但请注意这会扭曲您的图像。 If you want to avoid this distortion, you should use the ScaleToFit() method:如果你想避免这种失真,你应该使用ScaleToFit()方法:

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.在这个示例中,我使用0 , 0作为偏移量。 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).我不知道你想要多少边距(如果你想要一个边距,你必须调整widthheight ),也不知道你是否想要使图像居中(这需要一些小学数学)。

In any case, this answer solves your main problem: you forgot to scale the image.无论如何,这个答案解决了您的主要问题:您忘记缩放图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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