VB6 Add image to PDF

Discussions about PDF management.
Post Reply
Ben
Posts: 1
Joined: Thu Aug 08, 2019 4:47 pm

VB6 Add image to PDF

Post by Ben » Thu Aug 08, 2019 5:01 pm

Hi All,

I'm trying to create a PDF in VB6 (Maintaining legacy systems)

I need to add 4 images into the PDF, I have attached the following code but it is not working for some reason, no errors or anything.

Code: Select all

Dim pRow As Integer, cRow As Integer, printOF As Boolean, pQty As Integer
Dim myY As Integer
Dim fontArial As Long, fontArialB As Long, fontCourierNew As Long, fontCourierNewB As Long, nImage As Long

If cmdSave.Visible = True Then Call cmdSave_Click

printOF = True

pdf.PdfNewPdf Environ("temp") & "XXXXXXXXXXX"

pdf.PdfSetMeasurementUnits 1 'measure unit is millimeters
pdf.PdfSetPageDimensions 210, 297  'Pages dimensio

myY = 290: myPageNo = 0

Dim fRow As Integer, tRow As Integer
If gdParts.Row <= gdParts.RowSel Then
  fRow = gdParts.Row: tRow = gdParts.RowSel
Else
  fRow = gdParts.RowSel: tRow = gdParts.Row
End If

For pRow = fRow To tRow

  
  If gdParts.TextMatrix(pRow, 0) <> "" Then

  BarcodeX1.Caption = Trim(gdParts.TextMatrix(pRow, 2)) & "#" & gdParts.TextMatrix(pRow, 1) & "#" & Format(dtBuild, "ddmmyyyy") & "#" & Mid(gdParts.TextMatrix(pRow, 2), 1, 8) & "#"
  SavePicture BarcodeX1.Picture, Environ("temp") & "\" & BarcodeX1.Caption & ".bmp"
  
    If printOF = True Then
      printOF = False
      fontCourierNew = pdf.PdfAddFont("Courier New") 'Add Courier new font
      fontCourierNewB = pdf.PdfAddFont("Courier New", True)   'Add Courier new bold font
      
      pdf.PdfNewPage
      
      pdf.PdfDrawTextAlign 105, myY, "XXXX- " & Format(dtBuild, "ddd, d-MMM-yyyy"), fontCourierNewB, 16, 2
      myY = myY - 10
          
      pdf.PdfDrawTextAlign 10, myY, "XXXX", fontCourierNewB, 12, 0
      pdf.PdfDrawTextAlign 40, myY, "XXXX", fontCourierNewB, 12, 0
      pdf.PdfDrawTextAlign 80, myY, "XXX", fontCourierNewB, 12, 2
      pdf.PdfDrawTextAlign 100, myY, "XXXX", fontCourierNewB, 12, 0
           
      myY = myY - 8
    End If

    Dim barcodeID As Integer
    barcodeID = pdf.PdfAddImageFromFile(Environ("temp") & "\" & BarcodeX1.Caption & ".bmp")
    
    gdParts.Row = pRow
    
    If gdParts.Row = pRow Then
      Call gdParts_EnterCell
    Else
      gdParts.Row = pRow
    End If

    pdf.PdfDrawTextAlign 10, myY, gdParts.TextMatrix(pRow, 0) & ")" & gdParts.TextMatrix(pRow, 1), fontCourierNew, 12, 0
    pdf.PdfDrawTextAlign 40, myY, gdParts.TextMatrix(pRow, 2), fontCourierNew, 12, 0
    pQty = gdParts.TextMatrix(pRow, 4)
    pdf.PdfDrawTextAlign 80, myY, pQty, fontCourierNew, 12, 2
    pdf.PdfDrawCircle 80, myY + 1, 5, 0.5
    pdf.PdfDrawTextAlign 100, myY, gdParts.TextMatrix(pRow, 3), fontCourierNew, 12, 0
    
    myY = myY - 8
        
    'pdf.PdfDrawImage barcodeID, 10, myY, 500, 500
        
    For cRow = 1 To vwComps.ListItems.Count
      pdf.PdfDrawTextAlign 40, myY, vwComps.ListItems(cRow).Text, fontCourierNew, 12, 0
      'Printer.CurrentX = 2250: Printer.Print vwComps.ListItems(cRow).Text;
      pdf.PdfDrawTextAlign 100, myY, vwComps.ListItems(cRow).SubItems(1), fontCourierNew, 12, 0
      'Printer.CurrentX = 5000: Printer.Print vwComps.ListItems(cRow).SubItems(1) 
      myY = myY - 5
    Next cRow
    
    myY = myY - 3
    pdf.PdfDrawLine 0, myY, 210, myY, 0.5
    myY = myY - 9
    
    
    If myY < 35 Then
      printOF = True
      myY = 290
      pdf.PdfEndPage
    End If
  
  End If
  'Kill Environ("temp") & "\" & BarcodeX1.Caption & ".bmp"
Next pRow

If myY <> 295 Then pdf.PdfEndPage
pdf.PdfSavePdf

Set olMsg = Nothing
Set olObj = Nothing

User avatar
Fabio
Posts: 173
Joined: Thu Aug 27, 2020 9:57 am

Re: VB6 Add image to PDF

Post by Fabio » Fri Aug 28, 2020 4:38 pm

Hi Ben,

Here's the code you need:

Code: Select all

Dim oGdPicturePDF As New GdPicturePDF()
Dim oGdPictureImaging As New GdPictureImaging()
'Creating a new empty PDF document.
If oGdPicturePDF.NewPDF() = GdPictureStatus.OK Then
    'Just to remind you that units are set to points and the origin is set to bottom left by default.
    'Loading an image from a file.
    Dim imageID As Integer = oGdPictureImaging.CreateGdPictureImageFromFile("input.tif")
    If oGdPictureImaging.GetStat() = GdPictureStatus.OK Then
        'Adding a new empty page into the PDF document.
        oGdPicturePDF.NewPage(PdfPageSizes.PdfPageSizeA4)
        'Getting the page width and height.
        Dim pageWidth As Single = oGdPicturePDF.GetPageWidth()
        Dim pageHeight As Single = oGdPicturePDF.GetPageHeight()
        'Adding an image as a resource into the PDF document - we decided not to draw the image in this moment.
        Dim imageResName As String = oGdPicturePDF.AddImageFromGdPictureImage(imageID, False, False)
        'Drawing the image resource onto the current page and saving the PDF document.
        If (oGdPicturePDF.GetStat() <> GdPictureStatus.OK) OrElse
           (oGdPicturePDF.DrawImage(imageResName, pageWidth / 3, pageHeight / 3, pageWidth / 3, pageHeight / 3) <> GdPictureStatus.OK) OrElse
           (oGdPicturePDF.SaveToFile("output.pdf") <> GdPictureStatus.OK) Then
            MessageBox.Show("The example has NOT been followed successfully. Error: " + oGdPicturePDF.GetStat().ToString(), "Adding an image Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
        'Releasing the image.
        oGdPictureImaging.ReleaseGdPictureImage(imageID)
    Else
        MessageBox.Show("The image can't be loaded. Error: " + oGdPictureImaging.GetStat().ToString(), "Adding an image Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End If
    oGdPicturePDF.CloseDocument()
Else
    MessageBox.Show("The new PDF document can't be created. Error: " + oGdPicturePDF.GetStat().ToString(), "Adding an image Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
oGdPictureImaging.Dispose()
oGdPicturePDF.Dispose()
Tell me if it works this way.

Cheers,
Fabio de Rose
Solution Engineer

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest