สร้าง PDF โดยใช้ Java ห้องสมุด

ไลบรารี Java อันทรงพลังของเราช่วยให้นักพัฒนาสามารถสร้าง PDF แบบเป็นโปรแกรมได้ภายในไม่กี่ขั้นตอน

Java นักพัฒนาสามารถสร้าง PDF ได้อย่างง่ายดายโดยใช้เฉพาะ API ผลิตภัณฑ์ที่มีประสิทธิภาพของเรา หมายความว่าโซลูชันของเราจะมอบทุกสิ่งที่จำเป็นสำหรับโปรแกรมเมอร์ในการสร้าง PDF ใน Java

ดูข้อมูลโค้ด

สร้าง PDF ใน Java โดยทางโปรแกรม

ด้วยนักพัฒนาไลบรารี Java ของเรา นักพัฒนาสามารถสร้าง PDF ตั้งแต่เริ่มต้นได้อย่างง่ายดาย ในการดำเนินการนี้ นักพัฒนา Java ต้องดำเนินการเพียงไม่กี่ขั้นตอน:

  1. เพิ่มชื่อไฟล์
  2. เริ่มสร้างเอกสาร PDF โดยใช้ Java
  3. บันทึกไฟล์ PDF ที่ส่งออก

เป็นที่น่าสังเกตว่าเอกสารเปล่าควรจะมีหนึ่งย่อหน้าในทางเทคนิค ดังนั้นเมื่อคุณสร้างเอกสาร PDF โดยทางโปรแกรม คุณจะได้โครงสร้างเอกสารพื้นฐานที่แน่นอน

โปรดทราบว่าคุณสามารถเพิ่มเนื้อหาลงในไฟล์ PDF ที่สร้างขึ้นใหม่ได้ทันที ดังนั้น คุณจะได้ไม่เพียงแค่เอกสารเปล่า แต่ได้เอกสารที่มีเนื้อหาที่จำเป็น สำหรับข้อมูลเพิ่มเติมเกี่ยวกับวิธีการแก้ไขเอกสาร โปรดดูที่หน้าการแก้ไข

สร้าง PDF ใน Java

ไลบรารี Java นี้ช่วยให้คุณสร้างเอกสาร PDF โดยทางโปรแกรม ลองใช้ฟังก์ชันอันทรงพลังของเรา และดูวิธีสร้าง PDF ในบางรูปแบบโดยใช้ตัวอย่างต่อไปนี้:

สร้าง PDF ใหม่โดยใช้ Java
เลือกรูปแบบเป้าหมายจากรายการ
รันโค้ด
import com.aspose.words.*;

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

Font font = builder.getFont();
font.setName("Courier New");
font.setColor(Color.BLUE);
font.setSize(36);
font.setHighlightColor(Color.YELLOW);

builder.write("Morbi enim nunc faucibus a.");

doc.save("Output.pdf");
import com.aspose.words.*; Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Run firstRun = new Run(doc, "Proin eros metus, sagittis sed. "); Run secondRun = new Run(doc, "Morbi enim nunc faucibus a."); doc.getFirstSection().getBody().getFirstParagraph().appendChild(firstRun); doc.getFirstSection().getBody().getFirstParagraph().appendChild(secondRun); builder.moveTo(secondRun); builder.startBookmark("Aspose bookmark"); // หาก NextSibling เป็นโมฆะ เป็นไปได้มากว่านี่คือ Run สุดท้ายในย่อหน้า if (secondRun.getNextSibling() != null) builder.moveTo(secondRun.getNextSibling()); else builder.moveTo(secondRun.getParentParagraph()); builder.endBookmark("Aspose bookmark"); doc.save("Output.pdf"); import com.aspose.words.*; Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Run firstRun = new Run(doc, "Proin eros metus, sagittis sed. "); Run secondRun = new Run(doc, "Morbi enim nunc faucibus a."); doc.getFirstSection().getBody().getFirstParagraph().appendChild(firstRun); doc.getFirstSection().getBody().getFirstParagraph().appendChild(secondRun); builder.moveTo(secondRun); builder.startBookmark("Aspose bookmark"); // หาก NextSibling เป็นโมฆะ เป็นไปได้มากว่านี่คือ Run สุดท้ายในย่อหน้า if (secondRun.getNextSibling() != null) builder.moveTo(secondRun.getNextSibling()); else builder.moveTo(secondRun.getParentParagraph()); builder.endBookmark("Aspose bookmark"); PdfSaveOptions saveOptions = new PdfSaveOptions(); saveOptions.getOutlineOptions().getBookmarksOutlineLevels().add("Aspose bookmark", 1); doc.save("Output.pdf", saveOptions);
import com.aspose.words.*;

Document doc = new Document();

Run run = new Run(doc, "Proin eros metus, sagittis sed.");
Paragraph para = doc.getFirstSection().getBody().getFirstParagraph();
para.appendChild(run);

