0

i am passing html code to a variable in java. using aspose library, the html code should be executed and rendered into ppt (i am also giving the reference to css in the html). appreciated if the ppt is editable.

3 Answers3

1

Please use the following java equivalent code on your end.

public static void main(String[] args) throws Exception {

    // The path to the documents directory.
    String dataDir ="C:\\html\\";

    // Create Empty presentation instance
    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(dataDir + "file.html");
    Reader reader = new InputStreamReader(inputStream);

    int data = reader.read();
    String content = ReadFile(dataDir + "file.html");

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

    // Saving Presentation
    pres.save(dataDir + "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();

}
Mudassir
  • 433
  • 2
  • 7
1

@Balchandar Reddy,

I have observed your comments and like to share that ImportingHTMLTextInParagraphs.class points to path of file. I have updated the code relate to this.

Secondly, you need to call import com.aspose.slides.IAutoShape on your end to resolve the issue.

Mudassir
  • 433
  • 2
  • 7
  • i couldn't quite render the html as intended..it is rendering but couldn't load most of the styles. (for e.g. it could load the style color:red; but not the style background-color:red;), any suggestions? – Balchandar Reddy Sep 22 '17 at 06:09
  • 1
    I have shared response for your afore mentioned request in separate thread initiated by you. https://stackoverflow.com/questions/46358541/unable-to-embed-styling-in-aspose-ppt-which-is-rendered-from-html-file/46361091#46361091 – Mudassir Sep 22 '17 at 09:16
0

I have observed your requirements and regret to share that Aspose.Slides which is an API for managing PowerPoint slides, does not support feature for converting HTML to PPT/PPTX. However, it supports importing HTML text inside slide text frames that you may use.

// Create Empty presentation instance// Create Empty presentation instance
using (Presentation pres = new Presentation())
{
    // Acesss the default first slide of presentation
    ISlide slide = pres.Slides[0];

    // Adding the AutoShape to accomodate the HTML content
    IAutoShape ashape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 10, pres.SlideSize.Size.Width - 20, pres.SlideSize.Size.Height - 10);

    ashape.FillFormat.FillType = FillType.NoFill;

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

    // Clearing all paragraphs in added text frame
    ashape.TextFrame.Paragraphs.Clear();

    // Loading the HTML file using stream reader
    TextReader tr = new StreamReader(dataDir + "file.html");

    // Adding text from HTML stream reader in text frame
    ashape.TextFrame.Paragraphs.AddFromHtml(tr.ReadToEnd());

    // Saving Presentation
    pres.Save("output_out.pptx", Aspose.Slides.Export.SaveFormat.Pptx);
}

I am working as Support developer/ Evangelist at Aspose.

Mudassir
  • 433
  • 2
  • 7
  • Please use the following java equivalent code on your end. – Mudassir Sep 17 '17 at 07:06
  • @Balchandar Reddy, I have observed your comments and like to share that ImportingHTMLTextInParagraphs.class points to path of file. I have updated the code relate to this. Secondly, you need to call import com.aspose.slides.IAutoShape on your end to resolve the issue. – Mudassir Sep 20 '17 at 10:49