2017-09-14 136 views
1

我正在將html代碼傳遞給java中的變量。使用aspose庫,應該執行html代碼並將其渲染爲ppt(我也在html中引用了對css的引用)。如果ppt是可編輯的,則可以使用 。使用java aspose庫將html轉換爲ppt

+0

你有試過任何東西嗎? –

+0

無法找到aspose的任何工作代碼 –

回答

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擔任Support developer/Evangelist。

+0

我不認爲這是一個java代碼.. –

+0

請在您的末端使用以下java等效代碼。 – Mudassir

+0

@Balchandar Reddy, 我觀察了您的意見並希望分享ImportingHTMLTextInParagraphs.class指向文件的路徑。我已經更新了與此相關的代碼。 其次,您需要在您的最後調用import com.aspose.slides.IAutoShape來解決問題。 – Mudassir

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

} 
+0

我試過了,但IAutoShape沒有通過aspose庫解決。 –

+0

和什麼abt的類ImportingHTMLTextInParagraphs.class加載? –

1

@Balchandar雷迪,

我觀察到您的意見和喜歡分享ImportingHTMLTextInParagraphs.class指向文件的路徑。我已經更新了與此相關的代碼。其次,您需要致電導入com.aspose.slides.IAutoShape以解決問題。

+0

明白了..謝謝你..! –

+0

我不能完全呈現HTML的意圖..它呈現,但無法加載大部分的樣式。 (例如,它可以加載樣式顏色:紅色;但不是樣式背景顏色:紅色;),有什麼建議? –

+1

我在您發起的獨立線索中分享了您對上述請求的回覆。 https://stackoverflow.com/questions/46358541/unable-to-embed-styling-in-aspose-ppt-which-is-rendered-from-html-file/46361091#46361091 – Mudassir