使用java aspose库将html转换为ppt

时间:2017-09-14 10:46:49

标签: java html powerpoint aspose

我将html代码传递给java中的变量。使用aspose库,应该执行html代码并将其渲染为ppt(我也在html中提供对css的引用)。 感谢ppt是否可编辑。

3 个答案:

答案 0 :(得分:1)

请使用以下java等效代码。

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();

}

答案 1 :(得分:1)

@Balchandar Reddy,

我观察了您的评论,并希望分享 ImportingHTMLTextInParagraphs.class 指向文件路径。我已经更新了与此相关的代码。

其次,您需要在结束时调用 import com.aspose.slides.IAutoShape 来解决问题。

答案 2 :(得分:0)

我已经观察到您的要求并且遗憾地分享Aspose.Slides这是一个用于管理PowerPoint幻灯片的API,不支持将HTML转换为PPT / PPTX的功能。但是,它支持在您可能使用的幻灯片文本框架中导入HTML文本。

// 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);
}

我是Aspose的支持开发人员/传播者。