-
-
Notifications
You must be signed in to change notification settings - Fork 70
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
Examples or Documentation for Text Annotations? #342
Comments
Yes, much functionality has been added but not in terms of creating or handling annotations. Here is a small example on how to add a text annotation: require 'hexapdf'
HexaPDF::Composer.create('annot.pdf') do |comp|
comp.text("Text followed by an annotation", mask_mode: :box)
doc = comp.document
annot = doc.add({Type: :Annot, Subtype: :Text})
annot[:Contents] = 'This is the content of the annotation'
annot[:Rect] = [comp.x, comp.y, comp.x + 10, comp.y + 10]
annot.create_appearance.canvas.
fill_color("hp-blue").
circle(5, 5, 4).fill
comp.page[:Annots] = [annot]
comp.box(:base, height: 1)
end As you can see you still need to add the appropriate PDF annotation object and all the necessary information for the specific annotation type. As of PDF 2.0 each annotation needs to have an associated appearance stream. Can you describe a bit more what you want to achieve? |
Thanks for the super quick response! I'm still hacking together the prototype, so this is provisional. What I want to build will look like this:
So if I understand what you're saying correctly, I'll need something like... doc = HexaPDF::Document.open(original_file)
list_of_corrections.each do |correction|
doc.add({Type: :Annot, Subtype: :Text})
annot[:Contents] = correction.text
annot[:Rect] = [correction.top_x, correction.top_y, correction.bottom_x, correction.bottom_y]
annot.create_appearance # however I want it to look
annots = comp.page[:Annots] || []
annots << annot
comp.page[:Annots]
end Does that seem roughly right to you? Also, I'm happy to dig into any appropriate documentation, whether it's in HexaPDF or the pdf spec. Thanks again for your help. |
Your code seems fine except that you would need to get the page of the correction and use it like so: You also might want to set a few more attributes on the annotation, like The text annotation type displays a symbol (like a note) where it is placed and the annotation text can be revealed on clicking that symbol. In my tests it seems that some viewers automatically change the appearance of the annotation as soon as it is slightly moved. That may confuse users. Maybe another type of annotation is better suited for your needs, like the polygon annotation. As I said before it is necessary that you generate an appearance stream (so as to be forward-compatible with PDF 2.0 even if you generate a 1.7 PDF). The PDF 2.0 spec is freely available, see https://pdfa.org/sponsored-standards/. I would recommend downloading it and referring to section 12.5 "Annotations" for details. |
@gettalong Fantastic! Thank you. I really appreciate the thoughtful response. If I can trouble you with one more question. Let's say I have a PDF that already has a few "example" annotations, which represent what my annotations should look like. What's the best way to interrogate those objects using HexaPDF? I image there will be a lot I can just copy. |
You could either use irb, i.e. load the document, retrieve a page and then iterate over all annotations using Or you could use
And yes, this would be a good way to see how to duplicate an annotation. |
First, thanks so much for building this library.
Second, I was wondering if you had any examples for the application of
HexaPDF::Type::Annotations::Text
orHexaPDF::Type::Annotations::MarkupAnnotation
?I see that this was discussed in this older issue; however, it looks like you've added a ton of functionality since then.
Thanks in advance. for any guidance.
The text was updated successfully, but these errors were encountered: