scrivere il testo in pdf con itextsharp in vb.net

Ho un sub in vb.net come:

Public Shared f_cb As BaseFont = BaseFont.CreateFont(Directory.GetCurrentDirectory() + "\plantillas\fonts\trebucbd.ttf", BaseFont.CP1252, BaseFont.NOT_EMBEDDED)
    Public Shared f_cn As BaseFont = BaseFont.CreateFont(Directory.GetCurrentDirectory() + "\plantillas\fonts\trebuc.ttf", BaseFont.CP1252, BaseFont.NOT_EMBEDDED)


Public Shared Sub GenPDF()
Try
   Using fs As System.IO.FileStream = New FileStream(Directory.GetCurrentDirectory() + "\te\TEST.pdf", FileMode.Create)
                Dim document As New Document(PageSize.A4, 25, 25, 30, 1)
                Dim writer As PdfWriter = PdfWriter.GetInstance(document, fs)
                document.AddTitle("test")
                document.Open()
                Dim cb As PdfContentByte = writer.DirectContent
                Dim png As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(Current.Server.MapPath("Images/logo.jpg"))
                png.ScaleAbsolute(75, 52)
                png.SetAbsolutePosition(40, 770)
                cb.AddImage(png)
                cb.BeginText()

                Dim left_margin As Integer = 40
                Dim top_margin As Integer = 750
                write(cb, "Streets S.R.L.", left_margin, top_margin, f_cb, 10)
                write(cb, "Av. 900, C.A.B.A. (1001), Peru", left_margin, top_margin - 12, f_cn, 8)
                write(cb, "Tel: 4300-2147 - Fax: 4300-2148", left_margin, top_margin - 24, f_cn, 8)
                write(cb, "C.U.I.T. 20-30602227-0", left_margin, top_margin - 36, f_cn, 10)
                write(cb, "I.V.A.", left_margin, top_margin - 48, f_cn, 10)
                cb.EndText()
                document.Close()
                writer.Close()
                fs.Close()
            End Using
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

Public Shared Sub write(ByVal cb As PdfContentByte, ByVal Text As String, ByVal X As Integer, ByVal Y As Integer, ByVal font As BaseFont, ByVal Size As Integer)
        cb.SetFontAndSize(font, Size)
        cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, Text, X, Y, 0)
    End Sub

Così funziona ok, ora vorrei scrivere un pdf, ma non riesco a trovare analogico per vb.net stavo cercando pdfStamper, ma ha avuto alcun successo, Come funziona il codice per l’aggiunta di dati in formato pdf con itextsharp in vb.net come?

OriginaleL’autore cMinor | 2013-09-06

One Reply
  1. 1

    Qui è un esempio di scrittura di un testo da un file PDF esistente e salvarlo con un nuovo nome:

    Dim oldFile As String = "SomePath/Existing.pdf"
    Dim newFile As String = "SomePath/New.pdf"
    
    ' Create reader
    Dim reader As New PdfReader(oldFile)
    Dim size As Rectangle = reader.GetPageSizeWithRotation(1)
    Dim document As New Document(size)
    
    ' Create the writer
    Dim fs As New FileStream(newFile, FileMode.Create, FileAccess.Write)
    Dim writer As PdfWriter = PdfWriter.GetInstance(document, fs)
    document.Open()
    Dim cb As PdfContentByte = writer.DirectContent
    
    ' Set the font, color and size properties for writing text to the PDF
    Dim bf As BaseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED)
    cb.SetColorFill(BaseColor.DARK_GRAY)
    cb.SetFontAndSize(bf, 8)
    
    ' Write text in the PDF
    cb.BeginText()
    Dim text As String = "Some text here"
    
    ' Set the alignment and coordinates here
    cb.ShowTextAligned(1, text, 520, 640, 0)
    cb.EndText()
    
    ' Put the text on a new page in the PDF 
    Dim page As PdfImportedPage = writer.GetImportedPage(reader, 1)
    cb.AddTemplate(page, 0, 0)
    
    ' Close the objects
    document.Close()
    fs.Close()
    writer.Close()
    reader.Close()
    Mentre si fa questo, è sicuramente soluzione, non è necessariamente il migliori. L’aggiunta di modelli generati da PdfWriter.GetImportedPage rende) perde tutte le pagine di annotazioni (ad esempio gli elementi del modulo) e b) avvolge l’intera pagina in un xobject. Se è un tappo show, dipende dai requisiti e casi d’uso. Generalmente un approccio migliore è quello di utilizzare un PdfStamper che può anche aggiungere il contenuto della pagina tramite il suo OverContent e UnderContent.

    OriginaleL’autore Karl Anderson

Lascia un commento