首页 新闻 会员 周边

iTextSharp 水印签名出错

0
悬赏园豆:5 [待解决问题]

我有一段代码,是用来在PDF文件上加水印的,今天出现了一种情况,在其他文件上加水印是正确的,今天在一份文件上加水印在运行到倒数第二句“stamp.Close()”,系统就提示“未处理的“System.StackOverflowException”类型的异常出现在 itextsharp.dll 中。”,文件没有加密,有3.26M大小,找不到原因,有哪位大哥帮忙一下?给我点思路。

 

''' <summary>
''' 加入不同圖片
''' </summary>
''' <param name="filePath">原始PDF文件路徑</param>
''' <param name="toPath">要生成的PDF文件路徑</param>
''' <param name="watermarkStatus"></param>
''' <param name="WaterNamePJPath"></param>
''' <param name="fileName">文件編號</param>
''' <param name="fileRev">文件編號的版本</param>
''' <param name="title">Title</param>
''' <param name="model">Model</param>
''' <param name="WaterPercentage">水印百分比</param>
''' <param name="qianshuriqi">簽署日期</param>
''' <remarks></remarks>
Public Sub ConvertPDFToPDF(ByVal filePath As String, ByVal toPath As String, ByVal watermarkStatus As Integer, ByVal WaterNamePJPath As String, ByVal fileName As String, ByVal fileRev As String, ByVal title As String, ByVal Model As String, ByVal WaterPercentage As Single, ByVal qianshuriqi As String)
Dim reader As New PdfReader(filePath)
Dim n As Integer = reader.NumberOfPages
Dim stamp As New PdfStamper(reader, New FileStream(toPath, FileMode.Create))
Dim i As Integer = 0
Dim under As PdfContentByte
Dim mm As iTextSharp.text.Image

Dim PDfWidth As Single = 595.22F
Dim PDFHeight As Single = 842.0F

Dim xpos As Integer = 0
Dim ypos As Integer = 0
Dim ArrayWaterList As New ArrayList
'創建圖片列表
ArrayWaterList = CreateWaterList(n, WaterNamePJPath, fileName, fileRev, title, Model, WaterPercentage, qianshuriqi) '

While i < n
i += 1
mm = iTextSharp.text.Image.GetInstance(ArrayWaterList(i - 1))
PDfWidth = reader.GetPageSizeWithRotation(i).Width
PDFHeight = reader.GetPageSizeWithRotation(i).Height

Dim mmWidth As Single = 0.0F
Dim mmHeight As Single = 0.0F

mmWidth = mm.Width / CSng("1.5")
mmHeight = mm.Height / CSng("1.5")

If (PDfWidth > 1600 And PDFHeight > 2400) Then

mmWidth = mm.Width * CSng("1.5")
mmHeight = mm.Height * CSng("1.5")
End If

If (PDfWidth > 2000 And PDFHeight > 2900) Then

mmWidth = mm.Width * CSng("2")
mmHeight = mm.Height * CSng("2")
End If

If (PDfWidth > 2400 And PDFHeight > 3400) Then
mmWidth = mm.Width * CSng("3")
mmHeight = mm.Height * CSng("3")
End If

Select Case watermarkStatus
Case 1 '左上
xpos = 20
ypos = CInt(PDFHeight - mmHeight - CSng("20"))
Exit Select

Case 2 '中上
xpos = CInt((PDfWidth - mmWidth) / 2)
ypos = CInt(PDFHeight - mmHeight - CSng("20"))
Exit Select

Case 3 '右上(默認)
xpos = CInt(PDfWidth - mmWidth - CSng("20"))
ypos = CInt(PDFHeight - mmHeight - CSng("20"))
Exit Select

Case 4 '左中
xpos = 20
ypos = CInt((PDFHeight - mmHeight) / 2)
Exit Select

Case 5 '中
xpos = CInt((PDfWidth - mmWidth) / 2)
ypos = CInt((PDFHeight - mmHeight) / 2)
Exit Select

Case 6 '右中
xpos = CInt(PDfWidth - mmWidth - CSng("20"))
ypos = CInt((PDFHeight - mmHeight) / 2)
Exit Select

Case 7 '左下
xpos = 20
ypos = 20
Exit Select

Case 8 '中下
xpos = CInt((PDfWidth - mmWidth) / 2)
ypos = 20
Exit Select

Case 9 '右下
xpos = CInt(PDfWidth - mmWidth - CSng("20"))
ypos = 20
Exit Select
End Select

mm.SetAbsolutePosition(xpos, ypos)
mm.ScaleAbsolute(mmWidth, mmHeight)

under = stamp.GetOverContent(i)
'图片
under.AddImage(mm)
End While

stamp.Close()
reader.Close()

End Sub

青鋒的主页 青鋒 | 初学一级 | 园豆:5
提问于:2017-11-13 10:22
< >
分享
所有回答(2)
0

参考:https://www.cnblogs.com/LL-723/archive/2013/12/25/3490699.html

金琥 | 园豆:2605 (老鸟四级) | 2017-11-13 10:49

 看过了,但是没区别,然并卵,不能解决我的问题。

支持(0) 反对(0) 青鋒 | 园豆:5 (初学一级) | 2017-11-13 10:55
0

在你遍历的过程中你看下是不是有些资源需要在遍历周期内完成释放工作?

如果不是你看下能否将整个遍历过程分成若干次完成,比如一次只处理10页后close一次,然后再从11页开始再处理10页,和分页逻辑类似的。

Daniel Cai | 园豆:10424 (专家六级) | 2017-11-13 16:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册