【问题标题】:iTextSharp hides text when writeiTextSharp 在写入时隐藏文本
【发布时间】:2011-07-19 10:24:00
【问题描述】:

全部

我正在使用 iTextSharp 库在现有 PDF 文件中插入文本,有时如果我在 PDF 页面中写入文本的位置有任何图像,它会一直隐藏写入的文本,如何在现有图像上写入文本。

    Dim iNumOfPages As Integer = 0, iFile As Integer = 0, iRotation As Integer
    Dim objPdfReader As PdfReader
    Dim objDoc As Document = Nothing
    Dim objPdfWriter As PdfWriter = Nothing
    Dim objContentByte As PdfContentByte
    Dim objPage As PdfImportedPage
    Dim objFileStream As FileStream = Nothing

    objFileStream = New FileStream(sDestFile, FileMode.Create)
        objPdfReader = New PdfReader(slSourceFiles.Item(iFile))
        iNumOfPages = objPdfReader.NumberOfPages
        objDoc = New Document(objPdfReader.GetPageSizeWithRotation(1))
        objPdfWriter = PdfWriter.GetInstance(objDoc, objFileStream)
        objDoc.Open()
        objContentByte = objPdfWriter.DirectContent

        Dim objTempCB As PdfContentByte = objPdfWriter.DirectContent
        objPDFStamper = New PdfStamper(objPdfReader, objFileStream)

    While i < iNumOfPages
                i = i + 1
                objDoc.SetPageSize(objPdfReader.GetPageSizeWithRotation(i))
                objDoc.NewPage()
                objPage = objPdfWriter.GetImportedPage(objPdfReader, i)
                iRotation = objPdfReader.GetPageRotation(i)


                    Dim bf As BaseFont = BaseFont.CreateFont(BaseFont.TIMES_ROMAN,     BaseFont.CP1250, BaseFont.EMBEDDED)
                        Dim bc As BaseColor = BaseColor.BLUE


                        objContentByte.BeginText()
                        objContentByte.SetTextMatrix(400, 765) 'objContentByte.SetTextMatrix(400, 765)
                        objContentByte.SetFontAndSize(bf, 9)
                        objContentByte.SetColorFill(bc)
                        objContentByte.ShowText("Mytext................")
                        objContentByte.EndText()

        If (iRotation = 90 Or iRotation = 270) Then
                        objContentByte.AddTemplate(objPage, 0, -1.0F, 1.0F, 0, 0, objPdfReader.GetPageSizeWithRotation(i).Height)
                    Else
                        objContentByte.AddTemplate(objPage, 1.0F, 0, 0, 1.0F, 0, 0)
                    End If
    End While

任何一种帮助将上面的文本写入现有图像...

谢谢, 森提尔

【问题讨论】:

    标签: c# vb.net itextsharp


    【解决方案1】:

    在行中

       objContentByte.AddTemplate(objPage, 0, -1.0F, 1.0F, 0, 0, objPdfReader.GetPageSizeWithRotation(i).Height)
    

    和

       objContentByte.AddTemplate(objPage, 1.0F, 0, 0, 1.0F, 0, 0)
    

    您能否尝试从 PdfWriter 写入 DirectContentUnder:

        objContentByte = objPdfWriter.DirectContent
        Dim objContentByteUnder as PdfContentByte  = objPdfWriter.DirectContentUnder
                 .....
                 objContentByte.BeginText()
                 .....
                 objContentByteUnder.AddTemplate(objPage, 0, -1.0F, 1.0F, 0, 0, objPdfReader.GetPageSizeWithRotation(i).Height)
                 objContentByteUnder.AddTemplate(objPage, 1.0F, 0, 0, 1.0F, 0, 0)
    

    【讨论】:

    • 您好,感谢您的回复,我尝试使用 objContentByte = objPdfWriter.DirectContentUnder,如果出现图像,它仍然会隐藏文本。
    • 您必须将文本写入 objPdfWriter.DirectContent 并将图像写入 objPdfWriter.DirectContentUnder
    • 您好,感谢您的回复,我可以通过将 objContentByte.BeginText() ...... objContentByte.EndText() 代码替换为 next到'End If'语句..
    • 嗨,Snethil,我遇到了同样的问题,请告诉我应该在哪里调用 BeginText 和 EndText?
    • @Supereme:如果您将文本添加到 DirectContent 并将图像添加到 DirectContentUnder,那么您确实拥有 DirectContent 和 DirectContentUnder。文字应该出现在图片之上。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多