Skip to main content

pdfium_render/pdf/
link.rs

1//! Defines the [PdfLink] struct, exposing functionality related to a single link contained
2//! within a [PdfPage] or a [PdfPageAnnotation].
3
4use crate::bindgen::{FPDF_DOCUMENT, FPDF_LINK, FS_RECTF};
5use crate::bindings::PdfiumLibraryBindings;
6use crate::error::PdfiumError;
7use crate::pdf::action::PdfAction;
8use crate::pdf::destination::PdfDestination;
9use crate::pdf::rect::PdfRect;
10
11#[cfg(doc)]
12use {
13    crate::pdf::action::PdfActionType, crate::pdf::document::page::PdfPage,
14    crate::pdf::document::page::annotation::PdfPageAnnotation,
15};
16
17/// A single link contained within a [PdfPage] or a [PdfPageAnnotation].
18///
19/// Each link may have a corresponding [PdfAction] that will be triggered when the user
20/// interacts with the link, and a [PdfDestination] that indicates the target of any behaviour
21/// triggered by the [PdfAction].
22pub struct PdfLink<'a> {
23    handle: FPDF_LINK,
24    document: FPDF_DOCUMENT,
25    bindings: &'a dyn PdfiumLibraryBindings,
26}
27
28impl<'a> PdfLink<'a> {
29    #[inline]
30    pub(crate) fn from_pdfium(
31        handle: FPDF_LINK,
32        document: FPDF_DOCUMENT,
33        bindings: &'a dyn PdfiumLibraryBindings,
34    ) -> Self {
35        PdfLink {
36            handle,
37            document,
38            bindings,
39        }
40    }
41
42    /// Returns the internal `FPDF_LINK` handle for this [PdfLink].
43    #[inline]
44    pub(crate) fn handle(&self) -> FPDF_LINK {
45        self.handle
46    }
47
48    /// Returns the [PdfiumLibraryBindings] used by this [PdfLink].
49    #[inline]
50    pub fn bindings(&self) -> &'a dyn PdfiumLibraryBindings {
51        self.bindings
52    }
53
54    /// Returns the [PdfAction] associated with this [PdfLink], if any.
55    ///
56    /// The action indicates the behaviour that will occur when the user interacts with the
57    /// link in a PDF viewer. For most links, this will be a local navigation action
58    /// of type [PdfActionType::GoToDestinationInSameDocument], but the PDF file format supports
59    /// a variety of other actions.
60    pub fn action(&self) -> Option<PdfAction<'a>> {
61        let handle = self.bindings().FPDFLink_GetAction(self.handle());
62
63        if handle.is_null() {
64            None
65        } else {
66            Some(PdfAction::from_pdfium(handle, self.document, self.bindings()))
67        }
68    }
69
70    /// Returns the [PdfDestination] associated with this [PdfLink], if any.
71    ///
72    /// The destination specifies the page and region, if any, that will be the target
73    /// of any behaviour that will occur when the user interacts with the link in a PDF viewer.
74    pub fn destination(&self) -> Option<PdfDestination<'a>> {
75        let handle = self.bindings().FPDFLink_GetDest(self.document, self.handle());
76
77        if handle.is_null() {
78            None
79        } else {
80            Some(PdfDestination::from_pdfium(self.document, handle, self.bindings()))
81        }
82    }
83
84    /// Returns the area on the page that the user can use to interact with this [PdfLink]
85    /// in a PDF viewer, if any.
86    pub fn rect(&self) -> Result<PdfRect, PdfiumError> {
87        let mut rect = FS_RECTF {
88            left: 0.0,
89            top: 0.0,
90            right: 0.0,
91            bottom: 0.0,
92        };
93
94        PdfRect::from_pdfium_as_result(
95            self.bindings().FPDFLink_GetAnnotRect(self.handle(), &mut rect),
96            rect,
97            self.bindings(),
98        )
99    }
100}
101
102#[cfg(test)]
103mod tests {
104    use crate::prelude::*;
105    use crate::utils::test::{test_bind_to_pdfium, test_fixture_path};
106
107    #[test]
108    fn test_link_rect() -> Result<(), PdfiumError> {
109        let pdfium = test_bind_to_pdfium();
110
111        let document = pdfium.load_pdf_from_file(&test_fixture_path("links-test.pdf"), None)?;
112
113        const EXPECTED: PdfRect = PdfRect::new_from_values(733.3627, 207.85417, 757.6127, 333.1458);
114
115        const ABS_ERR: PdfPoints = PdfPoints::new(f32::EPSILON * 1000.);
116
117        let actual = document
118            .pages()
119            .iter()
120            .next()
121            .unwrap()
122            .links()
123            .iter()
124            .next()
125            .unwrap()
126            .rect()?;
127
128        assert!((actual.top() - EXPECTED.top()).abs() < ABS_ERR);
129        assert!((actual.bottom() - EXPECTED.bottom()).abs() < ABS_ERR);
130        assert!((actual.left() - EXPECTED.left()).abs() < ABS_ERR);
131        assert!((actual.right() - EXPECTED.right()).abs() < ABS_ERR);
132
133        Ok(())
134    }
135}