Zoom capability in PDFBox - java

PDFBox indicates zooming is capable using PDPageXYZDestination, however I'm really struggling to make it work. An example would be helpful, including where to make calls to this class.
The following was posted in another thread, however there's no indicator of exactly where to put this code:
PDPageXYZDestination dest = new PDPageXYZDestination();
dest.setPageNumber(0);
dest.setZoom(1);
dest.setTop(new Float(PDPage.PAGE_SIZE_LETTER.getHeight()).intValue());
PDActionGoTo action = new PDActionGoTo();
action.setDestination(dest);
document.getDocumentCatalog().setOpenAction(action);
If you have a complete example it would be truly helpful.

Related

In PDFBox, Is it possible to add a tooltip (hint) to a mark annotation?

In a Nutshell
I've been working on a program that gets a pdf, highlights some words (via pdfbox Mark Annotation obj) and saves the new pdf.
I'd like my highlighted words to show a tooltip with some small description on it, like a hint.
For instance, I want that on my pdf, the highlighted word activated shows the tooltip important word found when I stop the mouse over it.
This is the original test pdf.
My Code
With a couple of abstractions, in a nutshell, I have:
File file = new File("path/to/myfile/mypdf.pdf");
PDDocument document = PDDocument.load(file);
PDPage page = document.getPage(0);
List<PDAnnotation> annotations = page.getAnnotations();
PDAnnotationTextMarkup txtMark = new PDAnnotationTextMarkup(PDAnnotationTextMarkup.SUB_TYPE_HIGHLIGHT);
txtMark.setRectangle(pdRectangle);
txtMark.setQuadPoints(quadPoints);
txtMark.setColor(getColor());
annotations.add(txtMark);
Current Result
Currently it generates a pdf with mark annotations like below on the word activated:
What I Want
Now I want to add a tooltip on it, just like I have when I add a hyperlink, like showed below, but with a free text instead. I can only have a tooltip like this if this is attached to a url, even if I added the string I need, pdfbox would internally create a uri out of it...
FYI: this is the annotation link code:
PDAnnotationLink link = new PDAnnotationLink();
link.setAction("www.stackoverflow.com");
link.setRectangle(pdRectangle);
link.setQuadPoints(quads); annotations.add(link);
What I've Tried and Why I'm Not Satisfied Yet
1) I've tried to add an annotation link, as showed above, but with a description instead of a url, like important word found. The result isn't good, the tooltip is transformed to something like: file:///Users/myproject/root/important word found.
Also this link annotation is not the recommended way to go since in some cases I will want to have both a URL and a Tooltip. But if I could turn it around, it would be a real consideration.
2) I've tried to add a content to my mark annotation, which works like a popup, like showed below:
It works... when I mouse over it, my tooltip description shows up beautifully. However, you can see that bubble icon just above the word. That's the only problem with this solution, these bubbles are quite annoying and end up overlapping important part of the text and polluting the pdf. If I could hide them or something I'd be satisfied too.
here is the doc with this annotation.
And the code to add this popup was simple adding the line below:
txtMark.setContents("Important word found");
Conclusion
Any tip to either add a tooltip nicely or remove that annotation bubble will be hugely appreciated. Thanks in advance.
EDIT
As #Tilman Hausherr suggested on comments, I've added the following line to my code:
txtMark.setTitlePopup("Important word found");
Without setting the content. I don't have the annoying bubble anymore, but now I need to double click my annotation and a not much good looking or practical popup shows up:
This helps a little bit since it's the best I got so far.
EDIT 2
My attempt with PDAnnotationPopup: added to my code the lines below, as suggested by #Tilman:
...
PDAnnotationPopup pdAnnotationPopup = new PDAnnotationPopup();
pdAnnotationPopup.setParent(txtMark);
pdAnnotationPopup.setContents("Important word found");
// Just to make sure
pdAnnotationPopup.setInvisible(false);
pdAnnotationPopup.setNoView(false);
pdAnnotationPopup.setNoZoom(false);
pdAnnotationPopup.setLocked(false);
pdAnnotationPopup.setHidden(false);
annotations.add(pdAnnotationPopup);
I also explored other PDAnnotationPopup parameters such as setOpen, setRectangle... and tried to keep it coexisting with txtMark.setTitlePopup.
Unfortunately none of that affected my code in any way. Only when I set setOpen(true) plus setRectangle I could see something: a completely empty popup over each of my text mark annotations.
So I ended up going with #Tilman suggestion, adding a setTitlePopup to my mark without setting the content. In order to the annotations and their tooltips be visible through some viewers, like pdf.js, it's also needed to call the constructAppearances method:
txtMark.setTitlePopup("Important word found");
txtMark.constructAppearances(new PDHighlightAppearanceHandler(txtMark, pdDocument));
While this is not perfect, since it requires a double click on the annotation to be displayed and it's visible only by more robust pdf readers, like Adobe, it is the best solution I could find so far and will suffice. Also it meets my expectations perfectly if you wrap the pdf with pdf.js, like showed below:

How to add tab space to paragragh in itext java [duplicate]

