[Solved] [Calc] Export a page to PDF

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
savezin
Posts: 7
Joined: Thu Feb 04, 2021 6:01 pm

[Solved] [Calc] Export a page to PDF

Post by savezin »

 Edit: This is an English-speadking forum. Do not post in Italian again or all of your topics will be locked.
-- MrProgrammer, forum moderator 
Buongiorno,
chiedo aiuto per una macro che non mi riesce del tutto.
Vorrei esportare in pdf solo la pagina "ORDINE" da un file calc e salvarla in una cartella (C:/percorso/desktop/), rinominandola utilizzando il valore contenuto in una cella (B3).
allego il file e la macro che ho registrato, ma mi salva tutto il documento e non la sola pagina ORDINE.
GRAZIE PER L'AIUTO
PROVA NUOVA.ods
(22.14 KiB) Downloaded 227 times

Code: Select all

sub esporta_pdf2
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "ToPoint"
args1(0).Value = "$A$1:$G$43"

dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, args1())

rem ----------------------------------------------------------------------
dim args2(2) as new com.sun.star.beans.PropertyValue
args2(0).Name = "URL"
args2(0).Value = "file:///C:/Users/zinis/Desktop/schede/ORDINE%20.pdf"
args2(1).Name = "FilterName"
args2(1).Value = "calc_pdf_Export"
args2(2).Name = "FilterData"
args2(2).Value = 
dispatcher.executeDispatch(document, ".uno:ExportToPDF", "", 0, args2())


end sub
Title Edited. A descriptive title for posts helps others who are searching for solutions and increases the chances of a reply (Hagar, Moderator).
Last edited by Hagar Delest on Mon Feb 08, 2021 7:54 pm, edited 1 time in total.
Reason: tagged solved.
andr openoffice 4.6.0 android e win10pro
FJCC
Moderator
Posts: 9274
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: ESPORTARE PAGINA CALC IN PDF

Post by FJCC »

Try this macro. It will print only the sheet ORDINE if you go to the menu Tools -> Options -> OpenOffice Calc -> Print and select Print Only Selected Sheets.
Be sure to change the file path to what you need.
You do not need that whole FilterData value, I included the whole thing so you can see the options.

Code: Select all

sub esporta_pdf2
rem ----------------------------------------------------------------------
rem define variables
dim document as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "ToPoint"
args1(0).Value = "$A$1:$G$43"

dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, args1())
oSheet = ThisComponent.Sheets.getByName("ORDINE")
'ThisComponent
oCell = oSheet.getCellRangeByName("B3")
CellText = oCell.String
FileName = "file:///C:/Users/fjcc/Desktop/ORDINE" & CellText & ".pdf"
rem ----------------------------------------------------------------------
dim args2(2) as new com.sun.star.beans.PropertyValue
args2(0).Name = "URL"
args2(0).Value = FileName
args2(1).Name = "FilterName"
args2(1).Value = "calc_pdf_Export"
args2(2).Name = "FilterData"
args2(2).Value = Array(Array("UseLosslessCompression",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("Quality",0,90,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("ReduceImageResolution",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("MaxImageResolution",0,300,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("UseTaggedPDF",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("SelectPdfVersion",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("ExportNotes",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("ExportBookmarks",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("OpenBookmarkLevels",0,-1,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("UseTransitionEffects",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("IsSkipEmptyPages",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("IsAddStream",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("EmbedStandardFonts",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("FormsType",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("ExportFormFields",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("AllowDuplicateFieldNames",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("HideViewerToolbar",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("HideViewerMenubar",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("HideViewerWindowControls",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("ResizeWindowToInitialPage",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("CenterWindow",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("OpenInFullScreenMode",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("DisplayPDFDocumentTitle",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("InitialView",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("Magnification",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("Zoom",0,100,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("PageLayout",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("FirstPageOnLeft",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("InitialPage",0,1,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("Printing",0,2,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("Changes",0,4,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("EnableCopyingOfContent",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("EnableTextAccessForAccessibilityTools",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("ExportLinksRelativeFsys",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("PDFViewSelection",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("ConvertOOoTargetToPDFTarget",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("ExportBookmarksToPDFDestination",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("_OkButtonString",0,"",com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("EncryptFile",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("PreparedPasswords",0,,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("RestrictPermissions",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("PreparedPermissionPassword",0,Array(),com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("",0,,com.sun.star.beans.PropertyState.DIRECT_VALUE))

dispatcher.executeDispatch(document, ".uno:ExportToPDF", "", 0, args2())


end sub
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
savezin
Posts: 7
Joined: Thu Feb 04, 2021 6:01 pm

Re: ESPORTARE PAGINA CALC IN PDF

Post by savezin »

Thank you FJCC, I try your macro and It works :super: , but the only thing that goes wrong is that all the document is saved and not only the page 9 (ordine). I follow your instruction to go in option and it was already clicked "print only selected sheets".
What can i do to correct this problem?
Greetings
andr openoffice 4.6.0 android e win10pro
FJCC
Moderator
Posts: 9274
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: [Calc] Export a page to PDF

Post by FJCC »

One way to fix that is to select the cells A1:G43 on the sheet ORDINE and the choose the menu item Format -> Print Ranges -> Define.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
savezin
Posts: 7
Joined: Thu Feb 04, 2021 6:01 pm

Re: [Calc] Export a page to PDF

Post by savezin »

Thank you very much [Fjcc], now it works correctly. :-)
andr openoffice 4.6.0 android e win10pro
User avatar
DiGro
Posts: 173
Joined: Mon Oct 08, 2007 1:31 am
Location: Hoorn NH, The Netherlands

Re: [Solved] [Calc] Export a page to PDF

Post by DiGro »

If you don't want to use the Dispatcher you might want to use this one:

Code: Select all

Sub esporta_pdf2()

Dim Doc as Object
Dim Sheet as Object
Dim CellRange as Object
Dim Cell as Object

Doc   = ThisComponent
Controller = Doc.CurrentController
Sheet = Doc.Sheets.getByName("ORDINE")
CellRange = Sheet.getCellRangeByName("$A$1:$G$43")
Controller.select(CellRange)

Cell = Sheet.getCellRangeByName("B3")
CellText = Cell.String

Dim FilterProps(0) as New com.sun.star.beans.PropertyValue
FilterProps(0).Name = "PageRange"
FilterProps(0).Value = "9"
    
dim args2(1) as new com.sun.star.beans.PropertyValue
args2(0).Name = "FilterName"
args2(0).Value = "calc_pdf_Export"
args2(1).Name = "FilterData"
args2(1).Value = FilterProps

Doc.storeToURL("file:///c:/users/zinis/Desktop/schede/ORDINE"& CellText & ".pdf",args2())

End sub
Thanks to FJCC for earlier code at viewtopic.php?f=20&t=79013
____________
DiGro

AOO 4.1.15 (Dutch) on Windows 11. Scanned with Ziggo Safe Online (F-Secure)
Post Reply