pdf去除空白边及转为png
@ lsq | 星期三,十二月 9 日,2020 年 | 1 分钟阅读 | 更新于 星期三,十二月 9 日,2020 年

Crop white space of pdf and using gs convert to png

  1. PDFCROP takes a PDF file as input, calculates the BoundingBox for each page by the help of ghostscript and generates a output PDF file with removed margins.pdfcrop
  2. Normally pdfcrop will be installed by the TeX system.
  3. https://superuser.com/questions/415707/export-excel-graphs-as-vector-graphics-files-e-g-svgs
  4. briss/xltoolbox
  5. Export Excel chart to SVG creates an empty file
pdfcrop foo.pdf f.pdf
gswin64c.exe -dNOPAUSE -sDEVICE=png16m -r600  -sOutputFile=foo.png o.pdf -dBATCH
Sub SaveSelectedChartAsPng()
Dim tmpdir As String, outputPdf As String, outputPng As String, cropPdf As String
Dim wshell As Object

    Set wshell = CreateObject("WScript.Shell")
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Application.EnableEvents = False
    Application.DisplayAlerts = False
    
    tmpdir = VBA.Environ("temp")
    outputPdf = tmpdir & "\" & "figure.pdf"
    outputPng = ActiveWorkbook.Path & "\" & "outputValue.png"
    cropPdf = tmpdir & "\" & "figure_crop.pdf"
    
    With ActiveChart.PageSetup
        .Orientation = xlLandscape
        .FitToPagesWide = 1
        .FitToPagesTall = 1
        .LeftMargin = Application.InchesToPoints(0)
        .RightMargin = Application.InchesToPoints(0)
        .TopMargin = Application.InchesToPoints(0)
        .BottomMargin = Application.InchesToPoints(0)
        .HeaderMargin = Application.InchesToPoints(0)
        .FooterMargin = Application.InchesToPoints(0)
    End With
    ActiveChart.ExportAsFixedFormat xlTypePDF, outputPdf, Quality:=xlQualityStandard, IncludeDocProperties:=True, _
                IgnorePrintAreas:=False, OpenAfterPublish:=False
    
    wshell.Run "pdfcrop " & outputPdf & " " & cropPdf, 0, True
    wshell.Run "gswin64c.exe -dBATCH -dNOPAUSE -sDEVICE=png16m -r1200  -sOutputFile=" & outputPng & " " & cropPdf, 0, True
    wshell.Run "rm -rf " & outputPdf & " " & cropPdf, 0, True
    ' 需要安装texlive,其已经包含了pdfcrop程序
    'Shell ("pdfcrop " & outputPdf & " " & cropPdf & " && " & "gswin64c.exe -dBATCH -dNOPAUSE -sDEVICE=png16m -r600  -sOutputFile=" & outputPng & " " & cropPdf & " && " & "rm -rf " & outputPdf & " " & cropPdf)
    '需要安装ghostscript
    'Shell ("gswin64c.exe -dBATCH -dNOPAUSE -sDEVICE=png16m -r600  -sOutputFile=" & outputPng & " " & cropPdf)
    '需要安装msys2, rm命令是linux 下命令
    'Shell ("rm -rf " & outputPdf & " " & cropPdf)
    
    Set wshell = Nothing
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    Application.EnableEvents = True
    Application.DisplayAlerts = True
    
End Sub

© 2011 - 2023 lsq 的博客

Powered by Hugo with theme Dream.

avatar
关于我

lsq 的 ❤️ 博客

记录一些 🌈 生活上,财务相关技术上的事

毕业于 🏫 东北财经大学

空闲时间会做分享财务工作上相关技术

主要分享是:

  • VBA & Power Query
  • Excel 图表制作
  • LaTeX

写着玩:

  • Ruby
  • Bash shell
  • Python

目前在深圳工作

– 2021 年 04 月 11 日更新