Bind native last-link numbers to shipped PDF annotations

This commit is contained in:
gloamxun committed 2026-11-21 02:31:43 +08:00
1 parent 7f74251c8a
commit 60d7411dfe
12 files changed
+227 -19

No files matched your search

+40 -10
View File
@@ -2207,15 +2207,28 @@ fn write_pdf(
})
.collect::<Vec<_>>();
let annotation_base = image_base + used.len();
let annotation_count = links.len() + list.pdf_links.annotations.len();
let annots = if annotation_count == 0 {
let native_annotation_base = annotation_base + links.len();
let mut next_annotation = native_annotation_base;
let native_annotation_ids: Vec<_> = list.pdf_links.annotations.iter().enumerate()
.map(|(index, _)| {
remapped.as_ref()
.and_then(|resources| resources.link_objects.get(&(page_index, index)).copied())
.unwrap_or_else(|| {
let id = next_annotation;
next_annotation += 1;
id
})
})
.collect();
let annotation_refs = (annotation_base..native_annotation_base)
.chain(native_annotation_ids.iter().copied())
.map(|id| format!("{id} 0 R"))
.collect::<Vec<_>>()
.join(" ");
let annots = if annotation_refs.is_empty() {
String::new()
} else {
let refs = (0..annotation_count)
.map(|index| format!("{} 0 R", annotation_base + index))
.collect::<Vec<_>>()
.join(" ");
format!(" /Annots [{refs}]")
format!(" /Annots [{annotation_refs}]")
};
page_ids.push(page_id);
kids.push(format!("{page_id} 0 R"));
@@ -2295,7 +2308,7 @@ fn write_pdf(
.into_bytes(),
);
}
for annotation in &list.pdf_links.annotations {
for (index, (annotation, id)) in list.pdf_links.annotations.iter().zip(native_annotation_ids).enumerate() {
use crate::pdf_device::links::AnnotationAction;
let [left, top, right, bottom] = annotation.rect;
let coordinates = [left, height - bottom, right, height - top].map(physical);
@@ -2304,8 +2317,25 @@ fn write_pdf(
AnnotationAction::GotoName(name) =>
format!("/Subtype /Link /A << /S /GoTo /D {} >>", pdf_text(name)),
};
objects.push(format!("<< /Type /Annot {} /Rect [{}] {} >>",
annotation.attributes, coordinates.join(" "), action).into_bytes());
let entries = remapped.as_ref().and_then(|resources|
resources.annotations[page_index][index].as_deref());
let body = if let Some(entries) = entries {
let mut body = format!("<< /Type /Annot /Rect [{}] ", coordinates.join(" ")).into_bytes();
body.extend_from_slice(entries);
if matches!(annotation.action, AnnotationAction::GotoName(_)) {
body.extend_from_slice(action.as_bytes());
}
body.extend_from_slice(b" >>");
body
} else {
format!("<< /Type /Annot {} /Rect [{}] {} >>",
annotation.attributes, coordinates.join(" "), action).into_bytes()
};
if id == objects.len() + 1 {
objects.push(body);
} else {
objects[id - 1] = body;
}
}
for destination in &list.pdf_links.destinations {
use crate::pdf_device::links::DestinationView;