itext chunk background color for rtf - java

I used itext to create an rtf file. I want add a background color to the text with the following code:
public static void main(String[]args) throws IOException{
Document document = new Document();
try {
RtfWriter2 rtf = RtfWriter2.getInstance(document, new FileOutputStream("text.rtf"));
document.open();
Chunk chunk = new Chunk("This is a text");
chunk.setBackground(new Color(0xFF,0x00,0x00),0f,0f,0f,0f);
Phrase fa = new Phrase();
fa.add(chunk);
Paragraph p = new Paragraph();
p.add(fa);
document.add(p);
document.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
When I open it in Mac, I cannot see the background color shown. But when I try to use Word to open it, the background color is shown. Thus,
Is it the right way to highlight a text block in rtf?
When I use JEditorPane to open it, the background color still cannot show.

Related

Java Swing JEditorPane insert bold tags around selection

I'm using Java Swing and I have a JEditorPane with content type "text/html"
Currently the only content I have in the editor pane is:
<html><p>This is a short sentence</p></html>
What I want is the user to be able to select any part of that sentence with their mouse. When they hit a button (boldButton_), it needs to bold the text inside their selection.
I'm not sure how to accomplish this. This is the code I have so far, which is the event handler on the bold button. The best I can do is insert some text wrapped in bold at the start of the paragraph that has selected text, but what i want is to insert bold tags around the selected text only, so the effect is to bold the selection like you would in any word processor.
Thanks in advance.
JEditorPane editArea_;
public void actionPerformed(ActionEvent e) {
// bold button
if (e.getSource() == boldButton_) {
HTMLDocument doc = (HTMLDocument) editArea_.getDocument();
HTMLEditorKit ekit = (HTMLEditorKit) editArea_.getEditorKit();
int selectStart = editArea_.getSelectionStart();
int selectEnd = editArea_.getSelectionEnd();
Element startElem = doc.getParagraphElement(selectStart);
Element endElem = doc.getParagraphElement(selectEnd);
// for now only bold if some text has been selected with the mouse
if (selectStart != selectEnd) {
// 1. text is selected
// toggle bold tags around the text
try {
doc.insertAfterStart(startElem, "<b>WTF</b>");
} catch (BadLocationException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
editArea_.requestFocusInWindow();
}
}
i'm not sure, but you can try this code.
// toggle bold tags around the text
String txt = doc.getSelectedText();
if (selectStart != selectEnd) {
try {
// doc.insertAfterStart(startElem, "<b>WTF</b>");
doc.remove(selectStart, selectEnd-selectStart);
ekit.insertHTML((HTMLDocument) doc.getDocument(), selectStart, "<b>"+txt+"</b>", 0, 0, HTML.Tag.B);
} catch (BadLocationException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}

Android pdf maker code error

I'm trying to take a screenshot of my Android activity (bitmap) and then making a PDF file out of it. I used the itextpdf library. Here's what I have:
public void onSaveDataClicked(View reportsLayout){
//take screen shot
Bitmap screen; View v1 = reportsLayout.getRootView();
v1.setDrawingCacheEnabled(true);
screen = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
try {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(file));
document.open();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
screen.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
addImage(document,byteArray);
document.close();
}
catch (Exception e){
e.printStackTrace();
}
}
private static void addImage(Document document,byte[] byteArray) {
Image image = null;
try {
image = Image.getInstance(byteArray);
} catch (BadElementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// image.scaleAbsolute(150f, 150f);
try {
document.add(image);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
When I try to run I get the errors: "Image cannot be converted to Element" and "cannot find symbol method getInstance(byte[])"
A lot of the code I found online through a tutorial on how to achieve this. I'm kind of unfamiliar with this sort of thing. Any help and ideas would be appreciated.
Use import com.itextpdf.text.Image;and remove the default Android one.

Java: show GIF image doesn't work

I'm trying to show a GIF image inside a JLabel using the following code
Image waitImage = null;
JLabel l1;
try {
waitImage = ImageIO.read(IndexesPanel.class.getResource("/images/error.gif"));
l1 = new JLabel(new ImageIcon(waitImage));
l1.setBounds(20, 20, 100, 100);
waitPanel.add(l1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The image is shown, but it's not animated. I'm using the following Gif:
Any idea?
ImageIO returns BufferedImage
Use new ImageIcon(new URL("path to resource"));
Guess you can use new ImageIcon(IndexesPanel.class.getResource("/images/error.gif"));

How to add Image as header in JEditorPane

I want to print Header with some text and a specific logo and footer with some text and page no.
How to add image on header?
public class JEditorPaneTest {
public static void main(String args[]) {
JEditorPane pane = new JEditorPane();
JScrollPane js = new JScrollPane(pane);
try {
URL url = new URL("file:C:/temp/html/12.html");
// File f=new File("C:/temp/html/12.html");
pane.setPage(url);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pane.setContentType("text/html");
JFrame frmae = new JFrame();
frmae.setSize(200, 300);
try {
MessageFormat header = new MessageFormat("Order Details History");
MessageFormat footer = new MessageFormat(" Page #{0,number,integer}");
pane.print(header, footer);
} catch (PrinterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// frmae.add(pane);
frmae.add(js);
frmae.setVisible(true);
}
}
http://java-sl.com/JEditorPanePrinter.html this one is editor kit independent printing
http://java-sl.com/Pagination_In_JEditorPane_HF.html this one if you need WYSIWYG editor.
Or you can jus override paintComponent() method and after calling super draw your images over the content.

Scrollbar in JEditorPane in a Rectangle

I have a JEditorPane that is inside a rectangle in a window so that it doesnt fill the whole window. However when the HTML page gets too big, it cuts it off instead of putting in a scrollbar, I know I need a JScrollpane or something, but I dont want the scrollbar on the whole window, only inside the rectangle.
Here is a snippet of my code (rweb is already defined as a Rectangle):
JEditorPane web = new JEditorPane();
web.setEditable(false);
try {
web.setPage("http://www.google.com");
}catch (IOException e) {
web.setContentType("text/html");
web.setText("<html>Could not load webpage</html>");
}
web.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
if(Desktop.isDesktopSupported()) {
try {
Desktop.getDesktop().browse(e.getURL().toURI());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (URISyntaxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
});
rweb = new Rectangle(0, 0, width, 600);
web.setBorder(null);
web.setBounds(rweb);
window.add(web);
You never add the JEditorPane to a JScrollPane. Text components don't have scroll support by themselves.
Try using window.add(new JScrollPane(web)); instead
Take a look at How to use scroll panes for more details
ps- web.setBounds(rweb); will only work if you are using a null layout and if you are, you'd be better of using a EmptyBorder or a layout manager that provided the ability to specify insets, like GridBagLayout

Categories