ในหัวข้อง่ายๆ นี้ เราจะแนะนำ วิธีสร้าง PowerPoint Presentation โดยใช้ Java ในระบบปฏิบัติการ MS Windows, macOS หรือ Ubuntu หัวข้อนี้ครอบคลุมขั้นตอนโดยละเอียดในการตั้งค่าสภาพแวดล้อม และสามารถสร้างโดยใช้โค้ดง่าย ๆ สองสามบรรทัดในงานนำเสนอ Java PPT
ขั้นตอนในการสร้างงานนำเสนอ PowerPoint ใน Java
- ดาวน์โหลดและติดตั้ง Aspose.Slides for Java จากที่เก็บ Maven
- สร้างอินสแตนซ์วัตถุคลาส Presentation เพื่อสร้างงานนำเสนอที่ว่างเปล่า
- สร้างสไลด์เปล่าและเพิ่มลงในคอลเลกชันสไลด์งานนำเสนอ
- ใช้วิธี AddAutoShape แทรกรูปทรงสี่เหลี่ยมผืนผ้าในสไลด์ที่สร้างขึ้นใหม่
- แทรกกรอบข้อความโดยใช้วิธี addTextFrame และตั้งค่าคุณสมบัติที่เกี่ยวข้องกับข้อความ
- บันทึกงานนำเสนอบนดิสก์ในรูปแบบ PPTX
ขั้นตอนข้างต้นใน Java สร้างไฟล์ PPTX บนดิสก์โดยใช้อินเทอร์เฟซ API แบบธรรมดาและไม่ต้องพึ่งพา PowerPoint ประการแรก งานนำเสนอเปล่าจะถูกสร้างขึ้นโดยใช้อินสแตนซ์ของคลาสงานนำเสนอ ซึ่งตามด้วยการเพิ่มสไลด์เปล่าภายในงานนำเสนอ จากนั้น กรอบข้อความจะถูกเพิ่มเข้าไปภายในรูปร่าง และคุณสมบัติข้อความที่เกี่ยวข้องจะถูกตั้งค่าก่อนที่จะบันทึกงานนำเสนอบนดิสก์โดยใช้วิธีการบันทึก
รหัสเพื่อสร้างงานนำเสนอ PowerPoint โดยใช้ Java
import com.aspose.slides.FillType; | |
import com.aspose.slides.IAutoShape; | |
import com.aspose.slides.IPortionFormat; | |
import com.aspose.slides.ISlide; | |
import com.aspose.slides.ITextFrame; | |
import com.aspose.slides.License; | |
import com.aspose.slides.NullableBool; | |
import com.aspose.slides.Presentation; | |
import com.aspose.slides.SaveFormat; | |
import com.aspose.slides.ShapeType; | |
import com.aspose.slides.SlideLayoutType; | |
import java.awt.Color; | |
public class CreatePresentation { | |
public static void main(String[] args){ | |
// Setting the linence for the product | |
License SlidesLicense = new License(); | |
SlidesLicense.setLicense("Aspose.Total.lic"); | |
// Create an empty presentation using the Presentation class instance | |
Presentation presentation = new Presentation(); | |
// Insert a Blank slide inside the presentation | |
ISlide slide = presentation.getSlides().addEmptySlide(presentation.getLayoutSlides() | |
.getByType(SlideLayoutType.Blank)); | |
// Add an auto-shape of Rectangle type | |
IAutoShape autoShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 150, 300, 0); | |
// Fill the shape with Green color | |
autoShape.getFillFormat().setFillType(FillType.Solid); | |
autoShape.getFillFormat().getSolidFillColor().setColor(Color.GREEN); | |
// Adding some text inside the shape | |
ITextFrame textFrame = autoShape.addTextFrame("This is Aspose.Slides"); | |
// Set text related properties | |
IPortionFormat portionFormat = textFrame.getParagraphs().get_Item(0) | |
.getPortions().get_Item(0).getPortionFormat(); | |
portionFormat.getFillFormat().setFillType(FillType.Solid); | |
portionFormat.getFillFormat().getSolidFillColor().setColor(Color.RED); | |
portionFormat.setFontBold(NullableBool.True); | |
portionFormat.setFontItalic(NullableBool.True); | |
portionFormat.setFontHeight(14); | |
// Save the generated presentation on the disk | |
presentation.save("NewJavaPresentation.pptx", SaveFormat.Pptx); | |
} | |
} |
ใน งานนำเสนอ Java สามารถสร้างได้โดยใช้โค้ดสองสามบรรทัดตามตัวอย่างข้างต้น คุณยังสามารถบันทึกงานนำเสนอในรูปแบบอื่นๆ เช่น PPT, PPS, PPSX, ODP, POT และ POTX โดยใช้ตัวแจงนับ SaveFormat ข้อความภายในงานนำเสนอสามารถปรับแต่งได้โดยใช้ตัวเลือกต่างๆ ที่เปิดเผยโดยคลาส ParagraphFormat และ PortionFormat ซึ่งรวมถึงการตั้งค่าตัวเลือกต่างๆ เช่น การตัดข้อความ การปรับข้อความให้พอดีอัตโนมัติ การเยื้อง ระยะขอบ สัญลักษณ์แสดงหัวข้อย่อย การเน้นข้อความ และการขีดทับ
ในหัวข้อนี้ เราได้เรียนรู้วิธีสร้าง งานนำเสนอ Java PowerPoint ในรูปแบบต่างๆ หากคุณสนใจที่จะแปลงสไลด์นำเสนอเป็น SVG โปรดไปที่รายละเอียดที่กล่าวถึงในบทความเกี่ยวกับ วิธีแปลง PPTX เป็น SVG โดยใช้ Java