hipdf 1.3.7

A high-level PDF manipulation library built on lopdf
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
use hipdf::images::{Image, ImageManager};
use hipdf::lopdf::{content::Content, dictionary, Document, Object, Stream};

fn create_image_showcase_example() -> Result<(), Box<dyn std::error::Error>> {
    // Create a new PDF document
    let mut doc = Document::with_version("1.5");

    // Initialize the image manager for efficient embedding
    let mut image_manager = ImageManager::new();

    // Load various image formats to demonstrate quality preservation
    println!("Loading images...");

    // Load PNG images with different properties
    let png_rgb = Image::from_file("tests/assets/duck.png")?;
    let png_indexed = Image::from_file("tests/assets/indexed.png")?;
    let png_16bit = Image::from_file("tests/assets/16bit_test.png")?;
    let png_srgb = Image::from_file("tests/assets/srgb_profile.png")?;
    let png_transparent = Image::from_file("tests/assets/dot.png")?;

    // Load JPEG images
    let jpeg_standard = Image::from_file("tests/assets/test.jpg")?;
    let jpeg_print = Image::from_file("tests/assets/print.jpeg")?;

    println!("✅ All images loaded successfully!");
    println!(
        "PNG RGB: {}x{} pixels",
        png_rgb.metadata.width, png_rgb.metadata.height
    );
    println!(
        "PNG Indexed: {}x{} pixels",
        png_indexed.metadata.width, png_indexed.metadata.height
    );
    println!(
        "PNG 16-bit: {}x{} pixels",
        png_16bit.metadata.width, png_16bit.metadata.height
    );
    println!(
        "PNG sRGB: {}x{} pixels",
        png_srgb.metadata.width, png_srgb.metadata.height
    );
    println!(
        "PNG Transparent: {}x{} pixels",
        png_transparent.metadata.width, png_transparent.metadata.height
    );
    println!(
        "JPEG Standard: {}x{} pixels",
        jpeg_standard.metadata.width, jpeg_standard.metadata.height
    );
    println!(
        "JPEG Print: {}x{} pixels",
        jpeg_print.metadata.width, jpeg_print.metadata.height
    );

    // Store images for later use in drawing (before embedding)
    let images = vec![
        png_rgb.clone(),
        png_indexed.clone(),
        png_16bit.clone(),
        png_srgb.clone(),
        png_transparent.clone(),
        jpeg_standard.clone(),
        jpeg_print.clone(),
    ];

    // Embed all images in the document (with caching)
    println!("\nEmbedding images with perfect quality preservation...");

    let img1_id = image_manager.embed_image(&mut doc, png_rgb)?;
    let img2_id = image_manager.embed_image(&mut doc, png_indexed)?;
    let img3_id = image_manager.embed_image(&mut doc, png_16bit)?;
    let img4_id = image_manager.embed_image(&mut doc, png_srgb)?;
    let img5_id = image_manager.embed_image(&mut doc, png_transparent)?;
    let img6_id = image_manager.embed_image(&mut doc, jpeg_standard)?;
    let img7_id = image_manager.embed_image(&mut doc, jpeg_print)?;

    println!("✅ All images embedded successfully!");

    // Set up document structure
    let pages_id = doc.add_object(dictionary! {
        "Type" => "Pages",
        "Count" => 1,
    });

    let font_id = doc.add_object(dictionary! {
        "Type" => "Font",
        "Subtype" => "Type1",
        "BaseFont" => "Helvetica",
    });

    // Create page resources with all embedded images
    let mut resources = dictionary! {
        "Font" => dictionary! {
            "F1" => font_id,
        },
    };

    // Add all images to resources and get their names
    let img1_name = image_manager.add_to_resources(&mut resources, img1_id);
    let img2_name = image_manager.add_to_resources(&mut resources, img2_id);
    let img3_name = image_manager.add_to_resources(&mut resources, img3_id);
    let img4_name = image_manager.add_to_resources(&mut resources, img4_id);
    let img5_name = image_manager.add_to_resources(&mut resources, img5_id);
    let img6_name = image_manager.add_to_resources(&mut resources, img6_id);
    let img7_name = image_manager.add_to_resources(&mut resources, img7_id);

    let image_names = vec![
        img1_name.clone(),
        img2_name.clone(),
        img3_name.clone(),
        img4_name.clone(),
        img5_name.clone(),
        img6_name.clone(),
        img7_name.clone(),
    ];

    // Build page content with various image demonstrations
    let mut content = Content {
        operations: Vec::new(),
    };

    // Set font for text labels
    content
        .operations
        .push(lopdf::content::Operation::new("BT", vec![]));
    content.operations.push(lopdf::content::Operation::new(
        "F1",
        vec![Object::Integer(12)],
    ));
    content.operations.push(lopdf::content::Operation::new(
        "Tm",
        vec![
            Object::Integer(1),
            Object::Integer(0),
            Object::Integer(0),
            Object::Integer(1),
            Object::Integer(50),
            Object::Integer(800),
        ],
    ));

    // Add title
    content.operations.push(lopdf::content::Operation::new(
        "Tj",
        vec![Object::String(
            b"HiPDF Image Quality Showcase - 100% Quality Preservation".to_vec(),
            lopdf::StringFormat::Literal,
        )],
    ));
    content
        .operations
        .push(lopdf::content::Operation::new("ET", vec![]));

    // Section 1: PNG Images
    content
        .operations
        .push(lopdf::content::Operation::new("BT", vec![]));
    content.operations.push(lopdf::content::Operation::new(
        "F1",
        vec![Object::Integer(14)],
    ));
    content.operations.push(lopdf::content::Operation::new(
        "Tm",
        vec![
            Object::Integer(1),
            Object::Integer(0),
            Object::Integer(0),
            Object::Integer(1),
            Object::Integer(50),
            Object::Integer(750),
        ],
    ));
    content.operations.push(lopdf::content::Operation::new(
        "Tj",
        vec![Object::String(
            b"PNG Images - Perfect Quality Preservation".to_vec(),
            lopdf::StringFormat::Literal,
        )],
    ));
    content
        .operations
        .push(lopdf::content::Operation::new("ET", vec![]));

    // Draw PNG images with labels
    let png_images = vec![
        (0, "Standard RGB", 50.0, 650.0, 150.0, 100.0),
        (1, "Indexed Colors", 250.0, 650.0, 150.0, 100.0),
        (2, "16-bit Depth", 450.0, 650.0, 150.0, 100.0),
    ];

    for (img_idx, label, x, y, w, h) in png_images {
        let img_name = &image_names[img_idx];
        let img = &images[img_idx];

        // Add label
        content
            .operations
            .push(lopdf::content::Operation::new("BT", vec![]));
        content.operations.push(lopdf::content::Operation::new(
            "F1",
            vec![Object::Integer(10)],
        ));
        content.operations.push(lopdf::content::Operation::new(
            "Tm",
            vec![
                Object::Integer(1),
                Object::Integer(0),
                Object::Integer(0),
                Object::Integer(1),
                Object::Real(x),
                Object::Real(y - 15.0),
            ],
        ));
        content.operations.push(lopdf::content::Operation::new(
            "Tj",
            vec![Object::String(
                label.as_bytes().to_vec(),
                lopdf::StringFormat::Literal,
            )],
        ));
        content
            .operations
            .push(lopdf::content::Operation::new("ET", vec![]));

        // Draw image
        content
            .operations
            .extend(ImageManager::draw_image_fit(img_name, img, x, y, w, h));
    }

    // Section 2: Special PNG Features
    content
        .operations
        .push(lopdf::content::Operation::new("BT", vec![]));
    content.operations.push(lopdf::content::Operation::new(
        "F1",
        vec![Object::Integer(14)],
    ));
    content.operations.push(lopdf::content::Operation::new(
        "Tm",
        vec![
            Object::Integer(1),
            Object::Integer(0),
            Object::Integer(0),
            Object::Integer(1),
            Object::Integer(50),
            Object::Integer(500),
        ],
    ));
    content.operations.push(lopdf::content::Operation::new(
        "Tj",
        vec![Object::String(
            b"Special PNG Features - ICC Profiles & Transparency".to_vec(),
            lopdf::StringFormat::Literal,
        )],
    ));
    content
        .operations
        .push(lopdf::content::Operation::new("ET", vec![]));

    // Draw special PNG images
    let special_images = vec![
        (3, "sRGB Profile", 50.0, 400.0, 150.0, 100.0),
        (4, "Transparent", 250.0, 400.0, 150.0, 100.0),
    ];

    for (img_idx, label, x, y, w, h) in special_images {
        let img_name = &image_names[img_idx];
        let img = &images[img_idx];

        // Add label
        content
            .operations
            .push(lopdf::content::Operation::new("BT", vec![]));
        content.operations.push(lopdf::content::Operation::new(
            "F1",
            vec![Object::Integer(10)],
        ));
        content.operations.push(lopdf::content::Operation::new(
            "Tm",
            vec![
                Object::Integer(1),
                Object::Integer(0),
                Object::Integer(0),
                Object::Integer(1),
                Object::Real(x),
                Object::Real(y - 15.0),
            ],
        ));
        content.operations.push(lopdf::content::Operation::new(
            "Tj",
            vec![Object::String(
                label.as_bytes().to_vec(),
                lopdf::StringFormat::Literal,
            )],
        ));
        content
            .operations
            .push(lopdf::content::Operation::new("ET", vec![]));

        // Draw image
        content
            .operations
            .extend(ImageManager::draw_image_fit(img_name, img, x, y, w, h));
    }

    // Section 3: JPEG Images
    content
        .operations
        .push(lopdf::content::Operation::new("BT", vec![]));
    content.operations.push(lopdf::content::Operation::new(
        "F1",
        vec![Object::Integer(14)],
    ));
    content.operations.push(lopdf::content::Operation::new(
        "Tm",
        vec![
            Object::Integer(1),
            Object::Integer(0),
            Object::Integer(0),
            Object::Integer(1),
            Object::Integer(50),
            Object::Integer(250),
        ],
    ));
    content.operations.push(lopdf::content::Operation::new(
        "Tj",
        vec![Object::String(
            b"JPEG Images - Optimized Compression".to_vec(),
            lopdf::StringFormat::Literal,
        )],
    ));
    content
        .operations
        .push(lopdf::content::Operation::new("ET", vec![]));

    // Draw JPEG images
    let jpeg_images = vec![
        (5, "Standard JPEG", 50.0, 150.0, 150.0, 100.0),
        (6, "Print Quality", 250.0, 150.0, 150.0, 100.0),
    ];

    for (img_idx, label, x, y, w, h) in jpeg_images {
        let img_name = &image_names[img_idx];
        let img = &images[img_idx];

        // Add label
        content
            .operations
            .push(lopdf::content::Operation::new("BT", vec![]));
        content.operations.push(lopdf::content::Operation::new(
            "F1",
            vec![Object::Integer(10)],
        ));
        content.operations.push(lopdf::content::Operation::new(
            "Tm",
            vec![
                Object::Integer(1),
                Object::Integer(0),
                Object::Integer(0),
                Object::Integer(1),
                Object::Real(x),
                Object::Real(y - 15.0),
            ],
        ));
        content.operations.push(lopdf::content::Operation::new(
            "Tj",
            vec![Object::String(
                label.as_bytes().to_vec(),
                lopdf::StringFormat::Literal,
            )],
        ));
        content
            .operations
            .push(lopdf::content::Operation::new("ET", vec![]));

        // Draw image
        content
            .operations
            .extend(ImageManager::draw_image_fit(img_name, img, x, y, w, h));
    }

    // Add quality preservation note
    content
        .operations
        .push(lopdf::content::Operation::new("BT", vec![]));
    content.operations.push(lopdf::content::Operation::new(
        "F1",
        vec![Object::Integer(10)],
    ));
    content.operations.push(lopdf::content::Operation::new(
        "Tm",
        vec![
            Object::Integer(1),
            Object::Integer(0),
            Object::Integer(0),
            Object::Integer(1),
            Object::Integer(50),
            Object::Integer(50),
        ],
    ));
    content.operations.push(lopdf::content::Operation::new("Tj", vec![
        Object::String(b"Note: All images embedded with 100% quality preservation - no lossy compression applied!".to_vec(), lopdf::StringFormat::Literal)
    ]));
    content
        .operations
        .push(lopdf::content::Operation::new("ET", vec![]));

    // Create the page
    let content_stream = Stream::new(dictionary! {}, content.encode()?);
    let content_id = doc.add_object(content_stream);

    let page_id = doc.add_object(dictionary! {
        "Type" => "Page",
        "Parent" => pages_id,
        "MediaBox" => vec![0.into(), 0.into(), 595.into(), 842.into()],
        "Contents" => content_id,
        "Resources" => resources,
    });

    // Finalize document structure
    let pages_dict = doc
        .get_object_mut(pages_id)
        .and_then(Object::as_dict_mut)
        .unwrap();
    pages_dict.set("Kids", vec![Object::Reference(page_id)]);

    let catalog_id = doc.add_object(dictionary! {
        "Type" => "Catalog",
        "Pages" => Object::Reference(pages_id),
    });
    doc.trailer.set("Root", Object::Reference(catalog_id));

    // Save the resulting PDF
    doc.save("image_showcase_example.pdf")?;

    println!("\n✅ Image showcase PDF created successfully!");
    println!("📄 Output: image_showcase_example.pdf");
    println!("📊 Images embedded: 7 (PNG: 5, JPEG: 2)");
    println!("🎨 Features demonstrated:");
    println!("   • Perfect quality preservation");
    println!("   • Multiple PNG formats (RGB, Indexed, 16-bit, sRGB, Transparent)");
    println!("   • JPEG optimization");
    println!("   • Efficient image caching");
    println!("   • Automatic resource management");

    Ok(())
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    create_image_showcase_example()
}