Comment comment = new Comment(doc);
comment.setAuthor("John Doe");
comment.setInitial("JD");
comment.setDateTime(new Date());
comment.setText("Quisque fringilla leo.");

CommentRangeStart commentRangeStart = new CommentRangeStart(doc, comment.getId());
CommentRangeEnd commentRangeEnd = new CommentRangeEnd(doc, comment.getId());

run.getParentNode().insertBefore(commentRangeStart, run);
run.getParentNode().insertAfter(commentRangeEnd, run);
commentRangeEnd.getParentNode().insertAfter(comment, commentRangeEnd);

comment.addReply("Jane Doe", "JD", new Date(), "Pellentesque vel sapien justo.");

doc.save("Output.pdf");
import com.aspose.words.*; Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Run firstRun = new Run(doc, "Proin eros metus, sagittis sed. "); Run secondRun = new Run(doc, "Morbi enim nunc faucibus a."); doc.getFirstSection().getBody().getFirstParagraph().appendChild(firstRun); doc.getFirstSection().getBody().getFirstParagraph().appendChild(secondRun); builder.moveTo(secondRun); builder.startBookmark("Aspose bookmark"); // หาก NextSibling เป็นโมฆะ เป็นไปได้มากว่านี่คือ Run สุดท้ายในย่อหน้า if (secondRun.getNextSibling() != null) builder.moveTo(secondRun.getNextSibling()); else builder.moveTo(secondRun.getParentParagraph()); builder.endBookmark("Aspose bookmark"); doc.save("Output.pdf"); import com.aspose.words.*; Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Run firstRun = new Run(doc, "Proin eros metus, sagittis sed. "); Run secondRun = new Run(doc, "Morbi enim nunc faucibus a."); doc.getFirstSection().getBody().getFirstParagraph().appendChild(firstRun); doc.getFirstSection().getBody().getFirstParagraph().appendChild(secondRun); builder.moveTo(secondRun); builder.startBookmark("Aspose bookmark"); // หาก NextSibling เป็นโมฆะ เป็นไปได้มากว่านี่คือ Run สุดท้ายในย่อหน้า if (secondRun.getNextSibling() != null) builder.moveTo(secondRun.getNextSibling()); else builder.moveTo(secondRun.getParentParagraph()); builder.endBookmark("Aspose bookmark"); PdfSaveOptions saveOptions = new PdfSaveOptions(); saveOptions.getOutlineOptions().getBookmarksOutlineLevels().add("Aspose bookmark", 1); doc.save("Output.pdf", saveOptions);
import com.aspose.words.*;

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

Run firstRun = new Run(doc, "Proin eros metus, sagittis sed. ");
Run secondRun = new Run(doc, "Morbi enim nunc faucibus a.");
doc.getFirstSection().getBody().getFirstParagraph().appendChild(firstRun);
doc.getFirstSection().getBody().getFirstParagraph().appendChild(secondRun);

builder.moveTo(secondRun);
builder.startBookmark("Aspose bookmark");
// หาก NextSibling เป็นโมฆะ เป็นไปได้มากว่านี่คือ Run สุดท้ายในย่อหน้า
if (secondRun.getNextSibling() != null)
    builder.moveTo(secondRun.getNextSibling());
else
    builder.moveTo(secondRun.getParentParagraph());
builder.endBookmark("Aspose bookmark");

PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.getOutlineOptions().getBookmarksOutlineLevels().add("Aspose bookmark", 1);

doc.save("Output.pdf", saveOptions);
import com.aspose.words.*; Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Run firstRun = new Run(doc, "Proin eros metus, sagittis sed. "); Run secondRun = new Run(doc, "Morbi enim nunc faucibus a."); doc.getFirstSection().getBody().getFirstParagraph().appendChild(firstRun); doc.getFirstSection().getBody().getFirstParagraph().appendChild(secondRun); builder.moveTo(secondRun); builder.startBookmark("Aspose bookmark"); // หาก NextSibling เป็นโมฆะ เป็นไปได้มากว่านี่คือ Run สุดท้ายในย่อหน้า if (secondRun.getNextSibling() != null) builder.moveTo(secondRun.getNextSibling()); else builder.moveTo(secondRun.getParentParagraph()); builder.endBookmark("Aspose bookmark"); doc.save("Output.pdf"); import com.aspose.words.*; Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Run firstRun = new Run(doc, "Proin eros metus, sagittis sed. "); Run secondRun = new Run(doc, "Morbi enim nunc faucibus a."); doc.getFirstSection().getBody().getFirstParagraph().appendChild(firstRun); doc.getFirstSection().getBody().getFirstParagraph().appendChild(secondRun); builder.moveTo(secondRun); builder.startBookmark("Aspose bookmark"); // หาก NextSibling เป็นโมฆะ เป็นไปได้มากว่านี่คือ Run สุดท้ายในย่อหน้า if (secondRun.getNextSibling() != null) builder.moveTo(secondRun.getNextSibling()); else builder.moveTo(secondRun.getParentParagraph()); builder.endBookmark("Aspose bookmark"); PdfSaveOptions saveOptions = new PdfSaveOptions(); saveOptions.getOutlineOptions().getBookmarksOutlineLevels().add("Aspose bookmark", 1); doc.save("Output.pdf", saveOptions);
import com.aspose.words.*;

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

