kiwiwings

HTML to HSLFTextBox

Oct 23rd, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.63 KB | None | 0 0
  1. package org.apache.poi.hslf.usermodel;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Rectangle;
  5. import java.io.FileOutputStream;
  6. import java.lang.reflect.Field;
  7. import java.util.Enumeration;
  8.  
  9. import javax.swing.JTextPane;
  10. import javax.swing.text.AttributeSet;
  11. import javax.swing.text.Element;
  12. import javax.swing.text.StyledDocument;
  13. import javax.swing.text.html.CSS;
  14. import javax.swing.text.html.HTMLDocument;
  15. import javax.swing.text.html.HTMLEditorKit;
  16.  
  17. import org.junit.Test;
  18.  
  19. public class TestBla {
  20.  
  21.     @Test
  22.     public void htmlToTextbox() throws Exception {
  23.         // adding the empty slide
  24.         HSLFSlideShow ppt = new HSLFSlideShow();
  25.         HSLFSlide s = ppt.createSlide();
  26.         HSLFTextBox title = s.addTitle();
  27.         title.setText(" "); // doesn't work if an empty string is used
  28.         HSLFTextRun rt = title.getTextParagraphs().get(0).getTextRuns().get(0);
  29.         rt.setFontColor(Color.black);
  30.         rt.setFontFamily("Arial");
  31.         rt.setFontSize(44.);
  32.  
  33.         Rectangle anchorInTheSlide = new java.awt.Rectangle(5, 5, 710, 125);
  34.         title.setAnchor(anchorInTheSlide);
  35.  
  36.        
  37.         // prepare
  38.         JTextPane pad = new JTextPane();
  39.         pad.setContentType("text/html");
  40.         HTMLEditorKit kit = (HTMLEditorKit)pad.getEditorKit();
  41.         HTMLDocument htmldoc = (HTMLDocument)kit.createDefaultDocument();
  42.         kit.insertHTML(htmldoc, htmldoc.getLength(), "<p>paragraph <i>1</i></p>", 0, 0, null);
  43.         kit.insertHTML(htmldoc, htmldoc.getLength(), "<p>paragraph <span style=\"color:red\">2</span></p>", 0, 0, null);
  44.         pad.setDocument(htmldoc);
  45.  
  46.  
  47.         // convert
  48.         StyledDocument doc = pad.getStyledDocument();
  49.        
  50.         int lastPos=-1;
  51.         while (lastPos < doc.getLength()) {
  52.             Element line = doc.getParagraphElement(lastPos+1);
  53.             lastPos = line.getEndOffset();
  54.            
  55.             boolean newParagraph = true;
  56.             for (int elIdx=0; elIdx < line.getElementCount(); elIdx++) {
  57.                 final Element frag = line.getElement(elIdx);
  58.                 String subtext = doc.getText(frag.getStartOffset(), frag.getEndOffset()-frag.getStartOffset());
  59.                
  60.                 HSLFTextRun run = title.appendText(subtext, newParagraph);
  61.                 newParagraph = false;
  62.                
  63.                 final AttributeSet as = frag.getAttributes();
  64.                 final Enumeration<?> ae = as.getAttributeNames();
  65.  
  66.                 while (ae.hasMoreElements()) {
  67.                     final Object attrib = ae.nextElement();
  68.  
  69.                     if (CSS.Attribute.COLOR.equals(attrib)) {
  70.                         // I don't know how to really work with the CSS-swing class ...
  71.                         Field f = as.getAttribute(attrib).getClass().getDeclaredField("c");
  72.                         f.setAccessible(true);
  73.                         Color c = (Color)f.get(as.getAttribute(attrib));
  74.                         run.setFontColor(c);
  75.                     } else if (CSS.Attribute.FONT_STYLE.equals(attrib)) {
  76.                         if ("italic".equals(as.getAttribute(attrib).toString())) {
  77.                             run.setItalic(true);
  78.                         }
  79.                     } else if (CSS.Attribute.FONT_WEIGHT.equals(attrib)) {
  80.                         if ("bold".equals(as.getAttribute(attrib).toString())) {
  81.                             run.setBold(true);
  82.                         }
  83.                     }
  84.                 }                              
  85.             }
  86.         }
  87.        
  88.         FileOutputStream fos = new FileOutputStream("bla.ppt");
  89.         ppt.write(fos);
  90.         fos.close();
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment