Restore PDF Forms from native formats

This commit is contained in:
gloamxun committed 2026-11-27 15:31:24 +08:00
1 parent c2d3312e5a
commit 57a7097857
12 files changed
+300 -87

No files matched your search

+31 -1
View File
@@ -804,6 +804,8 @@ pub struct State {
pub(super) pdf_catalog: Option<Box<crate::pdf_device::catalog::PdfCatalog>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(super) pdf_objects: Option<Box<crate::pdf_device::page_resources::PdfObjects>>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub(super) pdf_forms: BTreeMap<u32, crate::pdf_device::forms::RegisteredForm>,
pub(super) pdf_info: Vec<u8>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub(super) pdf_pages_attributes: Vec<u8>,
@@ -1008,6 +1010,7 @@ impl Default for State {
xdv_navigation: None,
pdf_catalog: None,
pdf_objects: None,
pdf_forms: BTreeMap::new(),
initial_random_seed: None,
random: None,
pdf_info: Vec::new(),
@@ -1657,10 +1660,16 @@ impl State {
if !self
.pdf_objects
.as_deref_mut()
.is_some_and(crate::pdf_device::page_resources::PdfObjects::retain_format_links)
.is_some_and(crate::pdf_device::page_resources::PdfObjects::retain_format_objects)
{
self.pdf_objects = None;
}
use crate::pdf_device::forms::FormContents;
for form in self.pdf_forms.values_mut() {
if form.contents == FormContents::Shipped {
form.contents = FormContents::DiscardedAtDump;
}
}
self.pdf_info.clear();
self.pdf_pages_attributes.clear();
self.fonts.reset_format_font_state();
@@ -1673,9 +1682,30 @@ impl State {
for b in self.boxes.values_mut() {
super::format_image::boxes::rebind(b, images)?;
}
for form in self.pdf_forms.values_mut() {
if let crate::pdf_device::forms::FormContents::Pending(body) = &mut form.contents {
super::format_image::boxes::rebind(&mut std::sync::Arc::make_mut(body).box_node, images)?;
}
}
Ok(())
}
pub(super) fn validate_format_pdf_forms(&self) -> bool {
use crate::pdf_device::forms::FormContents;
let registered = self.pdf_objects.iter().flat_map(|objects| objects.form_numbers());
registered.eq(self.pdf_forms.keys().copied())
&& self.pdf_forms.iter().all(|(&number, form)| {
number == form.form.number && match &form.contents {
FormContents::Pending(body) => {
let b = &body.box_node;
[form.form.width, form.form.height, form.form.depth] == [b.width, b.height, b.depth]
}
FormContents::DiscardedAtDump => true,
FormContents::Shipped => false,
}
})
}
pub(super) fn restore_legacy_format_fonts(&mut self) -> Result<(), String> {
let replacements = self.fonts.restore_legacy_synthesized_fonts()?;
if !replacements.is_empty() {