צור 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 הוא null, סביר להניח שזו 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 הוא null, סביר להניח שזו 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 הוא null, סביר להניח שזו 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 הוא null, סביר להניח שזו 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 הוא null, סביר להניח שזו 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 הוא null, סביר להניח שזו 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 הוא null, סביר להניח שזו 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 הוא null, סביר להניח שזו 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 הוא null, סביר להניח שזו 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

קבל ניוזלטרים והצעות חודשיים שנשלחו ישירות לתיבת הדואר שלך.

© Aspose Pty Ltd 2001-2024. כל הזכויות שמורות.