Skip to main content

oxidize_pdf/writer/pdf_writer/
mod.rs

1use crate::document::Document;
2use crate::error::{PdfError, Result};
3use crate::objects::{Dictionary, Object, ObjectId};
4use crate::text::fonts::embedding::CjkFontType;
5use crate::text::fonts::truetype::CmapSubtable;
6use crate::writer::{ObjectStreamConfig, ObjectStreamWriter, XRefStreamWriter};
7use chrono::{DateTime, Utc};
8use std::collections::HashMap;
9use std::io::{BufWriter, Write};
10use std::path::Path;
11
12/// Configuration for PDF writer
13#[derive(Debug, Clone)]
14pub struct WriterConfig {
15    /// Use XRef streams instead of traditional XRef tables (PDF 1.5+)
16    pub use_xref_streams: bool,
17    /// Use Object Streams for compressing multiple objects together (PDF 1.5+)
18    pub use_object_streams: bool,
19    /// PDF version to write (default: 1.7)
20    pub pdf_version: String,
21    /// Enable compression for streams (default: true)
22    pub compress_streams: bool,
23    /// Enable incremental updates mode (ISO 32000-1 §7.5.6)
24    pub incremental_update: bool,
25}
26
27impl Default for WriterConfig {
28    fn default() -> Self {
29        Self {
30            use_xref_streams: false,
31            use_object_streams: false,
32            pdf_version: "1.7".to_string(),
33            compress_streams: true,
34            incremental_update: false,
35        }
36    }
37}
38
39impl WriterConfig {
40    /// Create a modern PDF 1.5+ configuration with all compression features enabled
41    pub fn modern() -> Self {
42        Self {
43            use_xref_streams: true,
44            use_object_streams: true,
45            pdf_version: "1.5".to_string(),
46            compress_streams: true,
47            incremental_update: false,
48        }
49    }
50
51    /// Create a legacy PDF 1.4 configuration without modern compression
52    pub fn legacy() -> Self {
53        Self {
54            use_xref_streams: false,
55            use_object_streams: false,
56            pdf_version: "1.4".to_string(),
57            compress_streams: true,
58            incremental_update: false,
59        }
60    }
61
62    /// Create configuration for incremental updates (ISO 32000-1 §7.5.6)
63    pub fn incremental() -> Self {
64        Self {
65            use_xref_streams: false,
66            use_object_streams: false,
67            pdf_version: "1.4".to_string(),
68            compress_streams: true,
69            incremental_update: true,
70        }
71    }
72}
73
74/// Escape the three characters that are meaningful inside a PDF literal
75/// string (ISO 32000-1 §7.3.4.2): backslash introduces escape sequences
76/// and MUST be doubled; parentheses delimit the string and MUST be
77/// prefixed with a backslash when they appear in the payload.
78///
79/// Other control characters (CR, LF, HT, BS, FF) are legal inside a
80/// literal string *unescaped*, so we leave them alone — the parser is
81/// required to accept them verbatim per §7.3.4.2 Table 3. Octal
82/// escapes are a valid alternative encoding but not required here.
83///
84/// Correct ordering is essential: `\` MUST be escaped first (otherwise
85/// the `\` we insert to escape a `(` would itself get doubled). This
86/// helper walks the input exactly once and emits the escaped form.
87///
88/// **Scope clarification (issue #240 follow-up):** this helper serves
89/// only `Object::String` payloads (metadata, dict entries, array
90/// elements). The show-text `(text) Tj` payloads inside content
91/// streams take an independent path (`Op::ShowText` bytes are produced
92/// by `text::encoding::escape_show_text_literal_bytes`, which DOES
93/// escape the high byte range `0x80..=0xFF` as `\NNN` octal because
94/// those payloads carry WinAnsi-encoded text whose bytes must survive
95/// 7-bit-safe intermediaries). The two helpers solve different
96/// problems and intentionally have different coverage; they are not
97/// coordinated and one is not "downstream" of the other.
98fn escape_pdf_string_bytes(input: &[u8]) -> Vec<u8> {
99    let mut out = Vec::with_capacity(input.len());
100    for &byte in input {
101        match byte {
102            b'\\' => out.extend_from_slice(b"\\\\"),
103            b'(' => out.extend_from_slice(b"\\("),
104            b')' => out.extend_from_slice(b"\\)"),
105            other => out.push(other),
106        }
107    }
108    out
109}
110
111pub struct PdfWriter<W: Write> {
112    writer: W,
113    xref_positions: HashMap<ObjectId, u64>,
114    current_position: u64,
115    next_object_id: u32,
116    // Maps for tracking object IDs during writing
117    catalog_id: Option<ObjectId>,
118    pages_id: Option<ObjectId>,
119    info_id: Option<ObjectId>,
120    // Maps for tracking form fields and their widgets
121    #[allow(dead_code)]
122    field_widget_map: HashMap<String, Vec<ObjectId>>, // field name -> widget IDs
123    #[allow(dead_code)]
124    field_id_map: HashMap<String, ObjectId>, // field name -> field ID
125    form_field_ids: Vec<ObjectId>, // form field IDs to add to page annotations
126    page_ids: Vec<ObjectId>,       // page IDs for form field references
127    // Configuration
128    config: WriterConfig,
129    // Characters used in document, bucketed by font name (issue #204).
130    // The writer uses this to subset each custom font with only its
131    // own characters — a single global set caused unused fonts to be
132    // embedded with the active fonts' character coverage, doubling
133    // emitted size when two fonts shared a family.
134    document_used_chars_by_font: std::collections::HashMap<String, std::collections::HashSet<char>>,
135    // Object stream buffering (when use_object_streams is enabled)
136    buffered_objects: HashMap<ObjectId, Vec<u8>>,
137    compressed_object_map: HashMap<ObjectId, (ObjectId, u32)>, // obj_id -> (stream_id, index)
138    // Incremental update support (ISO 32000-1 §7.5.6)
139    prev_xref_offset: Option<u64>,
140    base_pdf_size: Option<u64>,
141    // Encryption support
142    encrypt_obj_id: Option<ObjectId>,
143    file_id: Option<Vec<u8>>,
144    encryption_state: Option<WriterEncryptionState>,
145    pending_encrypt_dict: Option<Dictionary>,
146    // FormManager field tracking:
147    //  * `form_field_placeholder_map` translates the placeholder
148    //    `ObjectReference` returned by `FormManager::add_text_field` et al.
149    //    (those use a local counter unaware of writer-side allocation) into
150    //    the real `ObjectId` chosen by `allocate_object_id`. Widgets created
151    //    via `Page::add_form_widget_with_ref` store the placeholder in
152    //    `Annotation::field_parent`; when the annotation dict is written we
153    //    remap it through this table so `/Parent` points at the real field.
154    //  * `form_manager_field_refs` is the ordered (alphabetical by field
155    //    name) list of real refs; it's appended to `document.acro_form.fields`
156    //    during `write_catalog` and is what ends up in
157    //    `/AcroForm/Fields`.
158    form_field_placeholder_map: HashMap<crate::objects::ObjectReference, ObjectId>,
159    form_manager_field_refs: Vec<crate::objects::ObjectReference>,
160}
161
162/// Holds the encryption key and encryptor for encrypting objects during write
163struct WriterEncryptionState {
164    encryptor: crate::encryption::ObjectEncryptor,
165}
166
167impl<W: Write> PdfWriter<W> {
168    pub fn new_with_writer(writer: W) -> Self {
169        Self::with_config(writer, WriterConfig::default())
170    }
171
172    pub fn with_config(writer: W, config: WriterConfig) -> Self {
173        Self {
174            writer,
175            xref_positions: HashMap::new(),
176            current_position: 0,
177            next_object_id: 1, // Start at 1 for sequential numbering
178            catalog_id: None,
179            pages_id: None,
180            info_id: None,
181            field_widget_map: HashMap::new(),
182            field_id_map: HashMap::new(),
183            form_field_ids: Vec::new(),
184            page_ids: Vec::new(),
185            config,
186            document_used_chars_by_font: std::collections::HashMap::new(),
187            buffered_objects: HashMap::new(),
188            compressed_object_map: HashMap::new(),
189            prev_xref_offset: None,
190            base_pdf_size: None,
191            encrypt_obj_id: None,
192            file_id: None,
193            encryption_state: None,
194            pending_encrypt_dict: None,
195            form_field_placeholder_map: HashMap::new(),
196            form_manager_field_refs: Vec::new(),
197        }
198    }
199
200    pub fn write_document(&mut self, document: &mut Document) -> Result<()> {
201        // Store used characters for font subsetting
202        if !document.used_characters_by_font.is_empty() {
203            self.document_used_chars_by_font = document.used_characters_by_font.clone();
204        }
205
206        self.write_header()?;
207
208        // Reserve object IDs for fixed objects (written in order)
209        self.catalog_id = Some(self.allocate_object_id());
210        self.pages_id = Some(self.allocate_object_id());
211        self.info_id = Some(self.allocate_object_id());
212
213        // Initialize encryption state BEFORE writing objects
214        // (objects need to be encrypted as they are written)
215        if let Some(ref encryption) = document.encryption {
216            self.init_encryption(encryption)?;
217        }
218
219        // Write custom fonts first (so pages can reference them)
220        let font_refs = self.write_fonts(document)?;
221
222        // Pre-allocate object IDs for every field owned by the FormManager
223        // BEFORE writing pages, so widget annotations on those pages can
224        // emit `/Parent <real_id>` instead of pointing at the placeholder
225        // refs returned by `FormManager::add_text_field`. This is the piece
226        // that bridges the FormManager's local id counter and the writer's
227        // global id allocator. See `form_field_placeholder_map` for details.
228        self.preallocate_form_manager_fields(document)?;
229
230        // Write pages (they contain widget annotations and font references)
231        self.write_pages(document, &font_refs)?;
232
233        // Write form fields (must be after pages so we can track widgets)
234        self.write_form_fields(document)?;
235
236        // Write catalog (must be after forms so AcroForm has correct field references)
237        self.write_catalog(document)?;
238
239        // Write document info
240        self.write_info(document)?;
241
242        // Write /Encrypt dict AFTER all objects (it must NOT be encrypted itself)
243        self.write_encryption_dict()?;
244
245        // Flush buffered objects as object streams (if enabled)
246        if self.config.use_object_streams {
247            self.flush_object_streams()?;
248        }
249
250        // Write xref table or stream
251        let xref_position = self.current_position;
252        if self.config.use_xref_streams {
253            self.write_xref_stream()?;
254        } else {
255            self.write_xref()?;
256        }
257
258        // Write trailer (only for traditional xref)
259        if !self.config.use_xref_streams {
260            self.write_trailer(xref_position)?;
261        }
262
263        if let Ok(()) = self.writer.flush() {
264            // Flush succeeded
265        }
266        Ok(())
267    }
268
269    /// Write an incremental update to an existing PDF (ISO 32000-1 §7.5.6)
270    ///
271    /// This appends new/modified objects to the end of an existing PDF file
272    /// without modifying the original content. The base PDF is copied first,
273    /// then new pages are ADDED to the end of the document.
274    ///
275    /// For REPLACING specific pages (e.g., form filling), use `write_incremental_with_page_replacement`.
276    ///
277    /// # Arguments
278    ///
279    /// * `base_pdf_path` - Path to the existing PDF file
280    /// * `document` - Document containing NEW pages to add
281    ///
282    /// # Returns
283    ///
284    /// Returns Ok(()) if the incremental update was written successfully
285    ///
286    /// # Example - Adding Pages
287    ///
288    /// ```no_run
289    /// use oxidize_pdf::{Document, Page, writer::{PdfWriter, WriterConfig}};
290    /// use std::fs::File;
291    /// use std::io::BufWriter;
292    ///
293    /// let mut doc = Document::new();
294    /// doc.add_page(Page::a4()); // This will be added as a NEW page
295    ///
296    /// let file = File::create("output.pdf").unwrap();
297    /// let writer = BufWriter::new(file);
298    /// let config = WriterConfig::incremental();
299    /// let mut pdf_writer = PdfWriter::with_config(writer, config);
300    /// pdf_writer.write_incremental_update("base.pdf", &mut doc).unwrap();
301    /// ```
302    pub fn write_incremental_update(
303        &mut self,
304        base_pdf_path: impl AsRef<std::path::Path>,
305        document: &mut Document,
306    ) -> Result<()> {
307        use std::io::{BufReader, Read, Seek, SeekFrom};
308
309        // Step 1: Parse the base PDF to get catalog and page information
310        let base_pdf_file = std::fs::File::open(base_pdf_path.as_ref())?;
311        let mut pdf_reader = crate::parser::PdfReader::new(BufReader::new(base_pdf_file))?;
312
313        // Get catalog from base PDF
314        let base_catalog = pdf_reader.catalog()?;
315
316        // Extract Pages reference from base catalog
317        let (base_pages_id, base_pages_gen) = base_catalog
318            .get("Pages")
319            .and_then(|obj| {
320                if let crate::parser::objects::PdfObject::Reference(id, gen) = obj {
321                    Some((*id, *gen))
322                } else {
323                    None
324                }
325            })
326            .ok_or_else(|| {
327                crate::error::PdfError::InvalidStructure(
328                    "Base PDF catalog missing /Pages reference".to_string(),
329                )
330            })?;
331
332        // Get the pages dictionary from the base PDF using the reference
333        let base_pages_obj = pdf_reader.get_object(base_pages_id, base_pages_gen)?;
334        let base_pages_kids = if let crate::parser::objects::PdfObject::Dictionary(dict) =
335            base_pages_obj
336        {
337            dict.get("Kids")
338                .and_then(|obj| {
339                    if let crate::parser::objects::PdfObject::Array(arr) = obj {
340                        // Convert PdfObject::Reference to writer::Object::Reference
341                        // PdfArray.0 gives access to the internal Vec<PdfObject>
342                        Some(
343                            arr.0
344                                .iter()
345                                .filter_map(|item| {
346                                    if let crate::parser::objects::PdfObject::Reference(id, gen) =
347                                        item
348                                    {
349                                        Some(crate::objects::Object::Reference(
350                                            crate::objects::ObjectId::new(*id, *gen),
351                                        ))
352                                    } else {
353                                        None
354                                    }
355                                })
356                                .collect::<Vec<_>>(),
357                        )
358                    } else {
359                        None
360                    }
361                })
362                .unwrap_or_default()
363        } else {
364            Vec::new()
365        };
366
367        // Count existing pages
368        let base_page_count = base_pages_kids.len();
369
370        // Step 2: Copy the base PDF content
371        let base_pdf = std::fs::File::open(base_pdf_path.as_ref())?;
372        let mut base_reader = BufReader::new(base_pdf);
373
374        // Find the startxref offset in the base PDF
375        base_reader.seek(SeekFrom::End(-100))?;
376        let mut end_buffer = vec![0u8; 100];
377        let bytes_read = base_reader.read(&mut end_buffer)?;
378        end_buffer.truncate(bytes_read);
379
380        let end_str = String::from_utf8_lossy(&end_buffer);
381        let prev_xref = if let Some(startxref_pos) = end_str.find("startxref") {
382            let after_startxref = &end_str[startxref_pos + 9..];
383
384            let number_str: String = after_startxref
385                .chars()
386                .skip_while(|c| c.is_whitespace())
387                .take_while(|c| c.is_ascii_digit())
388                .collect();
389
390            number_str.parse::<u64>().map_err(|_| {
391                crate::error::PdfError::InvalidStructure(
392                    "Could not parse startxref offset".to_string(),
393                )
394            })?
395        } else {
396            return Err(crate::error::PdfError::InvalidStructure(
397                "startxref not found in base PDF".to_string(),
398            ));
399        };
400
401        // Copy entire base PDF
402        base_reader.seek(SeekFrom::Start(0))?;
403        let base_size = std::io::copy(&mut base_reader, &mut self.writer)? as u64;
404
405        // Store base PDF info for trailer
406        self.prev_xref_offset = Some(prev_xref);
407        self.base_pdf_size = Some(base_size);
408        self.current_position = base_size;
409
410        // Step 3: Write new/modified objects only
411        if !document.used_characters_by_font.is_empty() {
412            self.document_used_chars_by_font = document.used_characters_by_font.clone();
413        }
414
415        // Allocate IDs for new objects
416        self.catalog_id = Some(self.allocate_object_id());
417        self.pages_id = Some(self.allocate_object_id());
418        self.info_id = Some(self.allocate_object_id());
419
420        // Write custom fonts first
421        let font_refs = self.write_fonts(document)?;
422
423        // Write NEW pages only (not rewriting all pages)
424        self.write_pages(document, &font_refs)?;
425
426        // Write form fields
427        self.write_form_fields(document)?;
428
429        // Step 4: Write modified catalog that references BOTH old and new pages
430        let catalog_id = self.get_catalog_id()?;
431        let new_pages_id = self.get_pages_id()?;
432
433        let mut catalog = crate::objects::Dictionary::new();
434        catalog.set("Type", crate::objects::Object::Name("Catalog".to_string()));
435        catalog.set("Pages", crate::objects::Object::Reference(new_pages_id));
436
437        // Note: For now, we only preserve the Pages reference.
438        // Full catalog preservation (Outlines, AcroForm, etc.) would require
439        // converting parser::PdfObject to writer::Object, which is a future enhancement.
440
441        self.write_object(catalog_id, crate::objects::Object::Dictionary(catalog))?;
442
443        // Step 5: Write new Pages tree that includes BOTH base pages and new pages
444        let mut all_pages_kids = base_pages_kids;
445
446        // Add references to new pages
447        for page_id in &self.page_ids {
448            all_pages_kids.push(crate::objects::Object::Reference(*page_id));
449        }
450
451        let mut pages_dict = crate::objects::Dictionary::new();
452        pages_dict.set("Type", crate::objects::Object::Name("Pages".to_string()));
453        pages_dict.set("Kids", crate::objects::Object::Array(all_pages_kids));
454        pages_dict.set(
455            "Count",
456            crate::objects::Object::Integer((base_page_count + self.page_ids.len()) as i64),
457        );
458
459        self.write_object(new_pages_id, crate::objects::Object::Dictionary(pages_dict))?;
460
461        // Write document info
462        self.write_info(document)?;
463
464        // Step 6: Write new XRef table with /Prev pointer
465        let xref_position = self.current_position;
466        self.write_xref()?;
467
468        // Step 7: Write trailer with /Prev
469        self.write_trailer(xref_position)?;
470
471        self.writer.flush()?;
472        Ok(())
473    }
474
475    /// Replaces pages in an existing PDF using incremental update structure (ISO 32000-1 §7.5.6).
476    ///
477    /// # Use Cases
478    /// This API is ideal for:
479    /// - **Dynamic page generation**: You have logic to generate complete pages from data
480    /// - **Template variants**: Switching between multiple pre-generated page versions
481    /// - **Page repair**: Regenerating corrupted or problematic pages from scratch
482    ///
483    /// # Manual Content Recreation Required
484    /// **IMPORTANT**: This API requires you to **manually recreate** the entire page content.
485    /// The replaced page will contain ONLY what you provide in `document.pages`.
486    ///
487    /// If you need to modify existing content (e.g., fill form fields on an existing page),
488    /// you must recreate the base content AND add your modifications.
489    ///
490    /// # Example: Form Filling with Manual Recreation
491    /// ```rust,no_run
492    /// use oxidize_pdf::{Document, Page, text::Font, writer::{PdfWriter, WriterConfig}};
493    /// use std::fs::File;
494    /// use std::io::BufWriter;
495    ///
496    /// let mut filled_doc = Document::new();
497    /// let mut page = Page::a4();
498    ///
499    /// // Step 1: Recreate the template content (REQUIRED - you must know this)
500    /// page.text()
501    ///     .set_font(Font::Helvetica, 12.0)
502    ///     .at(50.0, 700.0)
503    ///     .write("Name: _______________________________")?;
504    ///
505    /// // Step 2: Add your filled data at the appropriate position
506    /// page.text()
507    ///     .set_font(Font::Helvetica, 12.0)
508    ///     .at(110.0, 700.0)
509    ///     .write("John Smith")?;
510    ///
511    /// filled_doc.add_page(page);
512    ///
513    /// let file = File::create("filled.pdf")?;
514    /// let writer = BufWriter::new(file);
515    /// let mut pdf_writer = PdfWriter::with_config(writer, WriterConfig::incremental());
516    ///
517    /// pdf_writer.write_incremental_with_page_replacement("template.pdf", &mut filled_doc)?;
518    /// # Ok::<(), Box<dyn std::error::Error>>(())
519    /// ```
520    ///
521    /// # ISO Compliance
522    /// This function implements ISO 32000-1 §7.5.6 incremental updates:
523    /// - Preserves original PDF bytes (append-only)
524    /// - Uses /Prev pointer in trailer
525    /// - Maintains cross-reference chain
526    /// - Compatible with digital signatures on base PDF
527    ///
528    /// # Future: Automatic Overlay API
529    /// For automatic form filling (load + modify + save) without manual recreation,
530    /// a future `write_incremental_with_overlay()` API is planned. This will require
531    /// implementation of `Document::load()` and content overlay system.
532    ///
533    /// # Parameters
534    /// - `base_pdf_path`: Path to the existing PDF to modify
535    /// - `document`: Document containing replacement pages (first N pages will replace base pages 0..N-1)
536    ///
537    /// # Returns
538    /// - `Ok(())` if incremental update was written successfully
539    /// - `Err(PdfError)` if base PDF cannot be read, parsed, or structure is invalid
540    pub fn write_incremental_with_page_replacement(
541        &mut self,
542        base_pdf_path: impl AsRef<std::path::Path>,
543        document: &mut Document,
544    ) -> Result<()> {
545        use std::io::Cursor;
546
547        // Step 1: Read the entire base PDF into memory (avoids double file open)
548        let base_pdf_bytes = std::fs::read(base_pdf_path.as_ref())?;
549        let base_size = base_pdf_bytes.len() as u64;
550
551        // Step 2: Parse from memory to get page information
552        let mut pdf_reader = crate::parser::PdfReader::new(Cursor::new(&base_pdf_bytes))?;
553
554        let base_catalog = pdf_reader.catalog()?;
555
556        let (base_pages_id, base_pages_gen) = base_catalog
557            .get("Pages")
558            .and_then(|obj| {
559                if let crate::parser::objects::PdfObject::Reference(id, gen) = obj {
560                    Some((*id, *gen))
561                } else {
562                    None
563                }
564            })
565            .ok_or_else(|| {
566                crate::error::PdfError::InvalidStructure(
567                    "Base PDF catalog missing /Pages reference".to_string(),
568                )
569            })?;
570
571        let base_pages_obj = pdf_reader.get_object(base_pages_id, base_pages_gen)?;
572        let base_pages_kids = if let crate::parser::objects::PdfObject::Dictionary(dict) =
573            base_pages_obj
574        {
575            dict.get("Kids")
576                .and_then(|obj| {
577                    if let crate::parser::objects::PdfObject::Array(arr) = obj {
578                        Some(
579                            arr.0
580                                .iter()
581                                .filter_map(|item| {
582                                    if let crate::parser::objects::PdfObject::Reference(id, gen) =
583                                        item
584                                    {
585                                        Some(crate::objects::Object::Reference(
586                                            crate::objects::ObjectId::new(*id, *gen),
587                                        ))
588                                    } else {
589                                        None
590                                    }
591                                })
592                                .collect::<Vec<_>>(),
593                        )
594                    } else {
595                        None
596                    }
597                })
598                .unwrap_or_default()
599        } else {
600            Vec::new()
601        };
602
603        let base_page_count = base_pages_kids.len();
604
605        // Step 3: Find startxref offset from the bytes
606        let start_search = if base_size > 100 { base_size - 100 } else { 0 } as usize;
607        let end_bytes = &base_pdf_bytes[start_search..];
608        let end_str = String::from_utf8_lossy(end_bytes);
609
610        let prev_xref = if let Some(startxref_pos) = end_str.find("startxref") {
611            let after_startxref = &end_str[startxref_pos + 9..];
612            let number_str: String = after_startxref
613                .chars()
614                .skip_while(|c| c.is_whitespace())
615                .take_while(|c| c.is_ascii_digit())
616                .collect();
617
618            number_str.parse::<u64>().map_err(|_| {
619                crate::error::PdfError::InvalidStructure(
620                    "Could not parse startxref offset".to_string(),
621                )
622            })?
623        } else {
624            return Err(crate::error::PdfError::InvalidStructure(
625                "startxref not found in base PDF".to_string(),
626            ));
627        };
628
629        // Step 4: Copy base PDF bytes to output
630        self.writer.write_all(&base_pdf_bytes)?;
631
632        self.prev_xref_offset = Some(prev_xref);
633        self.base_pdf_size = Some(base_size);
634        self.current_position = base_size;
635
636        // Step 3: Write replacement pages
637        if !document.used_characters_by_font.is_empty() {
638            self.document_used_chars_by_font = document.used_characters_by_font.clone();
639        }
640
641        self.catalog_id = Some(self.allocate_object_id());
642        self.pages_id = Some(self.allocate_object_id());
643        self.info_id = Some(self.allocate_object_id());
644
645        let font_refs = self.write_fonts(document)?;
646        self.write_pages(document, &font_refs)?;
647        self.write_form_fields(document)?;
648
649        // Step 4: Create Pages tree with REPLACEMENTS
650        let catalog_id = self.get_catalog_id()?;
651        let new_pages_id = self.get_pages_id()?;
652
653        let mut catalog = crate::objects::Dictionary::new();
654        catalog.set("Type", crate::objects::Object::Name("Catalog".to_string()));
655        catalog.set("Pages", crate::objects::Object::Reference(new_pages_id));
656        self.write_object(catalog_id, crate::objects::Object::Dictionary(catalog))?;
657
658        // Build new Kids array: replace first N pages, keep rest from base
659        let mut all_pages_kids = Vec::new();
660        let replacement_count = document.pages.len();
661
662        // Add replacement pages (these override base pages at same indices)
663        for page_id in &self.page_ids {
664            all_pages_kids.push(crate::objects::Object::Reference(*page_id));
665        }
666
667        // Add remaining base pages that weren't replaced
668        if replacement_count < base_page_count {
669            for i in replacement_count..base_page_count {
670                if let Some(page_ref) = base_pages_kids.get(i) {
671                    all_pages_kids.push(page_ref.clone());
672                }
673            }
674        }
675
676        let mut pages_dict = crate::objects::Dictionary::new();
677        pages_dict.set("Type", crate::objects::Object::Name("Pages".to_string()));
678        pages_dict.set(
679            "Kids",
680            crate::objects::Object::Array(all_pages_kids.clone()),
681        );
682        pages_dict.set(
683            "Count",
684            crate::objects::Object::Integer(all_pages_kids.len() as i64),
685        );
686
687        self.write_object(new_pages_id, crate::objects::Object::Dictionary(pages_dict))?;
688        self.write_info(document)?;
689
690        let xref_position = self.current_position;
691        self.write_xref()?;
692        self.write_trailer(xref_position)?;
693
694        self.writer.flush()?;
695        Ok(())
696    }
697
698    /// Overlays content onto existing PDF pages using incremental updates (PLANNED).
699    ///
700    /// **STATUS**: Not yet implemented. This API is planned for a future release.
701    ///
702    /// # What This Will Do
703    /// When implemented, this function will allow you to:
704    /// - Load an existing PDF
705    /// - Modify specific elements (fill form fields, add annotations, watermarks)
706    /// - Save incrementally without recreating entire pages
707    ///
708    /// # Difference from Page Replacement
709    /// - **Page Replacement** (`write_incremental_with_page_replacement`): Replaces entire pages with manually recreated content
710    /// - **Overlay** (this function): Modifies existing pages by adding/changing specific elements
711    ///
712    /// # Planned Usage (Future)
713    /// ```rust,ignore
714    /// // This code will work in a future release
715    /// let mut pdf_writer = PdfWriter::with_config(writer, WriterConfig::incremental());
716    ///
717    /// let overlays = vec![
718    ///     PageOverlay::new(0)
719    ///         .add_text(110.0, 700.0, "John Smith")
720    ///         .add_annotation(Annotation::text(200.0, 500.0, "Review this")),
721    /// ];
722    ///
723    /// pdf_writer.write_incremental_with_overlay("form.pdf", overlays)?;
724    /// ```
725    ///
726    /// # Implementation Requirements
727    /// This function requires:
728    /// 1. `Document::load()` - Load existing PDF into Document structure
729    /// 2. `Page::from_parsed()` - Convert parsed pages to writable format
730    /// 3. Content stream overlay system - Append to existing content streams
731    /// 4. Resource merging - Combine new resources with existing ones
732    ///
733    /// Estimated implementation effort: 6-7 days
734    ///
735    /// # Current Workaround
736    /// Until this is implemented, use `write_incremental_with_page_replacement()` with manual
737    /// page recreation. See that function's documentation for examples.
738    ///
739    /// # Parameters
740    /// - `base_pdf_path`: Path to the existing PDF to modify (future)
741    /// - `overlays`: Content to overlay on existing pages (future)
742    ///
743    /// # Returns
744    /// Currently always returns `PdfError::NotImplemented`
745    pub fn write_incremental_with_overlay<P: AsRef<std::path::Path>>(
746        &mut self,
747        base_pdf_path: P,
748        mut overlay_fn: impl FnMut(&mut crate::Page) -> Result<()>,
749    ) -> Result<()> {
750        use std::io::Cursor;
751
752        // Step 1: Read the entire base PDF into memory
753        let base_pdf_bytes = std::fs::read(base_pdf_path.as_ref())?;
754        let base_size = base_pdf_bytes.len() as u64;
755
756        // Step 2: Parse from memory to get page information
757        let pdf_reader = crate::parser::PdfReader::new(Cursor::new(&base_pdf_bytes))?;
758        let parsed_doc = crate::parser::PdfDocument::new(pdf_reader);
759
760        // Get all pages from base PDF
761        let page_count = parsed_doc.page_count()?;
762
763        // Step 3: Find startxref offset from the bytes
764        let start_search = if base_size > 100 { base_size - 100 } else { 0 } as usize;
765        let end_bytes = &base_pdf_bytes[start_search..];
766        let end_str = String::from_utf8_lossy(end_bytes);
767
768        let prev_xref = if let Some(startxref_pos) = end_str.find("startxref") {
769            let after_startxref = &end_str[startxref_pos + 9..];
770            let number_str: String = after_startxref
771                .chars()
772                .skip_while(|c| c.is_whitespace())
773                .take_while(|c| c.is_ascii_digit())
774                .collect();
775
776            number_str.parse::<u64>().map_err(|_| {
777                crate::error::PdfError::InvalidStructure(
778                    "Could not parse startxref offset".to_string(),
779                )
780            })?
781        } else {
782            return Err(crate::error::PdfError::InvalidStructure(
783                "startxref not found in base PDF".to_string(),
784            ));
785        };
786
787        // Step 5: Copy base PDF bytes to output
788        self.writer.write_all(&base_pdf_bytes)?;
789
790        self.prev_xref_offset = Some(prev_xref);
791        self.base_pdf_size = Some(base_size);
792        self.current_position = base_size;
793
794        // Step 6: Build temporary document with overlaid pages
795        let mut temp_doc = crate::Document::new();
796
797        for page_idx in 0..page_count {
798            // Convert parsed page to writable with content preservation
799            let parsed_page = parsed_doc.get_page(page_idx)?;
800            let mut writable_page =
801                crate::Page::from_parsed_with_content(&parsed_page, &parsed_doc)?;
802
803            // Apply overlay function
804            overlay_fn(&mut writable_page)?;
805
806            // Add to temporary document
807            temp_doc.add_page(writable_page);
808        }
809
810        // Step 7: Write document with standard writer methods
811        // This ensures consistent object numbering
812        if !temp_doc.used_characters_by_font.is_empty() {
813            self.document_used_chars_by_font = temp_doc.used_characters_by_font.clone();
814        }
815
816        self.catalog_id = Some(self.allocate_object_id());
817        self.pages_id = Some(self.allocate_object_id());
818        self.info_id = Some(self.allocate_object_id());
819
820        let font_refs = self.write_fonts(&temp_doc)?;
821        self.write_pages(&temp_doc, &font_refs)?;
822        self.write_form_fields(&mut temp_doc)?;
823
824        // Step 8: Create new catalog and pages tree
825        let catalog_id = self.get_catalog_id()?;
826        let new_pages_id = self.get_pages_id()?;
827
828        let mut catalog = crate::objects::Dictionary::new();
829        catalog.set("Type", crate::objects::Object::Name("Catalog".to_string()));
830        catalog.set("Pages", crate::objects::Object::Reference(new_pages_id));
831        self.write_object(catalog_id, crate::objects::Object::Dictionary(catalog))?;
832
833        // Build new Kids array with ALL overlaid pages
834        let mut all_pages_kids = Vec::new();
835        for page_id in &self.page_ids {
836            all_pages_kids.push(crate::objects::Object::Reference(*page_id));
837        }
838
839        let mut pages_dict = crate::objects::Dictionary::new();
840        pages_dict.set("Type", crate::objects::Object::Name("Pages".to_string()));
841        pages_dict.set(
842            "Kids",
843            crate::objects::Object::Array(all_pages_kids.clone()),
844        );
845        pages_dict.set(
846            "Count",
847            crate::objects::Object::Integer(all_pages_kids.len() as i64),
848        );
849
850        self.write_object(new_pages_id, crate::objects::Object::Dictionary(pages_dict))?;
851        self.write_info(&temp_doc)?;
852
853        let xref_position = self.current_position;
854        self.write_xref()?;
855        self.write_trailer(xref_position)?;
856
857        self.writer.flush()?;
858        Ok(())
859    }
860
861    fn write_header(&mut self) -> Result<()> {
862        let header = format!("%PDF-{}\n", self.config.pdf_version);
863        self.write_bytes(header.as_bytes())?;
864        // Binary comment to ensure file is treated as binary
865        self.write_bytes(&[b'%', 0xE2, 0xE3, 0xCF, 0xD3, b'\n'])?;
866        Ok(())
867    }
868
869    /// Convert pdf_objects types to writer objects types
870    /// This is a temporary bridge until type unification is complete
871    fn convert_pdf_objects_dict_to_writer(
872        &self,
873        pdf_dict: &crate::pdf_objects::Dictionary,
874    ) -> crate::objects::Dictionary {
875        let mut writer_dict = crate::objects::Dictionary::new();
876
877        for (key, value) in pdf_dict.iter() {
878            let writer_obj = self.convert_pdf_object_to_writer(value);
879            writer_dict.set(key.as_str(), writer_obj);
880        }
881
882        writer_dict
883    }
884
885    fn convert_pdf_object_to_writer(
886        &self,
887        obj: &crate::pdf_objects::Object,
888    ) -> crate::objects::Object {
889        use crate::objects::Object as WriterObj;
890        use crate::pdf_objects::Object as PdfObj;
891
892        match obj {
893            PdfObj::Null => WriterObj::Null,
894            PdfObj::Boolean(b) => WriterObj::Boolean(*b),
895            PdfObj::Integer(i) => WriterObj::Integer(*i),
896            PdfObj::Real(f) => WriterObj::Real(*f),
897            PdfObj::String(s) => {
898                WriterObj::String(String::from_utf8_lossy(s.as_bytes()).to_string())
899            }
900            PdfObj::Name(n) => WriterObj::Name(n.as_str().to_string()),
901            PdfObj::Array(arr) => {
902                let items: Vec<WriterObj> = arr
903                    .iter()
904                    .map(|item| self.convert_pdf_object_to_writer(item))
905                    .collect();
906                WriterObj::Array(items)
907            }
908            PdfObj::Dictionary(dict) => {
909                WriterObj::Dictionary(self.convert_pdf_objects_dict_to_writer(dict))
910            }
911            PdfObj::Stream(stream) => {
912                let dict = self.convert_pdf_objects_dict_to_writer(&stream.dict);
913                WriterObj::Stream(dict, stream.data.clone())
914            }
915            PdfObj::Reference(id) => {
916                WriterObj::Reference(crate::objects::ObjectId::new(id.number(), id.generation()))
917            }
918        }
919    }
920
921    fn write_catalog(&mut self, document: &mut Document) -> Result<()> {
922        let catalog_id = self.get_catalog_id()?;
923        let pages_id = self.get_pages_id()?;
924
925        let mut catalog = Dictionary::new();
926        catalog.set("Type", Object::Name("Catalog".to_string()));
927        catalog.set("Pages", Object::Reference(pages_id));
928
929        // Serialize fields owned by the FormManager (ISO 32000-1 §12.7.3).
930        //
931        // Before v2.5.6 this block did nothing: it bound `_form_manager`
932        // but never read its `fields` map, so only fields appended manually
933        // to `document.acro_form.fields` ever reached the output PDF. Any
934        // field created via `FormManager::add_text_field` / `add_combo_box`
935        // / etc. was silently dropped — exactly the gap the .NET wrapper
936        // hit.
937        //
938        // Object IDs for these fields were pre-allocated in
939        // `preallocate_form_manager_fields` (called before `write_pages`
940        // so widget `/Parent` refs could resolve). Here we only have to:
941        //   (a) write the field-body dict into each pre-allocated id, and
942        //   (b) append those ids to `document.acro_form.fields` so the
943        //       /AcroForm write block below emits
944        //       `/AcroForm/Fields [N 0 R ...]`.
945        //
946        // Iteration follows the same deterministic order used at
947        // pre-allocation time, so the order-vs-id pairing is stable.
948        if let Some(form_manager) = &document.form_manager {
949            if document.acro_form.is_none() {
950                document.acro_form = Some(crate::forms::AcroForm::new());
951            }
952
953            // Write each field dict into its reserved id.
954            // Surface a clean `PdfError` if the placeholder-ref → real-id
955            // map is missing any entry — a "can't happen" breach of the
956            // invariant established by `preallocate_form_manager_fields`,
957            // which must run before this function.
958            let mut sorted: Vec<(Dictionary, crate::objects::ObjectReference)> = Vec::new();
959            for (name, form_field, placeholder) in form_manager.iter_fields_sorted() {
960                let real_id = *self.form_field_placeholder_map.get(&placeholder).ok_or_else(
961                    || {
962                        PdfError::Internal(format!(
963                            "AcroForm writer internal invariant broken: field '{name}' (placeholder {placeholder}) has no pre-allocated real object id — preallocate_form_manager_fields must run before write_catalog"
964                        ))
965                    },
966                )?;
967                sorted.push((form_field.field_dict.clone(), real_id));
968            }
969            for (field_dict, real_id) in sorted {
970                self.write_object(real_id, Object::Dictionary(field_dict))?;
971            }
972
973            if let Some(acro) = document.acro_form.as_mut() {
974                for r in &self.form_manager_field_refs {
975                    if !acro.fields.contains(r) {
976                        acro.fields.push(*r);
977                    }
978                }
979            }
980        }
981
982        // Add AcroForm if present
983        if let Some(acro_form) = &document.acro_form {
984            // Reserve object ID for AcroForm
985            let acro_form_id = self.allocate_object_id();
986
987            // Write AcroForm object
988            self.write_object(acro_form_id, Object::Dictionary(acro_form.to_dict()))?;
989
990            // Reference it in catalog
991            catalog.set("AcroForm", Object::Reference(acro_form_id));
992        }
993
994        // Add Outlines if present
995        if let Some(outline_tree) = &document.outline {
996            if !outline_tree.items.is_empty() {
997                let outline_root_id = self.write_outline_tree(outline_tree)?;
998                catalog.set("Outlines", Object::Reference(outline_root_id));
999            }
1000        }
1001
1002        // Add StructTreeRoot if present (Tagged PDF - ISO 32000-1 §14.8)
1003        if let Some(struct_tree) = &document.struct_tree {
1004            if !struct_tree.is_empty() {
1005                let struct_tree_root_id = self.write_struct_tree(struct_tree)?;
1006                catalog.set("StructTreeRoot", Object::Reference(struct_tree_root_id));
1007                // Mark as Tagged PDF
1008                catalog.set("MarkInfo", {
1009                    let mut mark_info = Dictionary::new();
1010                    mark_info.set("Marked", Object::Boolean(true));
1011                    Object::Dictionary(mark_info)
1012                });
1013            }
1014        }
1015
1016        // Add XMP Metadata stream (ISO 32000-1 §14.3.2)
1017        // Generate XMP from document metadata and embed as stream
1018        let xmp_metadata = document.create_xmp_metadata();
1019        let xmp_packet = xmp_metadata.to_xmp_packet();
1020        let metadata_id = self.allocate_object_id();
1021
1022        // Create metadata stream dictionary
1023        let mut metadata_dict = Dictionary::new();
1024        metadata_dict.set("Type", Object::Name("Metadata".to_string()));
1025        metadata_dict.set("Subtype", Object::Name("XML".to_string()));
1026        metadata_dict.set("Length", Object::Integer(xmp_packet.len() as i64));
1027
1028        // Write XMP metadata stream
1029        self.write_object(
1030            metadata_id,
1031            Object::Stream(metadata_dict, xmp_packet.into_bytes()),
1032        )?;
1033
1034        // Reference it in catalog
1035        catalog.set("Metadata", Object::Reference(metadata_id));
1036
1037        // /OpenAction — ISO 32000-1 §7.7.2 Table 28
1038        if let Some(action) = &document.open_action {
1039            catalog.set("OpenAction", Object::Dictionary(action.to_dict()));
1040        }
1041
1042        // /ViewerPreferences — ISO 32000-1 §7.7.2 Table 28, detailed in §12.2
1043        if let Some(prefs) = &document.viewer_preferences {
1044            catalog.set("ViewerPreferences", Object::Dictionary(prefs.to_dict()));
1045        }
1046
1047        // /Names — ISO 32000-1 §7.7.4 Table 31 (Name Dictionary).
1048        // The /Dests sub-entry is the name tree for named destinations
1049        // (§12.3.2.3). Both the name tree and the Name Dictionary are
1050        // written as indirect objects.
1051        if let Some(named_dests) = &document.named_destinations {
1052            let dests_tree_id = self.allocate_object_id();
1053            self.write_object(dests_tree_id, Object::Dictionary(named_dests.to_dict()))?;
1054
1055            let mut names_dict = Dictionary::new();
1056            names_dict.set("Dests", Object::Reference(dests_tree_id));
1057            let names_dict_id = self.allocate_object_id();
1058            self.write_object(names_dict_id, Object::Dictionary(names_dict))?;
1059
1060            catalog.set("Names", Object::Reference(names_dict_id));
1061        }
1062
1063        // /PageLabels — ISO 32000-1 §7.7.2 Table 28, §12.4.2.
1064        // The value is a number tree; we emit it as an indirect object so
1065        // large documents can grow without reshuffling the catalog.
1066        if let Some(page_labels) = &document.page_labels {
1067            let labels_id = self.allocate_object_id();
1068            self.write_object(labels_id, Object::Dictionary(page_labels.to_dict()))?;
1069            catalog.set("PageLabels", Object::Reference(labels_id));
1070        }
1071
1072        self.write_object(catalog_id, Object::Dictionary(catalog))?;
1073        Ok(())
1074    }
1075
1076    fn write_page_content(&mut self, content_id: ObjectId, page: &crate::page::Page) -> Result<()> {
1077        let mut page_copy = page.clone();
1078        let content = page_copy.generate_content()?;
1079
1080        // Create stream with compression if enabled
1081        #[cfg(feature = "compression")]
1082        {
1083            use crate::objects::Stream;
1084            let mut stream = Stream::new(content);
1085            // Only compress if config allows it
1086            if self.config.compress_streams {
1087                stream.compress_flate()?;
1088            }
1089
1090            self.write_object(
1091                content_id,
1092                Object::Stream(stream.dictionary().clone(), stream.data().to_vec()),
1093            )?;
1094        }
1095
1096        #[cfg(not(feature = "compression"))]
1097        {
1098            let mut stream_dict = Dictionary::new();
1099            stream_dict.set("Length", Object::Integer(content.len() as i64));
1100
1101            self.write_object(content_id, Object::Stream(stream_dict, content))?;
1102        }
1103
1104        Ok(())
1105    }
1106
1107    fn write_outline_tree(
1108        &mut self,
1109        outline_tree: &crate::structure::OutlineTree,
1110    ) -> Result<ObjectId> {
1111        // Create root outline dictionary
1112        let outline_root_id = self.allocate_object_id();
1113
1114        let mut outline_root = Dictionary::new();
1115        outline_root.set("Type", Object::Name("Outlines".to_string()));
1116
1117        if !outline_tree.items.is_empty() {
1118            // Reserve IDs for all outline items
1119            let mut item_ids = Vec::new();
1120
1121            // Count all items and assign IDs
1122            fn count_items(items: &[crate::structure::OutlineItem]) -> usize {
1123                let mut count = items.len();
1124                for item in items {
1125                    count += count_items(&item.children);
1126                }
1127                count
1128            }
1129
1130            let total_items = count_items(&outline_tree.items);
1131
1132            // Reserve IDs for all items
1133            for _ in 0..total_items {
1134                item_ids.push(self.allocate_object_id());
1135            }
1136
1137            let mut id_index = 0;
1138
1139            // Write root items
1140            let first_id = item_ids[0];
1141            let last_id = item_ids[outline_tree.items.len() - 1];
1142
1143            outline_root.set("First", Object::Reference(first_id));
1144            outline_root.set("Last", Object::Reference(last_id));
1145
1146            // Visible count
1147            let visible_count = outline_tree.visible_count();
1148            outline_root.set("Count", Object::Integer(visible_count));
1149
1150            // Write all items recursively
1151            let mut written_items = Vec::new();
1152
1153            for (i, item) in outline_tree.items.iter().enumerate() {
1154                let item_id = item_ids[id_index];
1155                id_index += 1;
1156
1157                let prev_id = if i > 0 { Some(item_ids[i - 1]) } else { None };
1158                let next_id = if i < outline_tree.items.len() - 1 {
1159                    Some(item_ids[i + 1])
1160                } else {
1161                    None
1162                };
1163
1164                // Write this item and its children
1165                let children_ids = self.write_outline_item(
1166                    item,
1167                    item_id,
1168                    outline_root_id,
1169                    prev_id,
1170                    next_id,
1171                    &mut item_ids,
1172                    &mut id_index,
1173                )?;
1174
1175                written_items.extend(children_ids);
1176            }
1177        }
1178
1179        self.write_object(outline_root_id, Object::Dictionary(outline_root))?;
1180        Ok(outline_root_id)
1181    }
1182
1183    #[allow(clippy::too_many_arguments)]
1184    fn write_outline_item(
1185        &mut self,
1186        item: &crate::structure::OutlineItem,
1187        item_id: ObjectId,
1188        parent_id: ObjectId,
1189        prev_id: Option<ObjectId>,
1190        next_id: Option<ObjectId>,
1191        all_ids: &mut Vec<ObjectId>,
1192        id_index: &mut usize,
1193    ) -> Result<Vec<ObjectId>> {
1194        let mut written_ids = vec![item_id];
1195
1196        // Handle children if any
1197        let (first_child_id, last_child_id) = if !item.children.is_empty() {
1198            let first_idx = *id_index;
1199            let first_id = all_ids[first_idx];
1200            let last_idx = first_idx + item.children.len() - 1;
1201            let last_id = all_ids[last_idx];
1202
1203            // Write children
1204            for (i, child) in item.children.iter().enumerate() {
1205                let child_id = all_ids[*id_index];
1206                *id_index += 1;
1207
1208                let child_prev = if i > 0 {
1209                    Some(all_ids[first_idx + i - 1])
1210                } else {
1211                    None
1212                };
1213                let child_next = if i < item.children.len() - 1 {
1214                    Some(all_ids[first_idx + i + 1])
1215                } else {
1216                    None
1217                };
1218
1219                let child_ids = self.write_outline_item(
1220                    child, child_id, item_id, // This item is the parent
1221                    child_prev, child_next, all_ids, id_index,
1222                )?;
1223
1224                written_ids.extend(child_ids);
1225            }
1226
1227            (Some(first_id), Some(last_id))
1228        } else {
1229            (None, None)
1230        };
1231
1232        // Create item dictionary
1233        let item_dict = crate::structure::outline_item_to_dict(
1234            item,
1235            parent_id,
1236            first_child_id,
1237            last_child_id,
1238            prev_id,
1239            next_id,
1240        );
1241
1242        self.write_object(item_id, Object::Dictionary(item_dict))?;
1243
1244        Ok(written_ids)
1245    }
1246
1247    /// Writes the structure tree for Tagged PDF (ISO 32000-1 §14.8)
1248    fn write_struct_tree(
1249        &mut self,
1250        struct_tree: &crate::structure::StructTree,
1251    ) -> Result<ObjectId> {
1252        // Allocate IDs for StructTreeRoot and all elements
1253        let struct_tree_root_id = self.allocate_object_id();
1254        let mut element_ids = Vec::new();
1255        for _ in 0..struct_tree.len() {
1256            element_ids.push(self.allocate_object_id());
1257        }
1258
1259        // Build parent map: element_index -> parent_id
1260        let mut parent_map: std::collections::HashMap<usize, ObjectId> =
1261            std::collections::HashMap::new();
1262
1263        // Root element's parent is StructTreeRoot
1264        if let Some(root_index) = struct_tree.root_index() {
1265            parent_map.insert(root_index, struct_tree_root_id);
1266
1267            // Recursively map all children to their parents
1268            fn map_children_parents(
1269                tree: &crate::structure::StructTree,
1270                parent_index: usize,
1271                parent_id: ObjectId,
1272                element_ids: &[ObjectId],
1273                parent_map: &mut std::collections::HashMap<usize, ObjectId>,
1274            ) {
1275                if let Some(parent_elem) = tree.get(parent_index) {
1276                    for &child_index in &parent_elem.children {
1277                        parent_map.insert(child_index, parent_id);
1278                        map_children_parents(
1279                            tree,
1280                            child_index,
1281                            element_ids[child_index],
1282                            element_ids,
1283                            parent_map,
1284                        );
1285                    }
1286                }
1287            }
1288
1289            map_children_parents(
1290                struct_tree,
1291                root_index,
1292                element_ids[root_index],
1293                &element_ids,
1294                &mut parent_map,
1295            );
1296        }
1297
1298        // Write all structure elements with parent references
1299        for (index, element) in struct_tree.iter().enumerate() {
1300            let element_id = element_ids[index];
1301            let mut element_dict = Dictionary::new();
1302
1303            element_dict.set("Type", Object::Name("StructElem".to_string()));
1304            element_dict.set("S", Object::Name(element.structure_type.as_pdf_name()));
1305
1306            // Parent reference (ISO 32000-1 §14.7.2 - required)
1307            if let Some(&parent_id) = parent_map.get(&index) {
1308                element_dict.set("P", Object::Reference(parent_id));
1309            }
1310
1311            // Element ID (optional)
1312            if let Some(ref id) = element.id {
1313                element_dict.set("ID", Object::String(id.clone()));
1314            }
1315
1316            // Attributes
1317            if let Some(ref lang) = element.attributes.lang {
1318                element_dict.set("Lang", Object::String(lang.clone()));
1319            }
1320            if let Some(ref alt) = element.attributes.alt {
1321                element_dict.set("Alt", Object::String(alt.clone()));
1322            }
1323            if let Some(ref actual_text) = element.attributes.actual_text {
1324                element_dict.set("ActualText", Object::String(actual_text.clone()));
1325            }
1326            if let Some(ref title) = element.attributes.title {
1327                element_dict.set("T", Object::String(title.clone()));
1328            }
1329            if let Some(bbox) = element.attributes.bbox {
1330                element_dict.set(
1331                    "BBox",
1332                    Object::Array(vec![
1333                        Object::Real(bbox[0]),
1334                        Object::Real(bbox[1]),
1335                        Object::Real(bbox[2]),
1336                        Object::Real(bbox[3]),
1337                    ]),
1338                );
1339            }
1340
1341            // Kids (children elements + marked content references)
1342            let mut kids = Vec::new();
1343
1344            // Add child element references
1345            for &child_index in &element.children {
1346                kids.push(Object::Reference(element_ids[child_index]));
1347            }
1348
1349            // Add marked content references (MCIDs)
1350            for mcid_ref in &element.mcids {
1351                let mut mcr = Dictionary::new();
1352                mcr.set("Type", Object::Name("MCR".to_string()));
1353                mcr.set("Pg", Object::Integer(mcid_ref.page_index as i64));
1354                mcr.set("MCID", Object::Integer(mcid_ref.mcid as i64));
1355                kids.push(Object::Dictionary(mcr));
1356            }
1357
1358            if !kids.is_empty() {
1359                element_dict.set("K", Object::Array(kids));
1360            }
1361
1362            self.write_object(element_id, Object::Dictionary(element_dict))?;
1363        }
1364
1365        // Create StructTreeRoot dictionary
1366        let mut struct_tree_root = Dictionary::new();
1367        struct_tree_root.set("Type", Object::Name("StructTreeRoot".to_string()));
1368
1369        // Add root element(s) as K entry
1370        if let Some(root_index) = struct_tree.root_index() {
1371            struct_tree_root.set("K", Object::Reference(element_ids[root_index]));
1372        }
1373
1374        // Add RoleMap if not empty
1375        if !struct_tree.role_map.mappings().is_empty() {
1376            let mut role_map = Dictionary::new();
1377            for (custom_type, standard_type) in struct_tree.role_map.mappings() {
1378                role_map.set(
1379                    custom_type.as_str(),
1380                    Object::Name(standard_type.as_pdf_name().to_string()),
1381                );
1382            }
1383            struct_tree_root.set("RoleMap", Object::Dictionary(role_map));
1384        }
1385
1386        self.write_object(struct_tree_root_id, Object::Dictionary(struct_tree_root))?;
1387        Ok(struct_tree_root_id)
1388    }
1389
1390    /// Reserve an `ObjectId` for every field owned by `document.form_manager`
1391    /// and build the placeholder → real mapping used when widget annotations
1392    /// are serialised (see `Annotation::field_parent`).
1393    ///
1394    /// Called once from `write_document` before `write_pages`, so widget
1395    /// `/Parent` refs on pages resolve to real indirect objects. The field
1396    /// bodies themselves are written later, in `write_catalog`, reusing
1397    /// these pre-allocated IDs.
1398    ///
1399    /// Iteration order is deterministic (alphabetical by field name) via
1400    /// `FormManager::iter_fields_sorted` so object-ID allocation — and
1401    /// therefore the byte-for-byte output — is reproducible across builds.
1402    fn preallocate_form_manager_fields(&mut self, document: &Document) -> Result<()> {
1403        let Some(form_manager) = &document.form_manager else {
1404            return Ok(());
1405        };
1406
1407        for (_name, _form_field, placeholder) in form_manager.iter_fields_sorted() {
1408            let real_id = self.allocate_object_id();
1409            self.form_field_placeholder_map.insert(placeholder, real_id);
1410            self.form_manager_field_refs.push(real_id);
1411        }
1412        Ok(())
1413    }
1414
1415    fn write_form_fields(&mut self, document: &mut Document) -> Result<()> {
1416        // Add collected form field IDs to AcroForm
1417        if !self.form_field_ids.is_empty() {
1418            if let Some(acro_form) = &mut document.acro_form {
1419                // Clear any existing fields and add the ones we found
1420                acro_form.fields.clear();
1421                for field_id in &self.form_field_ids {
1422                    acro_form.add_field(*field_id);
1423                }
1424
1425                // Ensure AcroForm has the right properties
1426                acro_form.need_appearances = true;
1427                if acro_form.da.is_none() {
1428                    acro_form.da = Some("/Helv 12 Tf 0 g".to_string());
1429                }
1430            }
1431        }
1432        Ok(())
1433    }
1434
1435    fn write_info(&mut self, document: &Document) -> Result<()> {
1436        let info_id = self.get_info_id()?;
1437        let mut info_dict = Dictionary::new();
1438
1439        if let Some(ref title) = document.metadata.title {
1440            info_dict.set("Title", Object::String(title.clone()));
1441        }
1442        if let Some(ref author) = document.metadata.author {
1443            info_dict.set("Author", Object::String(author.clone()));
1444        }
1445        if let Some(ref subject) = document.metadata.subject {
1446            info_dict.set("Subject", Object::String(subject.clone()));
1447        }
1448        if let Some(ref keywords) = document.metadata.keywords {
1449            info_dict.set("Keywords", Object::String(keywords.clone()));
1450        }
1451        if let Some(ref creator) = document.metadata.creator {
1452            info_dict.set("Creator", Object::String(creator.clone()));
1453        }
1454        if let Some(ref producer) = document.metadata.producer {
1455            info_dict.set("Producer", Object::String(producer.clone()));
1456        }
1457
1458        // Add creation date
1459        if let Some(creation_date) = document.metadata.creation_date {
1460            let date_string = format_pdf_date(creation_date);
1461            info_dict.set("CreationDate", Object::String(date_string));
1462        }
1463
1464        // Add modification date
1465        if let Some(mod_date) = document.metadata.modification_date {
1466            let date_string = format_pdf_date(mod_date);
1467            info_dict.set("ModDate", Object::String(date_string));
1468        }
1469
1470        // Add PDF signature (anti-spoofing and licensing)
1471        // This is written AFTER user-configurable metadata so it cannot be overridden
1472        let edition = super::Edition::OpenSource;
1473
1474        let signature = super::PdfSignature::new(document, edition);
1475        signature.write_to_info_dict(&mut info_dict);
1476
1477        self.write_object(info_id, Object::Dictionary(info_dict))?;
1478        Ok(())
1479    }
1480
1481    fn write_fonts(&mut self, document: &Document) -> Result<HashMap<String, ObjectId>> {
1482        let mut font_refs = HashMap::new();
1483
1484        // Write custom fonts from the document. Fonts registered via
1485        // `add_font_from_bytes` but never referenced from any content
1486        // stream (i.e. never `set_font`'d on any page) are skipped —
1487        // embedding them waste space and was the direct cause of
1488        // issue #204 (two fonts in the same family both getting
1489        // subsetted with the active font's character set). The
1490        // per-font map is built during tracking by
1491        // `GraphicsContext::record_used_chars` / its `TextContext`
1492        // counterpart.
1493        for font_name in document.custom_font_names() {
1494            let has_usage = self
1495                .document_used_chars_by_font
1496                .get(&font_name)
1497                .map(|chars| !chars.is_empty())
1498                .unwrap_or(false);
1499            if !has_usage {
1500                continue;
1501            }
1502            if let Some(font) = document.get_custom_font(&font_name) {
1503                // For now, write all custom fonts as TrueType with Identity-H for Unicode support
1504                // The font from document is Arc<fonts::Font>, not text::font_manager::CustomFont
1505                let font_id = self.write_font_with_unicode_support(&font_name, &font)?;
1506                font_refs.insert(font_name.clone(), font_id);
1507            }
1508        }
1509
1510        // CID-keyed fonts (issue #358): registered explicitly for positioned
1511        // glyph-run drawing. Emitted unconditionally (registration is the intent
1512        // to use) and embedded whole; the CID semantics come from the caller's
1513        // `CidMapping`. Kept separate from the Unicode-keyed fonts above.
1514        // Deterministic order so output is reproducible (BTreeMap-style sort).
1515        let mut cid_fonts: Vec<(&String, &(Vec<u8>, crate::fonts::CidMapping))> =
1516            document.cid_keyed_fonts().iter().collect();
1517        cid_fonts.sort_by(|a, b| a.0.cmp(b.0));
1518        for (font_name, (data, mapping)) in cid_fonts {
1519            let font_id = self.write_cid_keyed_font(font_name, data, mapping)?;
1520            font_refs.insert(font_name.clone(), font_id);
1521        }
1522
1523        Ok(font_refs)
1524    }
1525
1526    /// Write font with automatic Unicode support detection
1527    fn write_font_with_unicode_support(
1528        &mut self,
1529        font_name: &str,
1530        font: &crate::fonts::Font,
1531    ) -> Result<ObjectId> {
1532        // Check if any text in the document needs Unicode
1533        // For simplicity, always use Type0 for full Unicode support
1534        self.write_type0_font_from_font(font_name, font)
1535    }
1536
1537    /// Write a Type0 font with CID support from fonts::Font
1538    fn write_type0_font_from_font(
1539        &mut self,
1540        font_name: &str,
1541        font: &crate::fonts::Font,
1542    ) -> Result<ObjectId> {
1543        // Per-font character set for subsetting (issue #204). Falls
1544        // back to a small ASCII/digit set only when the document
1545        // tracked no characters at all for this font — the ancient
1546        // code path pre-dating char tracking. Post-fix this fallback
1547        // shouldn't fire for any font reached through `write_fonts`
1548        // because that path already filters unused fonts out.
1549        let used_chars = self
1550            .document_used_chars_by_font
1551            .get(font_name)
1552            .cloned()
1553            .unwrap_or_else(|| {
1554                let mut chars = std::collections::HashSet::new();
1555                for ch in
1556                    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 .,!?".chars()
1557                {
1558                    chars.insert(ch);
1559                }
1560                chars
1561            });
1562
1563        // Diagnose characters the embedded font has no glyph for: they render
1564        // as .notdef (empty boxes). This is correct when the font genuinely
1565        // lacks the glyph, but warn so it is not a silent failure — the most
1566        // common cause of "my ✓/✗ show as boxes" reports (issue #287).
1567        // Fires once per font per save; a document saved repeatedly logs it
1568        // each time. `missing_glyphs` returns nothing when coverage is unknown
1569        // (e.g. an unparseable cmap), so this never produces false positives.
1570        let used_text: String = used_chars.iter().copied().collect();
1571        let mut missing = font.missing_glyphs(&used_text);
1572        if !missing.is_empty() {
1573            missing.sort_unstable();
1574            let list = missing
1575                .iter()
1576                .map(|c| format!("U+{:04X} {:?}", *c as u32, c))
1577                .collect::<Vec<_>>()
1578                .join(", ");
1579            tracing::warn!(
1580                "Custom font '{}' has no glyph for {} character(s): {}. \
1581                 They will render as .notdef (empty boxes); the embedded font \
1582                 does not contain these glyphs.",
1583                font_name,
1584                missing.len(),
1585                list
1586            );
1587        }
1588
1589        // Allocate IDs for all font objects
1590        let font_id = self.allocate_object_id();
1591        let descendant_font_id = self.allocate_object_id();
1592        let descriptor_id = self.allocate_object_id();
1593        let font_file_id = self.allocate_object_id();
1594        let to_unicode_id = self.allocate_object_id();
1595
1596        // Write font file. Large fonts are subsetted; the subsetter always
1597        // emits raw CFF for OpenType/CFF fonts, so OpenType font files are
1598        // embedded with /CIDFontType0C. TrueType fonts keep the SFNT wrapper.
1599        // IMPORTANT: We need the ORIGINAL font for width calculations, not the subset.
1600        let (font_data_to_embed, subset_glyph_mapping, original_font_for_widths) =
1601            if font.data.len() > 100_000 && !used_chars.is_empty() {
1602                match crate::text::fonts::truetype_subsetter::subset_font(
1603                    font.data.clone(),
1604                    &used_chars,
1605                ) {
1606                    Ok(subset_result) => (
1607                        subset_result.font_data,
1608                        Some(subset_result.glyph_mapping),
1609                        font.clone(),
1610                    ),
1611                    Err(_) => {
1612                        if font.data.len() < 25_000_000 {
1613                            (font.data.clone(), None, font.clone())
1614                        } else {
1615                            (Vec::new(), None, font.clone())
1616                        }
1617                    }
1618                }
1619            } else {
1620                (font.data.clone(), None, font.clone())
1621            };
1622
1623        if !font_data_to_embed.is_empty() {
1624            // Build the initial font-file dictionary carrying the format-specific
1625            // metadata. `/Length1` (uncompressed byte count) is required for
1626            // TrueType FontFile2 streams per ISO 32000-1 §9.9. `/Subtype
1627            // /CIDFontType0C` marks raw CFF bytes for OpenType FontFile3 streams.
1628            let mut font_file_dict = Dictionary::new();
1629            match font.format {
1630                crate::fonts::FontFormat::OpenType => {
1631                    font_file_dict.set("Subtype", Object::Name("CIDFontType0C".to_string()));
1632                }
1633                crate::fonts::FontFormat::TrueType => {
1634                    font_file_dict.set("Length1", Object::Integer(font_data_to_embed.len() as i64));
1635                }
1636            }
1637
1638            // Compress the font-file stream when the `compression` feature is
1639            // active and the writer config permits it. Uncompressed TTF glyf
1640            // data in particular compresses 60-70% with zlib — a 666 KB
1641            // subset PDF drops to under 200 KB after compression.
1642            #[cfg(feature = "compression")]
1643            {
1644                let font_stream_obj = if self.config.compress_streams {
1645                    let mut stream =
1646                        crate::objects::Stream::with_dictionary(font_file_dict, font_data_to_embed);
1647                    stream.compress_flate()?;
1648                    Object::Stream(stream.dictionary().clone(), stream.data().to_vec())
1649                } else {
1650                    Object::Stream(font_file_dict, font_data_to_embed)
1651                };
1652                self.write_object(font_file_id, font_stream_obj)?;
1653            }
1654            #[cfg(not(feature = "compression"))]
1655            {
1656                let font_stream_obj = Object::Stream(font_file_dict, font_data_to_embed);
1657                self.write_object(font_file_id, font_stream_obj)?;
1658            }
1659        } else {
1660            // No font data to embed
1661            let font_file_dict = Dictionary::new();
1662            let font_stream_obj = Object::Stream(font_file_dict, Vec::new());
1663            self.write_object(font_file_id, font_stream_obj)?;
1664        }
1665
1666        // Write font descriptor
1667        let mut descriptor = Dictionary::new();
1668        descriptor.set("Type", Object::Name("FontDescriptor".to_string()));
1669        descriptor.set("FontName", Object::Name(font_name.to_string()));
1670        descriptor.set("Flags", Object::Integer(4)); // Symbolic font
1671        descriptor.set(
1672            "FontBBox",
1673            Object::Array(vec![
1674                Object::Integer(font.descriptor.font_bbox[0] as i64),
1675                Object::Integer(font.descriptor.font_bbox[1] as i64),
1676                Object::Integer(font.descriptor.font_bbox[2] as i64),
1677                Object::Integer(font.descriptor.font_bbox[3] as i64),
1678            ]),
1679        );
1680        descriptor.set(
1681            "ItalicAngle",
1682            Object::Real(font.descriptor.italic_angle as f64),
1683        );
1684        descriptor.set("Ascent", Object::Real(font.descriptor.ascent as f64));
1685        descriptor.set("Descent", Object::Real(font.descriptor.descent as f64));
1686        descriptor.set("CapHeight", Object::Real(font.descriptor.cap_height as f64));
1687        descriptor.set("StemV", Object::Real(font.descriptor.stem_v as f64));
1688        // Use appropriate FontFile type based on font format
1689        let font_file_key = match font.format {
1690            crate::fonts::FontFormat::OpenType => "FontFile3", // CFF/OpenType fonts
1691            crate::fonts::FontFormat::TrueType => "FontFile2", // TrueType fonts
1692        };
1693        descriptor.set(font_file_key, Object::Reference(font_file_id));
1694        self.write_object(descriptor_id, Object::Dictionary(descriptor))?;
1695
1696        // Write CIDFont (descendant font)
1697        let mut cid_font = Dictionary::new();
1698        cid_font.set("Type", Object::Name("Font".to_string()));
1699        // ISO 32000-1 §9.7.4: CIDFontType0 for CFF/OpenType, CIDFontType2 for TrueType.
1700        let cid_font_subtype = match font.format {
1701            crate::fonts::FontFormat::OpenType => "CIDFontType0",
1702            crate::fonts::FontFormat::TrueType => "CIDFontType2",
1703        };
1704        cid_font.set("Subtype", Object::Name(cid_font_subtype.to_string()));
1705        cid_font.set("BaseFont", Object::Name(font_name.to_string()));
1706
1707        // CIDSystemInfo - Use appropriate values for CJK fonts
1708        let mut cid_system_info = Dictionary::new();
1709        let (registry, ordering, supplement) =
1710            if let Some(cjk_type) = CjkFontType::detect_from_name(font_name) {
1711                cjk_type.cid_system_info()
1712            } else {
1713                ("Adobe", "Identity", 0)
1714            };
1715
1716        cid_system_info.set("Registry", Object::String(registry.to_string()));
1717        cid_system_info.set("Ordering", Object::String(ordering.to_string()));
1718        cid_system_info.set("Supplement", Object::Integer(supplement as i64));
1719        cid_font.set("CIDSystemInfo", Object::Dictionary(cid_system_info));
1720
1721        cid_font.set("FontDescriptor", Object::Reference(descriptor_id));
1722
1723        // Calculate a better default width based on font metrics
1724        let default_width = self.calculate_default_width(font);
1725        cid_font.set("DW", Object::Integer(default_width));
1726
1727        // Generate proper width array from font metrics
1728        // IMPORTANT: Use the ORIGINAL font for width calculations, not the subset
1729        // But pass the subset mapping to know which characters we're using
1730        let w_array = self.generate_width_array(
1731            &original_font_for_widths,
1732            default_width,
1733            subset_glyph_mapping.as_ref(),
1734        );
1735        cid_font.set("W", Object::Array(w_array));
1736
1737        // CIDToGIDMap - Only required for CIDFontType2 (TrueType)
1738        // For CIDFontType0 (CFF/OpenType), CIDToGIDMap should NOT be present per ISO 32000-1:2008 §9.7.4.2
1739        // CFF fonts use CIDs directly as glyph identifiers, so no mapping is needed
1740        if cid_font_subtype == "CIDFontType2" {
1741            // TrueType fonts need CIDToGIDMap to map CIDs (Unicode code points) to Glyph IDs
1742            let cid_to_gid_map =
1743                self.generate_cid_to_gid_map(font_name, font, subset_glyph_mapping.as_ref())?;
1744            if !cid_to_gid_map.is_empty() {
1745                // Write the CIDToGIDMap as a stream, FlateDecode-compressed
1746                // when possible. The raw map is dimensioned to the highest
1747                // codepoint in use and is mostly zeros (only mapped code
1748                // points carry a 2-byte GID), so Flate compression typically
1749                // crushes it by 95-99%. For CJK-heavy documents this is the
1750                // difference between a 130 KB map (Issue #165) and a ~1 KB
1751                // stream.
1752                let cid_to_gid_map_id = self.allocate_object_id();
1753                let map_dict = Dictionary::new();
1754                #[cfg(feature = "compression")]
1755                let map_stream = if self.config.compress_streams {
1756                    let mut stream =
1757                        crate::objects::Stream::with_dictionary(map_dict, cid_to_gid_map);
1758                    stream.compress_flate()?;
1759                    Object::Stream(stream.dictionary().clone(), stream.data().to_vec())
1760                } else {
1761                    let mut d = map_dict;
1762                    d.set("Length", Object::Integer(cid_to_gid_map.len() as i64));
1763                    Object::Stream(d, cid_to_gid_map)
1764                };
1765                #[cfg(not(feature = "compression"))]
1766                let map_stream = {
1767                    let mut d = map_dict;
1768                    d.set("Length", Object::Integer(cid_to_gid_map.len() as i64));
1769                    Object::Stream(d, cid_to_gid_map)
1770                };
1771                self.write_object(cid_to_gid_map_id, map_stream)?;
1772                cid_font.set("CIDToGIDMap", Object::Reference(cid_to_gid_map_id));
1773            } else {
1774                cid_font.set("CIDToGIDMap", Object::Name("Identity".to_string()));
1775            }
1776        }
1777        // Note: For CIDFontType0 (CFF), we intentionally omit CIDToGIDMap
1778
1779        self.write_object(descendant_font_id, Object::Dictionary(cid_font))?;
1780
1781        // Write ToUnicode CMap. The CMap is filtered to the characters that
1782        // actually appear in the document (via `document_used_chars`) and the
1783        // stream is FlateDecode-compressed when the `compression` feature and
1784        // writer config allow it. The unfiltered, uncompressed version used to
1785        // dominate PDF output (~14 KB for a 2-char Latin document).
1786        let cmap_data = self.generate_tounicode_cmap_from_font(font_name, font);
1787        let cmap_dict = Dictionary::new();
1788        #[cfg(feature = "compression")]
1789        let cmap_stream = if self.config.compress_streams {
1790            let mut stream = crate::objects::Stream::with_dictionary(cmap_dict, cmap_data);
1791            stream.compress_flate()?;
1792            Object::Stream(stream.dictionary().clone(), stream.data().to_vec())
1793        } else {
1794            Object::Stream(cmap_dict, cmap_data)
1795        };
1796        #[cfg(not(feature = "compression"))]
1797        let cmap_stream = Object::Stream(cmap_dict, cmap_data);
1798        self.write_object(to_unicode_id, cmap_stream)?;
1799
1800        // Write Type0 font (main font)
1801        let mut type0_font = Dictionary::new();
1802        type0_font.set("Type", Object::Name("Font".to_string()));
1803        type0_font.set("Subtype", Object::Name("Type0".to_string()));
1804        type0_font.set("BaseFont", Object::Name(font_name.to_string()));
1805        type0_font.set("Encoding", Object::Name("Identity-H".to_string()));
1806        type0_font.set(
1807            "DescendantFonts",
1808            Object::Array(vec![Object::Reference(descendant_font_id)]),
1809        );
1810        type0_font.set("ToUnicode", Object::Reference(to_unicode_id));
1811
1812        self.write_object(font_id, Object::Dictionary(type0_font))?;
1813
1814        Ok(font_id)
1815    }
1816
1817    /// Write a CID-keyed Type0 font (issue #358) from explicit font bytes and a
1818    /// caller-supplied [`CidMapping`](crate::fonts::CidMapping).
1819    ///
1820    /// Unlike [`write_type0_font_from_font`](Self::write_type0_font_from_font),
1821    /// which is Unicode-keyed (CID = Unicode code point, subset by characters),
1822    /// this emits a `CIDFontType2` whose content-stream codes are the CIDs in
1823    /// `mapping`. `CIDToGIDMap` / `ToUnicode` / `/W` all come from the mapping's
1824    /// generators, so the run is drawn by glyph id and stays extractable. The
1825    /// font is embedded whole (no subsetting in this iteration).
1826    fn write_cid_keyed_font(
1827        &mut self,
1828        font_name: &str,
1829        data: &[u8],
1830        mapping: &crate::fonts::CidMapping,
1831    ) -> Result<ObjectId> {
1832        use crate::text::fonts::truetype::TrueTypeFont;
1833
1834        // Parse once for the descriptor/metrics and once for glyph advances.
1835        // Both use the ORIGINAL font: descriptor metrics are font-global and /W
1836        // advances are looked up by original GID (invariant under renumbering).
1837        let font = crate::fonts::Font::from_bytes(font_name, data.to_vec())?;
1838        let tt_font = TrueTypeFont::parse(data.to_vec())?;
1839
1840        // #358 Fase 2: subset the embedded font to exactly the glyphs drawn via
1841        // `show_cid_array`. The used GIDs are the values of `cid_to_gid` (the
1842        // consumer registers exactly the run's glyphs). The content stream keeps
1843        // using original GIDs as CIDs; `CIDToGIDMap` (below) bridges them to the
1844        // subset's compacted GID space via `gid_remap`. On any failure, fall back
1845        // to embedding the full font with an unchanged CIDToGIDMap.
1846        let used_gids: std::collections::HashSet<u16> =
1847            mapping.cid_to_gid.values().copied().collect();
1848        let (embed_bytes, gid_remap): (Vec<u8>, Option<std::collections::HashMap<u16, u16>>) =
1849            match crate::text::fonts::truetype_subsetter::subset_font_by_gids(
1850                data.to_vec(),
1851                &used_gids,
1852            ) {
1853                Ok(subset) => (subset.font_data, Some(subset.old_to_new)),
1854                Err(e) => {
1855                    tracing::debug!("CID-keyed subsetting failed ({e:?}); embedding full font");
1856                    (data.to_vec(), None)
1857                }
1858            };
1859
1860        let font_id = self.allocate_object_id();
1861        let descendant_font_id = self.allocate_object_id();
1862        let descriptor_id = self.allocate_object_id();
1863        let font_file_id = self.allocate_object_id();
1864        let to_unicode_id = self.allocate_object_id();
1865
1866        // FontFile2 stream — subset font, /Length1 = uncompressed byte count
1867        // (ISO 32000-1 §9.9), FlateDecode-compressed when configured.
1868        let mut font_file_dict = Dictionary::new();
1869        font_file_dict.set("Length1", Object::Integer(embed_bytes.len() as i64));
1870        #[cfg(feature = "compression")]
1871        {
1872            let font_stream_obj = if self.config.compress_streams {
1873                let mut stream =
1874                    crate::objects::Stream::with_dictionary(font_file_dict, embed_bytes);
1875                stream.compress_flate()?;
1876                Object::Stream(stream.dictionary().clone(), stream.data().to_vec())
1877            } else {
1878                let mut d = font_file_dict;
1879                d.set("Length", Object::Integer(embed_bytes.len() as i64));
1880                Object::Stream(d, embed_bytes)
1881            };
1882            self.write_object(font_file_id, font_stream_obj)?;
1883        }
1884        #[cfg(not(feature = "compression"))]
1885        {
1886            let mut d = font_file_dict;
1887            d.set("Length", Object::Integer(embed_bytes.len() as i64));
1888            self.write_object(font_file_id, Object::Stream(d, embed_bytes))?;
1889        }
1890
1891        // FontDescriptor — reuse the parsed font's metrics.
1892        let mut descriptor = Dictionary::new();
1893        descriptor.set("Type", Object::Name("FontDescriptor".to_string()));
1894        descriptor.set("FontName", Object::Name(font_name.to_string()));
1895        descriptor.set("Flags", Object::Integer(4)); // Symbolic
1896        descriptor.set(
1897            "FontBBox",
1898            Object::Array(vec![
1899                Object::Integer(font.descriptor.font_bbox[0] as i64),
1900                Object::Integer(font.descriptor.font_bbox[1] as i64),
1901                Object::Integer(font.descriptor.font_bbox[2] as i64),
1902                Object::Integer(font.descriptor.font_bbox[3] as i64),
1903            ]),
1904        );
1905        descriptor.set(
1906            "ItalicAngle",
1907            Object::Real(font.descriptor.italic_angle as f64),
1908        );
1909        descriptor.set("Ascent", Object::Real(font.descriptor.ascent as f64));
1910        descriptor.set("Descent", Object::Real(font.descriptor.descent as f64));
1911        descriptor.set("CapHeight", Object::Real(font.descriptor.cap_height as f64));
1912        descriptor.set("StemV", Object::Real(font.descriptor.stem_v as f64));
1913        descriptor.set("FontFile2", Object::Reference(font_file_id));
1914        self.write_object(descriptor_id, Object::Dictionary(descriptor))?;
1915
1916        // CIDFont (descendant). CIDFontType2 — TrueType only (validated at
1917        // registration time in `Document::add_cid_keyed_font`).
1918        let mut cid_font = Dictionary::new();
1919        cid_font.set("Type", Object::Name("Font".to_string()));
1920        cid_font.set("Subtype", Object::Name("CIDFontType2".to_string()));
1921        cid_font.set("BaseFont", Object::Name(font_name.to_string()));
1922        let mut cid_system_info = Dictionary::new();
1923        cid_system_info.set("Registry", Object::String("Adobe".to_string()));
1924        cid_system_info.set("Ordering", Object::String("Identity".to_string()));
1925        cid_system_info.set("Supplement", Object::Integer(0));
1926        cid_font.set("CIDSystemInfo", Object::Dictionary(cid_system_info));
1927        cid_font.set("FontDescriptor", Object::Reference(descriptor_id));
1928        cid_font.set("DW", Object::Integer(self.calculate_default_width(&font)));
1929
1930        // /W widths array from the mapping's per-CID advances (units already
1931        // normalised to 1000/em by the generator). Each entry is a singleton
1932        // `cid [ width ]`.
1933        let w_tuples = mapping.generate_width_array(&tt_font)?;
1934        let mut w_array: Vec<Object> = Vec::new();
1935        for (cid, _cid_last, width) in w_tuples {
1936            w_array.push(Object::Integer(cid as i64));
1937            w_array.push(Object::Array(vec![Object::Integer(width as i64)]));
1938        }
1939        cid_font.set("W", Object::Array(w_array));
1940
1941        // CIDToGIDMap: when the font was subsetted, remap each CID's GID to its
1942        // compacted id in the subset (so a CID = original GID resolves to the
1943        // right glyph in the smaller font). An empty generator output means
1944        // CID == GID for all entries → the `/Identity` name; otherwise a stream.
1945        let cid_to_gid_map = match &gid_remap {
1946            Some(remap) => {
1947                let mut remapped = mapping.clone();
1948                remapped.cid_to_gid = mapping
1949                    .cid_to_gid
1950                    .iter()
1951                    .map(|(&cid, &old_gid)| (cid, remap.get(&old_gid).copied().unwrap_or(0)))
1952                    .collect();
1953                remapped.generate_cid_to_gid_map()
1954            }
1955            None => mapping.generate_cid_to_gid_map(),
1956        };
1957        if cid_to_gid_map.is_empty() {
1958            cid_font.set("CIDToGIDMap", Object::Name("Identity".to_string()));
1959        } else {
1960            let cid_to_gid_map_id = self.allocate_object_id();
1961            let map_dict = Dictionary::new();
1962            #[cfg(feature = "compression")]
1963            let map_stream = if self.config.compress_streams {
1964                let mut stream = crate::objects::Stream::with_dictionary(map_dict, cid_to_gid_map);
1965                stream.compress_flate()?;
1966                Object::Stream(stream.dictionary().clone(), stream.data().to_vec())
1967            } else {
1968                let mut d = map_dict;
1969                d.set("Length", Object::Integer(cid_to_gid_map.len() as i64));
1970                Object::Stream(d, cid_to_gid_map)
1971            };
1972            #[cfg(not(feature = "compression"))]
1973            let map_stream = {
1974                let mut d = map_dict;
1975                d.set("Length", Object::Integer(cid_to_gid_map.len() as i64));
1976                Object::Stream(d, cid_to_gid_map)
1977            };
1978            self.write_object(cid_to_gid_map_id, map_stream)?;
1979            cid_font.set("CIDToGIDMap", Object::Reference(cid_to_gid_map_id));
1980        }
1981        self.write_object(descendant_font_id, Object::Dictionary(cid_font))?;
1982
1983        // ToUnicode CMap from the mapping (CID → Unicode), so extraction works.
1984        let cmap_data = mapping.generate_tounicode_cmap();
1985        let cmap_dict = Dictionary::new();
1986        #[cfg(feature = "compression")]
1987        let cmap_stream = if self.config.compress_streams {
1988            let mut stream = crate::objects::Stream::with_dictionary(cmap_dict, cmap_data);
1989            stream.compress_flate()?;
1990            Object::Stream(stream.dictionary().clone(), stream.data().to_vec())
1991        } else {
1992            let mut d = cmap_dict;
1993            d.set("Length", Object::Integer(cmap_data.len() as i64));
1994            Object::Stream(d, cmap_data)
1995        };
1996        #[cfg(not(feature = "compression"))]
1997        let cmap_stream = {
1998            let mut d = cmap_dict;
1999            d.set("Length", Object::Integer(cmap_data.len() as i64));
2000            Object::Stream(d, cmap_data)
2001        };
2002        self.write_object(to_unicode_id, cmap_stream)?;
2003
2004        // Type0 wrapper.
2005        let mut type0_font = Dictionary::new();
2006        type0_font.set("Type", Object::Name("Font".to_string()));
2007        type0_font.set("Subtype", Object::Name("Type0".to_string()));
2008        type0_font.set("BaseFont", Object::Name(font_name.to_string()));
2009        type0_font.set("Encoding", Object::Name("Identity-H".to_string()));
2010        type0_font.set(
2011            "DescendantFonts",
2012            Object::Array(vec![Object::Reference(descendant_font_id)]),
2013        );
2014        type0_font.set("ToUnicode", Object::Reference(to_unicode_id));
2015        self.write_object(font_id, Object::Dictionary(type0_font))?;
2016
2017        Ok(font_id)
2018    }
2019
2020    /// Calculate default width based on common characters
2021    fn calculate_default_width(&self, font: &crate::fonts::Font) -> i64 {
2022        use crate::text::fonts::truetype::TrueTypeFont;
2023
2024        // Try to calculate from actual font metrics
2025        if let Ok(tt_font) = TrueTypeFont::parse(font.data.clone()) {
2026            if let Ok(cmap_tables) = tt_font.parse_cmap() {
2027                if let Some(cmap) = CmapSubtable::select_best_or_first(&cmap_tables) {
2028                    if let Ok(widths) = tt_font.get_glyph_widths(&cmap.mappings) {
2029                        // NOTE: get_glyph_widths already returns widths in PDF units (1000 per em)
2030
2031                        // Calculate average width of common Latin characters
2032                        let common_chars =
2033                            "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ";
2034                        let mut total_width = 0;
2035                        let mut count = 0;
2036
2037                        for ch in common_chars.chars() {
2038                            let unicode = ch as u32;
2039                            if let Some(&pdf_width) = widths.get(&unicode) {
2040                                total_width += pdf_width as i64;
2041                                count += 1;
2042                            }
2043                        }
2044
2045                        if count > 0 {
2046                            return total_width / count;
2047                        }
2048                    }
2049                }
2050            }
2051        }
2052
2053        // Fallback default if we can't calculate
2054        500
2055    }
2056
2057    /// Generate width array for CID font
2058    fn generate_width_array(
2059        &self,
2060        font: &crate::fonts::Font,
2061        _default_width: i64,
2062        subset_mapping: Option<&HashMap<u32, u16>>,
2063    ) -> Vec<Object> {
2064        use crate::text::fonts::truetype::TrueTypeFont;
2065
2066        let mut w_array = Vec::new();
2067
2068        // Try to get actual glyph widths from the font
2069        if let Ok(tt_font) = TrueTypeFont::parse(font.data.clone()) {
2070            // IMPORTANT: Always use ORIGINAL mappings for width calculation
2071            // The subset_mapping has NEW GlyphIDs which don't correspond to the right glyphs
2072            // in the original font's width table
2073            let char_to_glyph = {
2074                // Parse cmap to get original mappings
2075                if let Ok(cmap_tables) = tt_font.parse_cmap() {
2076                    if let Some(cmap) = CmapSubtable::select_best_or_first(&cmap_tables) {
2077                        // If we have subset_mapping, filter to only include used characters
2078                        if let Some(subset_map) = subset_mapping {
2079                            let mut filtered = HashMap::new();
2080                            for unicode in subset_map.keys() {
2081                                // Get the ORIGINAL GlyphID for this Unicode
2082                                if let Some(&orig_glyph) = cmap.mappings.get(unicode) {
2083                                    filtered.insert(*unicode, orig_glyph);
2084                                }
2085                            }
2086                            filtered
2087                        } else {
2088                            cmap.mappings.clone()
2089                        }
2090                    } else {
2091                        HashMap::new()
2092                    }
2093                } else {
2094                    HashMap::new()
2095                }
2096            };
2097
2098            if !char_to_glyph.is_empty() {
2099                // Get actual widths from the font
2100                if let Ok(widths) = tt_font.get_glyph_widths(&char_to_glyph) {
2101                    // NOTE: get_glyph_widths already returns widths scaled to PDF units (1000 per em)
2102                    // So we DON'T need to scale them again here
2103
2104                    // Group consecutive characters with same width for efficiency
2105                    let mut sorted_chars: Vec<_> = widths.iter().collect();
2106                    sorted_chars.sort_by_key(|(unicode, _)| *unicode);
2107
2108                    let mut i = 0;
2109                    while i < sorted_chars.len() {
2110                        let start_unicode = *sorted_chars[i].0;
2111                        // Width is already in PDF units from get_glyph_widths
2112                        let pdf_width = *sorted_chars[i].1 as i64;
2113
2114                        // Find consecutive characters with same width
2115                        let mut end_unicode = start_unicode;
2116                        let mut j = i + 1;
2117                        while j < sorted_chars.len() && *sorted_chars[j].0 == end_unicode + 1 {
2118                            let next_pdf_width = *sorted_chars[j].1 as i64;
2119                            if next_pdf_width == pdf_width {
2120                                end_unicode = *sorted_chars[j].0;
2121                                j += 1;
2122                            } else {
2123                                break;
2124                            }
2125                        }
2126
2127                        // Add to W array
2128                        if start_unicode == end_unicode {
2129                            // Single character
2130                            w_array.push(Object::Integer(start_unicode as i64));
2131                            w_array.push(Object::Array(vec![Object::Integer(pdf_width)]));
2132                        } else {
2133                            // Range of characters
2134                            w_array.push(Object::Integer(start_unicode as i64));
2135                            w_array.push(Object::Integer(end_unicode as i64));
2136                            w_array.push(Object::Integer(pdf_width));
2137                        }
2138
2139                        i = j;
2140                    }
2141
2142                    return w_array;
2143                }
2144            }
2145        }
2146
2147        // Fallback to reasonable default widths if we can't parse the font
2148        let ranges = vec![
2149            // Space character should be narrower
2150            (0x20, 0x20, 250), // Space
2151            (0x21, 0x2F, 333), // Punctuation
2152            (0x30, 0x39, 500), // Numbers (0-9)
2153            (0x3A, 0x40, 333), // More punctuation
2154            (0x41, 0x5A, 667), // Uppercase letters (A-Z)
2155            (0x5B, 0x60, 333), // Brackets
2156            (0x61, 0x7A, 500), // Lowercase letters (a-z)
2157            (0x7B, 0x7E, 333), // More brackets
2158            // Extended Latin
2159            (0xA0, 0xA0, 250), // Non-breaking space
2160            (0xA1, 0xBF, 333), // Latin-1 punctuation
2161            (0xC0, 0xD6, 667), // Latin-1 uppercase
2162            (0xD7, 0xD7, 564), // Multiplication sign
2163            (0xD8, 0xDE, 667), // More Latin-1 uppercase
2164            (0xDF, 0xF6, 500), // Latin-1 lowercase
2165            (0xF7, 0xF7, 564), // Division sign
2166            (0xF8, 0xFF, 500), // More Latin-1 lowercase
2167            // Latin Extended-A
2168            (0x100, 0x17F, 500), // Latin Extended-A
2169            // Symbols and special characters
2170            (0x2000, 0x200F, 250), // Various spaces
2171            (0x2010, 0x2027, 333), // Hyphens and dashes
2172            (0x2028, 0x202F, 250), // More spaces
2173            (0x2030, 0x206F, 500), // General Punctuation
2174            (0x2070, 0x209F, 400), // Superscripts
2175            (0x20A0, 0x20CF, 600), // Currency symbols
2176            (0x2100, 0x214F, 700), // Letterlike symbols
2177            (0x2190, 0x21FF, 600), // Arrows
2178            (0x2200, 0x22FF, 600), // Mathematical operators
2179            (0x2300, 0x23FF, 600), // Miscellaneous technical
2180            (0x2500, 0x257F, 500), // Box drawing
2181            (0x2580, 0x259F, 500), // Block elements
2182            (0x25A0, 0x25FF, 600), // Geometric shapes
2183            (0x2600, 0x26FF, 600), // Miscellaneous symbols
2184            (0x2700, 0x27BF, 600), // Dingbats
2185        ];
2186
2187        // Convert ranges to W array format
2188        for (start, end, width) in ranges {
2189            if start == end {
2190                // Single character
2191                w_array.push(Object::Integer(start));
2192                w_array.push(Object::Array(vec![Object::Integer(width)]));
2193            } else {
2194                // Range of characters
2195                w_array.push(Object::Integer(start));
2196                w_array.push(Object::Integer(end));
2197                w_array.push(Object::Integer(width));
2198            }
2199        }
2200
2201        w_array
2202    }
2203
2204    /// Generate CIDToGIDMap for Type0 font
2205    fn generate_cid_to_gid_map(
2206        &mut self,
2207        font_name: &str,
2208        font: &crate::fonts::Font,
2209        subset_mapping: Option<&HashMap<u32, u16>>,
2210    ) -> Result<Vec<u8>> {
2211        use crate::text::fonts::truetype::TrueTypeFont;
2212
2213        // If we have a subset mapping, use it directly
2214        // Otherwise, parse the font to get the original cmap table
2215        let cmap_mappings = if let Some(subset_map) = subset_mapping {
2216            // Use the subset mapping directly
2217            subset_map.clone()
2218        } else {
2219            // Parse the font to get the original cmap table
2220            let tt_font = TrueTypeFont::parse(font.data.clone())?;
2221            let cmap_tables = tt_font.parse_cmap()?;
2222
2223            // Find the best cmap table (prefer Format 12 for CJK)
2224            let cmap = CmapSubtable::select_best_or_first(&cmap_tables).ok_or_else(|| {
2225                crate::error::PdfError::FontError("No Unicode cmap table found".to_string())
2226            })?;
2227
2228            cmap.mappings.clone()
2229        };
2230
2231        // Build the CIDToGIDMap
2232        // Since we use Unicode code points as CIDs, we need to map Unicode → GlyphID
2233        // The map is a binary array where index = CID (Unicode) * 2, value = GlyphID (big-endian)
2234
2235        // OPTIMIZATION: Only create map for characters actually used in the document
2236        // Get used characters from document tracking
2237        let used_chars = self
2238            .document_used_chars_by_font
2239            .get(font_name)
2240            .cloned()
2241            .unwrap_or_default();
2242
2243        // Find the maximum Unicode value from used characters or full font
2244        let max_unicode = if !used_chars.is_empty() {
2245            // If we have used chars tracking, only map up to the highest used character
2246            used_chars
2247                .iter()
2248                .map(|ch| *ch as u32)
2249                .max()
2250                .unwrap_or(0x00FF) // At least Basic Latin
2251                .min(0xFFFF) as usize
2252        } else {
2253            // Fallback to original behavior if no tracking
2254            cmap_mappings
2255                .keys()
2256                .max()
2257                .copied()
2258                .unwrap_or(0xFFFF)
2259                .min(0xFFFF) as usize
2260        };
2261
2262        // Create the map: 2 bytes per entry
2263        let mut map = vec![0u8; (max_unicode + 1) * 2];
2264
2265        // Fill in the mappings
2266        let mut sample_mappings = Vec::new();
2267        for (&unicode, &glyph_id) in &cmap_mappings {
2268            if unicode <= max_unicode as u32 {
2269                let idx = (unicode as usize) * 2;
2270                // Write glyph_id in big-endian format
2271                map[idx] = (glyph_id >> 8) as u8;
2272                map[idx + 1] = (glyph_id & 0xFF) as u8;
2273
2274                // Collect some sample mappings for debugging
2275                if unicode == 0x0041 || unicode == 0x0061 || unicode == 0x00E1 || unicode == 0x00F1
2276                {
2277                    sample_mappings.push((unicode, glyph_id));
2278                }
2279            }
2280        }
2281
2282        Ok(map)
2283    }
2284
2285    /// Generate ToUnicode CMap for Type0 font from fonts::Font
2286    fn generate_tounicode_cmap_from_font(
2287        &self,
2288        font_name: &str,
2289        font: &crate::fonts::Font,
2290    ) -> Vec<u8> {
2291        use crate::text::fonts::truetype::TrueTypeFont;
2292
2293        let mut cmap = String::new();
2294
2295        // CMap header
2296        cmap.push_str("/CIDInit /ProcSet findresource begin\n");
2297        cmap.push_str("12 dict begin\n");
2298        cmap.push_str("begincmap\n");
2299        cmap.push_str("/CIDSystemInfo\n");
2300        cmap.push_str("<< /Registry (Adobe)\n");
2301        cmap.push_str("   /Ordering (UCS)\n");
2302        cmap.push_str("   /Supplement 0\n");
2303        cmap.push_str(">> def\n");
2304        cmap.push_str("/CMapName /Adobe-Identity-UCS def\n");
2305        cmap.push_str("/CMapType 2 def\n");
2306        cmap.push_str("1 begincodespacerange\n");
2307        cmap.push_str("<0000> <FFFF>\n");
2308        cmap.push_str("endcodespacerange\n");
2309
2310        // Build the set of code points that must appear in the ToUnicode CMap.
2311        // With Identity-H encoding, CID == Unicode, so each used character
2312        // produces a single `<CID> <unicode>` entry. If the document tracked
2313        // no used characters (legacy path), fall back to the font's full cmap
2314        // filtered to the BMP — but that path is a backstop, not the norm.
2315        let used_codepoints: Option<std::collections::HashSet<u32>> = self
2316            .document_used_chars_by_font
2317            .get(font_name)
2318            .map(|chars| {
2319                chars
2320                    .iter()
2321                    .map(|c| *c as u32)
2322                    .filter(|cp| *cp <= 0xFFFF)
2323                    .collect()
2324            });
2325
2326        let mut mappings: Vec<(u32, u32)> = Vec::new();
2327
2328        if let Some(used) = &used_codepoints {
2329            // Fast path: every used codepoint maps to itself under Identity-H.
2330            for cp in used {
2331                mappings.push((*cp, *cp));
2332            }
2333        } else if let Ok(tt_font) = TrueTypeFont::parse(font.data.clone()) {
2334            // Legacy backstop: no used-char tracking, emit every font mapping.
2335            if let Ok(cmap_tables) = tt_font.parse_cmap() {
2336                if let Some(cmap_table) = CmapSubtable::select_best_or_first(&cmap_tables) {
2337                    for (&unicode, &glyph_id) in &cmap_table.mappings {
2338                        if glyph_id > 0 && unicode <= 0xFFFF {
2339                            mappings.push((unicode, unicode));
2340                        }
2341                    }
2342                }
2343            }
2344        }
2345
2346        // Sort mappings by CID for better organization
2347        mappings.sort_by_key(|&(cid, _)| cid);
2348
2349        // Use more efficient bfrange where possible
2350        let mut i = 0;
2351        while i < mappings.len() {
2352            // Check if we can use a range
2353            let start_cid = mappings[i].0;
2354            let start_unicode = mappings[i].1;
2355            let mut end_idx = i;
2356
2357            // Find consecutive mappings
2358            while end_idx + 1 < mappings.len()
2359                && mappings[end_idx + 1].0 == mappings[end_idx].0 + 1
2360                && mappings[end_idx + 1].1 == mappings[end_idx].1 + 1
2361                && end_idx - i < 99
2362            // Max 100 per block
2363            {
2364                end_idx += 1;
2365            }
2366
2367            if end_idx > i {
2368                // Use bfrange for consecutive mappings
2369                cmap.push_str("1 beginbfrange\n");
2370                cmap.push_str(&format!(
2371                    "<{:04X}> <{:04X}> <{:04X}>\n",
2372                    start_cid, mappings[end_idx].0, start_unicode
2373                ));
2374                cmap.push_str("endbfrange\n");
2375                i = end_idx + 1;
2376            } else {
2377                // Use bfchar for individual mappings
2378                let mut chars = Vec::new();
2379                let chunk_end = (i + 100).min(mappings.len());
2380
2381                for item in &mappings[i..chunk_end] {
2382                    chars.push(*item);
2383                }
2384
2385                if !chars.is_empty() {
2386                    cmap.push_str(&format!("{} beginbfchar\n", chars.len()));
2387                    for (cid, unicode) in chars {
2388                        cmap.push_str(&format!("<{:04X}> <{:04X}>\n", cid, unicode));
2389                    }
2390                    cmap.push_str("endbfchar\n");
2391                }
2392
2393                i = chunk_end;
2394            }
2395        }
2396
2397        // CMap footer
2398        cmap.push_str("endcmap\n");
2399        cmap.push_str("CMapName currentdict /CMap defineresource pop\n");
2400        cmap.push_str("end\n");
2401        cmap.push_str("end\n");
2402
2403        cmap.into_bytes()
2404    }
2405
2406    /// Write a regular TrueType font
2407    #[allow(dead_code)]
2408    fn write_truetype_font(
2409        &mut self,
2410        font_name: &str,
2411        font: &crate::text::font_manager::CustomFont,
2412    ) -> Result<ObjectId> {
2413        // Allocate IDs for font objects
2414        let font_id = self.allocate_object_id();
2415        let descriptor_id = self.allocate_object_id();
2416        let font_file_id = self.allocate_object_id();
2417
2418        // Write font file (embedded TTF data)
2419        if let Some(ref data) = font.font_data {
2420            let mut font_file_dict = Dictionary::new();
2421            font_file_dict.set("Length1", Object::Integer(data.len() as i64));
2422            let font_stream_obj = Object::Stream(font_file_dict, data.clone());
2423            self.write_object(font_file_id, font_stream_obj)?;
2424        }
2425
2426        // Write font descriptor
2427        let mut descriptor = Dictionary::new();
2428        descriptor.set("Type", Object::Name("FontDescriptor".to_string()));
2429        descriptor.set("FontName", Object::Name(font_name.to_string()));
2430        descriptor.set("Flags", Object::Integer(32)); // Non-symbolic font
2431        descriptor.set(
2432            "FontBBox",
2433            Object::Array(vec![
2434                Object::Integer(-1000),
2435                Object::Integer(-1000),
2436                Object::Integer(2000),
2437                Object::Integer(2000),
2438            ]),
2439        );
2440        descriptor.set("ItalicAngle", Object::Integer(0));
2441        descriptor.set("Ascent", Object::Integer(font.descriptor.ascent as i64));
2442        descriptor.set("Descent", Object::Integer(font.descriptor.descent as i64));
2443        descriptor.set(
2444            "CapHeight",
2445            Object::Integer(font.descriptor.cap_height as i64),
2446        );
2447        descriptor.set("StemV", Object::Integer(font.descriptor.stem_v as i64));
2448        descriptor.set("FontFile2", Object::Reference(font_file_id));
2449        self.write_object(descriptor_id, Object::Dictionary(descriptor))?;
2450
2451        // Write font dictionary
2452        let mut font_dict = Dictionary::new();
2453        font_dict.set("Type", Object::Name("Font".to_string()));
2454        font_dict.set("Subtype", Object::Name("TrueType".to_string()));
2455        font_dict.set("BaseFont", Object::Name(font_name.to_string()));
2456        font_dict.set("FirstChar", Object::Integer(0));
2457        font_dict.set("LastChar", Object::Integer(255));
2458
2459        // Create widths array (simplified - all 600)
2460        let widths: Vec<Object> = (0..256).map(|_| Object::Integer(600)).collect();
2461        font_dict.set("Widths", Object::Array(widths));
2462        font_dict.set("FontDescriptor", Object::Reference(descriptor_id));
2463
2464        // Use WinAnsiEncoding for regular TrueType
2465        font_dict.set("Encoding", Object::Name("WinAnsiEncoding".to_string()));
2466
2467        self.write_object(font_id, Object::Dictionary(font_dict))?;
2468
2469        Ok(font_id)
2470    }
2471
2472    fn write_pages(
2473        &mut self,
2474        document: &Document,
2475        font_refs: &HashMap<String, ObjectId>,
2476    ) -> Result<()> {
2477        let pages_id = self.get_pages_id()?;
2478        let mut pages_dict = Dictionary::new();
2479        pages_dict.set("Type", Object::Name("Pages".to_string()));
2480        pages_dict.set("Count", Object::Integer(document.pages.len() as i64));
2481
2482        let mut kids = Vec::new();
2483
2484        // Allocate page object IDs sequentially
2485        let mut page_ids = Vec::new();
2486        let mut content_ids = Vec::new();
2487        for _ in 0..document.pages.len() {
2488            page_ids.push(self.allocate_object_id());
2489            content_ids.push(self.allocate_object_id());
2490        }
2491
2492        for page_id in &page_ids {
2493            kids.push(Object::Reference(*page_id));
2494        }
2495
2496        pages_dict.set("Kids", Object::Array(kids));
2497
2498        self.write_object(pages_id, Object::Dictionary(pages_dict))?;
2499
2500        // Store page IDs for form field references
2501        self.page_ids = page_ids.clone();
2502
2503        // Write individual pages with font references
2504        for (i, page) in document.pages.iter().enumerate() {
2505            let page_id = page_ids[i];
2506            let content_id = content_ids[i];
2507
2508            self.write_page_with_fonts(page_id, pages_id, content_id, page, document, font_refs)?;
2509            self.write_page_content(content_id, page)?;
2510        }
2511
2512        Ok(())
2513    }
2514
2515    /// Compatibility alias for `write_pages` to maintain backwards compatibility
2516    #[allow(dead_code)]
2517    fn write_pages_with_fonts(
2518        &mut self,
2519        document: &Document,
2520        font_refs: &HashMap<String, ObjectId>,
2521    ) -> Result<()> {
2522        self.write_pages(document, font_refs)
2523    }
2524
2525    fn write_page_with_fonts(
2526        &mut self,
2527        page_id: ObjectId,
2528        parent_id: ObjectId,
2529        content_id: ObjectId,
2530        page: &crate::page::Page,
2531        _document: &Document,
2532        font_refs: &HashMap<String, ObjectId>,
2533    ) -> Result<()> {
2534        // Start with the page's dictionary which includes annotations
2535        let mut page_dict = page.to_dict();
2536
2537        page_dict.set("Type", Object::Name("Page".to_string()));
2538        page_dict.set("Parent", Object::Reference(parent_id));
2539        page_dict.set("Contents", Object::Reference(content_id));
2540
2541        // Get resources dictionary or create new one
2542        let mut resources = if let Some(Object::Dictionary(res)) = page_dict.get("Resources") {
2543            res.clone()
2544        } else {
2545            Dictionary::new()
2546        };
2547
2548        // Add font resources
2549        let mut font_dict = Dictionary::new();
2550
2551        // Add ALL standard PDF fonts (Type1) with WinAnsiEncoding
2552        // This fixes the text rendering issue in dashboards where HelveticaBold was missing
2553
2554        // Helvetica family
2555        let mut helvetica_dict = Dictionary::new();
2556        helvetica_dict.set("Type", Object::Name("Font".to_string()));
2557        helvetica_dict.set("Subtype", Object::Name("Type1".to_string()));
2558        helvetica_dict.set("BaseFont", Object::Name("Helvetica".to_string()));
2559        helvetica_dict.set("Encoding", Object::Name("WinAnsiEncoding".to_string()));
2560        font_dict.set("Helvetica", Object::Dictionary(helvetica_dict));
2561
2562        let mut helvetica_bold_dict = Dictionary::new();
2563        helvetica_bold_dict.set("Type", Object::Name("Font".to_string()));
2564        helvetica_bold_dict.set("Subtype", Object::Name("Type1".to_string()));
2565        helvetica_bold_dict.set("BaseFont", Object::Name("Helvetica-Bold".to_string()));
2566        helvetica_bold_dict.set("Encoding", Object::Name("WinAnsiEncoding".to_string()));
2567        font_dict.set("Helvetica-Bold", Object::Dictionary(helvetica_bold_dict));
2568
2569        let mut helvetica_oblique_dict = Dictionary::new();
2570        helvetica_oblique_dict.set("Type", Object::Name("Font".to_string()));
2571        helvetica_oblique_dict.set("Subtype", Object::Name("Type1".to_string()));
2572        helvetica_oblique_dict.set("BaseFont", Object::Name("Helvetica-Oblique".to_string()));
2573        helvetica_oblique_dict.set("Encoding", Object::Name("WinAnsiEncoding".to_string()));
2574        font_dict.set(
2575            "Helvetica-Oblique",
2576            Object::Dictionary(helvetica_oblique_dict),
2577        );
2578
2579        let mut helvetica_bold_oblique_dict = Dictionary::new();
2580        helvetica_bold_oblique_dict.set("Type", Object::Name("Font".to_string()));
2581        helvetica_bold_oblique_dict.set("Subtype", Object::Name("Type1".to_string()));
2582        helvetica_bold_oblique_dict.set(
2583            "BaseFont",
2584            Object::Name("Helvetica-BoldOblique".to_string()),
2585        );
2586        helvetica_bold_oblique_dict.set("Encoding", Object::Name("WinAnsiEncoding".to_string()));
2587        font_dict.set(
2588            "Helvetica-BoldOblique",
2589            Object::Dictionary(helvetica_bold_oblique_dict),
2590        );
2591
2592        // Times family
2593        let mut times_dict = Dictionary::new();
2594        times_dict.set("Type", Object::Name("Font".to_string()));
2595        times_dict.set("Subtype", Object::Name("Type1".to_string()));
2596        times_dict.set("BaseFont", Object::Name("Times-Roman".to_string()));
2597        times_dict.set("Encoding", Object::Name("WinAnsiEncoding".to_string()));
2598        font_dict.set("Times-Roman", Object::Dictionary(times_dict));
2599
2600        let mut times_bold_dict = Dictionary::new();
2601        times_bold_dict.set("Type", Object::Name("Font".to_string()));
2602        times_bold_dict.set("Subtype", Object::Name("Type1".to_string()));
2603        times_bold_dict.set("BaseFont", Object::Name("Times-Bold".to_string()));
2604        times_bold_dict.set("Encoding", Object::Name("WinAnsiEncoding".to_string()));
2605        font_dict.set("Times-Bold", Object::Dictionary(times_bold_dict));
2606
2607        let mut times_italic_dict = Dictionary::new();
2608        times_italic_dict.set("Type", Object::Name("Font".to_string()));
2609        times_italic_dict.set("Subtype", Object::Name("Type1".to_string()));
2610        times_italic_dict.set("BaseFont", Object::Name("Times-Italic".to_string()));
2611        times_italic_dict.set("Encoding", Object::Name("WinAnsiEncoding".to_string()));
2612        font_dict.set("Times-Italic", Object::Dictionary(times_italic_dict));
2613
2614        let mut times_bold_italic_dict = Dictionary::new();
2615        times_bold_italic_dict.set("Type", Object::Name("Font".to_string()));
2616        times_bold_italic_dict.set("Subtype", Object::Name("Type1".to_string()));
2617        times_bold_italic_dict.set("BaseFont", Object::Name("Times-BoldItalic".to_string()));
2618        times_bold_italic_dict.set("Encoding", Object::Name("WinAnsiEncoding".to_string()));
2619        font_dict.set(
2620            "Times-BoldItalic",
2621            Object::Dictionary(times_bold_italic_dict),
2622        );
2623
2624        // Courier family
2625        let mut courier_dict = Dictionary::new();
2626        courier_dict.set("Type", Object::Name("Font".to_string()));
2627        courier_dict.set("Subtype", Object::Name("Type1".to_string()));
2628        courier_dict.set("BaseFont", Object::Name("Courier".to_string()));
2629        courier_dict.set("Encoding", Object::Name("WinAnsiEncoding".to_string()));
2630        font_dict.set("Courier", Object::Dictionary(courier_dict));
2631
2632        let mut courier_bold_dict = Dictionary::new();
2633        courier_bold_dict.set("Type", Object::Name("Font".to_string()));
2634        courier_bold_dict.set("Subtype", Object::Name("Type1".to_string()));
2635        courier_bold_dict.set("BaseFont", Object::Name("Courier-Bold".to_string()));
2636        courier_bold_dict.set("Encoding", Object::Name("WinAnsiEncoding".to_string()));
2637        font_dict.set("Courier-Bold", Object::Dictionary(courier_bold_dict));
2638
2639        let mut courier_oblique_dict = Dictionary::new();
2640        courier_oblique_dict.set("Type", Object::Name("Font".to_string()));
2641        courier_oblique_dict.set("Subtype", Object::Name("Type1".to_string()));
2642        courier_oblique_dict.set("BaseFont", Object::Name("Courier-Oblique".to_string()));
2643        courier_oblique_dict.set("Encoding", Object::Name("WinAnsiEncoding".to_string()));
2644        font_dict.set("Courier-Oblique", Object::Dictionary(courier_oblique_dict));
2645
2646        let mut courier_bold_oblique_dict = Dictionary::new();
2647        courier_bold_oblique_dict.set("Type", Object::Name("Font".to_string()));
2648        courier_bold_oblique_dict.set("Subtype", Object::Name("Type1".to_string()));
2649        courier_bold_oblique_dict.set("BaseFont", Object::Name("Courier-BoldOblique".to_string()));
2650        courier_bold_oblique_dict.set("Encoding", Object::Name("WinAnsiEncoding".to_string()));
2651        font_dict.set(
2652            "Courier-BoldOblique",
2653            Object::Dictionary(courier_bold_oblique_dict),
2654        );
2655
2656        // Add custom fonts (Type0 fonts for Unicode support)
2657        for (font_name, font_id) in font_refs {
2658            font_dict.set(font_name, Object::Reference(*font_id));
2659        }
2660
2661        resources.set("Font", Object::Dictionary(font_dict));
2662
2663        // Add images and Form XObjects as XObjects
2664        let has_images = !page.images().is_empty();
2665        let has_forms = !page.form_xobjects().is_empty();
2666
2667        // Tracks name→ObjectId for every FormXObject written below.
2668        // Used downstream by the ExtGState SMask emission (ISO 32000-1
2669        // §11.6.4.3 Table 144 requires /G to be an INDIRECT reference
2670        // to a transparency-group Form XObject; the caller supplies the
2671        // group by name in `SoftMask::alpha(name)` and we resolve that
2672        // name to the ObjectId allocated here).
2673        let mut form_xobject_ids: HashMap<String, ObjectId> = HashMap::new();
2674
2675        if has_images || has_forms {
2676            let mut xobject_dict = Dictionary::new();
2677
2678            // Sort by name for reproducible output (images first, then
2679            // form xobjects — both sorted within their group). Sharing
2680            // the sort key produces the same layout across builds.
2681            let mut image_entries: Vec<(&String, &crate::graphics::Image)> =
2682                page.images().iter().collect();
2683            image_entries.sort_by_key(|(name, _)| name.as_str());
2684            for (name, image) in image_entries {
2685                // Use sequential ObjectId allocation to avoid conflicts
2686                let image_id = self.allocate_object_id();
2687
2688                // Check if image has transparency (alpha channel)
2689                if image.has_transparency() {
2690                    // Handle transparent images with SMask
2691                    let (mut main_obj, smask_obj) = image.to_pdf_object_with_transparency()?;
2692
2693                    // If we have a soft mask, write it as a separate object and reference it
2694                    if let Some(smask_stream) = smask_obj {
2695                        let smask_id = self.allocate_object_id();
2696                        self.write_object(smask_id, smask_stream)?;
2697
2698                        // Add SMask reference to the main image dictionary
2699                        if let Object::Stream(ref mut dict, _) = main_obj {
2700                            dict.set("SMask", Object::Reference(smask_id));
2701                        }
2702                    }
2703
2704                    // Write the main image XObject (now with SMask reference if applicable)
2705                    self.write_object(image_id, main_obj)?;
2706                } else {
2707                    // Write the image XObject without transparency
2708                    self.write_object(image_id, image.to_pdf_object())?;
2709                }
2710
2711                // Add reference to XObject dictionary
2712                xobject_dict.set(name, Object::Reference(image_id));
2713            }
2714
2715            // Write Form XObjects (used for overlay/watermark operations)
2716            let mut form_entries: Vec<(&String, &crate::graphics::FormXObject)> =
2717                page.form_xobjects().iter().collect();
2718            form_entries.sort_by_key(|(name, _)| name.as_str());
2719            for (name, form) in form_entries {
2720                let form_id = self.allocate_object_id();
2721                let stream = form.to_stream()?;
2722                let stream_obj =
2723                    Object::Stream(stream.dictionary().clone(), stream.data().to_vec());
2724                self.write_object(form_id, stream_obj)?;
2725                xobject_dict.set(name, Object::Reference(form_id));
2726                // Record the mapping so a downstream SoftMask with
2727                // `group_ref == name` can resolve to this indirect ref.
2728                form_xobject_ids.insert(name.clone(), form_id);
2729            }
2730
2731            resources.set("XObject", Object::Dictionary(xobject_dict));
2732        }
2733
2734        // Add ExtGState resources for transparency
2735        if let Some(extgstate_states) = page.get_extgstate_resources() {
2736            let mut extgstate_dict = Dictionary::new();
2737            // Sort ExtGState entries by name for reproducible output.
2738            let mut extgstate_entries: Vec<(&String, &crate::graphics::ExtGState)> =
2739                extgstate_states.iter().collect();
2740            extgstate_entries.sort_by_key(|(name, _)| name.as_str());
2741            for (name, state) in extgstate_entries {
2742                let mut state_dict = Dictionary::new();
2743                state_dict.set("Type", Object::Name("ExtGState".to_string()));
2744
2745                // Add transparency parameters
2746                if let Some(alpha_stroke) = state.alpha_stroke {
2747                    state_dict.set("CA", Object::Real(alpha_stroke));
2748                }
2749                if let Some(alpha_fill) = state.alpha_fill {
2750                    state_dict.set("ca", Object::Real(alpha_fill));
2751                }
2752
2753                // Add other parameters as needed
2754                if let Some(line_width) = state.line_width {
2755                    state_dict.set("LW", Object::Real(line_width));
2756                }
2757                if let Some(line_cap) = state.line_cap {
2758                    state_dict.set("LC", Object::Integer(line_cap as i64));
2759                }
2760                if let Some(line_join) = state.line_join {
2761                    state_dict.set("LJ", Object::Integer(line_join as i64));
2762                }
2763                if let Some(dash_pattern) = &state.dash_pattern {
2764                    let dash_objects: Vec<Object> = dash_pattern
2765                        .array
2766                        .iter()
2767                        .map(|&d| Object::Real(d))
2768                        .collect();
2769                    state_dict.set(
2770                        "D",
2771                        Object::Array(vec![
2772                            Object::Array(dash_objects),
2773                            Object::Real(dash_pattern.phase),
2774                        ]),
2775                    );
2776                }
2777
2778                // Blend mode (ISO 32000-1 §11.3.5, Table 137). Emitted as
2779                // a single name; blend-mode *arrays* (multiple fallback
2780                // modes) are not currently exposed by ExtGState.
2781                if let Some(ref bm) = state.blend_mode {
2782                    state_dict.set("BM", Object::Name(bm.pdf_name().to_string()));
2783                }
2784
2785                // Soft mask (ISO 32000-1 §11.6.4.3, Table 144).
2786                // `SoftMask::to_pdf_dictionary` returns a full mask dict
2787                // with /Type /Mask /S <Alpha|Luminosity|None> and,
2788                // when a transparency group is attached, the /G, /BC
2789                // and /TR entries. The `/SMask /None` Name shortcut is
2790                // *also* spec-legal per §11.6.4.3; we emit the dict
2791                // form unconditionally so callers see a consistent
2792                // shape (and because the builder already populated the
2793                // dict variant for them).
2794                //
2795                // /G MUST be an indirect reference (Table 144). The
2796                // `SoftMask` API models the group reference as a `String`
2797                // name matching a FormXObject registered on this page
2798                // via `Page::add_form_xobject(name, ...)`. Resolve the
2799                // name here to the indirect ObjectId allocated above.
2800                // If no matching FormXObject exists, surface a structured
2801                // error rather than emit a spec-invalid /G /<Name> token.
2802                if let Some(ref soft_mask) = state.soft_mask {
2803                    let mut mask_dict = soft_mask.to_pdf_dictionary()?;
2804                    if let Some(Object::Name(ref g_name)) = mask_dict.get("G").cloned() {
2805                        let form_id = form_xobject_ids.get(g_name).ok_or_else(|| {
2806                            crate::error::PdfError::InvalidStructure(format!(
2807                                "SoftMask references transparency group {:?} but no matching \
2808                                 FormXObject is registered on the page; call \
2809                                 Page::add_form_xobject({:?}, ...) before saving",
2810                                g_name, g_name
2811                            ))
2812                        })?;
2813                        mask_dict.set("G", Object::Reference(*form_id));
2814                    }
2815                    state_dict.set("SMask", Object::Dictionary(mask_dict));
2816                }
2817
2818                extgstate_dict.set(name, Object::Dictionary(state_dict));
2819            }
2820            if !extgstate_dict.is_empty() {
2821                resources.set("ExtGState", Object::Dictionary(extgstate_dict));
2822            }
2823        }
2824
2825        // ColorSpace resources (ISO 32000-1 §8.6, Table 62). Emitted as a
2826        // direct sub-dictionary — colour-space *parameters* (the dict
2827        // inside `[/CalRGB <<..>>]`) are generally small and inlining them
2828        // keeps the cross-reference table lean. Callers that need
2829        // larger / shared colour spaces can register them once and reuse
2830        // the same key across pages.
2831        // Deterministic emission of all three resource sub-dicts is
2832        // enforced at Dictionary write time (see QUAL-9 sort below in
2833        // `write_object_value`). We therefore iterate the source
2834        // HashMaps in any order here — the serializer reorders.
2835        // However we DO sort Pattern / Shading entries before
2836        // `allocate_object_id()` so object-id allocation is also
2837        // reproducible (two identical documents allocate ids in the
2838        // same sequence, producing byte-identical xref entries).
2839        if !page.color_spaces().is_empty() {
2840            let mut cs_dict = Dictionary::new();
2841            // Sort by name before allocating any stream object ids so id
2842            // allocation stays reproducible (mirrors the Pattern/Shading blocks).
2843            let mut entries: Vec<(&String, &crate::graphics::PageColorSpace)> =
2844                page.color_spaces().iter().collect();
2845            entries.sort_by_key(|(name, _)| name.as_str());
2846            for (name, cs) in entries {
2847                // ICCBased colour spaces MUST be an indirect stream carrying the
2848                // profile bytes (ISO 32000-1 §8.6.5.5) — a stream cannot be
2849                // inlined into the resource dict. Every other shape (device-name
2850                // alias, Cal*/Lab parameterised dict) is inline via `to_object`.
2851                if let Some((icc_dict, icc_data)) = cs.icc_stream_parts() {
2852                    let icc_id = self.allocate_object_id();
2853                    self.write_object(icc_id, Object::Stream(icc_dict, icc_data))?;
2854                    cs_dict.set(
2855                        name,
2856                        Object::Array(vec![
2857                            Object::Name("ICCBased".to_string()),
2858                            Object::Reference(icc_id),
2859                        ]),
2860                    );
2861                } else {
2862                    cs_dict.set(name, cs.to_object());
2863                }
2864            }
2865            resources.set("ColorSpace", Object::Dictionary(cs_dict));
2866        }
2867
2868        if !page.patterns().is_empty() {
2869            let mut pat_dict = Dictionary::new();
2870            let mut entries: Vec<(&String, &crate::graphics::TilingPattern)> =
2871                page.patterns().iter().collect();
2872            entries.sort_by_key(|(name, _)| name.as_str());
2873            for (name, pattern) in entries {
2874                let pattern_id = self.allocate_object_id();
2875                let pattern_dict = pattern.to_pdf_dictionary()?;
2876                self.write_object(
2877                    pattern_id,
2878                    Object::Stream(pattern_dict, pattern.content_stream.clone()),
2879                )?;
2880                pat_dict.set(name, Object::Reference(pattern_id));
2881            }
2882            resources.set("Pattern", Object::Dictionary(pat_dict));
2883        }
2884
2885        if !page.shadings().is_empty() {
2886            let mut sh_dict = Dictionary::new();
2887            let mut entries: Vec<(&String, &crate::graphics::ShadingDefinition)> =
2888                page.shadings().iter().collect();
2889            entries.sort_by_key(|(name, _)| name.as_str());
2890            for (name, shading) in entries {
2891                let mut shading_dict = shading.to_pdf_dictionary()?;
2892                // Hoist the inline /Function to an indirect object (issue #297 B).
2893                // ISO 32000-1 §8.7.4.5.2: functions are normally indirect. Only
2894                // a dictionary value is hoisted; FunctionBased shadings carry an
2895                // external function id (an Integer) which is left untouched.
2896                if let Some(Object::Dictionary(_)) = shading_dict.get("Function") {
2897                    if let Some(func_obj) = shading_dict.remove("Function") {
2898                        let func_id = self.allocate_object_id();
2899                        self.write_object(func_id, func_obj)?;
2900                        shading_dict.set("Function", Object::Reference(func_id));
2901                    }
2902                }
2903                let shading_id = self.allocate_object_id();
2904                self.write_object(shading_id, Object::Dictionary(shading_dict))?;
2905                sh_dict.set(name, Object::Reference(shading_id));
2906            }
2907            resources.set("Shading", Object::Dictionary(sh_dict));
2908        }
2909
2910        // Merge preserved resources from original PDF (if any)
2911        // Phase 2.3: Rename preserved fonts to avoid conflicts with overlay fonts
2912        if let Some(preserved_res) = page.get_preserved_resources() {
2913            // Convert pdf_objects::Dictionary to writer Dictionary FIRST
2914            let mut preserved_writer_dict = self.convert_pdf_objects_dict_to_writer(preserved_res);
2915
2916            // Step 1: Rename preserved fonts (F1 → OrigF1)
2917            if let Some(Object::Dictionary(fonts)) = preserved_writer_dict.get("Font") {
2918                // Rename font dictionary keys using our utility function
2919                let renamed_fonts = crate::writer::rename_preserved_fonts(fonts);
2920
2921                // Replace Font dictionary with renamed version
2922                preserved_writer_dict.set("Font", Object::Dictionary(renamed_fonts));
2923            }
2924
2925            // Phase 3.3: Write embedded font streams as indirect objects
2926            // Fonts that were resolved in Phase 3.2 have embedded Stream objects
2927            // We need to write these streams as separate PDF objects and replace with References
2928            if let Some(Object::Dictionary(fonts)) = preserved_writer_dict.get("Font") {
2929                let mut fonts_with_refs = crate::objects::Dictionary::new();
2930
2931                for (font_name, font_obj) in fonts.iter() {
2932                    if let Object::Dictionary(font_dict) = font_obj {
2933                        // Try to extract and write embedded font streams
2934                        let updated_font = self.write_embedded_font_streams(font_dict)?;
2935                        fonts_with_refs.set(font_name, Object::Dictionary(updated_font));
2936                    } else {
2937                        // Not a dictionary, keep as-is
2938                        fonts_with_refs.set(font_name, font_obj.clone());
2939                    }
2940                }
2941
2942                // Replace Font dictionary with version that has References instead of Streams
2943                preserved_writer_dict.set("Font", Object::Dictionary(fonts_with_refs));
2944            }
2945
2946            // Write preserved XObject streams as indirect objects
2947            // XObjects resolved in from_parsed_with_content may contain inline Stream data.
2948            // Per ISO 32000-1 §7.3.8, streams MUST be indirect objects.
2949            if let Some(Object::Dictionary(xobjects)) = preserved_writer_dict.get("XObject") {
2950                let mut xobjects_with_refs = crate::objects::Dictionary::new();
2951                tracing::debug!(
2952                    "Externalizing {} preserved XObject entries as indirect objects",
2953                    xobjects.len()
2954                );
2955
2956                for (xobj_name, xobj_obj) in xobjects.iter() {
2957                    match xobj_obj {
2958                        Object::Stream(dict, data) => {
2959                            let obj_id = self.allocate_object_id();
2960                            self.write_object(obj_id, Object::Stream(dict.clone(), data.clone()))?;
2961                            xobjects_with_refs.set(xobj_name, Object::Reference(obj_id));
2962                        }
2963                        Object::Dictionary(dict) => {
2964                            // Dictionary XObjects may contain nested streams (e.g., SMask)
2965                            let externalized = self.externalize_streams_in_dict(dict)?;
2966                            xobjects_with_refs.set(xobj_name, Object::Dictionary(externalized));
2967                        }
2968                        _ => {
2969                            xobjects_with_refs.set(xobj_name, xobj_obj.clone());
2970                        }
2971                    }
2972                }
2973
2974                preserved_writer_dict.set("XObject", Object::Dictionary(xobjects_with_refs));
2975            }
2976
2977            // Merge each resource category (Font, XObject, ColorSpace, etc.)
2978            for (key, value) in preserved_writer_dict.iter() {
2979                // If the resource category already exists, merge dictionaries
2980                if let Some(Object::Dictionary(existing)) = resources.get(key) {
2981                    if let Object::Dictionary(preserved_dict) = value {
2982                        let mut merged = existing.clone();
2983                        // Add all preserved resources, giving priority to existing (overlay wins)
2984                        for (res_name, res_obj) in preserved_dict.iter() {
2985                            if !merged.contains_key(res_name) {
2986                                merged.set(res_name, res_obj.clone());
2987                            }
2988                        }
2989                        resources.set(key, Object::Dictionary(merged));
2990                    }
2991                } else {
2992                    // Resource category doesn't exist yet, add it directly
2993                    resources.set(key, value.clone());
2994                }
2995            }
2996        }
2997
2998        page_dict.set("Resources", Object::Dictionary(resources));
2999
3000        // Collect all annotation references for the /Annots array
3001        let mut annot_refs: Vec<Object> = Vec::new();
3002
3003        // 1. Process widget annotations already in page_dict (legacy form field path)
3004        if let Some(Object::Array(annots)) = page_dict.get("Annots") {
3005            for annot in annots {
3006                if let Object::Dictionary(ref annot_dict) = annot {
3007                    if let Some(Object::Name(subtype)) = annot_dict.get("Subtype") {
3008                        if subtype == "Widget" {
3009                            let widget_id = self.allocate_object_id();
3010                            self.write_object(widget_id, annot.clone())?;
3011                            annot_refs.push(Object::Reference(widget_id));
3012
3013                            // Track widget for form fields
3014                            if let Some(Object::Name(_ft)) = annot_dict.get("FT") {
3015                                if let Some(Object::String(field_name)) = annot_dict.get("T") {
3016                                    self.field_widget_map
3017                                        .entry(field_name.clone())
3018                                        .or_default()
3019                                        .push(widget_id);
3020                                    self.field_id_map.insert(field_name.clone(), widget_id);
3021                                    self.form_field_ids.push(widget_id);
3022                                }
3023                            }
3024                            continue;
3025                        }
3026                    }
3027                }
3028                annot_refs.push(annot.clone());
3029            }
3030        }
3031
3032        // 2. Write annotations from Page.annotations() (programmatic annotations)
3033        //    Handles highlights, text notes, stamps, links, etc. added via
3034        //    page.add_annotation(). Each is written as an indirect object.
3035        for annotation in page.annotations() {
3036            let annot_id = self.allocate_object_id();
3037            let mut annot_dict = annotation.to_dict();
3038
3039            // Remap `/Parent` from FormManager placeholder → real ObjectId.
3040            // `Annotation::field_parent` stores the placeholder ref returned
3041            // by FormManager::add_*_field (which uses a counter disjoint
3042            // from the writer's allocator). At this point the writer has
3043            // already pre-allocated real ids for every FormManager field
3044            // via `preallocate_form_manager_fields`, so we translate.
3045            //
3046            // We read `field_parent` straight off the struct instead of
3047            // round-tripping through `annot_dict.get("Parent")`: the
3048            // dictionary representation is what we're producing, not a
3049            // source of truth. The struct field is authoritative and
3050            // avoids matching on a value we just computed.
3051            //
3052            // Widgets whose parent placeholder is NOT in the map (e.g.
3053            // the caller supplied a hand-built ref, or `field_parent` was
3054            // set from outside the FormManager) are left unchanged — not
3055            // every `/Parent` necessarily comes from the FormManager.
3056            if let Some(placeholder) = annotation.field_parent {
3057                if let Some(real_id) = self.form_field_placeholder_map.get(&placeholder) {
3058                    annot_dict.set("Parent", Object::Reference(*real_id));
3059                }
3060            }
3061
3062            // Externalize inline streams inside /AP.
3063            //
3064            // `Widget::generate_appearance` (and any user-supplied appearance
3065            // dictionary) stores the /N, /R, /D entries as inline
3066            // `Object::Stream` values inside the /AP sub-dictionary. Per
3067            // ISO 32000-1 §7.3.8.1, "all streams shall be indirect objects" —
3068            // inline streams as dictionary values are not permitted. We
3069            // therefore externalize each inline stream to a freshly
3070            // allocated indirect object and replace it with a /Reference.
3071            //
3072            // /AP itself has two legal shapes (§12.5.5):
3073            //   * A single stream (direct or indirect) → the "default" state.
3074            //   * A sub-dictionary mapping state names (/N, /R, /D) to
3075            //     streams, where /D may further be a dict mapping values to
3076            //     streams (radio buttons, checkboxes).
3077            // We handle the sub-dict shape (which is what `fill_field`
3078            // emits); the legacy single-stream shape falls through to the
3079            // writer's default handling below.
3080            if let Some(Object::Dictionary(ap_dict)) = annot_dict.get("AP") {
3081                let mut updated_ap = crate::objects::Dictionary::new();
3082                for (state_key, state_val) in ap_dict.iter() {
3083                    match state_val {
3084                        Object::Stream(sd, data) => {
3085                            // Patch `/Resources/Font/<name>` placeholders to
3086                            // indirect references to the document-level fonts
3087                            // (issue #212 Fase 3). The placeholder is emitted
3088                            // by form-field appearance generators that don't
3089                            // know the Type0 font's ObjectId.
3090                            let patched_sd = Self::rewrite_ap_stream_font_resources(sd, font_refs);
3091                            let stream_id = self.allocate_object_id();
3092                            self.write_object(stream_id, Object::Stream(patched_sd, data.clone()))?;
3093                            updated_ap.set(state_key, Object::Reference(stream_id));
3094                        }
3095                        Object::Dictionary(down_dict) => {
3096                            // /D sub-dict case: map value → stream.
3097                            let externalized = self
3098                                .externalize_streams_in_dict_with_font_refs(down_dict, font_refs)?;
3099                            updated_ap.set(state_key, Object::Dictionary(externalized));
3100                        }
3101                        _ => {
3102                            updated_ap.set(state_key, state_val.clone());
3103                        }
3104                    }
3105                }
3106                annot_dict.set("AP", Object::Dictionary(updated_ap));
3107            }
3108
3109            self.write_object(annot_id, Object::Dictionary(annot_dict))?;
3110            annot_refs.push(Object::Reference(annot_id));
3111
3112            // Track widget annotations for AcroForm if they come through this path
3113            if annotation.annotation_type == crate::annotations::AnnotationType::Widget {
3114                if let Some(Object::String(field_name)) = annotation.properties.get("T") {
3115                    self.field_widget_map
3116                        .entry(field_name.clone())
3117                        .or_default()
3118                        .push(annot_id);
3119                    self.field_id_map.insert(field_name.clone(), annot_id);
3120                    self.form_field_ids.push(annot_id);
3121                }
3122            }
3123        }
3124
3125        // Set or remove /Annots based on whether we have any
3126        if !annot_refs.is_empty() {
3127            page_dict.set("Annots", Object::Array(annot_refs));
3128        } else {
3129            page_dict.remove("Annots");
3130        }
3131
3132        self.write_object(page_id, Object::Dictionary(page_dict))?;
3133        Ok(())
3134    }
3135}
3136
3137impl PdfWriter<BufWriter<std::fs::File>> {
3138    pub fn new(path: impl AsRef<Path>) -> Result<Self> {
3139        let file = std::fs::File::create(path)?;
3140        let writer = BufWriter::new(file);
3141
3142        Ok(Self {
3143            writer,
3144            xref_positions: HashMap::new(),
3145            current_position: 0,
3146            next_object_id: 1,
3147            catalog_id: None,
3148            pages_id: None,
3149            info_id: None,
3150            field_widget_map: HashMap::new(),
3151            field_id_map: HashMap::new(),
3152            form_field_ids: Vec::new(),
3153            page_ids: Vec::new(),
3154            config: WriterConfig::default(),
3155            document_used_chars_by_font: std::collections::HashMap::new(),
3156            buffered_objects: HashMap::new(),
3157            compressed_object_map: HashMap::new(),
3158            prev_xref_offset: None,
3159            base_pdf_size: None,
3160            encrypt_obj_id: None,
3161            file_id: None,
3162            encryption_state: None,
3163            pending_encrypt_dict: None,
3164            form_field_placeholder_map: HashMap::new(),
3165            form_manager_field_refs: Vec::new(),
3166        })
3167    }
3168}
3169
3170impl<W: Write> PdfWriter<W> {
3171    /// Write embedded font streams as indirect objects (Phase 3.3 + Phase 3.4)
3172    ///
3173    /// Takes a font dictionary that may contain embedded Stream objects
3174    /// in its FontDescriptor, writes those streams as separate PDF objects,
3175    /// and returns an updated font dictionary with References instead of Streams.
3176    ///
3177    /// For Type0 (composite) fonts, also handles:
3178    /// - DescendantFonts array with embedded CIDFont dictionaries
3179    /// - ToUnicode stream embedded directly in Type0 font
3180    /// - CIDFont → FontDescriptor → FontFile2/FontFile3 chain
3181    ///
3182    /// # Example
3183    /// FontDescriptor:
3184    ///   FontFile2: Stream(dict, font_data)  → Write stream as obj 50
3185    ///   FontFile2: Reference(50, 0)          → Updated reference
3186    /// Walks a dictionary and writes any inline Stream values as indirect objects,
3187    /// replacing them with References. Required because PDF streams must be indirect
3188    /// objects (ISO 32000-1 §7.3.8).
3189    fn externalize_streams_in_dict(
3190        &mut self,
3191        dict: &crate::objects::Dictionary,
3192    ) -> Result<crate::objects::Dictionary> {
3193        self.externalize_streams_in_dict_with_font_refs(dict, &HashMap::new())
3194    }
3195
3196    /// Same as [`externalize_streams_in_dict`] but also rewrites any
3197    /// `/Resources/Font/<name>` placeholders inside the externalised stream
3198    /// dictionaries to indirect references from `font_refs` (issue #212).
3199    fn externalize_streams_in_dict_with_font_refs(
3200        &mut self,
3201        dict: &crate::objects::Dictionary,
3202        font_refs: &HashMap<String, ObjectId>,
3203    ) -> Result<crate::objects::Dictionary> {
3204        let mut result = crate::objects::Dictionary::new();
3205        for (key, value) in dict.iter() {
3206            match value {
3207                Object::Stream(d, data) => {
3208                    let patched_d = Self::rewrite_ap_stream_font_resources(d, font_refs);
3209                    let obj_id = self.allocate_object_id();
3210                    self.write_object(obj_id, Object::Stream(patched_d, data.clone()))?;
3211                    result.set(key, Object::Reference(obj_id));
3212                }
3213                _ => {
3214                    result.set(key, value.clone());
3215                }
3216            }
3217        }
3218        Ok(result)
3219    }
3220
3221    /// Rewrite `/Resources/Font/<name>` entries inside an appearance-stream
3222    /// dictionary: any entry whose name appears in `font_refs` is replaced
3223    /// by an `Object::Reference` to the document-level font object.
3224    ///
3225    /// Why: form-field appearance generators cannot know the ObjectId of
3226    /// the Type0 font at content-stream build time — they emit a
3227    /// placeholder dict (see `TextFieldAppearance::generate_appearance_with_font`).
3228    /// This pass wires that placeholder to the real indirect object produced
3229    /// by `write_fonts`. Built-in Type1 fonts (Helvetica etc.) stay as
3230    /// inline dictionaries, since they have no document-level object.
3231    ///
3232    /// Returns a copy of the input dictionary with the /Resources/Font
3233    /// rewrite applied. All non-/Resources keys are passed through intact.
3234    /// Called on the stream DICTIONARY (not the stream data) so the original
3235    /// content bytes remain untouched.
3236    fn rewrite_ap_stream_font_resources(
3237        stream_dict: &crate::objects::Dictionary,
3238        font_refs: &HashMap<String, ObjectId>,
3239    ) -> crate::objects::Dictionary {
3240        // Fast path: if the document has no custom fonts registered (i.e.
3241        // `font_refs` is empty), no placeholder entry can possibly match.
3242        // Skip the clone+walk entirely — this is the common case for
3243        // built-in-font forms, and `externalize_streams_in_dict` (the
3244        // legacy non-AP path) calls us with an empty map for every stream
3245        // it externalises.
3246        if font_refs.is_empty() {
3247            return stream_dict.clone();
3248        }
3249
3250        let mut out = stream_dict.clone();
3251
3252        // Drill /Resources → /Font. Both may be direct dicts; we rebuild
3253        // them rather than mutate in place so reference semantics are
3254        // explicit. Indirect /Resources isn't emitted by our generators, so
3255        // only the direct-dict shape is handled here (defensive: anything
3256        // else is left untouched).
3257        let Some(Object::Dictionary(resources)) = stream_dict.get("Resources") else {
3258            return out;
3259        };
3260        let Some(Object::Dictionary(fonts)) = resources.get("Font") else {
3261            return out;
3262        };
3263
3264        let mut patched_fonts = crate::objects::Dictionary::new();
3265        let mut changed = false;
3266        for (font_name, entry) in fonts.iter() {
3267            // Rewrite when (a) this is the placeholder inline dict shape our
3268            // generator emits (Object::Dictionary with /Subtype /Type0), AND
3269            // (b) the name is registered as a document-level custom font.
3270            let should_rewrite = match entry {
3271                Object::Dictionary(d) => {
3272                    matches!(d.get("Subtype"), Some(Object::Name(s)) if s == "Type0")
3273                }
3274                _ => false,
3275            };
3276            if should_rewrite {
3277                if let Some(font_id) = font_refs.get(font_name.as_str()) {
3278                    patched_fonts.set(font_name, Object::Reference(*font_id));
3279                    changed = true;
3280                    continue;
3281                }
3282            }
3283            patched_fonts.set(font_name, entry.clone());
3284        }
3285
3286        if changed {
3287            let mut patched_resources = resources.clone();
3288            patched_resources.set("Font", Object::Dictionary(patched_fonts));
3289            out.set("Resources", Object::Dictionary(patched_resources));
3290        }
3291        out
3292    }
3293
3294    fn write_embedded_font_streams(
3295        &mut self,
3296        font_dict: &crate::objects::Dictionary,
3297    ) -> Result<crate::objects::Dictionary> {
3298        let mut updated_font = font_dict.clone();
3299
3300        // Phase 3.4: Check for Type0 fonts with embedded DescendantFonts
3301        if let Some(Object::Name(subtype)) = font_dict.get("Subtype") {
3302            if subtype == "Type0" {
3303                // Process DescendantFonts array
3304                if let Some(Object::Array(descendants)) = font_dict.get("DescendantFonts") {
3305                    let mut updated_descendants = Vec::new();
3306
3307                    for descendant in descendants {
3308                        match descendant {
3309                            Object::Dictionary(cidfont) => {
3310                                // CIDFont is embedded as Dictionary, process its FontDescriptor
3311                                let updated_cidfont =
3312                                    self.write_cidfont_embedded_streams(cidfont)?;
3313                                // Write CIDFont as a separate object
3314                                let cidfont_id = self.allocate_object_id();
3315                                self.write_object(cidfont_id, Object::Dictionary(updated_cidfont))?;
3316                                // Replace with reference
3317                                updated_descendants.push(Object::Reference(cidfont_id));
3318                            }
3319                            Object::Reference(_) => {
3320                                // Already a reference, keep as-is
3321                                updated_descendants.push(descendant.clone());
3322                            }
3323                            _ => {
3324                                updated_descendants.push(descendant.clone());
3325                            }
3326                        }
3327                    }
3328
3329                    updated_font.set("DescendantFonts", Object::Array(updated_descendants));
3330                }
3331
3332                // Process ToUnicode stream if embedded
3333                if let Some(Object::Stream(stream_dict, stream_data)) = font_dict.get("ToUnicode") {
3334                    let tounicode_id = self.allocate_object_id();
3335                    self.write_object(
3336                        tounicode_id,
3337                        Object::Stream(stream_dict.clone(), stream_data.clone()),
3338                    )?;
3339                    updated_font.set("ToUnicode", Object::Reference(tounicode_id));
3340                }
3341
3342                return Ok(updated_font);
3343            }
3344        }
3345
3346        // Original Phase 3.3 logic for simple fonts (Type1, TrueType, etc.)
3347        // Check if font has a FontDescriptor
3348        if let Some(Object::Dictionary(descriptor)) = font_dict.get("FontDescriptor") {
3349            let mut updated_descriptor = descriptor.clone();
3350            let font_file_keys = ["FontFile", "FontFile2", "FontFile3"];
3351
3352            // Check each font file key for embedded streams
3353            for key in &font_file_keys {
3354                if let Some(Object::Stream(stream_dict, stream_data)) = descriptor.get(*key) {
3355                    // Found embedded stream! Write it as a separate object
3356                    let stream_id = self.allocate_object_id();
3357                    let stream_obj = Object::Stream(stream_dict.clone(), stream_data.clone());
3358                    self.write_object(stream_id, stream_obj)?;
3359
3360                    // Replace Stream with Reference to the newly written object
3361                    updated_descriptor.set(*key, Object::Reference(stream_id));
3362                }
3363                // If it's already a Reference, leave it as-is
3364            }
3365
3366            // Update FontDescriptor in font dictionary
3367            updated_font.set("FontDescriptor", Object::Dictionary(updated_descriptor));
3368        }
3369
3370        Ok(updated_font)
3371    }
3372
3373    /// Helper function to process CIDFont embedded streams (Phase 3.4)
3374    fn write_cidfont_embedded_streams(
3375        &mut self,
3376        cidfont: &crate::objects::Dictionary,
3377    ) -> Result<crate::objects::Dictionary> {
3378        let mut updated_cidfont = cidfont.clone();
3379
3380        // Process FontDescriptor
3381        if let Some(Object::Dictionary(descriptor)) = cidfont.get("FontDescriptor") {
3382            let mut updated_descriptor = descriptor.clone();
3383            let font_file_keys = ["FontFile", "FontFile2", "FontFile3"];
3384
3385            // Write embedded font streams
3386            for key in &font_file_keys {
3387                if let Some(Object::Stream(stream_dict, stream_data)) = descriptor.get(*key) {
3388                    let stream_id = self.allocate_object_id();
3389                    self.write_object(
3390                        stream_id,
3391                        Object::Stream(stream_dict.clone(), stream_data.clone()),
3392                    )?;
3393                    updated_descriptor.set(*key, Object::Reference(stream_id));
3394                }
3395            }
3396
3397            // Write FontDescriptor as a separate object
3398            let descriptor_id = self.allocate_object_id();
3399            self.write_object(descriptor_id, Object::Dictionary(updated_descriptor))?;
3400
3401            // Update CIDFont to reference the FontDescriptor
3402            updated_cidfont.set("FontDescriptor", Object::Reference(descriptor_id));
3403        }
3404
3405        // Process CIDToGIDMap if present and embedded as stream
3406        if let Some(Object::Stream(map_dict, map_data)) = cidfont.get("CIDToGIDMap") {
3407            let map_id = self.allocate_object_id();
3408            self.write_object(map_id, Object::Stream(map_dict.clone(), map_data.clone()))?;
3409            updated_cidfont.set("CIDToGIDMap", Object::Reference(map_id));
3410        }
3411
3412        Ok(updated_cidfont)
3413    }
3414
3415    fn allocate_object_id(&mut self) -> ObjectId {
3416        let id = ObjectId::new(self.next_object_id, 0);
3417        self.next_object_id += 1;
3418        id
3419    }
3420
3421    /// Get catalog_id, returning error if not initialized
3422    fn get_catalog_id(&self) -> Result<ObjectId> {
3423        self.catalog_id.ok_or_else(|| {
3424            PdfError::InvalidOperation(
3425                "catalog_id not initialized - write_document() must be called first".to_string(),
3426            )
3427        })
3428    }
3429
3430    /// Get pages_id, returning error if not initialized
3431    fn get_pages_id(&self) -> Result<ObjectId> {
3432        self.pages_id.ok_or_else(|| {
3433            PdfError::InvalidOperation(
3434                "pages_id not initialized - write_document() must be called first".to_string(),
3435            )
3436        })
3437    }
3438
3439    /// Get info_id, returning error if not initialized
3440    fn get_info_id(&self) -> Result<ObjectId> {
3441        self.info_id.ok_or_else(|| {
3442            PdfError::InvalidOperation(
3443                "info_id not initialized - write_document() must be called first".to_string(),
3444            )
3445        })
3446    }
3447
3448    fn write_object(&mut self, id: ObjectId, object: Object) -> Result<()> {
3449        use crate::writer::ObjectStreamWriter;
3450
3451        // Encrypt the object if encryption is active
3452        let object = if let Some(ref enc_state) = self.encryption_state {
3453            let mut obj = object;
3454            enc_state.encryptor.encrypt_object(&mut obj, &id)?;
3455            obj
3456        } else {
3457            object
3458        };
3459
3460        // If object streams enabled and object is compressible, buffer it
3461        if self.config.use_object_streams && ObjectStreamWriter::can_compress(&object) {
3462            let mut buffer = Vec::new();
3463            self.write_object_value_to_buffer(&object, &mut buffer)?;
3464            self.buffered_objects.insert(id, buffer);
3465            return Ok(());
3466        }
3467
3468        // Otherwise write immediately (streams, encryption dicts, etc.)
3469        self.xref_positions.insert(id, self.current_position);
3470
3471        // Pre-format header to count exact bytes once
3472        let header = format!("{} {} obj\n", id.number(), id.generation());
3473        self.write_bytes(header.as_bytes())?;
3474
3475        self.write_object_value(&object)?;
3476
3477        self.write_bytes(b"\nendobj\n")?;
3478        Ok(())
3479    }
3480
3481    fn write_object_value(&mut self, object: &Object) -> Result<()> {
3482        match object {
3483            Object::Null => self.write_bytes(b"null")?,
3484            Object::Boolean(b) => self.write_bytes(if *b { b"true" } else { b"false" })?,
3485            Object::Integer(i) => self.write_bytes(i.to_string().as_bytes())?,
3486            Object::Real(f) => self.write_bytes(
3487                format!("{f:.6}")
3488                    .trim_end_matches('0')
3489                    .trim_end_matches('.')
3490                    .as_bytes(),
3491            )?,
3492            Object::String(s) => {
3493                // ISO 32000-1 §7.3.4.2: inside a literal string, the
3494                // characters `\`, `(` and `)` MUST be escaped (as `\\`,
3495                // `\(`, `\)` respectively) so the parser does not
3496                // terminate the string early or treat `\` as an escape
3497                // introducer for the following byte. Without this, a
3498                // caller-supplied value containing `)` (e.g. through
3499                // `Document::fill_field`) would close the literal and
3500                // allow dict-level injection into the enclosing object.
3501                self.write_bytes(b"(")?;
3502                self.write_bytes(&escape_pdf_string_bytes(s.as_bytes()))?;
3503                self.write_bytes(b")")?;
3504            }
3505            Object::ByteString(bytes) => {
3506                // Write as PDF hex string <AABB...> for byte-perfect binary data
3507                self.write_bytes(b"<")?;
3508                for byte in bytes {
3509                    self.write_bytes(format!("{byte:02X}").as_bytes())?;
3510                }
3511                self.write_bytes(b">")?;
3512            }
3513            Object::Name(n) => {
3514                self.write_bytes(b"/")?;
3515                self.write_bytes(n.as_bytes())?;
3516            }
3517            Object::Array(arr) => {
3518                self.write_bytes(b"[")?;
3519                for (i, obj) in arr.iter().enumerate() {
3520                    if i > 0 {
3521                        self.write_bytes(b" ")?;
3522                    }
3523                    self.write_object_value(obj)?;
3524                }
3525                self.write_bytes(b"]")?;
3526            }
3527            Object::Dictionary(dict) => {
3528                // Sort entries lexicographically by key for reproducible
3529                // output. `Dictionary` is backed by `HashMap` (with
3530                // per-instance randomised iteration order), so two
3531                // identical logical documents would otherwise emit
3532                // byte-different PDFs. PDF dict entries are unordered
3533                // by spec (ISO 32000-1 §7.3.7 Table 5: "the order of
3534                // entries ... is not significant"), so sorting is safe.
3535                self.write_bytes(b"<<")?;
3536                let mut entries: Vec<(&String, &Object)> = dict.entries().collect();
3537                entries.sort_by_key(|(k, _)| k.as_str());
3538                for (key, value) in entries {
3539                    self.write_bytes(b"\n/")?;
3540                    self.write_bytes(key.as_bytes())?;
3541                    self.write_bytes(b" ")?;
3542                    self.write_object_value(value)?;
3543                }
3544                self.write_bytes(b"\n>>")?;
3545            }
3546            Object::Stream(dict, data) => {
3547                // CRITICAL: Ensure Length in dictionary matches actual data length
3548                // This prevents "Bad Length" PDF syntax errors
3549                let mut corrected_dict = dict.clone();
3550                corrected_dict.set("Length", Object::Integer(data.len() as i64));
3551
3552                self.write_object_value(&Object::Dictionary(corrected_dict))?;
3553                self.write_bytes(b"\nstream\n")?;
3554                self.write_bytes(data)?;
3555                self.write_bytes(b"\nendstream")?;
3556            }
3557            Object::Reference(id) => {
3558                let ref_str = format!("{} {} R", id.number(), id.generation());
3559                self.write_bytes(ref_str.as_bytes())?;
3560            }
3561        }
3562        Ok(())
3563    }
3564
3565    /// Write object value to a buffer (for object streams)
3566    fn write_object_value_to_buffer(&self, object: &Object, buffer: &mut Vec<u8>) -> Result<()> {
3567        match object {
3568            Object::Null => buffer.extend_from_slice(b"null"),
3569            Object::Boolean(b) => buffer.extend_from_slice(if *b { b"true" } else { b"false" }),
3570            Object::Integer(i) => buffer.extend_from_slice(i.to_string().as_bytes()),
3571            Object::Real(f) => buffer.extend_from_slice(
3572                format!("{f:.6}")
3573                    .trim_end_matches('0')
3574                    .trim_end_matches('.')
3575                    .as_bytes(),
3576            ),
3577            Object::String(s) => {
3578                // Same escape rules as the streaming `write_object_value`
3579                // path — see ISO 32000-1 §7.3.4.2.
3580                buffer.push(b'(');
3581                buffer.extend_from_slice(&escape_pdf_string_bytes(s.as_bytes()));
3582                buffer.push(b')');
3583            }
3584            Object::ByteString(bytes) => {
3585                buffer.push(b'<');
3586                for byte in bytes {
3587                    buffer.extend_from_slice(format!("{byte:02X}").as_bytes());
3588                }
3589                buffer.push(b'>');
3590            }
3591            Object::Name(n) => {
3592                buffer.push(b'/');
3593                buffer.extend_from_slice(n.as_bytes());
3594            }
3595            Object::Array(arr) => {
3596                buffer.push(b'[');
3597                for (i, obj) in arr.iter().enumerate() {
3598                    if i > 0 {
3599                        buffer.push(b' ');
3600                    }
3601                    self.write_object_value_to_buffer(obj, buffer)?;
3602                }
3603                buffer.push(b']');
3604            }
3605            Object::Dictionary(dict) => {
3606                // Same deterministic-order rule as the streaming writer
3607                // (see `write_object_value`): sort entries by key for
3608                // reproducible output across builds.
3609                buffer.extend_from_slice(b"<<");
3610                let mut entries: Vec<(&String, &Object)> = dict.entries().collect();
3611                entries.sort_by_key(|(k, _)| k.as_str());
3612                for (key, value) in entries {
3613                    buffer.extend_from_slice(b"\n/");
3614                    buffer.extend_from_slice(key.as_bytes());
3615                    buffer.push(b' ');
3616                    self.write_object_value_to_buffer(value, buffer)?;
3617                }
3618                buffer.extend_from_slice(b"\n>>");
3619            }
3620            Object::Stream(_, _) => {
3621                // Streams should never be compressed in object streams
3622                return Err(crate::error::PdfError::ObjectStreamError(
3623                    "Cannot compress stream objects in object streams".to_string(),
3624                ));
3625            }
3626            Object::Reference(id) => {
3627                let ref_str = format!("{} {} R", id.number(), id.generation());
3628                buffer.extend_from_slice(ref_str.as_bytes());
3629            }
3630        }
3631        Ok(())
3632    }
3633
3634    /// Flush buffered objects as compressed object streams
3635    fn flush_object_streams(&mut self) -> Result<()> {
3636        if self.buffered_objects.is_empty() {
3637            return Ok(());
3638        }
3639
3640        // Create object stream writer
3641        let config = ObjectStreamConfig {
3642            max_objects_per_stream: 100,
3643            compression_level: 6,
3644            enabled: true,
3645        };
3646        let mut os_writer = ObjectStreamWriter::new(config);
3647
3648        // Sort buffered objects by ID for deterministic output
3649        let mut buffered: Vec<_> = self.buffered_objects.iter().collect();
3650        buffered.sort_by_key(|(id, _)| id.number());
3651
3652        // Add all buffered objects to the stream writer
3653        for (id, data) in buffered {
3654            os_writer.add_object(*id, data.clone())?;
3655        }
3656
3657        // Finalize and get completed streams
3658        let streams = os_writer.finalize()?;
3659
3660        // Write each object stream to the PDF
3661        for mut stream in streams {
3662            let stream_id = stream.stream_id;
3663
3664            // Generate compressed stream data
3665            let compressed_data = stream.generate_stream_data(6)?;
3666
3667            // Generate stream dictionary
3668            let dict = stream.generate_dictionary(&compressed_data);
3669
3670            // Track compressed object mapping for xref
3671            for (index, (obj_id, _)) in stream.objects.iter().enumerate() {
3672                self.compressed_object_map
3673                    .insert(*obj_id, (stream_id, index as u32));
3674            }
3675
3676            // Write the object stream itself
3677            self.xref_positions.insert(stream_id, self.current_position);
3678
3679            let header = format!("{} {} obj\n", stream_id.number(), stream_id.generation());
3680            self.write_bytes(header.as_bytes())?;
3681
3682            self.write_object_value(&Object::Dictionary(dict))?;
3683
3684            self.write_bytes(b"\nstream\n")?;
3685            self.write_bytes(&compressed_data)?;
3686            self.write_bytes(b"\nendstream\nendobj\n")?;
3687        }
3688
3689        Ok(())
3690    }
3691
3692    fn write_xref(&mut self) -> Result<()> {
3693        self.write_bytes(b"xref\n")?;
3694
3695        // Sort by object number and write entries
3696        let mut entries: Vec<_> = self
3697            .xref_positions
3698            .iter()
3699            .map(|(id, pos)| (*id, *pos))
3700            .collect();
3701        entries.sort_by_key(|(id, _)| id.number());
3702
3703        // Find the highest object number to determine size
3704        let max_obj_num = entries.iter().map(|(id, _)| id.number()).max().unwrap_or(0);
3705
3706        // Write subsection header - PDF 1.7 spec allows multiple subsections
3707        // For simplicity, write one subsection from 0 to max
3708        self.write_bytes(b"0 ")?;
3709        self.write_bytes((max_obj_num + 1).to_string().as_bytes())?;
3710        self.write_bytes(b"\n")?;
3711
3712        // Write free object entry
3713        self.write_bytes(b"0000000000 65535 f \n")?;
3714
3715        // Write entries for all object numbers from 1 to max
3716        // Fill in gaps with free entries
3717        for obj_num in 1..=max_obj_num {
3718            let _obj_id = ObjectId::new(obj_num, 0);
3719            if let Some((_, position)) = entries.iter().find(|(id, _)| id.number() == obj_num) {
3720                let entry = format!("{:010} {:05} n \n", position, 0);
3721                self.write_bytes(entry.as_bytes())?;
3722            } else {
3723                // Free entry for gap
3724                self.write_bytes(b"0000000000 00000 f \n")?;
3725            }
3726        }
3727
3728        Ok(())
3729    }
3730
3731    fn write_xref_stream(&mut self) -> Result<()> {
3732        let catalog_id = self.get_catalog_id()?;
3733        let info_id = self.get_info_id()?;
3734
3735        // Allocate object ID for the xref stream
3736        let xref_stream_id = self.allocate_object_id();
3737        let xref_position = self.current_position;
3738
3739        // Create XRef stream writer with trailer information
3740        let mut xref_writer = XRefStreamWriter::new(xref_stream_id);
3741        xref_writer.set_trailer_info(catalog_id, info_id);
3742
3743        // Add free entry for object 0
3744        xref_writer.add_free_entry(0, 65535);
3745
3746        // Sort entries by object number
3747        let mut entries: Vec<_> = self
3748            .xref_positions
3749            .iter()
3750            .map(|(id, pos)| (*id, *pos))
3751            .collect();
3752        entries.sort_by_key(|(id, _)| id.number());
3753
3754        // Find the highest object number (including the xref stream itself)
3755        let max_obj_num = entries
3756            .iter()
3757            .map(|(id, _)| id.number())
3758            .max()
3759            .unwrap_or(0)
3760            .max(xref_stream_id.number());
3761
3762        // Add entries for all objects (including compressed objects)
3763        for obj_num in 1..=max_obj_num {
3764            let obj_id = ObjectId::new(obj_num, 0);
3765
3766            if obj_num == xref_stream_id.number() {
3767                // The xref stream entry will be added with the correct position
3768                xref_writer.add_in_use_entry(xref_position, 0);
3769            } else if let Some((stream_id, index)) = self.compressed_object_map.get(&obj_id) {
3770                // Type 2: Object is compressed in an object stream
3771                xref_writer.add_compressed_entry(stream_id.number(), *index);
3772            } else if let Some((id, position)) =
3773                entries.iter().find(|(id, _)| id.number() == obj_num)
3774            {
3775                // Type 1: Regular in-use entry
3776                xref_writer.add_in_use_entry(*position, id.generation());
3777            } else {
3778                // Type 0: Free entry for gap
3779                xref_writer.add_free_entry(0, 0);
3780            }
3781        }
3782
3783        // Mark position for xref stream object
3784        self.xref_positions.insert(xref_stream_id, xref_position);
3785
3786        // Write object header
3787        self.write_bytes(
3788            format!(
3789                "{} {} obj\n",
3790                xref_stream_id.number(),
3791                xref_stream_id.generation()
3792            )
3793            .as_bytes(),
3794        )?;
3795
3796        // Get the encoded data
3797        let uncompressed_data = xref_writer.encode_entries();
3798        let final_data = if self.config.compress_streams {
3799            crate::compression::compress(&uncompressed_data)?
3800        } else {
3801            uncompressed_data
3802        };
3803
3804        // Create and write dictionary
3805        let mut dict = xref_writer.create_dictionary(None);
3806        dict.set("Length", Object::Integer(final_data.len() as i64));
3807
3808        // Add filter if compression is enabled
3809        if self.config.compress_streams {
3810            dict.set("Filter", Object::Name("FlateDecode".to_string()));
3811        }
3812        self.write_bytes(b"<<")?;
3813        for (key, value) in dict.iter() {
3814            self.write_bytes(b"\n/")?;
3815            self.write_bytes(key.as_bytes())?;
3816            self.write_bytes(b" ")?;
3817            self.write_object_value(value)?;
3818        }
3819        self.write_bytes(b"\n>>\n")?;
3820
3821        // Write stream
3822        self.write_bytes(b"stream\n")?;
3823        self.write_bytes(&final_data)?;
3824        self.write_bytes(b"\nendstream\n")?;
3825        self.write_bytes(b"endobj\n")?;
3826
3827        // Write startxref and EOF
3828        self.write_bytes(b"\nstartxref\n")?;
3829        self.write_bytes(xref_position.to_string().as_bytes())?;
3830        self.write_bytes(b"\n%%EOF\n")?;
3831
3832        Ok(())
3833    }
3834
3835    /// Write the encryption dictionary as an indirect object and store
3836    /// the object ID and file ID for the trailer.
3837    /// Initialize encryption state: generates file ID, creates encryption dict,
3838    /// computes encryption key, and builds the ObjectEncryptor.
3839    /// The /Encrypt dict object is written later (after all other objects) since it
3840    /// must NOT be encrypted itself (ISO 32000-1 §7.6.1).
3841    fn init_encryption(&mut self, encryption: &crate::document::DocumentEncryption) -> Result<()> {
3842        use crate::encryption::{
3843            CryptFilterManager, CryptFilterMethod, FunctionalCryptFilter, ObjectEncryptor,
3844        };
3845        use std::sync::Arc;
3846
3847        // Generate file ID (16 random bytes, required by ISO 32000-1 §7.5.5)
3848        let mut fid = vec![0u8; 16];
3849        use rand::Rng;
3850        rand::rng().fill_bytes(&mut fid);
3851
3852        let enc_dict = encryption
3853            .create_encryption_dict(Some(&fid))
3854            .map_err(|e| PdfError::EncryptionError(format!("encryption dict: {}", e)))?;
3855
3856        // Compute encryption key
3857        let enc_key = encryption
3858            .get_encryption_key(&enc_dict, Some(&fid))
3859            .map_err(|e| PdfError::EncryptionError(format!("encryption key: {}", e)))?;
3860
3861        // Build CryptFilterManager based on encryption strength
3862        let handler = encryption.handler();
3863        let (method, key_len) = match encryption.strength {
3864            crate::document::EncryptionStrength::Rc4_40bit => (CryptFilterMethod::V2, Some(5)),
3865            crate::document::EncryptionStrength::Rc4_128bit => (CryptFilterMethod::V2, Some(16)),
3866            crate::document::EncryptionStrength::Aes128 => (CryptFilterMethod::AESV2, Some(16)),
3867            crate::document::EncryptionStrength::Aes256 => (CryptFilterMethod::AESV3, Some(32)),
3868        };
3869
3870        let std_filter = FunctionalCryptFilter {
3871            name: "StdCF".to_string(),
3872            method,
3873            length: key_len,
3874            auth_event: crate::encryption::AuthEvent::DocOpen,
3875            recipients: None,
3876        };
3877
3878        let mut filter_manager =
3879            CryptFilterManager::new(Box::new(handler), "StdCF".to_string(), "StdCF".to_string());
3880        filter_manager.add_filter(std_filter);
3881
3882        let encryptor =
3883            ObjectEncryptor::new(Arc::new(filter_manager), enc_key, enc_dict.encrypt_metadata);
3884
3885        // Reserve ID for /Encrypt dict (will be written at the end)
3886        let encrypt_id = self.allocate_object_id();
3887        self.encrypt_obj_id = Some(encrypt_id);
3888        self.file_id = Some(fid);
3889        self.encryption_state = Some(WriterEncryptionState { encryptor });
3890
3891        // Store the dict to write later
3892        self.pending_encrypt_dict = Some(enc_dict.to_dict());
3893
3894        Ok(())
3895    }
3896
3897    /// Write the /Encrypt dictionary object (must NOT be encrypted per ISO 32000-1 §7.6.1)
3898    fn write_encryption_dict(&mut self) -> Result<()> {
3899        if let (Some(encrypt_id), Some(dict)) =
3900            (self.encrypt_obj_id, self.pending_encrypt_dict.take())
3901        {
3902            // Temporarily disable encryption so the /Encrypt dict is not encrypted
3903            let enc_state = self.encryption_state.take();
3904            self.write_object(encrypt_id, Object::Dictionary(dict))?;
3905            self.encryption_state = enc_state;
3906        }
3907        Ok(())
3908    }
3909
3910    fn write_trailer(&mut self, xref_position: u64) -> Result<()> {
3911        let catalog_id = self.get_catalog_id()?;
3912        let info_id = self.get_info_id()?;
3913        // Find the highest object number to determine size
3914        let max_obj_num = self
3915            .xref_positions
3916            .keys()
3917            .map(|id| id.number())
3918            .max()
3919            .unwrap_or(0);
3920
3921        let mut trailer = Dictionary::new();
3922        trailer.set("Size", Object::Integer((max_obj_num + 1) as i64));
3923        trailer.set("Root", Object::Reference(catalog_id));
3924        trailer.set("Info", Object::Reference(info_id));
3925
3926        // Add /Prev pointer for incremental updates (ISO 32000-1 §7.5.6)
3927        if let Some(prev_xref) = self.prev_xref_offset {
3928            trailer.set("Prev", Object::Integer(prev_xref as i64));
3929        }
3930
3931        // Add /Encrypt reference and /ID array for encrypted documents
3932        if let Some(encrypt_id) = self.encrypt_obj_id {
3933            trailer.set("Encrypt", Object::Reference(encrypt_id));
3934        }
3935        if let Some(ref fid) = self.file_id {
3936            trailer.set(
3937                "ID",
3938                Object::Array(vec![
3939                    Object::ByteString(fid.clone()),
3940                    Object::ByteString(fid.clone()),
3941                ]),
3942            );
3943        }
3944
3945        self.write_bytes(b"trailer\n")?;
3946        self.write_object_value(&Object::Dictionary(trailer))?;
3947        self.write_bytes(b"\nstartxref\n")?;
3948        self.write_bytes(xref_position.to_string().as_bytes())?;
3949        self.write_bytes(b"\n%%EOF\n")?;
3950
3951        Ok(())
3952    }
3953
3954    fn write_bytes(&mut self, data: &[u8]) -> Result<()> {
3955        self.writer.write_all(data)?;
3956        self.current_position += data.len() as u64;
3957        Ok(())
3958    }
3959
3960    #[allow(dead_code)]
3961    fn create_widget_appearance_stream(&mut self, widget_dict: &Dictionary) -> Result<ObjectId> {
3962        // Get widget rectangle
3963        let rect = if let Some(Object::Array(rect_array)) = widget_dict.get("Rect") {
3964            if rect_array.len() >= 4 {
3965                if let (
3966                    Some(Object::Real(x1)),
3967                    Some(Object::Real(y1)),
3968                    Some(Object::Real(x2)),
3969                    Some(Object::Real(y2)),
3970                ) = (
3971                    rect_array.first(),
3972                    rect_array.get(1),
3973                    rect_array.get(2),
3974                    rect_array.get(3),
3975                ) {
3976                    (*x1, *y1, *x2, *y2)
3977                } else {
3978                    (0.0, 0.0, 100.0, 20.0) // Default
3979                }
3980            } else {
3981                (0.0, 0.0, 100.0, 20.0) // Default
3982            }
3983        } else {
3984            (0.0, 0.0, 100.0, 20.0) // Default
3985        };
3986
3987        let width = rect.2 - rect.0;
3988        let height = rect.3 - rect.1;
3989
3990        // Create appearance stream content
3991        let mut content = String::new();
3992
3993        // Set graphics state
3994        content.push_str("q\n");
3995
3996        // Draw border (black) — single source of truth for color emission.
3997        crate::graphics::color::write_stroke_color(&mut content, crate::graphics::Color::black());
3998        content.push_str("1 w\n"); // 1pt line width
3999
4000        // Draw rectangle border
4001        content.push_str(&format!("0 0 {width} {height} re\n"));
4002        content.push_str("S\n"); // Stroke
4003
4004        // Fill with white background
4005        crate::graphics::color::write_fill_color(&mut content, crate::graphics::Color::white());
4006        content.push_str(&format!("0.5 0.5 {} {} re\n", width - 1.0, height - 1.0));
4007        content.push_str("f\n"); // Fill
4008
4009        // Restore graphics state
4010        content.push_str("Q\n");
4011
4012        // Create stream dictionary
4013        let mut stream_dict = Dictionary::new();
4014        stream_dict.set("Type", Object::Name("XObject".to_string()));
4015        stream_dict.set("Subtype", Object::Name("Form".to_string()));
4016        stream_dict.set(
4017            "BBox",
4018            Object::Array(vec![
4019                Object::Real(0.0),
4020                Object::Real(0.0),
4021                Object::Real(width),
4022                Object::Real(height),
4023            ]),
4024        );
4025        stream_dict.set("Resources", Object::Dictionary(Dictionary::new()));
4026        stream_dict.set("Length", Object::Integer(content.len() as i64));
4027
4028        // Write the appearance stream
4029        let stream_id = self.allocate_object_id();
4030        self.write_object(stream_id, Object::Stream(stream_dict, content.into_bytes()))?;
4031
4032        Ok(stream_id)
4033    }
4034
4035    #[allow(dead_code)]
4036    fn create_field_appearance_stream(
4037        &mut self,
4038        field_dict: &Dictionary,
4039        widget: &crate::forms::Widget,
4040    ) -> Result<ObjectId> {
4041        let width = widget.rect.upper_right.x - widget.rect.lower_left.x;
4042        let height = widget.rect.upper_right.y - widget.rect.lower_left.y;
4043
4044        // Create appearance stream content
4045        let mut content = String::new();
4046
4047        // Set graphics state
4048        content.push_str("q\n");
4049
4050        // Draw background if specified — routed through the shared
4051        // NaN-sanitising helpers (issues #220, #221).
4052        if let Some(bg_color) = &widget.appearance.background_color {
4053            crate::graphics::color::write_fill_color(&mut content, *bg_color);
4054            content.push_str(&format!("0 0 {width} {height} re\n"));
4055            content.push_str("f\n");
4056        }
4057
4058        // Draw border
4059        if let Some(border_color) = &widget.appearance.border_color {
4060            crate::graphics::color::write_stroke_color(&mut content, *border_color);
4061            content.push_str(&format!("{} w\n", widget.appearance.border_width));
4062            content.push_str(&format!("0 0 {width} {height} re\n"));
4063            content.push_str("S\n");
4064        }
4065
4066        // For checkboxes, add a checkmark if checked
4067        if let Some(Object::Name(ft)) = field_dict.get("FT") {
4068            if ft == "Btn" {
4069                if let Some(Object::Name(v)) = field_dict.get("V") {
4070                    if v == "Yes" {
4071                        // Draw checkmark
4072                        crate::graphics::color::write_stroke_color(
4073                            &mut content,
4074                            crate::graphics::Color::black(),
4075                        );
4076                        content.push_str("2 w\n");
4077                        let margin = width * 0.2;
4078                        content.push_str(&format!("{} {} m\n", margin, height / 2.0));
4079                        content.push_str(&format!("{} {} l\n", width / 2.0, margin));
4080                        content.push_str(&format!("{} {} l\n", width - margin, height - margin));
4081                        content.push_str("S\n");
4082                    }
4083                }
4084            }
4085        }
4086
4087        // Restore graphics state
4088        content.push_str("Q\n");
4089
4090        // Create stream dictionary
4091        let mut stream_dict = Dictionary::new();
4092        stream_dict.set("Type", Object::Name("XObject".to_string()));
4093        stream_dict.set("Subtype", Object::Name("Form".to_string()));
4094        stream_dict.set(
4095            "BBox",
4096            Object::Array(vec![
4097                Object::Real(0.0),
4098                Object::Real(0.0),
4099                Object::Real(width),
4100                Object::Real(height),
4101            ]),
4102        );
4103        stream_dict.set("Resources", Object::Dictionary(Dictionary::new()));
4104        stream_dict.set("Length", Object::Integer(content.len() as i64));
4105
4106        // Write the appearance stream
4107        let stream_id = self.allocate_object_id();
4108        self.write_object(stream_id, Object::Stream(stream_dict, content.into_bytes()))?;
4109
4110        Ok(stream_id)
4111    }
4112}
4113
4114/// Format a DateTime as a PDF date string (D:YYYYMMDDHHmmSSOHH'mm)
4115fn format_pdf_date(date: DateTime<Utc>) -> String {
4116    // Format the UTC date according to PDF specification
4117    // D:YYYYMMDDHHmmSSOHH'mm where O is the relationship of local time to UTC (+ or -)
4118    let formatted = date.format("D:%Y%m%d%H%M%S");
4119
4120    // For UTC, the offset is always +00'00
4121    format!("{formatted}+00'00")
4122}
4123
4124#[cfg(test)]
4125mod tests;
4126
4127#[cfg(test)]
4128mod rigorous_tests;