ahstax
Goto Top

Mit vb.net und itextsharp formatierten Text aus übergebenem String in bestehendes PDF einfügen

Hallo,

ich würde gerne den Inhalt einer (mehrerer, wenn es mit einer klappt, gehen dann sicherlich auch mehr...) String-Variablen in eine bestehende PDF-Vorlage einfügen. Inhalt der String-Variablen ist Text MIT Zeilenumbrüchen.

Mit einem "Stamper" hab ich es versucht, der will aber (mit meinem Code? s.u.) irgendwie die Zeilenumbrüche nicht berücksichtigen...

Imports System
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf



Public Sub InfoPDFErstellenVonVorlageXX(ByRef strType As String, ByRef strAusgabePfad As String, _
                                          ByRef strText01 As String, Optional ByRef strText02 As String = Nothing, _
                                          Optional ByRef strText03 As String = Nothing, Optional ByRef strText04 As String = Nothing)

        Dim pdffont1 As New iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)
        Dim pdffont2 As New iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)
        Dim pdffont3 As New iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)
        Dim pdffont4 As New iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)

        Dim strVorlagenDatei As String = Nothing

        Select Case strType
            Case "typ1"  
                strVorlagenDatei = strMyPathMailInfo
            Case "typ2" 'Nacharbeiten  
                Exit Sub
        End Select

        Try

            Using inputPdfStream As Stream = New FileStream(strVorlagenDatei, FileMode.Open, FileAccess.Read, FileShare.Read)
                Using outputPdfStream As Stream = New FileStream(strAusgabePfad, FileMode.Create, FileAccess.Write, FileShare.None)

                    Dim reader = New PdfReader(inputPdfStream)
                    Dim stamper = New PdfStamper(reader, outputPdfStream)
                    Dim pdfContentByte = stamper.GetOverContent(1)


                    Dim pageWidth As Single = reader.GetPageSize(1).Width
                    Dim pageHeight As Single = reader.GetPageSize(1).Height


                    Dim XPos As Single = 45
                    Dim YPos As Single = (pageHeight) - 200

                    Dim canvas1 As PdfContentByte = stamper.GetOverContent(1)
                    ColumnText.ShowTextAligned(canvas1, Element.ALIGN_LEFT, New Phrase(strText01, pdffont1), XPos, YPos, 0)

                    If strText02 <> Nothing Then
                        Dim canvas2 As PdfContentByte = stamper.GetOverContent(1)
                        XPos = 20
                        YPos = (pageHeight) - 300
                        ColumnText.ShowTextAligned(canvas2, Element.ALIGN_LEFT, New Phrase(strText02, pdffont1), XPos, YPos, 0)
                    End If
                    If strText03 <> Nothing Then
                        Dim canvas3 As PdfContentByte = stamper.GetOverContent(1)
                        XPos = 20
                        YPos = (pageHeight) - 400
                        ColumnText.ShowTextAligned(canvas3, Element.ALIGN_LEFT, New Phrase(strText03, pdffont1), XPos, YPos, 0)
                    End If
                    If strText04 <> Nothing Then
                        Dim canvas4 As PdfContentByte = stamper.GetOverContent(1)
                        XPos = 20
                        YPos = (pageHeight) - 500
                        ColumnText.ShowTextAligned(canvas4, Element.ALIGN_LEFT, New Phrase(strText04, pdffont1), XPos, YPos, 0)
                    End If

                    stamper.Close()
                    reader.Close()
                End Using
            End Using

        Catch ex As Exception

        End Try

    End Sub

Kann mir jemand helfen?

Neugierige Grüße,
Andreas

Content-Key: 248419

Url: https://administrator.de/contentid/248419

Printed on: April 23, 2024 at 11:04 o'clock

Member: colinardo
Solution colinardo Sep 05, 2014, updated at Sep 23, 2014 at 10:39:07 (UTC)
Goto Top
Moin Andreas,
bin jetzt nicht so firm mit iTextSharp, aber für Text mit Zeilennumbrüchen ist ein ColumnText-Objekt besser geeignet, da es mit Zeilenumbrüchen umgehen kann:
http://stackoverflow.com/questions/15165357/centered-multiline-text-usi ...
ansonsten musst du deinen Text an den Zeilenumbrüchen splitten und mit einer Schleife untereinander schreiben:
http://stackoverflow.com/questions/3992617/itextsharp-insert-text-to-an ...

Grüße Uwe