|
| 1 | +# Aspose.PDF for Go via C++ |
| 2 | + |
| 3 | +The package asposepdf is a powerful toolkit that allows developers to manipulate PDF files directly and helps do various tasks for PDF. |
| 4 | +Contains unique features for converting PDF to other formats. |
| 5 | + |
| 6 | +## Features |
| 7 | + |
| 8 | +### PDF Processing |
| 9 | + |
| 10 | +- **Core operation:** New, Open, Save, SaveAs, Close, SetLicense, WordCount, CharacterCount |
| 11 | +- **Page core operation:** Add, Insert, Delete, Count, WordCount, CharacterCount, IsBlank |
| 12 | +- **Organize:** Optimize, OptimizeResource, Grayscale, Rotate, SetBackground, Repair |
| 13 | +- **Page organize:** Rotate, SetSize, Grayscale, AddText |
| 14 | +- **Others:** Get contents as plain text |
| 15 | + |
| 16 | +### PDF converting and saving |
| 17 | + |
| 18 | +- **Microsoft Office:** DOC, DOCX, XLSX, PPTX |
| 19 | +- **Images:** JPEG, PNG, BMP, TIFF |
| 20 | +- **Others:** EPUB, DICOM, SVG, XPS, TEX, TXT |
| 21 | + |
| 22 | +## Platforms |
| 23 | + |
| 24 | +Implemented support for Linux x64, macOS x86_64, macOS arm64 and Windows x64 platforms. Used [cgo](https://go.dev/wiki/cgo). |
| 25 | + |
| 26 | +The platform-specific version of the dynamic library from the 'lib'-folder in the package's root directory is required for distributing the resulting application: |
| 27 | +- *libAsposePDFforGo_linux_amd64.so* for Linux x64 platform |
| 28 | +- *libAsposePDFforGo_darwin_arm64.dylib* for macOS arm64 platform |
| 29 | +- *libAsposePDFforGo_darwin_amd64.dylib* for macOS x86_64 platform |
| 30 | +- *AsposePDFforGo_windows_amd64.dll* for Windows x64 platform. |
| 31 | + |
| 32 | +Windows x64 platform requires [MinGW-W64](https://www.mingw-w64.org/) installed. |
| 33 | + |
| 34 | +## Installation |
| 35 | + |
| 36 | +This package includes a large file which is stored as a bzip2 archive. |
| 37 | + |
| 38 | +1. Add the asposepdf package to Your Project: |
| 39 | + ```sh |
| 40 | + go get github.com/aspose-pdf/aspose-pdf-go-cpp@latest |
| 41 | + ``` |
| 42 | + |
| 43 | +2. Generate the large file: |
| 44 | + |
| 45 | + - **macOS and linux** |
| 46 | + |
| 47 | + 1. Open Terminal |
| 48 | + |
| 49 | + 2. List the folders of the github.com/aspose-pdf within the Go module cache: |
| 50 | + |
| 51 | + ```sh |
| 52 | + ls $(go env GOMODCACHE)/github.com/aspose-pdf/ |
| 53 | + ``` |
| 54 | + |
| 55 | + 3. Change curent folder to the specific version folder of the package obtained in the previous step: |
| 56 | + |
| 57 | + ```sh |
| 58 | + cd $(go env GOMODCACHE)/github.com/aspose-pdf/[email protected] |
| 59 | + ``` |
| 60 | + Replace `@vx.x.x` with the actual package version. |
| 61 | + |
| 62 | + |
| 63 | + 4. Run go generate with superuser privileges: |
| 64 | + |
| 65 | + ```sh |
| 66 | + sudo go generate |
| 67 | + ``` |
| 68 | + |
| 69 | + - **Windows** |
| 70 | + |
| 71 | + 1. Open Command Prompt |
| 72 | + |
| 73 | + 2. List the folders of the github.com/aspose-pdf within the Go module cache: |
| 74 | + |
| 75 | + ```cmd |
| 76 | + for /f "delims=" %G in ('go env GOMODCACHE') do for /d %a in ("%G\github.com\aspose-pdf\*") do echo %~fa |
| 77 | + ``` |
| 78 | +
|
| 79 | + 3. Change curent folder to the specific version folder of the package obtained in the previous step: |
| 80 | +
|
| 81 | + ```cmd |
| 82 | + cd <specific version folder of the package> |
| 83 | + ``` |
| 84 | +
|
| 85 | + 4. Run go generate: |
| 86 | +
|
| 87 | + ```cmd |
| 88 | + go generate |
| 89 | + ``` |
| 90 | +
|
| 91 | + 5. Add specific version folder of the package to the %PATH% environment variable: |
| 92 | +
|
| 93 | + ```cmd |
| 94 | + setx PATH "%PATH%;<specific version folder of the package>\lib\" |
| 95 | + ``` |
| 96 | + Replace `<specific version folder of the package>` with the actual path obtained from step 2. |
| 97 | +
|
| 98 | +
|
| 99 | +## Quick Start |
| 100 | +All code snippets are contained in the [snippet](https://github.com/aspose-pdf/aspose-pdf-go-cpp). |
| 101 | +
|
| 102 | +### Hello World! |
| 103 | +
|
| 104 | +```go |
| 105 | +package main |
| 106 | +
|
| 107 | +import "github.com/aspose-pdf/aspose-pdf-go-cpp" |
| 108 | +import "log" |
| 109 | +
|
| 110 | +func main() { |
| 111 | + // Create new PDF-document |
| 112 | + pdf, err := asposepdf.New() |
| 113 | + if err != nil { |
| 114 | + log.Fatal(err) |
| 115 | + } |
| 116 | + // Add new page |
| 117 | + err = pdf.PageAdd() |
| 118 | + if err != nil { |
| 119 | + log.Fatal(err) |
| 120 | + } |
| 121 | + // Set page size A4 |
| 122 | + err = pdf.PageSetSize(1, asposepdf.PageSizeA4) |
| 123 | + if err != nil { |
| 124 | + log.Fatal(err) |
| 125 | + } |
| 126 | + // Add text on first page |
| 127 | + err = pdf.PageAddText(1, "Hello World!") |
| 128 | + if err != nil { |
| 129 | + log.Fatal(err) |
| 130 | + } |
| 131 | + // Save PDF-document with "hello.pdf" name |
| 132 | + err = pdf.SaveAs("hello.pdf") |
| 133 | + if err != nil { |
| 134 | + log.Fatal(err) |
| 135 | + } |
| 136 | + // Release allocated resources |
| 137 | + defer pdf.Close() |
| 138 | +} |
| 139 | +``` |
| 140 | +
|
| 141 | +### Save PDF as Office Formats |
| 142 | +
|
| 143 | +One of the most popular features of Aspose.PDF for Go via C++ is to convert PDF documents to other formats without needing to understand the underlying structure of the resultant format. |
| 144 | +
|
| 145 | +Give the following snippet a try with your samples: |
| 146 | +
|
| 147 | +```go |
| 148 | +package main |
| 149 | +
|
| 150 | +import "github.com/aspose-pdf/aspose-pdf-go-cpp" |
| 151 | +import "log" |
| 152 | +
|
| 153 | +func main() { |
| 154 | + // Open(filename string) opens a PDF-document with filename |
| 155 | + pdf, err := asposepdf.Open("sample.pdf") |
| 156 | + if err != nil { |
| 157 | + log.Fatal(err) |
| 158 | + } |
| 159 | + // SaveDocX(filename string) saves previously opened PDF-document as DocX-document with filename |
| 160 | + err = pdf.SaveDocX("sample.docx") |
| 161 | + if err != nil { |
| 162 | + log.Fatal(err) |
| 163 | + } |
| 164 | + // Close() releases allocated resources for PDF-document |
| 165 | + defer pdf.Close() |
| 166 | +} |
| 167 | +``` |
| 168 | +### Extract Text From Whole PDF |
| 169 | +
|
| 170 | +```go |
| 171 | +package main |
| 172 | +
|
| 173 | +import "github.com/aspose-pdf/aspose-pdf-go-cpp" |
| 174 | +import "log" |
| 175 | +import "fmt" |
| 176 | +
|
| 177 | +func main() { |
| 178 | + // Open(filename string) opens a PDF-document with filename |
| 179 | + pdf, err := asposepdf.Open("sample.pdf") |
| 180 | + if err != nil { |
| 181 | + log.Fatal(err) |
| 182 | +
|
| 183 | + } |
| 184 | + // ExtractText() returns PDF-document contents as plain text |
| 185 | + txt, err := pdf.ExtractText() |
| 186 | + if err != nil { |
| 187 | + log.Fatal(err) |
| 188 | + } |
| 189 | + // Print |
| 190 | + fmt.Println("Extracted text:\n", txt) |
| 191 | + // Close() releases allocated resources for PDF-document |
| 192 | + defer pdf.Close() |
| 193 | +} |
| 194 | +``` |
| 195 | +
|
| 196 | +## Testing |
| 197 | +
|
| 198 | +The test run from the root package folder: |
| 199 | +
|
| 200 | +```sh |
| 201 | +go test -v |
| 202 | +``` |
| 203 | +
|
| 204 | +## License |
| 205 | +
|
| 206 | +- The **Go source code** is licensed under the [MIT License](LICENSE). |
| 207 | +- The **shared library (`AsposePDFforGo_windows_amd64.dll`, `libAsposePDFforGo_linux_amd64.so`, `libAsposePDFforGo_darwin_amd64.dylib`, `libAsposePDFforGo_darwin_arm64.dylib`)** is proprietary and requires a commercial license. |
| 208 | + To use the full functionality, you must obtain a license. |
| 209 | +
|
| 210 | +### Evaluation version |
| 211 | +
|
| 212 | +You can use Aspose.PDF for Go via C++ free of cost for evaluation.The evaluation version provides almost all functionality of the product with certain limitations. The same evaluation version becomes licensed when you purchase a license and add a couple of lines of code to apply the license. |
| 213 | +
|
| 214 | +>If you want to test Aspose.PDF for Go without the evaluation version limitations, you can also request a 30-day Temporary License. Please refer to [How to get a Temporary License?](https://purchase.aspose.com/temporary-license) |
| 215 | +
|
| 216 | +### Limitation of an evaluation version |
| 217 | +
|
| 218 | +We want our customers to test our components thoroughly before buying so the evaluation version allows you to use it as you would normally. |
| 219 | +
|
| 220 | +- **Documents created with an evaluation watermark.** The evaluation version of Aspose.PDF for Go provides full product functionality, but all pages in the generated files are watermarked with the text "Evaluation Only. Created with Aspose.PDF. Copyright 2002-2025 Aspose Pty Ltd." at the top. |
| 221 | +- **Limit the number of pages that can be processed.** In the evaluation version, you can only process the first four pages of a document. |
| 222 | +
|
| 223 | +### Use in production |
| 224 | +
|
| 225 | +A commercial license key is required in a production environment. Please contact us to <a href="https://purchase.aspose.com/buy">purchase a commercial license</a>. |
| 226 | +
|
| 227 | +### Apply license |
| 228 | +
|
| 229 | +Applying a license to enable full functionality of the Aspose.PDF for Go using a license file (Aspose.PDF.GoViaCPP.lic). |
| 230 | +
|
| 231 | +```go |
| 232 | +
|
| 233 | + package main |
| 234 | +
|
| 235 | + import "github.com/aspose-pdf/aspose-pdf-go-cpp" |
| 236 | + import "log" |
| 237 | +
|
| 238 | + func main() { |
| 239 | + // Open(filename string) opens a PDF-document with filename |
| 240 | + pdf, err := asposepdf.Open("sample.pdf") |
| 241 | + if err != nil { |
| 242 | + log.Fatal(err) |
| 243 | + } |
| 244 | + // SetLicense(filename string) licenses with filename |
| 245 | + err = pdf.SetLicense("Aspose.PDF.GoViaCPP.lic") |
| 246 | + if err != nil { |
| 247 | + log.Fatal(err) |
| 248 | + } |
| 249 | + // Working with PDF-document |
| 250 | + // ... |
| 251 | + // Close() releases allocated resources for PDF-document |
| 252 | + defer pdf.Close() |
| 253 | + } |
| 254 | +``` |
| 255 | +
|
| 256 | +[Home](https://www.aspose.com/) | [Product Page](https://products.aspose.com/pdf/go-cpp/) | [Docs](https://docs.aspose.com/pdf/go-cpp/) | [Demos](https://products.aspose.app/pdf/family) | [API Reference](https://reference.aspose.com/pdf/go-cpp/) | [Examples](https://github.com/aspose-pdf/aspose-pdf-go-cpp/) | [Blog](https://blog.aspose.com/category/pdf/) | [Search](https://search.aspose.com/) | [Free Support](https://forum.aspose.com/c/pdf) | [Temporary License](https://purchase.aspose.com/temporary-license) |
0 commit comments