0

我使用 iTextSharp 创建 pdf,并使用 PdfStamper 添加脚注和水印。这一直运作良好。最近在 MS Edge 中查看 pdf 时没有出现页脚和水印。但是,如果我在 Chrome 中查看相同的 pdf,页脚和水印会正确显示。

我将 pdf 文件存储在 Azure 的 blob 存储中。

我最近将 iTextSharp 版本从 V4.1.2.0 更改为 V5.5.13。

添加水印和页脚的代码如下:

    Dim byteArray As Byte()
    Using stream As MemoryStream = New MemoryStream
        reportBlockBlob.DownloadToStream(stream)
        reader = New PdfReader(CType(stream.ToArray(), Byte()))
        If reader IsNot Nothing Then
            Using stamper As PdfStamper = New PdfStamper(reader, stream)
                Dim PageCount As Integer = reader.NumberOfPages
                If bReportInvalid Then
                    For i As Integer = 1 To PageCount
                        StampWaterMark(stamper, i, "INVALID", fontReport60, 35, New text.BaseColor(70, 70, 255), reader.GetPageSizeWithRotation(i))
                    Next
                ElseIf Not UserRoles.Contains(WaspWAVB.con.csUserRoleCertificateSignOff) Then
                    For i As Integer = 1 To PageCount
                        StampWaterMark(stamper, i, "DRAFT", fontReport60, 35, New text.BaseColor(70, 70, 255), reader.GetPageSizeWithRotation(i))
                    Next
                End If
                If bAddFooter Then
                    Dim sRepUCN As String = "UCN"
                    Dim sCopyright As String = "Copyright"
                    Dim yPos As Integer = 12
                    Dim xLeftPos As Integer = 36
                    Dim xMidPos As Single = 297.5
                    Dim xRightPos As Integer = 559
                    For i As Integer = 3 To PageCount - iAppendixCount
                        ColumnText.ShowTextAligned(stamper.GetOverContent(i), text.Element.ALIGN_LEFT, New text.Phrase(sRepUCN, fontMedium), xLeftPos, yPos, 0)
                        ColumnText.ShowTextAligned(stamper.GetOverContent(i), text.Element.ALIGN_CENTER, New text.Phrase(sCopyright, fontMedium), xMidPos, yPos, 0)
                        Dim rttnPg As Integer = reader.GetPageRotation(i)
                        If rttnPg <> 0 Then
                            xRightPos = 806
                        End If
                        ColumnText.ShowTextAligned(stamper.GetOverContent(i), text.Element.ALIGN_RIGHT, New text.Phrase(String.Format(WaspWAVB.con.csPageXofY, i - 2, PageCount - 2 - iAppendixCount), fontMedium), xRightPos, yPos, 0)
                    Next
                End If
            End Using
            byteArray = stream.ToArray()
        End If
    End Using
    reportBlockBlob.Properties.ContentType = "application/pdf"
    reportBlockBlob.UploadFromByteArray(byteArray, 0, byteArray.Length)

    Public Shared Sub StampWaterMark(ByRef stamper As PdfStamper,
                                   ByVal i As Integer,
                                   ByVal watermark As String,
                                   ByVal font As text.Font,
                                   ByVal angle As Single,
                                   ByVal color As text.BaseColor,
                                   ByVal realPageSize As text.Rectangle,
                                   Optional ByVal rect As text.Rectangle = Nothing)

        Dim gstate = New PdfGState()
        gstate.FillOpacity = 0.1F
        gstate.StrokeOpacity = 0.3F
        stamper.GetOverContent(i).SaveState()
        stamper.GetOverContent(i).SetGState(gstate)
        stamper.GetOverContent(i).SetColorFill(color)
        stamper.GetOverContent(i).BeginText()
        Dim ps = If(rect, realPageSize)
        Dim x = (ps.Right + ps.Left) / 2
        Dim y = (ps.Bottom + ps.Top) / 2
        ColumnText.ShowTextAligned(stamper.GetOverContent(i), text.Element.ALIGN_CENTER, New text.Phrase(watermark, font), x, y, angle)

    End Sub

我尝试重新安排页脚和水印的应用顺序,并注释掉添加水印或页脚。这些都没有帮助。

我使用代码在代码的其他地方添加页脚,这很有效。在它起作用的地方,页脚将应用于单个页面。在它不起作用的地方,我刚刚将存储在单独 blob 中的页面收集在一起,并将它们合并到一个内存流中。页脚和水印应用于此。

令人困惑的是,pdf 在 Chrome 中可以正常工作,但在 Edge 中却不行。这两种方式都适用——即,如果我在 Chrome 中创建它并在 Edge 中查看它,页脚就会消失,如果我在 Edge 中创建它并在 Chrome 中查看它,页脚就会出现。

有没有其他人看到这个问题并知道如何解决它?

4

0 回答 0