Macro Importing PDF into Calc ODS

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
LinuksGuru
Posts: 33
Joined: Fri Feb 12, 2010 11:32 pm

Macro Importing PDF into Calc ODS

Post by LinuksGuru »

Hi !

I'm need to brew a macro which imports PDF (via drag-n-drop on any opened Calc document page) into spreadsheet. Since Calc can't embed PDF the only visible solution is to preliminary convert PDF to ODG, store it in temporary file, and then embed into spreadsheet.

1) How to intercept drop PDF file event onto *any* spreadsheet within worksheet, and convert to temporary ODG without "File -> Save as" dialog?

2) Since original PDF may span several pages, resulting ODG pages (A4) needed to be imported one by one, 1st page at X=1, Y=1; 2md page at X=1, Y = 297+1, etc.
I checked how multi-page ODG (made from PDF) is being imported into Calc ODS via drag-n-drop, it doesn't looks good, so importing pages one by one and anchoring them into particular coordinates within Calc spreadsheet seems like the best solution.

Thanks in advance.
User avatar
Zizi64
Volunteer
Posts: 11352
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Q: Macro Importing PDF into Calc ODS

Post by Zizi64 »

Do you want to import some calculable data, or only some decorative images?

Can you upload some sample files here?
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
LinuksGuru
Posts: 33
Joined: Fri Feb 12, 2010 11:32 pm

Re: Q: Macro Importing PDF into Calc ODS

Post by LinuksGuru »

No need to parse data, only readable ODG picture made from PDF.
Sample PDF attached, it could be 1, 2 or even 3 pages long.
Attachments
ServiceReport-Sample_2022-04-25_21-57.pdf
(57.58 KiB) Downloaded 131 times
LinuksGuru
Posts: 33
Joined: Fri Feb 12, 2010 11:32 pm

Re: Q: Macro Importing PDF into Calc ODS

Post by LinuksGuru »

Document Event-Driven Macros
https://help.libreoffice.org/latest/he/ ... 40000.html

No "drop object / file" event definition unfortunately...
nickGiard
Posts: 16
Joined: Fri Nov 10, 2017 5:57 pm

Re: Macro Importing PDF into Calc ODS

Post by nickGiard »

PDF is imported well in Draw. If you examine the file content.xml you can understand how pdf is : a lot of draw:frame svg:width="6.386cm" svg:height="0.797cm" svg:x="5.594cm" svg:y="3.395cm" with inner draw:text-box.
PDF is a description of positions and string of words in the page. To import in ods you must elaborate this data.
LibreOffice 6.3 on Windows 10 64bit
User avatar
Sébastien C
Posts: 111
Joined: Mon Jan 04, 2010 5:06 pm
Location: Meymac, France

Re: Macro Importing PDF into Calc ODS

Post by Sébastien C »

It’s an idea like any other ... with elements that I already have. But maybe it can help.

The PDF files must be in the same folder as the spreadsheet (but this is obviously changeable according to your needs).

Clicking once on the preview IT SELF launches the PDF file reader installed by default on your system, and therefore, to view PDFs correctly (better than with Draw), especially if they are several pages long.

Previewing PDFs in Calc DOES NOT WORK under Apache OpenOffice.

Code: Select all

 Option Explicit


' ╔═════════════════════════════════════════════════════════════════════════════════════════════╗
' ║ Preview specific PDF file on the spreadsheet.                                               ║█
' ╚═════════════════════════════════════════════════════════════════════════════════════════════╝█
'  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀

Sub PDFpreview(myURL As String)
 Dim   myPreview As Object, mySheet As Object
 Dim myDirectory As String,  myFile As String, myFinalURL As String, interline As String

 If Not globalScope.basicLibraries.isLibraryLoaded("Tools") Then globalScope.basicLibraries.loadLibrary("Tools")

     mySheet = thisComponent.sheets.getByName("PDF preview")
   myPreview = mySheet.drawPage.Forms.getByIndex(0).getByIndex(0)
      myFile = mySheet.getCellRangeByName(getArgumentFromURL(myURL, "myCell")).string
 myDirectory = directoryNameoutofPath(convertFromUrl(thisComponent.URL), getPathSeparator)
  myFinalURL = convertToUrl(convertFromUrl(myDirectory) & getPathSeparator & myFile)


 If fileExists(myFinalURL) Then
  myPreview.imageURL = myFinalURL
 Else
  interline = chr(13) & chr(13)
  msgBox "The file at URL" & interline & convertFromUrl(myFinalURL) & interline & "doesn't exist.", 16, "ERROR : impossible preview."
 End If
End Sub

Sub clearPreview()
 Dim   myPreview As Object, mySheet As Object

            mySheet = thisComponent.sheets.getByName("PDF preview")
          myPreview = mySheet.drawPage.Forms.getByIndex(0).getByIndex(0)
 myPreview.imageURL = ""
End Sub


' ╔═════════════════════════════════════════════════════════════════════════════════════════════╗
' ║ Launch a extern application for viewing a PDF file.                                         ║█
' ╚═════════════════════════════════════════════════════════════════════════════════════════════╝█
'  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀

Sub launchPDFsystem()
 Dim mySheet As Object, myShell As Object
 Dim   myUrl As String

 myShell = createUnoService("com.sun.star.system.SystemShellExecute")
 mySheet = thisComponent.sheets.getByName("PDF preview")
   myUrl = convertToUrl(mySheet.drawPage.Forms.getByIndex(0).getByIndex(0).imageURL)

 If myUrl <> "" Then
  If getGUIType() = 1 Then myUrl = convertFromUrl(myUrl)  ' Usefull just for M$-Window$.

  myShell.execute(myUrl, "", False)                       ' The system choice the application by défault lo launch the PDF file.
 End If
End Sub
Attachments
viewPDF.ods
(37.93 KiB) Downloaded 115 times
LibreOffice v. 7.3.2.2, under GNU-Linux Mint and, in virtualization and just for tests, LibreOffice v. 7.3.2.2 an OpenOffice v. 4.1.12 under M$-W 10 :ouch: .
Post Reply