C# 使用itextsharp在pdf中将图像设置为水印的问题
我正在尝试使用itextsharp在PDF中添加图像,但问题是图像背景设置不正确(水印) 我想要这样: 但是输出是这样的: 以下是我发布的一些代码:C# 使用itextsharp在pdf中将图像设置为水印的问题,c#,itext,C#,Itext,我正在尝试使用itextsharp在PDF中添加图像,但问题是图像背景设置不正确(水印) 我想要这样: 但是输出是这样的: 以下是我发布的一些代码: public class PdfWriterEvents : IPdfPageEvent { string watermarkText = string.Empty; public PdfWriterEvents(string watermark) { watermarkText = watermark
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) { }
}
在此调用代码:
writer.PageEvent = new PdfWriterEvents(LogoImage);
代码中有大量不必要的行。例如,您定义了
fontSize
、xPosition
、yPosition
和角度,但您没有对这些变量做任何操作。这就好像你从互联网上复制/粘贴了一些代码,却不知道这些代码应该做什么。真奇怪
假设要缩放图像使其适合页面大小,则必须获得页面的宽度和高度:document.PageSize.width
和document.PageSize.height
然后,您必须决定是否希望图像保持其纵横比。如果没有,您可以使用img.ScaleAbsolute(宽度、高度)
,但请注意这可能会扭曲您的图像。如果要避免这种失真,应使用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);
}
}
在这个示例中,我使用0
,0
作为偏移量。我不知道你想要多少边距(如果你想要边距,你必须调整宽度
和高度
),也不知道你是否想要将图像居中(这需要一些小学数学)
无论如何,这个答案解决了您的主要问题:您忘记缩放图像。“我想要这样:……但输出是这样:……”-显然,您必须用您想要的颜色替换图像(不可否认,你可以使用PDF方法更改颜色,但为什么你不简单地使用已经按需要着色的图像呢?)B必须缩放图像图像
,以适应空间。是的,我希望图像作为背景作为水印,我尝试使用此代码(我从互联网获得),但输出与秒(B)类似图像,那么如何为所有页面设置水印@mkl,第一个图像(A)其html视图(截图)第二个图像(B)为pdf视图(截图)。如果在两个屏幕截图中都使用了相同的图像作为背景,那么在HTML中肯定设置了一些特殊属性来删除颜色,因为iText并不是凭空发明的。首先感谢@Bruno Lowagie,你是对的,我只是将代码中的某些部分更改为image.ScaleToFit(宽度,高度);改为image.ScaleToFit(275f,275f);和image.SetAbsolutePosition(0,0);到image.SetAbsolutePosition(150300);(使图像位于pdf的中心)