|
1 |
| -# Aspose.PDF for Rust via C++ — Example Code |
| 1 | +# Aspose.PDF for Rust via C++ |
2 | 2 |
|
3 |
| -This repository contains working examples for the `asposepdf` Rust crate — a Rust library powered by the Aspose.PDF for С++ engine. |
| 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. |
4 | 5 |
|
5 |
| -> ⚠️ These examples require the full library distribution, which includes platform-specific native binaries built specifically for Rust. |
| 6 | +## Features |
6 | 7 |
|
7 |
| ---- |
| 8 | +### PDF Processing |
8 | 9 |
|
9 |
| -## Download the Full Library |
| 10 | +- **Create and manage documents** |
| 11 | + - `new`, `open`, `save`, `save_as`, `set_license` |
| 12 | + Create, load, save as, and save PDF; apply license keys. |
10 | 13 |
|
11 |
| -You can download the latest version of Aspose.PDF for Rust via C++ here: |
12 |
| -👉 https://releases.aspose.com/pdf/rustcpp/ |
| 14 | +- **Page management** |
| 15 | + - `add`, `insert`, `delete`, `count` |
| 16 | + Add, insert, delete, and count pages in a document. |
13 | 17 |
|
14 |
| ---- |
| 18 | +- **Document-level operations** |
| 19 | + - `optimize`, `optimize_resource`, `grayscale`, `rotate`, `set_background`, `repair` |
| 20 | + Optimize PDF layout and resources, convert to grayscale, rotate pages, set background, and repair corrupted documents. |
15 | 21 |
|
16 |
| -## Useful Links |
| 22 | +- **Page-level operations** |
| 23 | + - `rotate`, `set_size`, `grayscale`, `add_text` |
| 24 | + Rotate individual pages, set page size, convert pages to grayscale, and add text. |
17 | 25 |
|
18 |
| -[Home](https://www.aspose.com/) | [Product Page](https://products.aspose.com/pdf/rust-cpp/) | [Docs](https://docs.aspose.com/pdf/rust-cpp/) | [Demos](https://products.aspose.app/pdf/family) | [API Reference](https://reference.aspose.com/pdf/rust-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) |
| 26 | +- **Content extraction** |
| 27 | + - `extract_text` |
| 28 | + Retrieve plain text content from PDF pages. |
| 29 | + |
| 30 | +### PDF converting and saving |
| 31 | + |
| 32 | +- **Microsoft Office:** |
| 33 | + - `DOC`, `DOCX`, `XLSX`, `PPTX` |
| 34 | +- **Images:** |
| 35 | + - `JPEG`, `PNG`, `BMP`, `TIFF` |
| 36 | +- **PDFs:** |
| 37 | + - `N-UP`, `BOOKLET` |
| 38 | +- **Others:** |
| 39 | + - `EPUB`, `DICOM`, `SVG`, `XPS`, `TEX`, `TXT`, `MARKDOWN` |
| 40 | + |
| 41 | +## Platforms |
| 42 | + |
| 43 | +Implemented support for Linux x64, macOS x86_64, macOS arm64 and Windows x64 platforms. |
| 44 | + |
| 45 | +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: |
| 46 | +- *libAsposePDFforRust_linux_amd64.so* for Linux x64 platform |
| 47 | +- *libAsposePDFforRust_darwin_arm64.dylib* for macOS arm64 platform |
| 48 | +- *libAsposePDFforRust_darwin_amd64.dylib* for macOS x86_64 platform |
| 49 | +- *AsposePDFforRust_windows_amd64.dll and AsposePDFforRust_windows_amd64.lib* for Windows x64 platform. |
| 50 | + |
| 51 | +## Installation |
| 52 | + |
| 53 | +### Installation from Aspose website |
| 54 | + |
| 55 | +This package includes a large file which is stored as a bzip2 archive. |
| 56 | + |
| 57 | +1. **Download** the archive **Aspose.PDF for Rust via C++** from the official Aspose website. |
| 58 | + The latest (most recent) version is listed at the top and is downloaded by default when you click the **Download** button. |
| 59 | + It is recommended to use this latest version. Only download a previous version if needed. |
| 60 | + Example: `Aspose.PDF-for-Rust-via-CPP-25.6.zip` |
| 61 | + |
| 62 | + The archive filename format is: `Aspose.PDF-for-Rust-via-CPP-YY.M.zip`, where: |
| 63 | + - `YY` = last two digits of the year (e.g., `25` for 2025) |
| 64 | + - `M` = month number from `1` to `12` |
| 65 | + |
| 66 | +2. **Extract** the archive to your chosen directory `{path}` using a suitable tool: |
| 67 | + - On Linux/macOS: |
| 68 | + ```bash |
| 69 | + unzip Aspose.PDF-for-Rust-via-CPP-YY.M.zip -d {path} |
| 70 | + ``` |
| 71 | + - On Windows, use built-in Explorer extraction or any unzip tool (7-Zip, WinRAR). |
| 72 | + |
| 73 | +3. **Add** the library as a dependency in your Rust project. You can do this in two ways: |
| 74 | + |
| 75 | + - **Using the command line:** |
| 76 | + ```bash |
| 77 | + cargo add asposepdf --path {path}/asposepdf |
| 78 | + ``` |
| 79 | + |
| 80 | + - **Manually editing `Cargo.toml`:** |
| 81 | + Open your project's `Cargo.toml` and add the following under `[dependencies]`: |
| 82 | + ```toml |
| 83 | + [dependencies] |
| 84 | + asposepdf = { path = "{path}/asposepdf" } |
| 85 | + ``` |
| 86 | +
|
| 87 | +4. **Build** your project (`cargo build`). On the first build, the dynamic library for your platform will be unpacked automatically from the `.bz2` archive in the `lib` folder. This may cause a short delay. |
| 88 | +
|
| 89 | +> **Notes** |
| 90 | +> - The `lib` folder contains all platform-specific `.bz2` archives with corresponding `.sha256` checksum files. |
| 91 | +> - If the checksum file is missing or invalid, the build will fail. |
| 92 | +> - Update the library by replacing the extracted files with a newer archive version. |
| 93 | +
|
| 94 | +### Installation from GitHub |
| 95 | +
|
| 96 | +This package includes precompiled native libraries (`.dll`, `.so`, `.dylib`) which are stored as compressed `.bz2` archives inside the GitHub repository. |
| 97 | +
|
| 98 | +1. **Add** the library as a dependency in your Rust project. You can do this in two ways: |
| 99 | +
|
| 100 | + - **Using the command line:** |
| 101 | + ```bash |
| 102 | + cargo add asposepdf --git https://github.com/aspose-pdf/aspose-pdf-rust-cpp.git |
| 103 | + ``` |
| 104 | +
|
| 105 | + - **Manually editing `Cargo.toml`:** |
| 106 | + Open your project's `Cargo.toml` and add the following under `[dependencies]`: |
| 107 | + ```toml |
| 108 | + [dependencies] |
| 109 | + asposepdf = { git = "https://github.com/aspose-pdf/aspose-pdf-rust-cpp.git" } |
| 110 | + ``` |
| 111 | +
|
| 112 | + > **Note:** To use a specific release version, you can specify a tag: |
| 113 | + > |
| 114 | + > ```toml |
| 115 | + > asposepdf = { git = "https://github.com/aspose-pdf/aspose-pdf-rust-cpp.git", tag = "v1.25.7" } |
| 116 | + > ``` |
| 117 | +
|
| 118 | +2. **Build** your project (`cargo build`). On the first build, the appropriate dynamic library for your platform will be automatically unpacked from the `.bz2` archive in the `lib` folder. This may cause a short delay. |
| 119 | +
|
| 120 | +> **Notes** |
| 121 | +> - You do not need to manually download or extract any files — everything is included in the GitHub repository. |
| 122 | +> - All `.bz2` archives have matching `.sha256` checksum files. The checksum is verified before unpacking. |
| 123 | +> - If the checksum verification fails or the archive is missing, the build will fail with a detailed error. |
| 124 | +> - The build script links the appropriate native library and ensures runtime availability using platform-specific options. |
| 125 | +
|
| 126 | +### Installation from crates.io |
| 127 | +
|
| 128 | +This package is available on [crates.io](https://crates.io/crates/asposepdf) and includes a build script that automatically extracts the required native library (`.dll`, `.so`, or `.dylib`) from a compressed `.bz2` archive during the build process. |
| 129 | +
|
| 130 | +1. **Add** the library as a dependency in your Rust project. You can do this in two ways: |
| 131 | +
|
| 132 | + - **Using the command line:** |
| 133 | + ```bash |
| 134 | + cargo add asposepdf |
| 135 | + ``` |
| 136 | +
|
| 137 | + - **Manually editing `Cargo.toml`:** |
| 138 | + Open your project's `Cargo.toml` and add the following under `[dependencies]`: |
| 139 | + ```toml |
| 140 | + [dependencies] |
| 141 | + asposepdf = "1.25.7" |
| 142 | + ``` |
| 143 | + > **Note:** The crates.io package requires you to provide the native dynamic libraries yourself (the .dll, .so, .dylib files). |
| 144 | +
|
| 145 | +2. **Set the path** to the directory containing the native libraries and download the required files: |
| 146 | +
|
| 147 | + - **Set the environment variable `ASPOSE_PDF_LIB_DIR`** to point to the folder where you will place the native `.bz2` archives, their `.sha256` checksum files, and the extracted native libraries (`.dll`, `.so`, `.dylib`, and for Windows also `.lib`): |
| 148 | +
|
| 149 | + - On Linux/macOS: |
| 150 | + ```bash |
| 151 | + export ASPOSE_PDF_LIB_DIR=/path/to/lib |
| 152 | + ``` |
| 153 | +
|
| 154 | + - On Windows (Command Prompt): |
| 155 | + ```cmd |
| 156 | + set ASPOSE_PDF_LIB_DIR=C:\path\to\lib |
| 157 | + ``` |
| 158 | +
|
| 159 | + - On Windows (PowerShell): |
| 160 | + ```powershell |
| 161 | + $env:ASPOSE_PDF_LIB_DIR = "C:\path\to\lib" |
| 162 | + ``` |
| 163 | +
|
| 164 | + - **Download the required `.bz2` archives** and checksum files from the GitHub repository's [`lib/` folder](https://github.com/aspose-pdf/aspose-pdf-rust-cpp/tree/main/lib) and **place them** into the folder set in `ASPOSE_PDF_LIB_DIR`: |
| 165 | +
|
| 166 | + - For **Linux x64**, download: |
| 167 | + - `libAsposePDFforRust_linux_amd64.so.bz2` |
| 168 | + - `libAsposePDFforRust_linux_amd64.so.bz2.sha256` |
| 169 | +
|
| 170 | + - For **macOS x86_64**, download: |
| 171 | + - `libAsposePDFforRust_darwin_amd64.dylib.bz2` |
| 172 | + - `libAsposePDFforRust_darwin_amd64.dylib.bz2.sha256` |
| 173 | +
|
| 174 | + - For **macOS arm64**, download: |
| 175 | + - `libAsposePDFforRust_darwin_arm64.dylib.bz2` |
| 176 | + - `libAsposePDFforRust_darwin_arm64.dylib.bz2.sha256` |
| 177 | +
|
| 178 | + - For **Windows x64**, download: |
| 179 | + - `AsposePDFforRust_windows_amd64.dll.bz2` |
| 180 | + - `AsposePDFforRust_windows_amd64.dll.bz2.sha256` |
| 181 | + - `AsposePDFforRust_windows_amd64.lib` (native import library, not compressed) |
| 182 | +
|
| 183 | + > **Note:** You need to manually download these files from GitHub and place them into the directory pointed by `ASPOSE_PDF_LIB_DIR`. |
| 184 | + > The build script will automatically unpack the native libraries from the `.bz2` archives on first build. |
| 185 | +
|
| 186 | +3. **Build** your project (`cargo build`). On the first build, the native library matching your platform will be automatically extracted and linked. This step may take a few seconds. |
| 187 | +
|
| 188 | +> **Notes** |
| 189 | +> - You must provide the folder containing the `.bz2` and `.sha256` files separately, as these binary archives are not distributed via crates.io. |
| 190 | +> - If the required archive is missing or the checksum fails, the build will fail with a detailed error. |
| 191 | +> - The same binary files used for installation via GitHub or the Aspose website can be reused here. |
| 192 | +
|
| 193 | +## Quick Start |
| 194 | +All code snippets are contained in the [examples](./examples) folder. |
| 195 | +
|
| 196 | +### Hello World! |
| 197 | +
|
| 198 | +```rust |
| 199 | +use asposepdf::{Document, PageSize}; |
| 200 | +use std::error::Error; |
| 201 | +
|
| 202 | +fn main() -> Result<(), Box<dyn Error>> { |
| 203 | + // Create a new PDF-document |
| 204 | + let pdf = Document::new()?; |
| 205 | +
|
| 206 | + // Add a new page |
| 207 | + pdf.page_add()?; |
| 208 | +
|
| 209 | + // Set the size of the first page to A4 |
| 210 | + pdf.page_set_size(1, PageSize::A4)?; |
| 211 | +
|
| 212 | + // Add "Hello World!" text to the first page |
| 213 | + pdf.page_add_text(1, "Hello World!")?; |
| 214 | +
|
| 215 | + // Save the PDF-document as "hello.pdf" |
| 216 | + pdf.save_as("hello.pdf")?; |
| 217 | +
|
| 218 | + println!("Saved PDF-document: hello.pdf"); |
| 219 | +
|
| 220 | + Ok(()) |
| 221 | +} |
| 222 | +``` |
| 223 | +
|
| 224 | +### Save PDF as Office Formats |
| 225 | +
|
| 226 | +One of the most popular features of Aspose.PDF for Rust via C++ is to convert PDF documents to other formats without needing to understand the underlying structure of the resultant format. |
| 227 | +
|
| 228 | +Give the following snippet a try with your samples: |
| 229 | +
|
| 230 | +```rust |
| 231 | +use asposepdf::Document; |
| 232 | +
|
| 233 | +fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 234 | + // Open a PDF-document with filename |
| 235 | + let pdf = Document::open("sample.pdf")?; |
| 236 | +
|
| 237 | + // Convert and save the previously opened PDF-document as DocX-document |
| 238 | + pdf.save_docx("sample.docx")?; |
| 239 | +
|
| 240 | + Ok(()) |
| 241 | +} |
| 242 | +``` |
| 243 | +### Extract Text From Whole PDF |
| 244 | +
|
| 245 | +```rust |
| 246 | +use asposepdf::Document; |
| 247 | +
|
| 248 | +fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 249 | + // Open a PDF-document with filename |
| 250 | + let pdf = Document::open("sample.pdf")?; |
| 251 | +
|
| 252 | + // Return the PDF-document contents as plain text |
| 253 | + let txt = pdf.extract_text()?; |
| 254 | +
|
| 255 | + // Print extracted text |
| 256 | + println!("Extracted text:\n{}", txt); |
| 257 | +
|
| 258 | + Ok(()) |
| 259 | +} |
| 260 | +``` |
| 261 | +
|
| 262 | +## Testing |
| 263 | +
|
| 264 | +Run the tests from the root of the package directory: |
| 265 | +
|
| 266 | +```sh |
| 267 | +cargo test |
| 268 | +``` |
| 269 | +
|
| 270 | +## License |
| 271 | +
|
| 272 | +- The **Rust source code** is licensed under the [MIT License](LICENSE). |
| 273 | +- The **shared library (`AsposePDFforRust_windows_amd64.dll`, `libAsposePDFforRust_linux_amd64.so`, `libAsposePDFforRust_darwin_amd64.dylib`, `libAsposePDFforRust_darwin_arm64.dylib`)** is proprietary and requires a commercial license. |
| 274 | + To use the full functionality, you must obtain a license. |
| 275 | +
|
| 276 | +### Evaluation version |
| 277 | +
|
| 278 | +You can use Aspose.PDF for Rust 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. |
| 279 | +
|
| 280 | +>If you want to test Aspose.PDF for Rust 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) |
| 281 | +
|
| 282 | +### Limitation of an evaluation version |
| 283 | +
|
| 284 | +We want our customers to test our components thoroughly before buying so the evaluation version allows you to use it as you would normally. |
| 285 | +
|
| 286 | +- **Documents created with an evaluation watermark.** The evaluation version of Aspose.PDF for Rust 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. |
| 287 | +- **Limit the number of pages that can be processed.** In the evaluation version, you can only process the first four pages of a document. |
| 288 | +
|
| 289 | +### Use in production |
| 290 | +
|
| 291 | +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>. |
| 292 | +
|
| 293 | +### Apply license |
| 294 | +
|
| 295 | +Applying a license to enable full functionality of the Aspose.PDF for Rust using a license file (Aspose.PDF.RustViaCPP.lic). |
| 296 | +
|
| 297 | +```rust |
| 298 | +use asposepdf::Document; |
| 299 | +
|
| 300 | +fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 301 | + // Open a PDF-document with filename |
| 302 | + let pdf = Document::open("sample.pdf")?; |
| 303 | +
|
| 304 | + // Set license with filename |
| 305 | + pdf.set_license("Aspose.PDF.RustViaCPP.lic")?; |
| 306 | +
|
| 307 | + // Now you can work with the licensed PDF document |
| 308 | + // ... |
| 309 | +
|
| 310 | + Ok(()) |
| 311 | +} |
| 312 | +``` |
| 313 | +
|
| 314 | +[Home](https://www.aspose.com/) | [Product Page](https://products.aspose.com/pdf/rust-cpp/) | [Docs](https://docs.aspose.com/pdf/rust-cpp/) | [GitHub](https://github.com/aspose-pdf/aspose-pdf-rust-cpp) | [Examples](https://github.com/aspose-pdf/aspose-pdf-rust-cpp/tree/main/examples) | [API Reference](https://reference.aspose.com/pdf/rust-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