Optimize PDF using Aspose.PDF for Go via C++

Optimize PDF Document

Toolkit by Aspose.PDF for Go via C++ allows you to optimize a PDF document.

This snippet is useful for applications where reducing the size or enhancing the efficiency of PDF files is critical, such as for web uploads, archiving, or sharing over limited bandwidth.

  1. The Open method loads the specified PDF file (sample.pdf) into memory.
  2. The Optimize method reduces the file size by performing optimizations like removing unused objects, compressing images, flattening annotations, unembedding fonts, and enabling content reuse. These steps help reduce storage requirements and improve processing speed for the PDF document.
  3. The SaveAs method saves the optimized PDF to a new file.

    package main

    import "github.com/aspose-pdf/aspose-pdf-go-cpp"
    import "log"

    func main() {
      // Open(filename string) opens a PDF-document with filename
      pdf, err := asposepdf.Open("sample.pdf")
      if err != nil {
        log.Fatal(err)
      }
      // Optimize() optimizes PDF-document content
      err = pdf.Optimize()
      if err != nil {
        log.Fatal(err)
      }
      // SaveAs(filename string) saves previously opened PDF-document with new filename
      err = pdf.SaveAs("sample_Optimize.pdf")
      if err != nil {
        log.Fatal(err)
      }
      // Close() releases allocated resources for PDF-document
      defer pdf.Close()
    }