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 water
public class PdfWriterEvents : IPdfPageEvent
{
string watermarkText = string.Empty;
public PdfWriterEvents(string watermark)
{
watermarkText = watermark;
}
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(275f, 275f);
image.SetAbsolutePosition(150, 300);
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);
所以,我怎样才能像普通水印图像一样将颜色更改为黑白
谢谢大家! 您可以通过两种方式更改图像颜色:
itextsharp
:static void Main(字符串[]args)
{
尝试
{
位图bmp=null;
//debug\bin\Big中的源目录\
string[]files=Directory.GetFiles(“Big\\”);
foreach(文件中的字符串文件名)
{
bmp=(位图)Image.FromFile(文件名);
bmp=变色(bmp);
string[]spliter=filename.Split('\\');
//目标目录调试\bin\BigGreen\
bmp.Save(“BigGreen\\”+拆分器[1]);
}
}
catch(System.Exception-ex)
{
Console.WriteLine(例如ToString());
}
}
公共静态位图更改颜色(位图SCRITMAP)
{
//你可以在这里改变你的新颜色。红、绿、草坪任何颜色。。
颜色newColor=Color.Red;
实际颜色;
//使空位图的大小与SCRITMAP相同
位图newBitmap=新位图(scrBitmap.Width、scrBitmap.Height);
对于(int i=0;i 150,因为..图像边缘可以是低像素颜色。如果我们将所有像素颜色设置为新,则将没有平滑度。
如果(实际颜色A>150)
newBitmap.SetPixel(i,j,newColor);
其他的
newBitmap.SetPixel(i,j,实际颜色);
}
}
返回newBitmap;
}
学分:
您可以使用提供的方法编辑图像
PS:当你完成并对结果感到满意时,向上投票@DareDevil的答案,这是一个精彩的发现 首先,您使用
OnStartPage
创建背景。这实际上是错误的做法。iText开发人员一再强调,不得在开始页面的中向文档添加任何内容。相反,应该使用onedpage
,在您的情况下,它不会带来任何问题,因此您应该真正这样做
如果您只有一个位图,最好的方法当然是在一些图像处理软件中打开该位图,然后更改颜色以使图像成为最佳的水印背景
另一方面,如果您有许多可能的图像可用作背景,甚至可能会为每个新文档获得一个单独的图像,那么您无法再手动将每个图像调整到最佳状态。相反,您可以通过某些服务操作位图本身,也可以使用特定于PDF的功能来操作图像外观
例如,使用您的页面事件侦听器,我可以得到以下信息:
使用混合模式色调用白色覆盖背景,这将变成:
这看起来很暗,但我们可以在混合模式下用浅灰色覆盖背景使其变亮屏幕:
为此,我将代码从您的PdfWriterEvents
方法OnStartPage
移动到onedpage
(请参见我答案的开头),并将其更改为:
public void OnEndPage(PdfWriter writer, Document document)
{
float width = document.PageSize.Width;
float height = document.PageSize.Height;
try
{
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(watermarkText);
image.ScaleToFit(275f, 275f);
image.SetAbsolutePosition(150, 300);
PdfGState gStateHue = new PdfGState();
gStateHue.BlendMode = new PdfName("Hue");
PdfGState gStateScreen = new PdfGState();
gStateScreen.BlendMode = new PdfName("Screen");
PdfContentByte under = writer.DirectContentUnder;
under.SaveState();
under.SetColorFill(BaseColor.WHITE);
under.Rectangle(document.PageSize.Left, document.PageSize.Bottom, document.PageSize.Width, document.PageSize.Height);
under.Fill();
under.AddImage(image);
under.SetGState(gStateHue);
under.SetColorFill(BaseColor.WHITE);
under.Rectangle(document.PageSize.Left, document.PageSize.Bottom, document.PageSize.Width, document.PageSize.Height);
under.Fill();
under.SetGState(gStateScreen);
under.SetColorFill(BaseColor.LIGHT_GRAY);
under.Rectangle(document.PageSize.Left, document.PageSize.Bottom, document.PageSize.Width, document.PageSize.Height);
under.Fill();
under.RestoreState();
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
}
}
我从中复制了图像。可能将背景图像源编辑为黑白?抱歉,但如何做到这一点?您能解释我吗?在图像编辑器中像photoshop一样编辑图像文件。如果您一般不知道如何编辑图像,那么一个简单的任务就是在MS Paint(有史以来最好的图像编辑器!!)中打开图像,并使用Fill
工具将图像的绿色部分涂成黑色或灰色。或者是的,我知道@JericCruz,我们可以使用photoshop或其他编辑器工具编辑图像,但我想知道是否有其他选项可以通过使用itextsharp的后端代码更改图像的颜色。这是可行的,但它适用于整个图像。我想要相同的图像,但颜色为浅黑白`PdfContentByte canvas=writer.DirectContentUnder;Image=Image.GetInstance(水印文本);图像缩放拟合(275f,275f);图像。设置绝对位置(165300);canvas.SaveState();PdfGState=新的PdfGState();state.FillOpacity=0.1f;canvas.SetGState(state);canvas.AddImage(图像);canvas.RestoreState();`----------------------你是对的,但在你的代码发布之前,我也尝试了同样的方法,但这种方法对我来说很有效,但最后谢谢你!为了回应你好!如果我在document.open()之前调用它(**writer.PageEvent=new PdfWriterEvents(LogoImage);
**),那么它来自第一页,有时会添加空白的新页,如果我在document.open()之后调用它,那么它不会出现在第一页,而是来自第二页,但同样的问题也会出现(有时会添加空白的新页)因此,如何打电话给这个,在哪里打电话,请张贴。。非常感谢。您必须在打开文档之前进行设置,否则会错过一些事件。此外,你还应该阅读我在回答的末尾写的关于使用OnStartPage
的内容,因为你的观察结果就是症状。是的,当你在后面发布这个回复时,我已经准备好了
public void OnEndPage(PdfWriter writer, Document document)
{
float width = document.PageSize.Width;
float height = document.PageSize.Height;
try
{
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(watermarkText);
image.ScaleToFit(275f, 275f);
image.SetAbsolutePosition(150, 300);
PdfGState gStateHue = new PdfGState();
gStateHue.BlendMode = new PdfName("Hue");
PdfGState gStateScreen = new PdfGState();
gStateScreen.BlendMode = new PdfName("Screen");
PdfContentByte under = writer.DirectContentUnder;
under.SaveState();
under.SetColorFill(BaseColor.WHITE);
under.Rectangle(document.PageSize.Left, document.PageSize.Bottom, document.PageSize.Width, document.PageSize.Height);
under.Fill();
under.AddImage(image);
under.SetGState(gStateHue);
under.SetColorFill(BaseColor.WHITE);
under.Rectangle(document.PageSize.Left, document.PageSize.Bottom, document.PageSize.Width, document.PageSize.Height);
under.Fill();
under.SetGState(gStateScreen);
under.SetColorFill(BaseColor.LIGHT_GRAY);
under.Rectangle(document.PageSize.Left, document.PageSize.Bottom, document.PageSize.Width, document.PageSize.Height);
under.Fill();
under.RestoreState();
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
}
}