How to display image on textpane? - java

I am working on a note taking application which is supposed to include image ,audio ,video along with text
So im including a feature to add links(like in evernote )which should display an icon on clicking which that file should open
My question is how write to and read from a file so that when a link is encountered an imageicon mus be displayed in textpane similary a link must be written in the file
I am already able to implement text links i want to know how to do the same with images(image links)

You can Use JEditorPane to use the functionality of both text and Images in java . This would also enable you to add support for custom text formats to the components of your application.
For details Refer : http://docs.oracle.com/javase/tutorial/uiswing/components/editorpane.html

As an alternative to JEditorPane: you might want to check out Flying Saucer, which can render XML/XHTML. If you use a suitable XML model internally, you might be able to render your content directly via CSS.

Related

Setting the font and style of jeditorpane

I am currently developing an application for this company as my last year of studies project, and in this application there's this function that allows you to print a paper, so i used the jeditorpane obviously, with the Charles Bell HTMLEditorKit for the predefined functions like setting the font and printing, etc... and there's this pre-written text that i have to load from a text file and then the program is supposed to add some text inside the file automatically, and the user also is allowed to add more if he needs to, or change the font or style or whatever.
So here is the problem, since the predefined text should be already aligned with the font already set and some other properties already in place, and since the txt file doesn't allow that, i tried to use a docx file, which makes the text incomprehensible in the editorpane, i also tried the docx4j api, but then i wouldn't have the HTMLEditorKit menu and functions, (or maybe i could but i don't know how) and the execution of the app becomes rather slow with this api. I also tried this metaphase editor kit which i found it to be really useful with a lot of functions to offer, but i couldn't load a text file to it's textpane.
So my question(s) to be exact is(are):
1)How can i use the charles bell HTMLEditorKit and load a docx file into the edtiorpane in question?
2)Or how can i use the docx4j api with the HTMLEditorKit with the functions menu (style, print, font...) or maybe another predefined menu?
3)Or even better, how can i still use the metaphaseEditorPane with its full of functions menu and at the same time load an existing docx file in its textpane?
Sorry for the long question and any help would count, i would really need and appreciate it.
See Adding custom shapes to JTextpane and saved to new Word document for references to two projects which use docx4j to provide rich text editing of docx within Swing.
I found finally the solution after days of searching and testing, It turns out to be very simple actually.
All i had to do is use a metaphase editor panel, (which has a great toolbar by the way) create a DocxEditorkit and then set the editorkit of the metaphase editor panel's textpane to this DocxEditorKit, although the metamorphose works with HTML text pane, it miraculously worked...
DocxEditorKit DEK=new DocxEditorKit;
MetaphaseEditorPanel MEP=new MetaphaseEditorPanel;
MEP.getHTMLTextPane().setEditorKit(DEK);
//Try and Catch blocks of course to read the file
DEK.read(new FileInputStream(PathToFile), MEP.getHTMLTextPane().getDocument(), 0);
getContentPane.add(MEP, BorderLayout.CENTER);
And that was it... Who thought it would be as simple as this.

Insert text to a generated pdf document

I have a library which generates pdf document with images.
I want to be able to add text after each image. What is the syntax for that? How to insert text into pdf documents?
I have to use the library I have, not another one.
First of all, mkl is correct, have a look at the specification for all of the details. PDF is an exact language, if you make mistakes they will routinely be punished severely once you open the PDF in viewers.
Secondly, when you think about putting text on the page, don't forget that besides the text operators to draw the text on the page, you'll also have to specify the font to use to draw this text. Which will include making sure there is a font resource included in the PDF file if your library doesn't automatically handle all of that for you.
If you want to cut corners (I shiver while writing this) and perhaps don't read the specification as thoroughly, try this.
1) Create a PDF file that looks more or less like what you want.
2) Use a tool such as pdfToolbox from callas (http://www.callassoftware.com/callas/doku.php/en:download) or Browser from Enfocus (http://www.enfocus.com/en/products/browser). Both of these tools allow you to investigate the low-level structure of a PDF file, including looking at the actual page description code. This will show you how fonts are embedded (if you have to do it yourself that could be very handy) and how text is rendered on the page (and how you set the font, size, color etc... to use).

Replace text with an image docx4j

I have an word template. There is an word photo that has to be replaced with an image. This has to be done with Docx4Java.
How do I do this?
If specifically looking to replace a text with an image(which is not possible using docx4j as answered above), you can use replace bookmark with image as an alternative.
Just open your templated word file, position the cursor at desired location and insert->bookmark and name your bookmark.
I followed the instructions here to replace this bookmark with an image
Disclosure: I manage the docx4j project
The VariableReplace code doesn't handle images.
The best way to do this would be to use data bound content controls, specifically a picture content control pointing via XPath at a base-64 encoded image in an XML document (see Getting Started for details).
However, if you want to replace a word with an image, you can do so, but you'll have to write a bit of glue code. It is pretty straightforward.
First, find the word. You can do this using XPath or TraversalUtil (again, see Getting Started for details).
Hopefully it is in a run (w:r/w:t) by itself. If not, you'll need to split the run up so you don't replace adjacent text.
Then, add the image. See the sample ImageAdd.
I suggest you have a look at the XML created when you add an image in Word (ie save and unzip your docx, then look at document.xml). Take care that the XML representing the image is at the correct level (eg child of w:p).

Editing a PDF file with Java

I'm trying to edit a existing PDF file. It is a file where I need to fill in some addresses and other stuff. I want to connect an addressbook to the application so the user can select a user from the addressbook and a part get filled in automatic.
My questions are:
Is it possible to edit a existing PDF file and fill in some fields (+/- 20), because I know there is Itext (http://www.itextpdf.com) but I read that the possiblities are very small.
Or can I better convert the PDF to JPG and get it as background. And create JLabels on the places where I need to fill in the fields. And then print the whole Frame on a A4.
Or are there better posibilities?
So what I need to do, step by step:
Select one of the PDF's (they are in the program)
Fill some fields with content/addresses
Print the PDF/Form with a printer
There is a toolkit given by Adobe named as Acrobat Forms Data Format (FDF) Toolkit which gives API for different languages to fill forms.
You can get the java code at the bottom of that page or check this link
We didn't edit existing PDF's but created totally new letters/reports/doco from our java app using iReport
You could use pdf form and edit field values programmatically using IText or Apache PDFBox (download pdfbox and see SetField.java example)

PDF annotations in Java

Actually, still now I have not got the exact way to add different types of annotation/lines/boxes/highlights using the mouse directly on PDF.
I have tried different API (pdfbox, icepdf, iText, aspose) and successfully able to add text, text annotation, box annotation, images, but for each one i have some issue like:
when i add object, it add in a new page
when i add object it remove the old data
i want use the existing ICEPDF viewer to add text, but it does not allow it. and if i try to mix other API with ICEPDF, then I am not able to get the mouse click position where need to add.
There is only one API (ICE PDF) which allow to add box annotation direct on GUI by mouse but it does allow other things.
If not possible in pdf viewer then is there any class which will allow to do this?
I'm also open for any commercial library/class with a one time fee to do that.
I think BFO have added quite a few annotation tools. Have you looked at their tool?

Categories