itextSharp 代码错误

Error on the itextSharp code

我正在使用以下代码为我的 PDF 添加水印

using System;
using System.Collections.Generic;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

namespace WatermarkDemo
{
    public class Class1
    {
       string watermarkedFile = "Watermarked.pdf";
       // Creating watermark on a separate layer
       // Creating iTextSharp.text.pdf.PdfReader object to read the Existing PDF Document
       PdfReader reader1 = new PdfReader(originalFile);
       using (FileStream fs = new FileStream(watermarkedFile, FileMode.Create, FileAccess.Write, FileShare.None))
       // Creating iTextSharp.text.pdf.PdfStamper object to write Data from iTextSharp.text.pdf.PdfReader object to FileStream object

       using (PdfStamper stamper = new PdfStamper(reader1, fs))
       {
            // Getting total number of pages of the Existing Document
            int pageCount = reader1.NumberOfPages;

            // Create New Layer for Watermark
            PdfLayer layer = new PdfLayer("WatermarkLayer", stamper.Writer);
            // Loop through each Page
            for (int i = 1; i <= pageCount; i++)
            {
                // Getting the Page Size
                Rectangle rect = reader1.GetPageSize(i);

                // Get the ContentByte object
                PdfContentByte cb = stamper.GetUnderContent(i);

                // Tell the cb that the next commands should be "bound" to this new layer
                cb.BeginLayer(layer);
                cb.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 50);

                PdfGState gState = new PdfGState();
                gState.FillOpacity = 0.25f;
                cb.SetGState(gState);

                cb.SetColorFill(BaseColor.BLACK);
                cb.BeginText();
                cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, watermarkText, rect.Width / 2, rect.Height / 2, 45f);
                cb.EndText();

                // Close the layer
                cb.EndLayer();
            }
        }
    }
}

我不知道为什么我的大部分变量下都会出现红色波浪线。 他们中的大多数人说

A field initializer cannot reference the non static field.

请帮忙,因为我被这段代码卡住了。

您已将代码立即放入 class 正文中:

namespace WatermarkDemo
{
    public class Class1
    {
       string watermarkedFile = "Watermarked.pdf";
       // Creating watermark on a separate layer
       // Creating iTextSharp.text.pdf.PdfReader object to read the Existing PDF Document
       PdfReader reader1 = new PdfReader(originalFile);
       using (FileStream fs = new FileStream(watermarkedFile, FileMode.Create, FileAccess.Write, FileShare.None))
    ...

你应该把它放到一个方法中

namespace WatermarkDemo
{
    public class Class1
    {
       void myMethod()
       {
           string watermarkedFile = "Watermarked.pdf";
           // Creating watermark on a separate layer
           // Creating iTextSharp.text.pdf.PdfReader object to read the Existing PDF Document
           PdfReader reader1 = new PdfReader(originalFile);
           using (FileStream fs = new FileStream(watermarkedFile, FileMode.Create, FileAccess.Write, FileShare.None))

    ...