pdf上的水印无法正常工作

时间:2015-06-10 10:44:41

标签: c# pdf itextsharp

我正在使用itextsharp创建pdf。现在我想在我试过的每个页面上添加水印

public override void OnStartPage(PdfWriter wr, iTextSharp.text.Document doc)

现在水印即将到来,但如果包含图像的pdf页面则隐藏水印

public class itsevent : PdfPageEventHelper
{
    string watermarkText = string.Empty;
    public itsevent(string watermark)
    {
        watermarkText = watermark;
    } 

    public override void OnStartPage(PdfWriter wr, iTextSharp.text.Document doc)
    {
        float fontSize = 80;
        float xPosition = 300;
        float yPosition = 300;
        float angle = 45;
        PdfContentByte u = wr.DirectContentUnder;
        PdfGState gs = new PdfGState();
        u.SaveState();
        u.SetGState(gs);
        BaseFont baseFont =BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
        u.BeginText();
        u.SetColorFill(iTextSharp.text.pdf.CMYKColor.LIGHT_GRAY);
        u.SetFontAndSize(baseFont, fontSize);
        u.ShowTextAligned(PdfContentByte.ALIGN_CENTER, watermarkText, xPosition, yPosition, angle);
        u.EndText();
        under.RestoreState();
    } 
}

itsevent ev = new itsevent("watermark");
pdfW.PageEvent = ev;
doc.Open();

2 个答案:

答案 0 :(得分:2)

使用哪个yum install monodevelop Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirror.ubiquityservers.com * epel: mirror.steadfast.net * extras: cosmos.cites.illinois.edu * updates: mirror.tzulo.com Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package monodevelop.noarch 0:5.9.0.431-2 set to be updated --> Processing Dependency: mono-devel for package: monodevelop --> Processing Dependency: xsp for package: monodevelop --> Processing Dependency: mono-basic for package: monodevelop --> Processing Dependency: mono-web for package: monodevelop --> Processing Dependency: NUnit for package: monodevelop --> Running transaction check ---> Package NUnit.noarch 0:2.6.3-0 set to be updated --> Processing Dependency: mono(System.Windows.Forms) = 2.0.0.0 for package: NUnit ---> Package mono-core.i386 0:1.2.4-2.el5.centos set to be updated --> Processing Dependency: libgdiplus for package: mono-core ---> Package mono-devel.i386 0:1.2.6-6.1.el5 set to be updated --> Processing Dependency: mono-core = 1.2.6-6.1.el5 for package: mono-devel --> Processing Dependency: mono(Mono.Cecil) = 0.6.8.8607 for package: mono-devel ---> Package mono-web.i386 0:1.2.6-6.1.el5 set to be updated --> Processing Dependency: mono(System.EnterpriseServices) = 2.0.0.0 for package: mono-web --> Processing Dependency: mono(System.EnterpriseServices) = 1.0.5000.0 for package: mono-web --> Processing Dependency: mono(System.Data) = 2.0.0.0 for package: mono-web --> Processing Dependency: mono(System.Data) = 1.0.5000.0 for package: mono-web ---> Package xsp.i386 0:1.2.1-1.el5.centos set to be updated --> Running transaction check ---> Package libgdiplus.i386 0:1.2.6-1.el5 set to be updated --> Processing Dependency: libungif.so.4 for package: libgdiplus ---> Package mono-core.i386 0:1.2.4-2.el5.centos set to be updated --> Processing Dependency: mono-basic for package: monodevelop ---> Package mono-core.i386 0:1.2.6-6.1.el5 set to be updated ---> Package mono-data.i386 0:1.2.6-6.1.el5 set to be updated ---> Package mono-winforms.i386 0:1.2.6-6.1.el5 set to be updated --> Processing Dependency: libgdiplus-devel for package: mono-winforms --> Running transaction check ---> Package giflib.i386 0:4.1.3-7.3.3.el5 set to be updated ---> Package libgdiplus-devel.i386 0:1.2.6-1.el5 set to be updated ---> Package mono-basic.noarch 0:4.0.1-1 set to be updated --> Processing Dependency: mono(System.Windows.Forms) = 4.0.0.0 for package: mono-basic --> Processing Dependency: mono(System.Drawing) = 4.0.0.0 for package: mono-basic --> Processing Dependency: mono(System) = 4.0.0.0 for package: mono-basic --> Processing Dependency: mono(System.Core) = 4.0.0.0 for package: mono-basic --> Processing Dependency: mono(mscorlib) = 4.0.0.0 for package: mono-basic --> Finished Dependency Resolution mono-basic-4.0.1-1.noarch from mono has depsolving problems --> Missing Dependency: mono(System.Core) = 4.0.0.0 is needed by package mono-basic-4.0.1-1.noarch (mono) mono-basic-4.0.1-1.noarch from mono has depsolving problems --> Missing Dependency: mono(mscorlib) = 4.0.0.0 is needed by package mono-basic-4.0.1-1.noarch (mono) mono-basic-4.0.1-1.noarch from mono has depsolving problems --> Missing Dependency: mono(System) = 4.0.0.0 is needed by package mono-basic-4.0.1-1.noarch (mono) mono-basic-4.0.1-1.noarch from mono has depsolving problems --> Missing Dependency: mono(System.Drawing) = 4.0.0.0 is needed by package mono-basic-4.0.1-1.noarch (mono) mono-basic-4.0.1-1.noarch from mono has depsolving problems --> Missing Dependency: mono(System.Windows.Forms) = 4.0.0.0 is needed by package mono-basic-4.0.1-1.noarch (mono) Error: Missing Dependency: mono(System.Core) = 4.0.0.0 is needed by package mono-basic-4.0.1-1.noarch (mono) Error: Missing Dependency: mono(System) = 4.0.0.0 is needed by package mono-basic-4.0.1-1.noarch (mono) Error: Missing Dependency: mono(mscorlib) = 4.0.0.0 is needed by package mono-basic-4.0.1-1.noarch (mono) Error: Missing Dependency: mono(System.Drawing) = 4.0.0.0 is needed by package mono-basic-4.0.1-1.noarch (mono) Error: Missing Dependency: mono(System.Windows.Forms) = 4.0.0.0 is needed by package mono-basic-4.0.1-1.noarch (mono)

您使用PdfContentByte用于在正常添加的内容下添加内容:

wr.DirectContentUnder

因此,显然正常内容中的图像将覆盖水印。请改用PdfContentByte u = wr.DirectContentUnder;

使用哪个页面事件

您正在wr.DirectContent添加水印; iText开发人员建议不要这样做。而是在OnStartPage中执行此操作。

如何改善外观

您可能希望通过设置OnEndPage的填充不透明度来应用透明度。

答案 1 :(得分:0)