c# itextsharp PDF creation with watermark on each page

后端 未结 7 1215
Happy的楠姐
Happy的楠姐 2020-12-04 15:39

I am trying to programmatically create a number of PDF documents with a watermark on each page using itextsharp (a C# port of Java\'s itext).

I am able to do this

7条回答
  •  無奈伤痛
    2020-12-04 16:13

    I used the first solution. I was having trouble getting it to work at first. I getting green underlines under all of my public voids saying that it was going to hide some inherit member.

    Basically I realized that I already had added a PagePageEventHelper and I basically just cut out the code for the OnStartPage. ALSO! For some reason I had to make all of my public void's public override void.

      public override void OnStartPage(PdfWriter writer, Document document)
            {
                if (condition)
                {
                    string watermarkText = "-whatever you want your watermark to say-";
                    float fontSize = 80;
                    float xPosition = 300;
                    float yPosition = 400;
                    float angle = 45;
                    try
                    {
                        PdfContentByte under = writer.DirectContentUnder;
                        BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
                        under.BeginText();
                        under.SetColorFill(iTextSharp.text.pdf.CMYKColor.LIGHT_GRAY);
                        under.SetFontAndSize(baseFont, fontSize);
                        under.ShowTextAligned(PdfContentByte.ALIGN_CENTER, watermarkText, xPosition, yPosition, angle);
                        under.EndText();
                    }
                    catch (Exception ex)
                    {
                        Console.Error.WriteLine(ex.Message);
                    }
                }
            }
    

提交回复
热议问题