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

删除回忆录丶 提交于 2019-11-28 09:37:36

问题


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

ActiveSheet.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    fileName:=ThisWorkbook.path & "\rep.pdf", _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=False

In the ActiveSheet, there were specified Print Area, Witch contains a Table, so table's column filtered value have change programmatically, and need:

I want new face of the print area that reforms by changing filtering criteria; get appends at the end of initial pdf file quietly, without creating a new pdf file, in a new page section.

How can I do that?

I have installed ADOBE Acrobat Professional on my system and able to add appropriate references in VBA references.


回答1:


In the Solution, I provide there is no need of Acrobat Pro. We suppose that we have a table which named: Table2. We have also a help sheet(to store filtered tables) which is Hiden and named: Help.

Option Explicit

Sub print_to_pdf()
    Dim sh  As Long
    Dim rg  As Range
    Dim Rng As Range
    Dim rw  As Range

    Application.ScreenUpdating = False

    For Each rw In Range("Table2[#All]").Rows

        If rw.EntireRow.Hidden = False Then
            If Rng Is Nothing Then Set Rng = rw
            Set Rng = Union(rw, Rng)
        End If

    Next

    Rng.Copy

    With Sheets("help")
        .Visible = True
         sh = .Cells(Rows.Count, "A").End(xlUp).Row + 2
        Set rg = Range("a3" & ":" & "a" & sh - 2)

        .Activate
        .Cells(sh, "A").Select
        ActiveSheet.Paste

        ActiveSheet.PageSetup.PrintArea = rg

        ActiveSheet.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=ThisWorkbook.Path & "\rep.pdf", _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False

        .Visible = False
    End With

    Application.ScreenUpdating = True

    MsgBox "Your PDF Has been Created with Success!!", vbInformation
End Sub



回答2:


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



来源:https://stackoverflow.com/questions/46327666/add-print-area-content-appending-at-the-end-of-initial-existed-pdf-file-in-a-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!