2019年4月10日 星期三

[GrapeCity][Spread Windows Forms] combine sheet to one PDF 實作合併sheet 匯出一個PDF檔案




'單一FPSpread

    Public Sub CombinePDFFiles()
        Dim printset As FarPoint.Win.Spread.PrintInfo = New FarPoint.Win.Spread.PrintInfo()
        printset.PrintToPdf = True
        printset.PdfWriteMode = PdfWriteMode.Append
        printset.PdfFileName = "D:\TEST\testPDF_" & Format(Now, "yyyyMMddHHmmss") & ".pdf"

        FpSpread1.Sheets(0).PrintInfo = printset
        FpSpread1.Sheets(1).PrintInfo = printset
        FpSpread1.Sheets(2).PrintInfo = printset

        FpSpread1.PrintSheet(-1)
    End Sub


原本希望將多個FPSpread 直接合併匯出一個PDF檔案,實測後是不支援的
所以比較好的做法是直接將兩個檔案合併到同一個FPSpread中




'多個FPSpread

    Public Sub CombinePDFFiles()
        Dim Spread As FarPoint.Win.Spread.FpSpread = New FarPoint.Win.Spread.FpSpread()
        Spread.BringToFront()

        Spread.Size = New System.Drawing.Size(600, 300)
        Spread.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
        Dim coll As FarPoint.Win.Spread.SheetViewCollection
        coll = Spread.Sheets
        coll.Add(FpSpread1.Sheets(0))
        coll.Add(FpSpread2.Sheets(0))

        Dim printset As FarPoint.Win.Spread.PrintInfo = New FarPoint.Win.Spread.PrintInfo()
        printset.PrintToPdf = True
        printset.PdfWriteMode = PdfWriteMode.Append

        printset.PdfFileName = "D:\TEST\testPDF_" & Format(Now, "yyyyMMddHHmmss") & ".pdf"

        Me.Controls.Add(Spread)
        Spread.Sheets(0).PrintInfo = printset
        Spread.Sheets(1).PrintInfo = printset
        Spread.PrintSheet(-1)


    End Sub


這方法也是可行,但我覺得如果設計成自動產生會造成元件的負擔
應該蠻容易crash的


程式碼