Optimize PDF Resources using Go via C++

Optimize PDF Resources

Optimize resources in the document:

  1. Resources that are not used on the document pages are removed.
  2. Equal resources are joined into a single object.
  3. Unused objects are deleted.

Optimization may include compressing images, removing unused objects, and optimizing fonts for reduced file size and improved performance. Any error during this operation is logged and terminates the program.

  1. The Open method loads the specified PDF file (sample.pdf) into memory.
  2. Optimizes the resources within the PDF for efficiency using OptimizeResource method.
  3. The SaveAs method saves the optimized PDF to a new file.

The following code snippet shows how to optimize a PDF document.


    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)
      }
      // OptimizeResource() optimizes resources of PDF-document
      err = pdf.OptimizeResource()
      if err != nil {
        log.Fatal(err)
      }
      // SaveAs(filename string) saves previously opened PDF-document with new filename
      err = pdf.SaveAs("sample_OptimizeResource.pdf")
      if err != nil {
        log.Fatal(err)
      }
      // Close() releases allocated resources for PDF-document
      defer pdf.Close()
    }