Niket Thada
Niket Thada

Reputation: 103

Can we embed HTML code in ppt through Java using aspose or POI Api?

I want to embed HTML code in ppt like if I am having HTML code as Name So in ppt this should be displayed as Name. Because in Database I would be inserting some data which would be having html code. I tried many ways but I could not find any suitable way to match my need. So any suggestions on how to proceed.

Upvotes: 0

Views: 530

Answers (1)

Mudassir
Mudassir

Reputation: 433

@Niket Thada,

I have observed your requirements and like to share that Aspose.Slides does allow importing HTML text related tags in presentation slides text frames. Aspose.Slides currently support text related tags in presentation. I suggest you to please try using following sample code on your end.

    Presentation pres = new Presentation();

    // Access the default first slide of presentation
    ISlide slide = pres.getSlides().get_Item(0);

    // Adding the AutoShape to accommodate the HTML content
    IAutoShape ashape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 10, 10,
                    (float) pres.getSlideSize().getSize().getWidth(), (float) pres.getSlideSize().getSize().getHeight());

    ashape.getFillFormat().setFillType(FillType.NoFill);

    // Adding text frame to the shape
    ashape.addTextFrame("");

    // Clearing all paragraphs in added text frame
    ashape.getTextFrame().getParagraphs().clear();

    // Loading the HTML file using InputStream
    InputStream inputStream = new FileInputStream("html from pp2010 clipboard.html");
    Reader reader = new InputStreamReader(inputStream);

    int data = reader.read();
    String content = ReadFile("html from pp2010 clipboard.html");

    // Adding text from HTML stream reader in text frame
    ashape.getTextFrame().getParagraphs().addFromHtml(content);

    // Saving Presentation
    pres.save("output.pptx", SaveFormat.Pptx);

    public static String ReadFile(String FileName) throws Exception {

            File file = new File(FileName);
            StringBuilder contents = new StringBuilder();
            BufferedReader reader = null;

            try {
                    reader = new BufferedReader(new FileReader(file));
                    String text = null;

                    // repeat until all lines is read
                    while ((text = reader.readLine()) != null) {
                            contents.append(text).append(System.getProperty("line.separator"));
                    }
            } catch (IOException e) {
                    e.printStackTrace();
            } finally {
                    try {
                            if (reader != null) {
                                    reader.close();
                            }
                    } catch (IOException e) {
                            e.printStackTrace();
                            return null;
                    }
            }

            return contents.toString();

    }

I am working as Support developer/ Evangelist at Aspose.

Upvotes: 1

Related Questions