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.
Related
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:
I have color image document with text and images and tables.
Document can have two columns.
Document is composite from areas: area header and text (bigger font, can have different font color and something like sub-header additional data).
This is exemplary image but real one can be color:
What i need to do.
I need find on image document this areas of text with headers.
What i need to know.
Method how to divide document to divide document on particular parts.
I try with opencv in java(if someone have python and c++ version i can convert it for java version by myself). I found few similar problem on stack overflow, but none of them can help me. You must know that my opencv knowledge is not very well and it is only from on-line tutorials and stack overflow.
Is there any fine solution on my problem in opencv way or i need use something else, different library or application to achieve this?
One and only requirement is that it must be done from command line.
If i had this areas i can do what i need next, but this is step which stops me.
have you solved the problem?
I'm working on a similar problem.
My solution is to use HoughLines https://docs.opencv.org/3.4.0/d9/db0/tutorial_hough_lines.html
You can use text detection combined with dilation to detect bold text i.e. headers and then group the text boxes between two consecutive headers as the text under first header.
I am trying to use the Williams objectdraw library (http://eventfuljava.cs.williams.edu/library/objectdrawJavadocV1.1.2/index.html) to make a line on my drawing canvas. I am very new to Java and am unsure whether I am setting it up correctly. Attached is a screenshot of what happens when I run the code.
You have extended WindowController,i think you are suppose to extend Applet..
http://spot.colorado.edu/~cathyb/hiapplet.html have a look at this page it will be useful.
I'm currently using Java and the PDFBox library to create some PDFs on the fly.
I need to be able to set the character spacing/tracking of some text but can't seem to figure it out.
It looks as there is a method to do so : http://ci.apache.org/projects/pdfbox/javadoc/index.html?org/apache/pdfbox/util/operator/SetCharSpacing.html
But I'm not quite sure how to apply this in the situation.
cs.beginText();
cs.setFont( font, fontSize );
cs.setNonStrokingColor(color);
cs.moveTextPositionByAmount(position[0], position[1]);
cs.drawString(text);
cs.endText();
Any help would be appreciated! Thanks.
You need to do it the hard way, because the "Tc" operator isn't supported by the PDPageContentStream class:
cs.appendRawCommands("0.25 Tc\n");
The SetCharSpacing method you mentioned is for parsing existing PDFs.
PS: don't forget to call close after finishing writing to the content stream!
PPS: setCharacterSpacing() is available in version 2.0.4 and higher.
I'm trying to generate an xsl to be printed in a pre-printed sheet which works fine.
Now i want to give the user a better previsualization (in the pdf screen version) adding a background image which emulates the "pre-printed" stuf on the sheet to give the user a "context" of what is he printing.
The question is: Is there any way I can set a background image in xsl (using apache fop) visible only in pdf but not in the printed version of it?
Thank you all for reading or givin any advice.
Although as the comments state, you can't have content in the PDF that does not come out in a physical printed copy, here is one possible work around for you. Depending on how your users are ultimately going to be using FOP for PDF rendering and how your a driving the work flow, it's possible to pass a parameter into an xslt file before the transofrmation phase is run, so potentially, you could do a dual rendering of the same PDF, one that is presented to the user where the background image is enabled, and one that gets printed, you could just set a variable similar to how they do in this Example, and call it something like $isPreview, and just use a simple if or choose statement to check for 'Y' or 'N'.
Since you are sending to a printer, you may even want to take advantage of FOP's ability to generate to Postscript rather than PDF, I've used this feature quite extensively for print documents using FOP while also producing a PDF copy for electronic delivery via email or hosted services, and I've yet to find any discrepancy between the PDF rendering and what is printed after sending a rendered postscript file, so it should work well for you as well.
As I said, this is not truly a solution to your problem as you've presented it, but as a work around, it could get you the desired results if your clever about how you implement it.
I don;t think the statement that it is not possible is true, I am just not sure how to create such a PDF with FOP. Certainly you can add an image field. One would use a button field and place the image in the button. Then you would set the properties of that button to not print (printable false).
PDF support images in fields: https://answers.acrobatusers.com/adding-image-field-form-q41825.aspx
RenderX supports PDF Form fields but I do not see where they support an image inside the button, only text: http://www.renderx.com/reference.html#PDF%20Forms. But they do support setting a field to "printable".