Printing RTF-Text using JTextPane
G'day
Does someone know how to print the drawed content of a JTextPane/JEditorPane?
I derived a class from JTextPane and implemented Printable:
public void print(Graphics g)
paintComponent(g);
public void paintComponent(Graphics g)
super.paintComponent(g);
}Seems a little ugly, uhm?! It does not work :(
I get the following errors:
java.lang.NullPointerException
at java.util.Hashtable.get(Hashtable.java:336)
at java.awt.Component.getFontMetrics(Component.java:2503)
at javax.swing.JComponent.getFontMetrics(JComponent.java:1589)
at com.sun.java.swing.SwingUtilities2.getFRC(SwingUtilities2.java:368)
at com.sun.java.swing.SwingUtilities2.drawChars(SwingUtilities2.java:576)
at javax.swing.text.Utilities.drawTabbedText(Utilities.java:159)
at javax.swing.text.GlyphPainter1.paint(GlyphPainter1.java:102)
at javax.swing.text.GlyphView.paintTextUsingColor(GlyphView.java:472)
at javax.swing.text.GlyphView.paint(GlyphView.java:463)
at javax.swing.text.BoxView.paintChild(BoxView.java:144)
at javax.swing.text.BoxView.paint(BoxView.java:407)
at javax.swing.text.BoxView.paintChild(BoxView.java:144)
at javax.swing.text.BoxView.paint(BoxView.java:407)
at javax.swing.text.ParagraphView.paint(ParagraphView.java:584)
at javax.swing.text.BoxView.paintChild(BoxView.java:144)
at javax.swing.text.BoxView.paint(BoxView.java:407)
at javax.swing.plaf.basic.BasicTextUI$RootView.paint(BasicTextUI.java:1338)
at javax.swing.plaf.basic.BasicTextUI.paintSafely(BasicTextUI.java:643)
at javax.swing.plaf.basic.BasicTextUI.paint(BasicTextUI.java:781)
at javax.swing.plaf.basic.BasicTextUI.update(BasicTextUI.java:760)
at javax.swing.JComponent.paintComponent(JComponent.java:743)
at fd.gui.Component.paintComponent(Component.java:149)Does that make any sense?
Btw: I used Sun JRE 1.5 (Ubuntu)
Cahadras
Edited by: Cahadras on Oct 23, 2007 1:50 PM
Cahadras wrote:
....It must be an internal bug of the Sun VM that causes this NullPointerException when the JTextPane class passes Font instances to JComponent.getFontMetrics().You may be right, but I'm going to say "careful there partner" to your statement claiming that the Sun VM must have a bug. Please see this excellent web site titled "How to Ask Questions the Smart Way" and is found here:
http://www.catb.org/~esr/faqs/smart-questions.html
Here's a subsection of that page titled:
Don't Claim that you've found a bug:
When you are having problems with a piece of software, don't claim you have found a bug unless you are very, very sure of your ground. Hint: unless you can provide a source-code patch that fixes the problem, or a regression test against a previous version that demonstrates incorrect behavior, you are probably not sure enough. This applies to webpages and documentation, too; if you have found a documentation �bug�, you should supply replacement text and which pages it should go on.
Remember, there are many other users that are not experiencing your problem. Otherwise you would have learned about it while reading the documentation and searching the Web (you did do that before complaining, didn't you?). This means that very probably it is you who are doing something wrong, not the software.
The people who wrote the software work very hard to make it work as well as possible. If you claim you have found a bug, you'll be impugning their competence, which may offend some of them even if you are correct. It's especially undiplomatic to yell �bug� in the Subject line.
When asking your question, it is best to write as though you assume you are doing something wrong, even if you are privately pretty sure you have found an actual bug. If there really is a bug, you will hear about it in the answer. Play it so the maintainers will want to apologize to you if the bug is real, rather than so that you will owe them an apology if you have messed up.
Similar Messages
-
Directly Printing RTF document using java
Hi,
can we directly print RTF document using java, without giving any preview or any other screen in between.
Actually we created inventory system, in which when user create any sale information, it should print the bill for the same with single button click.
Any sample code or help will be highly appreciated
Thanks in advanceHi,
can we directly print RTF document using java, without giving any preview or any other screen in between.
Actually we created inventory system, in which when user create any sale information, it should print the bill for the same with single button click.
Any sample code or help will be highly appreciated
Thanks in advance -
Problems with printing the text in JTextPane
Hello
I need to print the text in a text pane. It prints the first page of the text pane and it doesn't print the rest of the text. I don't know what the problem is.
The code I am using, is below. I
Thank you.
LilyLay
import java.awt.*;
import javax.swing.*;
import java.awt.print.*;
public class PrintUtilities implements Printable {
private Component componentToBePrinted;
public static void printComponent(Component c) {
new PrintUtilities(c).print();
public PrintUtilities(Component componentToBePrinted) {
this.componentToBePrinted = componentToBePrinted;
public void print() {
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(this);
if (printJob.printDialog())
try {
printJob.print();
} catch(PrinterException pe) {
System.out.println("Error printing: " + pe);
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
if (pageIndex > 0) {
return(NO_SUCH_PAGE);
} else {
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
disableDoubleBuffering(componentToBePrinted);
componentToBePrinted.paint(g2d);
enableDoubleBuffering(componentToBePrinted);
return(PAGE_EXISTS);
public static void disableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(false);
public static void enableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(true);public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
if (pageIndex > 0) {
return(NO_SUCH_PAGE);
} else {if (pageIndex > 0) return (NO_SUCH_PAGE)
prevents printing any more than the first page.
change >0 to >= numPages
Howard Amos -
Printing the text in JTextPane
Hello
I need to print the text in a text pane. It prints the first page of the text pane and it doesn't print the rest of the text. I don't know what the problem is.
The code I am using, is below. I
Thank you.
LilyLay
import java.awt.*;
import javax.swing.*;
import java.awt.print.*;
public class PrintUtilities implements Printable {
private Component componentToBePrinted;
public static void printComponent(Component c) {
new PrintUtilities(c).print();
public PrintUtilities(Component componentToBePrinted) {
this.componentToBePrinted = componentToBePrinted;
public void print() {
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(this);
if (printJob.printDialog())
try {
printJob.print();
} catch(PrinterException pe) {
System.out.println("Error printing: " + pe);
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
if (pageIndex > 0) {
return(NO_SUCH_PAGE);
} else {
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
disableDoubleBuffering(componentToBePrinted);
componentToBePrinted.paint(g2d);
enableDoubleBuffering(componentToBePrinted);
return(PAGE_EXISTS);
public static void disableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(false);
public static void enableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(true);Could it be that in your print(Graphics g, ...) method you are saying if pageIndex is greater than 0 return. If you are using 0 based page numbers, that would print page 0 (the first page) and none after that.
-
Using Read_text want to print continuous text
Hi,
My requirement is to print continuous text using read_text.
Here we are not supposed to use include text to print continuosly.
For ex:The text in script is like as below
*Firstline
Secondline
Reqirement is to ptint as Firstline Secondline by using read_text only.
Please help.
Thanks & Regards,
Shirisha KandulaHi Hari,
If the text is as below the concatenate will not work because for the newline also it will print in the sameline.
Ex:
Firstline
Secondline
*Thirdline
O/P if we concatenate is: Firstline Secondline Thirdline
But my requirement is to print like
Firstline Secondline
Thirdline
Hope u got my point.
Thanks,
Shirisha -
Printing standard text with TAB
Hi Folks,
I am very new to SAP Scripts and need some help here. I am printing some text using Standard Text object (using INCLUDE statement in sapscript). But now I want to print the text after a TAB. I tried with below syntax but no luck.
/: ,,INCLUDE ZTEXT OBJECT TEXT ID ST
Request you to please suggest some approach.
Thank you.
Regards,
SudThanks Christian !
Yes, m using SO10 for creating this text.
But as u suggested, I cannot use tab in ZTEXT, because this will affect the alignment for all the other scripts where I am using this ZTEXT.
For every other script, I do not need this extra tab before the ZTEXT. Only for this one script I want to intorduce a Tab before printing this ZTEXT.
Any other approach?
Thanks
Regards,
Sud -
Printing Arabic text in Oracle Application(R12i) using XML Publisher.
Hi,
I am using Oracle Application R12i version with Oracle database as 10g, base language as English and installed language as Arabic.
I am trying to print the data which is there in English/Arabic in Oracle Application(using XML Publisher).
I've created a rdf using Report Builder 10g and registered as concurrent program (XML as Output format). There after i executed the concurrent program and generated the XML file.
Based on the XML file, i created a rtf file using XML Desktop(in Ms word) and register the same rtf file in XML Publisher Administrator for creating the XML definition and Template and Output as PDF.
After creating a XML template, when i executed the Concurrent program, english text is shown in english but arabic text is shown some ascii characters like Question Mark(up side down).
Could anyone please let us know the steps for doing the same or plz let us know are we missing any step.
Quiet urgent.
You could also reply to [email protected]
Thanks in advance
Your help will be appreciated.
Edited by: user10772035 on Aug 20, 2009 10:23 PMSame Problem here, if you are able to solve it, please also help me.
-
How to print a text file using Java
How can I print a text file using Java without converting the output to an image format. Is there anyway I can send the characters in the text file as it is for a print job? I did get a listing doing this ... but that converted the text to an image format before printing....
THanks,.Hi I had to write a print api from scratch, and I did not convert the output to image. Go and read up on the following code. I know there is a Tutorial on Sun about the differant sections of the snippet.
private void printReport()
Frame tempFrame = new Frame(getName());
PrintJob printerJob = Toolkit.getDefaultToolkit().getPrintJob(tempFrame, "Liesltext", null);
Graphics g = printerJob.getGraphics();
//I wrote the method below for calculations
printBasics(g);
g.dispose();
printerJob.end();
}This alone wont print it you have to do all the calculations in the printBasics method. And as I said I wrote this from scratch and all I did was research first the tutorial and the white papers
Ciao -
How to print the texts retrived by using READ_TEXT fun module in Smartform
Please tell me how to print the text which is rertrived by using the READ_TEXT function module in smartform.
I have coded all things in the program lines and in that i am retriveing the long texts.
I am getting the text lines in my internal table clearly, the thing is that I am not able to pass these lines to the text.
I have to print the trouble ticket. in that the notes log I have to pass.
its urgent. Points will be rewarded for any type of clue. whether it will work or not.There are a few ways to do it. If you need to take all of the text in the text type, in your SF text element choose "Include Text".
Populate the fields with the data that corresponds to the text type. It is similar to the interface to the FM "Read_Text.
Text Name
Text Obje
Text ID
Language
Encase any variables with the "&" symbol.
If you have already coded the call to the FM "READ_TEXT" and loaded the text into an internal table, create a loop and loop through the itab. Inside of the loop create a text element and add a variable in the text element for the field you are looking to output. -
How can I print a text message conversation from my iPhone? Not using screenshots.
How can I print a text message conversation from my iPhone? Not using screenshots. It's around 500 messages so screenshots is not a option.
Yes, you can delete from iphone:
http://manuals.info.apple.com/enUS/iPhone_iOS4_UserGuide.pdf
It is covered in the manual.
Page 172:
"Delete a podcast: In the Podcasts list in iPod, swipe left or right over the podcast, then
tap Delete. " -
How to use Read_text in Smart Forms for printing Header Texts
Dear ALL,
I want to print Header Texts in SmartForms, For that
I am using T/Code VL02N .. and choosed Header ..
got this details... from Text Header.
Text Name :0080000441
Language :EN
Text ID :Z002
Text Object :VBBK.
So in Smart forms Under Template i have created a text and Program Code .
Inside that I have used this below code .
CALL FUNCTION 'READ_TEXT'
EXPORTING
*CLIENT= SY-MANDT
ID = 'Z002'
LANGUAGE = SY-LANGU
NAME = NAME
OBJECT = 'VBBK'
TABLES
LINES = IT_TLINE
EXCEPTIONS
ID = 1
LANGUAGE = 2
NAME = 3
NOT_FOUND = 4
OBJECT = 5
REFERENCE_CHECK = 6
WRONG_ACCESS_TO_ARCHIVE = 7
OTHERS = 8.
IF SY-SUBRC 0.
CLEAR IT_TLINE.
ENDIF.
Kindly suggest me, Where to declare the variables and loops for using this Read_Text Syntax in Smartforms .
Response to this will be highly appreciated........
Regards ,
Vinoth.hi
good
please check this code
CONSTANTS:
*For text reading like in LCNMMFTX / FORM SET_CONTROL_TEXT
text_id like thead-tdid value 'MATK',
text_obj like thead-tdobject value 'AUFK '.
data tlines like tline occurs 0 with header line.
data tdname like thead-tdname.
data tdheader like thead.
if not it_proj-ltxsp is initial.
refresh tlines.
tdname = sy-mandt.
tdname+3 = it_proj-rsnum.
tdname+13 = it_proj-rspos.
tdname+17 = it_proj-rsart.
call function 'READ_TEXT'
exporting
id = text_id
language = it_proj-ltxsp
name = tdname
object = text_obj
importing
header = tdheader
tables
lines = tlines.
exceptions
not_found = 01.
thanks
mrutyun^ -
Will not print text from PDFs - all other print is fine - Using nitro reader - Win7- HP4255
Will not print text from PDFs - all other print is fine - Using nitro reader - Win7- HP4255
Mulga
Welcome to the HP Community Forum.
Have you tried asking your question on the Nitro-Reader Forum?
Nitro Reader Forum
If you would like to try using the Adobe Reader, you might find help here:
Manage Print Output with Print Preview
See the section on PDF files
Click the Kudos Thumbs-Up to show you appreciate the help.
Click Accept as Solution when the Answer provides a Fix or Workaround!
I am pleased to provide assistance on behalf of HP. I do not work for HP.
Kind Regards,
Dragon-Fur -
I am having this problem both in my online banking site and when using Yahoo Mail. When I click on Print on either of these sites and then close the Print dialogue box that automatically comes up, highlight just the text I want printed, then hit Ctrl+P and choose Selection, Print Preview shows me a blank page. When I try printing the selection using either of the 2 printers I have, it prints a blank page. And yet when I do the same thing in Internet Explorer, I have no trouble printing the text I want. I tried doing the prefs.js troubleshoot, deleting everything in that file that starts with print, but it made no difference. I used to be able to print this way in Firefox no problem, but now it doesn't work. Why not, and how do I get it to work again? I'm using Windows XP and Firefox 11.
A bug was introduced in the latest version of Firefox that affects printing selections that are inside a table. Because many pages use tables for general layout (without displaying the borders), this may appear to strike at random.
A fix is expected in Firefox 12. More information: [https://support.mozilla.org/en-US/questions/923294 how to get print selection to work | Firefox Support Forum | Firefox Help]
If you expand your selection to include some content outside the table, then the feature should work, but of course it would include some unnecessary information. -
I have created a little application that gathers data from various text boxes and then combines the data into a formatted richTextBox. A person using the tool asked if I could then add a button that would allow that text (with formatting) to be copied
and pasted into an Outlook Email. When I do this, I can get the text into an email, but I either strip out the formatting or I get all the RTF encoding tags to come along with it.
I have tested to see that the copy of the data from the richTextBox has indeed made it to the clipboard correctly. This has been verified by the fact that I can manually press "ctrl" + "v" and I can past the text with proper
formatting into the mail body.
I do know that I can brute force things by trying to manually wrap HTML tags around my textBox fields and then pipe that into an HTMLBody property. In fact I can get a lot of things to work with the HTMLBody property, but while it is nice that will
work, I feel that I must be missing something really simple while trying to work with RTF.
I currently am pasting (using the Clipboard.GetText(TestDataFormat.RTF)) the RTF data into the Body property of the mail item. This is bringing in all the RTF encoding tags. I have tried to paste this directly into the RTFBody of the mail item,
yet an execption gets thrown when executed. Oddly, though, if I change RTFBody to HTMLBody, I do not get the exception, but I still have the RTF encoding tags in it. Though if I feed the HTMLBody property straight, HTML text with the encoding tags,
it will render properly.
This has me confused then why if I feed HTMLBody HTML text with the encoding tags that it will render, but if I try and do the same for RTFBody and feed it RTF text with encoding tags it still throws an exception. Any help in the matter would be greatly
appreciated. I have included the code snippet below for sending the richTextBox information to the clipboard and then attempting to retrieve it and paste it into an Outlook email.
Thanks for the help.
Some pertinent information:
Blend for Visual Studio 2015 CTP6 (I switched from VS2012 as intellisense was added to Blend in 2015)
Because of this Systems.Windows.Forms is not in use
private void buttonEmail_Click(object sender, RoutedEventArgs e)
//Copy richTextBox information to Clipboard
Clipboard.Clear();
richTextBox.SelectAll();
richTextBox.Copy();
//Get current date to add to email subject line
DateTime myNewDate = new DateTime();
myNewDate = DateTime.Now;
//Create Email
Outlook.Application myNewApplication = new Outlook.Application();
Outlook.MailItem myNewMailIoI = myNewApplication.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
myNewMailIoI.To = " "; //An attempt to ensure that the focus moves down to the body field
myNewMailIoI.Subject = "IoI - " + myNewDate.Date.ToString("d");
myNewMailIoI.BodyFormat = Outlook.OlBodyFormat.olFormatRichText;
//Pasting data into body of email
myNewMailIoI.Body = Clipboard.GetText(TextDataFormat.Rtf); //This will past the text with encoding tags into email
//If this section is uncommented, it will add a properly formatted HTML text to the email
//myNewMailIoI.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
//myNewMailIoI.HTMLBody = "<p>This stinks!!!</p>" + "<p style='color: green; font-size:10pt; font-family:arial'>different font and color</p>";
myNewMailIoI.Display(); //Allow for window to be minimized
myNewMailIoI.Display(true);Ok, I found the solution. Part of the issue was caused by the confusion of the HTML body property acting as a text encoder for HTML text when a string is passed in vs the RTF Body property needing to have a plain text file taken and encoded into a
byte stream. Even though the RTF text is in readable text (albeit with the RTF Encoding Tags) it needs to be reencoded into a series of bytes.
This is where I failed to understand the previous answers. I knew that RTF Body needed to have a array of bytes, and the previous answers seemed to indicate that the data in the clipboard was already in this byte format (this may have just been my
misunderstanding) and that I needed to somehow take this byte array and do something with it. My misunderstanding was that all the methods for getting values from the clipboard returned strings and not bytes.
after some digging last night and with the few hints that were offered before, I came across a post on CodeProject (apparently I cannot post links, so I will try and include this so that you can find the postcodeproject(DOTCOM)/Questions/766917/How-to-convert-richtextbox-output-into-bytearray).
This showed that I needed to take the raw, RTF text with its RTF encoding tags from the clipboard and and encode it to a byte array and then pass this into the RTF Body property. Included below is the final code such that I hope that it helps some other
person as there seem to be a lot of searches for this on the web, but not a lot of direct answers.
using System.Text;
using Outlook = Microsoft.Office.Interop.Outlook;
private void buttonEmail_Click(object sender, RoutedEventArgs e)
//Copy richTextBox information to Clipboard
Clipboard.Clear();
richTextBox.SelectAll();
richTextBox.Copy();
//Get current date to add to email subject line
DateTime myNewDate = new DateTime();
myNewDate = DateTime.Now;
//Create Email
Outlook.Application myNewApplication = new Outlook.Application();
Outlook.MailItem myNewMailIoI = myNewApplication.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
//Add Subject
myNewMailIoI.Subject = "IoI - " + myNewDate.Date.ToString("d");
//Set Body Format to RTF
myNewMailIoI.BodyFormat = Outlook.OlBodyFormat.olFormatRichText;
//Converting RAW RTF data from string to bytes. THIS IS THE FIX THAT WAS NEEDED TO MAKE IT WORK
string myNewText = Clipboard.GetText(TextDataFormat.Rtf);
byte[] myNewRtfBytes = Encoding.UTF8.GetBytes(myNewText); //This line cost me way too many hours of lost sleep!!!
//Inserting RTF bytes into RTFBody property
myNewMailIoI.RTFBody = myNewRtfBytes;
myNewMailIoI.Display(); //Displays the newlycreated email
//If this section is uncommented, it will add a properly formatted HTML text to the email
//myNewMailIoI.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
//myNewMailIoI.HTMLBody = "<p>This works for the HTMLbody property!!!</p>" + "<p style='color: green; font-size:10pt; font-family:arial'>this renders properly</p>"; -
How can i Change the Size of the selected text in JTextPane using ConboBox
plzz help...
How can i Change the Size of the selected text in JTextPane using ConboBox ???
i m using if(cb.getSelectedItem=="small")
cb.setAction(new StyledEditorKit.FontSizeAction("double click", 12);)
if(cb.getSelectedItem=="medium")
cb.setAction(new StyledEditorKit.FontSizeAction("double click", 14);)
if(cb.getSelectedItem=="large")
cb.setAction(new StyledEditorKit.FontSizeAction("double click", 16);)
this code is not working properly according to the action i set on comboBox.
when i select medium the previously set action on comboBox works like small action work.
when i select large the medium action starts .
means its not working in correct time when i select item of combox n action of that item is not working at that time..
plzz plzz help me:(Action action1 = new StyledEditorKit.FontSizeAction(
"double click", 12);
Action action2 = new StyledEditorKit.FontSizeAction(
"double click", 14);
Action action3 = new StyledEditorKit.FontSizeAction(
"double click", 18);
s2 = (String) cb7.getSelectedItem();
if (s2.equals("Small")) {
cb7.setAction(action1);
e1.setSource(cb7);
} else
if (s2.equals("Medium")) {
cb7.setAction(action2);
e1.setSource(cb7);
} else if (s2.equals("Large")) {
cb7.setAction(action3);
// e1.setSource(cb7);
when i chooze any combobox item then according to that item i set the Action on ComboBox but that action is not working properly on the selected text in the JTextPane..means selected text in JText Pane is not changes its Size according to the comboBox selected ITEM.
PLZ plzzzzzzzzzz help me:((.i will be thankfull to u.
Maybe you are looking for
-
FCP 6.0.3 & XDCAM EX-1 - How are xdcam MPEG4 clips processed by FCP
Greetings, I've been using XDCam Ex-1 clips in FCP for a while now.The FCP Sony XDCam import option is seamless, however now I need to send clients the QT .mov files of the clips for them to use on their systems - both FCP and Avid. I thought these w
-
In channel table view Rows Ch name as column
while viewing data as channel table how to view the rows in the column field & the columns in the row field
-
Boot Camp, Partitioning, Windows (but which version?) and FrontPage
Hi there, I have an iMac purchased in August of 2009. I need to install Windows in order to use FrontPage for updating websites. I tried partitioning the iMac using Boot Camp, and I installed WindowsXP, SP2, 32 bit, but it was a copy of an original i
-
Itunes is not recognizing my iphone.
Itunes is not recognizing my iphone. when i plug my iphone in, iphoto pops up and my photos appear but nothing happens on itunes. i have a mac pro.
-
How can I take pictures of my Safari screen?
I am needing to send pictures of my Safari screen to my employer to resolve some other issues. How do I take pictures of the various screens that I need to bring up and then send the pictures off to my employer? Thanks for your input. Ralph