I'm still trying to learn iText and have a few of the concepts down. However I can't figure out what TabStop is or how to use it. My particular problem is that I want to fill the end of all paragraphs with a bunch of dashes. I believe this is called a TabStop and I see the class in the itext jar but I have no clue on how to use it. I must be searching the wrong thing on google, but I've come up with nothing. The iText in Action book also doesnt seem to even know of the existance of this class so any help is much appreciated!
Please take a look at the ChunkTest class in iText's test suite. It contains several use cases of the tab stop functionality. For instance:
java.util.List<TabStop> tabStopsList = new ArrayList<TabStop>();
tabStopsList.add(new TabStop(100, new DottedLineSeparator()));
tabStopsList.add(new TabStop(200, new LineSeparator(), TabStop.Alignment.CENTER));
tabStopsList.add(new TabStop(300, new DottedLineSeparator(), TabStop.Alignment.RIGHT));
p = new Paragraph(new Chunk("Hello world", f));
p.setTabSettings(new TabSettings(tabStopsList, 50));
addTabs(p, f, 0, "la|la");
ct.addElement(p);
The TabStop functionality was introduced after the iText in Action books were written. They'll be documented in one of the new books.
For another example, see http://developers.itextpdf.com/examples/itext-building-blocks/tabbing-examples

Apache Batik: How to force JSVGComponent view to update when SVG Dom modified

Using Apache Batik,
Trying to get the JSVGComponent to update (repaint) after adding a new svg element to the DOM. I know about ALWAYS_DYNAMIC, and that is set. The new element(s) are successfully added to the document, but the only way I can get the component to repaint is to resize the view. Upon resize, the new elements pop (appear) into place. I have also tried to add the elements via a Runnable using the UpdateManager and the RunnableQueue. Again, the elements are successfully added, but don't appear until the component is resized (by resizing the housing frame). I have also tried invoking the repaint via a RunQ Runnable, but that also does not work (tho the repaint is actually called).
I think I may have to actually go into the GVT bridge, but I'd sure rather not.
Help extraordinarily appreciated. Thx.
Almost same problem for me : I had to dynamically modifiy the background of boxes (polygon node).
According to MelodyBibi, it works with setSVGDocument(doc); but the SVG flashes during refresh. It's not very appreciable.
My (better) solution is to call
svgCanvas.getCanvasGraphicsNode().fireGraphicsNodeChangeCompleted();
after modifying the DOM model using polygonElement.setAttribute("fill", "red"); on the appropriate Node.
I also added JSVGCanvas.ALWAYS_DYNAMIC before the first loading of the document.
Now my SVG refreshes in realtime, without blinking.
Hoping this will help you.
You can do
svgCanvas.setDocument(doc);
It's works for an JSVGCanvas, may be it will works for you.
You can also try method :
setSVGDocument(doc);
I found this :
There are two common causes for this. The first is that the JSVGCanvas doesn’t know it’s a dynamic document. Normally Batik detects this by looking for script elements but when you modify the document from Java it can’t tell. So call myJSVGCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC); before loading the document (with setURI, setDocument, setSVGDocument, etc.).
The second common reason is that the changes aren’t made in the UpdateManager ’s thread. You can run code in the UpdateManager ’s thread with the following: UpdateManager um = JSVGCanvas.getUpdateManager(); um.getUpdateRunnableQueue().invokeLater(Runnable); NOTE: The update manager only becomes available after the first rendering completes. You can be notified when this happens by registering a GVTTreeRendererListener.
But i don't try this yet.

Add Background Image and Add Text on the Image

I am new to iText Library. My requirement is My Servlet will create an Mark Sheet(PDF). It will add image to the complete page of the document and Text on the specific location on the image of the document.
Please help?
It is unclear what the parameter text is about. Maybe you picked the direct content that goes under the image, but that's not the main issue.
You must have read some documentation, because you're using beginText(), setFontAndSize(), showText() and endText(), but you didn't read the documentation very well because:
(1) You use lineTo() without a moveTo() first and without a stroke() after. In other words: you're creating a strange path that is never drawn.
(2) You use showText(), but I don't see you defining coordinates for the text anywhere. What happened to your setTextMatrix() method?
(3) You're a newbie, but instead of using simple code, such as:
ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
new Phrase("This is a test"), 100, 100, 0);
Seems like you want to be able to run before you've learned to walk.
Also: you're probaly using an old version of iText, because you don't mention that an exception is thrown when you use the illegal statement lineTo() inside a text block. You can't use lineTo() inside a beginText()/endText() sequence.
Please follow the advice given by mkl and read the documentation first.

Calling a Bitmap Class in Java

I found this post on the site:
Blackberry Clickable BitmapField
explaining a class where you can draw a clickable bitmap. This is exactly what I want to do but the issue for me is that I'm quite new to Java and I don't know how to properly call this statement. What are the commands to create this bitmap? So far all I have is:
CustomMenuButtonField buttonInstance = new CustomMenuButtonField("img1.png", "img2.png");
aside from that I don't know how to make the drawing appear. If I try:
buttonInstance.paint(graphics);
I need to declare graphics which I am not sure how to do. I've checked the API but its still a bit confusing to me. Also where should these call statements be used? In the userInterface constructor?
Once you create your custom button field, you just need to add it to your screen:
add(buttonInstance);
It will then be displayed by the framework automatically. For more info, see the Getting Started Guide for the SDK version you are using in the Development Guides.

Categories

Resources