Coder Social home page Coder Social logo

feature request: bookmarks about printpdf HOT 4 CLOSED

fschutt avatar fschutt commented on June 29, 2024
feature request: bookmarks

from printpdf.

Comments (4)

g-w1 avatar g-w1 commented on June 29, 2024 1

After some investigation I found this is how you do it:

509 0 obj
<<
/Parent 5 0 R
/Next 524 0 R
/Title (ir\056zig\056pdf)
/A 508 0 R
/Prev 505 0 R
>>

It seems like this is a doubly linked list with a prev and next (I think the object numbers, I dont really know anything about pdfs, this was just examining it with a text editor. /A is actually the page it is referring (marked) to.)

Hopefully this gives more insight.

from printpdf.

fschutt avatar fschutt commented on June 29, 2024 1

Yeah, it's probably not that hard to implement, but I've not done it since it wasn't important for me. If you want to implement it, you'd probably want to start like this:

/// Set the current font, only valid in a `begin_text_section` to
/// `end_text_section` block
#[inline]
pub fn set_font(&self, font: &IndirectFontRef, font_size: f64)
-> ()
{
self.internal_add_operation(Operation::new("Tf",
vec![font.name.clone().into(), (font_size).into()]
));
}

I.e. .add_bookmark(page: &PdfPageId, text: &str)

The only difficulty is that you need to create the bookmark after the page that you're referencing has been created, plus you'd need access to the internal PDF object ID. Maybe instead of writing the bookmarks directly, just store all bookmarks as a HashMap and defer writing them until the document is being saved - because you can't really reference the PDF page until the document is actually written. Otherwise you're going to have a problem trying to reference a page whose PDF object ID hasn't yet been determined.

You need to insert a Reference to reference a page, otherwise refer to the Adobe PDF spec, it probably has some info on creating bookmarks.

from printpdf.

g-w1 avatar g-w1 commented on June 29, 2024

Thanks. Would the object I would want to use be Object::Dictionary(Dictionary) from LoPdf to implement this feature?
You were right, the spec did have a feature for this spec pic.

Edit:

This feature is turning out harder than I thought. The main confusion point for me is where I insert the information. It does not seem like the font setting, because the font is a /Tf name size, while this requires a whole new object (Dictionary) like this (listing the first, last, and how many bookmarks)

5 0 obj
<<
/Count 49
/First 6 0 R
/Last 1559 0 R
>>
endobj

plus an individual object for each bookmark

6 0 obj
<<
/Parent 5 0 R
/Title (astgen\056zig\056pdf)
/A 4 0 R
/Next 73 0 R
>>
endobj

If you could catch me up to speed on where exactly and what functions I should use to insert these "objects" that would be awesome!

from printpdf.

fschutt avatar fschutt commented on June 29, 2024

Dictionaries can be built from a Vec<(String, Object)>, if you pass that to lopdf, it'll create a dictionary. See here:

let mut catalog = LoDictionary::from_iter(vec![
("Type", "Catalog".into()),
("PageLayout", "OneColumn".into()),
("PageMode", "Use0".into()),
("Pages", Reference(pages_id)),
]);

(I've also noticed that Use0 is technically not a valid value, should be UseNone instead). But basically you need to add a new ("Outlines", Reference(outline_dictionary)) to that /Catalog, after the PageMode

image

image

You need to "reserve" a new object ID for the catalog with document.inner_doc.new_object_id(), because it has to be an indirect reference, not a direct one. If you're not sure about how the final PDF should look like, one technique I like to do is to download PDFExplorer (see README) and open an existing PDF that already has working bookmarks, that way you know where to insert them.

from printpdf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.