Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iTextSharp:尝试使TextField透明:PdfContentByte什么都不是_Itext - Fatal编程技术网

iTextSharp:尝试使TextField透明:PdfContentByte什么都不是

iTextSharp:尝试使TextField透明:PdfContentByte什么都不是,itext,Itext,我想将文本字段添加到我的PDF中。 这些文本字段的背景应该是透明的 我在网上找到了一个例子,展示了如何通过以下方式实现这一点: PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("TransparencyPDF.pdf")); document.open(); PdfContentByte cb = writer.getDirectContent(); PdfGSt

我想将文本字段添加到我的PDF中。 这些文本字段的背景应该是透明的

我在网上找到了一个例子,展示了如何通过以下方式实现这一点:

PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("TransparencyPDF.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
        
PdfGState gs1 = new PdfGState();
gs1.setFillOpacity(0.5f);
        
cb.setGState(gs1);
然而,在我的代码中,我没有PDFWriter。我有一个PDFStamper。 但是它的特性看起来完全一样,所以我采用如下方式:

Dim cb As PdfContentByte = stamper.GetOverContent(0)
Dim gs As New PdfGState
gs.FillOpacity = 0.5
cb.SetGState(gs)
最后一行抛出错误“System.NullReferenceException:cb为nothing”

我做错了什么

多谢各位

这是我的全部代码:

Dim stamper As PdfStamper = New PdfStamper(New PdfReader(sInputFile), File.Create(sOutputFile))

Dim iPageNumer As Integer = 1

Dim tf As TextField
tf = New TextField(stamper.Writer, New iTextSharp.text.Rectangle(iLowerLeftX, iLowerLeftY, iUpperRightX, iUpperRightY), n.Name)
Dim bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, False)
With tf
    .Alignment = Element.ALIGN_LEFT And Element.ALIGN_MIDDLE '  Element.ALIGN_CENTER And Element.ALIGN_MIDDLE
    .BackgroundColor = GrayColor.WHITE
    .BorderColor = Color.RED
    .BorderStyle = PdfBorderDictionary.STYLE_SOLID
    .DefaultText = "This is a new text field."
    .Font = bf
    .FontSize = 7
    .MaxCharacterLength = 25
    .Options = TextField.BORDER_WIDTH_MEDIUM ' TextField.REQUIRED Or TextField.MULTILINE
    .Rotation = 0 '90
    .Text = "" 'This is the assigned value."
End With

stamper.AddAnnotation(tf.GetTextField(), iPageNumer)

Dim cb As PdfContentByte = stamper.GetOverContent(0)
Dim gs As New PdfGState
gs.FillOpacity = 0.5
cb.SetGState(gs) 'This line throws the error. cb is nothing. Why?

stamper.Close()
问题在于

Dim cb As PdfContentByte = stamper.GetOverContent(0)
它请求页面
0
OverContent
,而iText中的页面编号从
1
开始。因此,使用

Dim cb As PdfContentByte = stamper.GetOverContent(1)
相反。

的问题

Dim cb As PdfContentByte = stamper.GetOverContent(0)
它请求页面
0
OverContent
,而iText中的页面编号从
1
开始。因此,使用

Dim cb As PdfContentByte = stamper.GetOverContent(1)
相反