Problem With HTML display in JEditorPane java - java

Im trying to display html in a JEditorPane. Initially the type is set to "text/html".
When I use setPage(URL) it works fine and the resulting output is displayed but If I have a String that contains HTML code and I used setText(String) to display the result on the JEditorPane nothing is displayed I see only white space.
Of-course if I copied the whats in the string pasted it in notpad, saved it as .html then opened the resulting file in the browser it displayed correctly. The real problem is in how or what the JEditorPane does with the string inorder to display whats inside it. The JEditorPane is inside a JscrollPane which is inside a Jframe. and I only used setContentType( "text/html" ) and setText(String) methods for html display.
Is there anyway to get around this than wrting the resulting html code to a file and using SetPage(URL)? I can post the html code if you need it (but its quite large). Thanks for your help.

Don't know why setText does not work. But here is a workaround.
Try this URL. (the whole file in the URL) (This is what Android's WebView calls when you setText in it)
data:text/html;charset=utf-8,%3C%21DOCTYPE%20html%3E%0D%0A%3Chtml%20lang%3D%22en%22%3E%0D%0A%3Chead%3E%3Ctitle%3EEmbedded%20Window%3C%2Ftitle%3E%3C%2Fhead%3E%0D%0A%3Cbody%3E%3Ch1%3E42%3C%2Fh1%3E%3C%2Fbody%3E%0A%3C%2Fhtml%3E%0A%0D%0A
It starts with data:text/html;charset=utf-8, and is followed by your HTML.
However you do have to encode it.. At least you have to replace % with %25 The rest might just work without encoding though.
You can also use this code to embed images without calling a file
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD///+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4Ug9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC" />
You just have to base64 encode your image and then you can paste it right in.

Related

How to save HTML code in Sql Server database for correct display?

My website is just like Stack Overflow and under development. I am using plain textarea to take text input as I do not have any WMD editor like Stack Overflow's.
When I take HTML code as input and store it in database table in a text or nvarchar(max) column, it is stored successfully. But when I call that data for display, it displays the corresponding HTML page instead of that HTML code on screen. I am not able to resolve it. For better understanding I'm putting here input page and output page images of my website.
This is image of input page:
This is the image of output page:
What is going wrong here ?
One easy way is to replace
< with < and > with >
in the HTML string which you retrived and then display it on page.
Have you tried that ?
You need to escape the HTML so it's not interpreted by the browser. How to do that depends on the view technology you're using.
With JSP and JSTL the escaping is automatically done with <c:out value="${myString}"/>. If you're not using JSTL yet, now's the time to start (there's a lot of other helpful things in there too).
you can save the html codes just like text. You can use varchar(max) type column to save the html code in table. Display the code is depending the browser. But if you use nvarchar type that will cause problems in display.
Another possible solution is to replace the html tags before storing in database. What I did is :-
text=text.replaceAll("<", "<");
text=text.replaceAll(">", ">");
and then stored text in database and its working. Thanks to Bibin Mathew.

Extract String from html page which is in Assets folder?

I have couple of html pages in my assets folder, i am able to open them and get them in a string. My problem lies ahead of it, I just to extract text between certain tags. For example if i am having a line in my html page as <h3>Hello have a nice day</h3> inside h3 tag.
I just want to get "Hello have a nice day". Till now i tried it to string functions but no success. How can i achieve this?
UPDATE
I got the solution from link
Use Html.fromHtml(), pass the html source and it will return only the text..
check http://developer.android.com/reference/android/text/Html.html
If you are able to read html files, then everything should be easy. If it's simple html page you can use xpath to parse it and retrieve whatever you want, or you can use some libaries such as jsoup to parse the html.

Displaying xml in textarea without rendering htmlentities

I've been reading about this for a while now and can't find the solution.
This looks like the solution I need:
How to stop html textarea from interpreting html entities into their characters
But when I do this I just get in the textarea. What gives?
This is my first time trying to use jstl. Please help.
use jquery's text() method and assign it to text area.

Convert html code into plain text

I am developing android application in which i want to convert html code in to plain text and display it in the editText. I use Jsoup.jar for that but it shrink the data. Like it just remove the tag.I will not got to the next line. Does any one has the solution to display html code in the plain text? Any help or suggestions are accepted. Thank you.
Use Html.fromHtml(htmltext).

Itext PDF manipulation in java

I have a requirement to convert a PDF from HTML using itext lib in java.
My input HTML page has a tabular structure and it has separate header and footer. I've converted this page to PDF , however as my HTML page has a footer associated with it, I'm trying to relocate footer of my generated PDF to location where actual data of a particular page ends(i.e if a page contains only 10 lines of data from header then I'm trying to add footer after 10th line). PDFEventHelper class helped me to add header and footer,but I'm getting struck with this relocation requirement.
Can anybody tell me how to deal with such scenarios??
does anyone knows how to read blank(unwritten) sectors on PDF page ??
Thanks in advance.
The Document passed into your onEndPage override is actually a PdfDocument.
Once you've cast the parameter to a PdfDocument, you can call PdfDocument.getVerticalPosition(true) to determine where the last Element was laid out and draw your footer accordingly. Note that this only gives the Y position, but that's all you really need for a footer.
The parameter passed to getVerticalPosition is called "ensureNewLine". I strongly suspect what you pass won't matter because this is the End Page Event, and any trailing lines will have already been written/finished/closed/whatever-its-called.

Categories

Resources