首页 > 解决方案 > printing pdf to multiple pages using jscrip via VBA

问题描述

After many days searching to print multiples pages of a pdf file in a single sheet by using only VBA code on Excel I find a way to use javascript via VBA thanks to user ReFran https://stackoverflow.com/a/49538950/16279586

Combining the code ReFran wrote with this reference https://opensource.adobe.com/dc-acrobat-sdk-docs/acrobatsdk/pdfs/acrobatsdk_jsapiref.pdf#G4.1959527 I build some Frankenstein monster that doesn't work at all.

Suposedly it will open a pdf file, then print 2x2 pages on a single sheet and save in the same document the changes (turning from a 4 pages pdf to a 1 page pdf) I'm a newbie in VBA and absolutely ignorant in javascript, can anyone give me a helping hand with this?

Sub test()
Dim app As acroApp
Dim avdoc As AcroAVDoc
Dim aform As AFormApp


Path = "C:\1.pdf"

Set app = CreateObject("Acroexch.app")
app.Show
Set avdoc = CreateObject("AcroExch.AVDoc")
Set aform = CreateObject("AFormAut.App") 'from AFormAPI

If avdoc.Open(Path, "") Then
    '// write some js code on a vbs variable
    
    js = "pp = this.getPrintParams();" _
    & "pp.printerName = "";" _
    & "pp.pageHandling = pp.constants.handling.nUp;" _
    & "pp.nUpPageOrders = pp.constants.nUpPageOrders.Horizontal;" _
    & "pp.nUpNumPagesH = 2;" _
    & "pp.nUpNumPagesV = 2;" _
    & "pp.nUpPageBorder=true;" _
    & "this.print(pp);"
     '//execute the js code
    aform.Fields.ExecuteThisJavaScript js
End If

Set aform = Nothing
Set avdoc = Nothing
Set app = Nothing
End Sub

标签: javascriptexcelvbapdf

解决方案


推荐阅读