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
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 a java application where i have to use "Bodoni MT Black" font using FontFactory in itextPdf how should i modify my code?
This is my code,
Font base = FontFactory.getFont(FontFactory.TIMES_ROMAN, 6);
how to change font to "Bodoni MT Black" (Not supported in FontFactory) instead of TIMES_ROMAN? please help.
The code in your question is code that uses iText 5. However, iText 5 is no longer supported. The current version of iText is iText 7.1.2:
If you are working on a new project, you should abandon iText 5 and upgrade to iText 7 because all new development will be done on iText 7, not on iText 5. Suppose that at some point you need support for PDF 2.0, then you'll need to throw away your iText 5 code, because support for PDF 2.0 will never be supported in iText 5. Suppose that at some point you need support for SVG in the context of HTML to PDF conversion, you will need to throw away all your iText code and start anew with iText 7.
The iText 7 solution
If you follow my advice and upgrade, then you should read the iText 7 tutorial chapter about fonts: https://developers.itextpdf.com/content/itext-7-building-blocks/chapter-1
This tutorial explains that you need a font program if you don't want to use one of the standard Type 1 fonts such as times roman. Mpre specifically, if you want to use "Bodoni MT Black", you need the file BodoniMTBlack.ttf somewhere on your computer, for instance:
public static final String BODONIBLACK = "src/main/resources/fonts/BodoniMTBlack.ttf";
Next, you can use this path to create a FontProgram object obtained from the FontProgramFactory:
FontProgram fontProgram = FontProgramFactory.createFont(BODONIBLACK);
Using the FontProgram instance, you can create a PdfFont object.
PdfFont font = PdfFontFactory.createFont(fontProgram, PdfEncodings.WINANSI, true);
The font instance can be used as a parameter for the setFont() method:
Paragraph bodoni = new Paragraph().setFont(font).add("Bodoni");
The iText 5 solution
In the unlikely event that you don't have any other choice than to use iText 5, then you should read the Using fonts in PDF and iText
Just like with iText 7 you need a font program:
public static final String BODONIBLACK = "src/main/resources/fonts/BodoniMTBlack.ttf";
Now you can create a Font object like this:
BaseFont baseFont = BaseFont.createFont(BODONIBLACK, BaseFont.WINANSI, BaseFont.EMBEDDED);
Font bodoni = new Font(basefont, 12);
Additional note:
Stack Overflow has introduced a new Code of Conduct that aims to create a healthier atmosphere on the web site (be kind, contribute, show respect are some of the aspects highlighted in its subtitle).
In the context of that Code of Conduct, I want to inform you that you can contribute to a better atmosphere in the future by using the information that is provided on the official iText web site before asking a question.
When you are using iText and you are confronted with an iText-related question, your first reflex should be to visit the official iText web site where you will find the information I summarized above. People have done a great effort writing tutorials in answer to questions similar to yours. By ignoring that great content, you fail to appreciate the hard work that was done. Please take that into consideration in the future.
One important note on Bruno's answer above. (my rep<50, so cannot comment on his answer).
FontProgramFactory (at least in C#) is found in 'iText.IO.Font.FontProgramFactory'.
I was initially looking in a different 'Font' namespace : 'iText.Kernel.Font'.
And as Bruno says, all of this can be found on their website. This particular tibdit is at:
https://api.itextpdf.com/iText7/7.0.1/com/itextpdf/io/font/FontProgramFactory.html
So perhaps my note should just be deleted based on the Code of Conduct. Someone else can decide. But as I found Bruno's info helpful here, perhaps this note will save someone a few minutes.
Also, turns out that (at least for me in C#) the 'C' in .createFont needs to be capitalized. So...
'PdfFontFactory.CreateFont' rather than 'PdfFontFactory.createFont'
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.
We have a program in Java using Eclipse Kepler which I am testing for any errors. I found out that one of the modules in which, Product maintenance has a slight misplacement of the design. The problem that I saw was that the product description (text area which has 3,20 for the scroll pane in-case the description was long) exceeded its space and is now colliding with the product price (another text area who has only 0,20 or "",20). Now I believe that the programmer used a Windows Builder to assist him in his design but never told me how to use it. We have our own copies of the system in case we want something change. But I don't think he taught me how to setup it up and use it. Now I was wondering how I should code this by myself so that I could understand how it should go. Frankly, I am not a Java person but willing to learn. Can you help me? If you need more info, feel free to ask.
taProdDesc = new JTextArea(3,20);
spProdDesc = new JScrollPane(taProd, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
tfProdPrice = new JTextField("",20);
it looks something like this
I wanted more space in between the two cause I know it should look like a perfect rectangular shape and then a spacing then the next text area/field.
That's what it looks like in the code but I don't think this is the one is where the actual code that I should edit. But for the meantime so me and the programmer can see it, I want to place it there so later on I could tell him where to place it.
depending on the layout manager you are using, you could create space by adding a border,
panel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
or by using 'setAutoCreateGaps' and 'setAutoCreateContainerGaps' in GroupLayout:
GroupLayout layout = new GroupLayout(panel);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
For more about the LayoutManagers, you should take a look here
For more about borders: take a look here
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.