Click here to Skip to main content
15,922,427 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
Hello,

i want to use the PDFFileWriter in VB.net but it doesn't work.
I have only an empty page. Have you got any examples for VB.NET 2017?

It's a good class.

Thanks
Best regards Frank

What I have tried:

Here is my small testprogram:

VB
Imports PdfFileWriter
Imports System.IO
Imports System.Drawing

Public Class Form1

    Dim ArialFont As PdfFont
    Dim FontSize As Double
    Dim Ascent As Double
    Dim Descent As Double
    Dim FontHeight2 As Double

    Dim Document As PdfDocument
    Dim CenterX As Double = 4.25
    Dim CenterY As Double = 5.5

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim npfad As String = ""

        SaveFileDialog1.Title = "PDF speicher unter..."
        SaveFileDialog1.OverwritePrompt = True

        If SaveFileDialog1.ShowDialog = DialogResult.OK And SaveFileDialog1.FileName > "" Then
            npfad = SaveFileDialog1.FileName
            If UCase(npfad).EndsWith("PDF") = False Then
                npfad = npfad & ".PDF"
            End If

            Dim PDFDokument As New PdfDocument(PaperType.A4, False, UnitOfMeasure.Inch, npfad)
            'PDFDokument.Debug = True

            Dim FontName1 As String = "Arial"
            Dim FontName2 As String = "Times New Roman"

            ArialFont = PdfFont.CreatePdfFont(PDFDokument, FontName1, FontStyle.Regular, True)
            Dim ArialNormal As PdfFont = PdfFont.CreatePdfFont(PDFDokument, FontName1, FontStyle.Regular, True)
            Dim ArialBold As PdfFont = PdfFont.CreatePdfFont(PDFDokument, FontName1, FontStyle.Bold, True)
            Dim ArialItalic As PdfFont = PdfFont.CreatePdfFont(PDFDokument, FontName1, FontStyle.Italic, True)
            Dim ArialBoldItalic As PdfFont = PdfFont.CreatePdfFont(PDFDokument, FontName1, FontStyle.Bold And FontStyle.Italic, True)
            Dim TimesNormal As PdfFont = PdfFont.CreatePdfFont(PDFDokument, FontName2, FontStyle.Regular, True)

            Dim PDFPage As New PdfPage(PDFDokument)
            Dim Contents As New PdfContents(PDFDokument)

            Dim left As Double = CenterX - 0.5 * 1
            Dim Right As Double = CenterX + 0.5 * 1
            Dim Top As Double = CenterY + 0.5 * 1
            Dim Bottom As Double = CenterY - 0.5 * 1

            Contents.DrawText(ArialFont, 10, 10, 10, TextJustify.Center, DrawStyle.Normal, Color.Black, "Hallo")

            ' save state
            Contents.SaveGraphicsState()

            Contents.SetLineWidth(0.1)
            Contents.SetColorStroking(Color.Black)
            Contents.SetColorNonStroking(Color.LightBlue)
            Contents.DrawRectangle(left, Bottom, 3, 3, PaintOp.CloseFillStroke)
            Contents.RestoreGraphicsState()

            'PDFContents.SaveGraphicsState()
            'Contents.Translate(1.1, 1.1)

            'Dim TextBox As New TextBox(Width, 0.25)
            'Dim PosY As Double = Height
            'TextBox.AddText(ArialNormal, 10, "Hallo Welt")
            Dim nText As String = "Hallo Welt"

            Dim Breite As Double = ArialBold.TextWidth(25, nText)
            Dim Höhe As Double = ArialBold.LineSpacing(25)

            'Stop

            Contents.SaveGraphicsState()

            Dim nBarcode As New Barcode128("2018-2928282")
            Contents.SetColorNonStroking(Color.Red)
            Contents.DrawBarcode(3, 7, 0.01, 1, nBarcode, ArialNormal, 8)

            Contents.SetLineWidth(0.01)
            Contents.BeginTextMode()
            Contents.SetColorStroking(Color.DarkBlue)
            Contents.DrawText(ArialBold, 25, 4, 9, TextJustify.Center, 0.02, Color.FromArgb(128, 0, 0, 255), Color.FromArgb(255, 255, 0, 0), "Hallo Frank")
            Contents.DrawLine(10, 10, 50, 20)
            Contents.EndTextMode()
            Contents.DrawRectangle(1, 1, 6, 9, PaintOp.CloseFillStroke)
            Contents.RestoreGraphicsState()
            PDFDokument.CreateFile()

            Process.Start(npfad)
        End If
    End Sub
End Class
Posted
Updated 9-Oct-18 5:05am

1 solution

The problem is you are adding the PdfContents object to your PdfDocument object but it needs to be added to the PdfPage object.
Change:
Dim PDFPage As New PdfPage(PDFDokument)
Dim Contents As New PdfContents(PDFDokument)
to
Dim PDFPage As New PdfPage(PDFDokument)
Dim Contents As New PdfContents(PDFPage)
This is extrapolated from the example on PDF File Writer C# Class Library (Version 1.20.0)[^]
// Step 3: Add new page
Page = new PdfPage(Document);

// Step 4:Add contents to page
Contents = new PdfContents(Page);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900