-
Notifications
You must be signed in to change notification settings - Fork 1.5k
ENH: Automatically preserve links in added pages #3298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+263
−2
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9259778
ENH: Automatically preserve links in added pages
larsga ffe57a3
Changes to comply with review comments
larsga acecd83
improve wording
stefan6419846 67cf156
improve naming
stefan6419846 34cbfb5
improve formatting
stefan6419846 a5aa7d9
improve formatting
stefan6419846 8ef73b3
improve formatting
stefan6419846 4d95859
ENH: Automatically preserve links in added pages
larsga 7af1b66
Changes to comply with review comments
larsga 3e913e8
Fix errors introduced by resolving merge conflicts
larsga d6d47e2
fix whitespace
stefan6419846 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
# All rights reserved. | ||
# | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions are | ||
# met: | ||
# | ||
# * Redistributions of source code must retain the above copyright notice, | ||
# this list of conditions and the following disclaimer. | ||
# * Redistributions in binary form must reproduce the above copyright notice, | ||
# this list of conditions and the following disclaimer in the documentation | ||
# and/or other materials provided with the distribution. | ||
# * The name of the author may not be used to endorse or promote products | ||
# derived from this software without specific prior written permission. | ||
# | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
# POSSIBILITY OF SUCH DAMAGE. | ||
|
||
|
||
# This module contains code used by _writer.py to track links in pages | ||
# being added to the writer until the links can be resolved. | ||
|
||
from typing import TYPE_CHECKING, List, Optional, Tuple, Union, cast | ||
|
||
from . import ArrayObject, DictionaryObject, IndirectObject, PdfObject, TextStringObject | ||
|
||
if TYPE_CHECKING: | ||
from .._page import PageObject | ||
from .._reader import PdfReader | ||
from .._writer import PdfWriter | ||
|
||
|
||
class NamedReferenceLink: | ||
"""Named reference link being preserved until we can resolve it correctly.""" | ||
|
||
def __init__(self, reference: TextStringObject, source_pdf: "PdfReader") -> None: | ||
"""reference: TextStringObject with named reference""" | ||
self._reference = reference | ||
self._source_pdf = source_pdf | ||
|
||
def find_referenced_page(self) -> Union[IndirectObject, None]: | ||
destination = self._source_pdf.named_destinations.get(str(self._reference)) | ||
return destination.page if destination else None | ||
|
||
def patch_reference(self, target_pdf: "PdfWriter", new_page: IndirectObject) -> None: | ||
"""target_pdf: PdfWriter which the new link went into""" | ||
# point named destination in new PDF to the new page | ||
if str(self._reference) not in target_pdf.named_destinations: | ||
target_pdf.add_named_destination(str(self._reference), new_page.page_number) | ||
|
||
|
||
class DirectReferenceLink: | ||
"""Direct reference link being preserved until we can resolve it correctly.""" | ||
|
||
def __init__(self, reference: ArrayObject) -> None: | ||
"""reference: an ArrayObject whose first element is the Page indirect object""" | ||
self._reference = reference | ||
|
||
def find_referenced_page(self) -> IndirectObject: | ||
return self._reference[0] | ||
|
||
def patch_reference(self, target_pdf: "PdfWriter", new_page: IndirectObject) -> None: | ||
"""target_pdf: PdfWriter which the new link went into""" | ||
self._reference[0] = new_page | ||
|
||
|
||
ReferenceLink = Union[NamedReferenceLink, DirectReferenceLink] | ||
|
||
|
||
def extract_links(new_page: "PageObject", old_page: "PageObject") -> List[Tuple[ReferenceLink, ReferenceLink]]: | ||
"""Extracts links from two pages on the assumption that the two pages are | ||
the same. Produces one list of (new link, old link) tuples. | ||
""" | ||
new_links = [_build_link(link, new_page) for link in new_page.get("/Annots", [])] | ||
old_links = [_build_link(link, old_page) for link in old_page.get("/Annots", [])] | ||
|
||
return [ | ||
(new_link, old_link) for (new_link, old_link) | ||
in zip(new_links, old_links) | ||
if new_link and old_link | ||
] | ||
|
||
|
||
def _build_link(indirect_object: IndirectObject, page: "PageObject") -> Optional[ReferenceLink]: | ||
src = cast("PdfReader", page.pdf) | ||
link = cast(DictionaryObject, indirect_object.get_object()) | ||
if (not isinstance(link, DictionaryObject)) or link.get("/Subtype") != "/Link": | ||
return None | ||
|
||
if "/A" in link: | ||
action = cast(DictionaryObject, link["/A"]) | ||
if action.get("/S") != "/GoTo": | ||
return None | ||
|
||
return _create_link(action["/D"], src) | ||
|
||
if "/Dest" in link: | ||
return _create_link(link["/Dest"], src) | ||
|
||
return None # Nothing to do here | ||
|
||
|
||
def _create_link(reference: PdfObject, source_pdf: "PdfReader")-> Optional[ReferenceLink]: | ||
if isinstance(reference, TextStringObject): | ||
return NamedReferenceLink(reference, source_pdf) | ||
if isinstance(reference, ArrayObject): | ||
return DirectReferenceLink(reference) | ||
return None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.