iTextSharp在写入时隐藏文本

时间:2011-07-19 10:24:01

标签: c# vb.net itextsharp

所有

我使用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

任何人都可以帮助将上面的文字写入现有图像......

谢谢, 塞特希

1 个答案:

答案 0 :(得分:0)

   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)