Shape shape = builder.insertChart(ChartType.PIE, 432, 252);
Chart chart = shape.getChart();
chart.getTitle().setText("Demo Chart");

chart.getSeries().clear();
chart.getSeries().add("Series 1",
        new String[] { "Category1", "Category2", "Category3" },
        new double[] { 2.7, 3.2, 0.8 });

doc.save("Output.pdf");
import com.aspose.words.*; Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Run firstRun = new Run(doc, "Proin eros metus, sagittis sed. "); Run secondRun = new Run(doc, "Morbi enim nunc faucibus a."); doc.getFirstSection().getBody().getFirstParagraph().appendChild(firstRun); doc.getFirstSection().getBody().getFirstParagraph().appendChild(secondRun); builder.moveTo(secondRun); builder.startBookmark("Aspose bookmark"); // หาก NextSibling เป็นโมฆะ เป็นไปได้มากว่านี่คือ Run สุดท้ายในย่อหน้า if (secondRun.getNextSibling() != null) builder.moveTo(secondRun.getNextSibling()); else builder.moveTo(secondRun.getParentParagraph()); builder.endBookmark("Aspose bookmark"); doc.save("Output.pdf"); import com.aspose.words.*; Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Run firstRun = new Run(doc, "Proin eros metus, sagittis sed. "); Run secondRun = new Run(doc, "Morbi enim nunc faucibus a."); doc.getFirstSection().getBody().getFirstParagraph().appendChild(firstRun); doc.getFirstSection().getBody().getFirstParagraph().appendChild(secondRun); builder.moveTo(secondRun); builder.startBookmark("Aspose bookmark"); // หาก NextSibling เป็นโมฆะ เป็นไปได้มากว่านี่คือ Run สุดท้ายในย่อหน้า if (secondRun.getNextSibling() != null) builder.moveTo(secondRun.getNextSibling()); else builder.moveTo(secondRun.getParentParagraph()); builder.endBookmark("Aspose bookmark"); PdfSaveOptions saveOptions = new PdfSaveOptions(); saveOptions.getOutlineOptions().getBookmarksOutlineLevels().add("Aspose bookmark", 1); doc.save("Output.pdf", saveOptions);
รันโค้ด

วิธีสร้าง PDF ใน Java

  1. ติดตั้ง Aspose.Words for Java
  2. เพิ่มการอ้างอิงไลบรารี (นำเข้าไลบรารี) ไปยัง Java โครงการของคุณ
  3. สร้างเอกสาร PDF ใหม่
  4. เรียกเมธอด save() โดยส่งชื่อไฟล์
  5. รับผลลัพธ์เป็นไฟล์แยกต่างหาก

Java ไลบรารีเพื่อสร้าง PDF

เราโฮสต์ Java ของเราในที่เก็บ Maven 'Aspose.Words สำหรับ Java' เป็น JAR ทั่วไปที่มีโค้ดไบต์ โปรดปฏิบัติตาม คำแนะนำทีละขั้นตอน เกี่ยวกับวิธีการติดตั้งในสภาพแวดล้อมนักพัฒนา Java ของคุณ

ความต้องการของระบบ

Java SE 7 และ Java เวอร์ชันล่าสุด นอกจากนี้เรายังมีแพ็คเกจแยกต่างหากสำหรับ Java SE 6 ในกรณีที่คุณจำเป็นต้องใช้งาน JRE ล้าสมัยนี้

Java ของเราเป็นแบบข้ามแพลตฟอร์มและทำงานบนระบบปฏิบัติการทั้งหมดที่มีการใช้งาน JVM Microsoft Windows, Linux, macOS, Android และ iOS

สำหรับข้อมูลเกี่ยวกับการพึ่งพาแพ็คเกจเสริม เช่น JogAmp JOGL, Harfbuzz ฟอนต์ Java Advanced Imaging JAI โปรดดู เอกสารประกอบผลิตภัณฑ์

รูปแบบไฟล์อื่นๆ ที่รองรับ

คุณสามารถสร้างเอกสารในรูปแบบไฟล์อื่น:

5%

สมัครสมาชิก Aspose Product Updates

รับจดหมายข่าวและข้อเสนอรายเดือนที่ส่งตรงถึงกล่องจดหมายของคุณ