Add print area content (appending) at the end of initial existed pdf file in a new page section

后端 未结 2 769
无人及你
无人及你 2020-12-21 11:30

For generating a report, I have create pdf with bellow approach.

ActiveSheet.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    fileName:=ThisWorkbook.path &am         


        
2条回答
  •  南方客
    南方客 (楼主)
    2020-12-21 11:49

    You just need the "Acrobat" library.

    One simple solution is to use the native ExportAsFixedFormat method to save each section as a separate PDF file first, e.g. "C:\temp\Part1.pdf" and "C:\temp\Part2.pdf"

    Then use the InsertPages method in the Acrobat API as per example below:

    Sub MergePDF()
    
    Dim AcroApp As Acrobat.CAcroApp
    
    Dim Part1Document As Acrobat.CAcroPDDoc
    Dim Part2Document As Acrobat.CAcroPDDoc
    
    Dim numPages As Integer
    
    Set AcroApp = CreateObject("AcroExch.App")
    
    Set Part1Document = CreateObject("AcroExch.PDDoc")
    Set Part2Document = CreateObject("AcroExch.PDDoc")
    
    Part1Document.Open ("C:\temp\Part1.pdf")
    Part2Document.Open ("C:\temp\Part2.pdf")
    
    ' Insert the pages of Part2 after the end of Part1
    numPages = Part1Document.GetNumPages()
    
    If Part1Document.InsertPages(numPages - 1, Part2Document, 
    0, Part2Document.GetNumPages(), True) = False Then
        MsgBox "Cannot insert pages"
    End If
    
    If Part1Document.Save(PDSaveFull, "C:\temp\MergedFile.pdf") = False Then
        MsgBox "Cannot save the modified document"
    End If
    
    Part1Document.Close
    Part2Document.Close
    
    AcroApp.Exit
    Set AcroApp = Nothing
    Set Part1Document = Nothing
    Set Part2Document = Nothing
    
    MsgBox "Done"
    
    End Sub
    

    Reference: http://www.khk.net/wordpress/2009/03/04/adobe-acrobat-and-vba-an-introduction/

    Adobe Developer Guide: http://www.adobe.com/devnet/acrobat/pdfs/iac_developer_guide.pdf

    Adobe API Reference: http://www.adobe.com/devnet/acrobat/pdfs/iac_api_reference.pdf

提交回复
热议问题