Display a pdf-file in a report?

Ole

Registered User.
Local time
Today, 11:48
Joined
Apr 15, 2012
Messages
34
Hi
I have a table containing different kind of data including attachment.
When I generate a report I can’t display the attached pdf-file. If I change it to a J-peg it’s no problem.
Does anyone knows how to display the pdf-file in a report?
 
Assuming the file is in a directory and not an attachment, use a web control using the path as the controlsource.
 
  • Like
Reactions: Ole
Assuming the file is in a directory and not an attachment, use a web control using the path as the controlsource.
It’s an attachment
 
In that case, don’t think you can. You could try setting a web control control source to the attachment but doubt it would work

only way would be to export the file to a folder and have the web control reference that per my first post
 
  • Like
Reactions: Ole
Maybe overkill for what you're looking to do but, you could write VBA to launch a console app written in .net (vb or c#) that you pass in some parameters, it executes the report to PDF, saves the attachment as a file and merges the PDFs, then deletes the PDF it created from the attachment file. There is a NuGet package called FreeSpire.PDF that could be used to merge PDF files.
 
  • Like
Reactions: Ole
Even if in attachment. The code
1. Saves to local folder
2. Displays in browser
3. Kills file when done.
 
To display a pdf you can convert the pdf to a displayable format (bitmap, jpg, ...) and display the images. That allows usage of print preview with the contents of the pdf(s) shown.

On Report_Open I use IrfanView via shell command to extract from pdf and wscriptshell.run with waitOnReturn = true as argument, to ensure that the command is executed before the rest of vba code, then a table (bound to the report) is filled with the jpg(s) path(s) to display them in an image control:
Code:
Private Sub Report_Open(Cancel As Integer)

Dim rs As DAO.Recordset
Dim sql As String

Dim db As DAO.Database
Set db = CurrentDb
  
sql = "SELECT pathToPDF FROM TableWithPathToPDF WHERE getType(pathToPDF) = 'pdf';"
Debug.Print sql

Set rs = db.OpenRecordset(sql, dbOpenSnapshot)

If rs.RecordCount > 0 Then
    Dim wsh As Object
    Set wsh = CreateObject("WScript.Shell")

    Do Until rs.EOF
        Dim wShellCmd As String
        wShellCmd = """\Path\to\irfanview\i_view32.exe"" """ & rs.Fields("pathToPDF") & """ /extract=(\Path\to\extract\to,jpg) /cmdexit"
        Debug.Print wShellCmd
        Dim ret As Variant
        ret = wsh.Run(wShellCmd, 0, True)
        rs.MoveNext
    Loop
    Set wsh = Nothing
   
    Dim oFSO As Object
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Dim oFile As Object
   
    For Each oFile In oFSO.GetFolder("\Path\to\extract\to").Files
        db.Execute "INSERT INTO TableWithPathToExtractedJPGS (pathToExtractedJPGs) VALUES ('" & oFile.Path & "');",dbFailOnError
    Next oFile
    Set oFSO = Nothing
End If
rs.Close
Set rs = Nothing
Set db = Nothing
End Sub
btw: you are aware of size limits in Access backends (2GB). Only one reason not to use the Attachment-Field type, as files can be huge. Instead save the files to a share and only save the path to file in share in db.
 
Last edited:
  • Like
Reactions: Ole

Users who are viewing this thread

Back
Top Bottom