Creați PDF folosind Java biblioteca

Biblioteca noastră puternică Java permite dezvoltatorilor să creeze programatic PDF în doar câțiva pași

Dezvoltatorii Java pot crea cu ușurință PDF folosind doar API ul nostru puternic pentru produse. Înseamnă că soluția noastră va oferi programatorilor tot ce au nevoie pentru a crea PDF în Java.

Vizualizați fragmentul de cod

Faceți PDF în Java în mod programatic

Cu biblioteca noastră Java, dezvoltatorii pot face cu ușurință PDF de la zero. Pentru a face acest lucru, dezvoltatorii Java trebuie să efectueze doar câțiva pași:

  1. Adăugați numele fișierului
  2. Începeți să creați un PDF document folosind Java
  3. Salvați PDF fișierul de ieșire

Este demn de remarcat faptul că, din punct de vedere tehnic, un document gol trebuie să conțină un paragraf, așa că atunci când creați un PDF document în mod programatic, veți obține exact acea structură de bază a documentului.

Rețineți că puteți adăuga instantaneu conținut la un PDF fișier nou creat. Astfel, veți obține nu doar un document gol, ci și un document care conține conținutul necesar. Pentru mai multe informații despre cum să editați un document, consultați pagina Editare.

Creați PDF în Java

Această bibliotecă Java vă permite să creați în mod programatic PDF documente. Încercați funcționalitatea noastră puternică și vedeți cum să creați PDF în unele formate folosind următorul exemplu:

Creați un nou PDF folosind Java
Selectați formatul țintă din listă
Rulați codul
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"); // Dacă NextSibling este nul, atunci cel mai probabil aceasta este ultima Run din Paragraf. 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"); // Dacă NextSibling este nul, atunci cel mai probabil aceasta este ultima Run din Paragraf. 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"); // Dacă NextSibling este nul, atunci cel mai probabil aceasta este ultima Run din Paragraf. 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"); // Dacă NextSibling este nul, atunci cel mai probabil aceasta este ultima Run din Paragraf. 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");
// Dacă NextSibling este nul, atunci cel mai probabil aceasta este ultima Run din Paragraf.
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"); // Dacă NextSibling este nul, atunci cel mai probabil aceasta este ultima Run din Paragraf. 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"); // Dacă NextSibling este nul, atunci cel mai probabil aceasta este ultima Run din Paragraf. 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"); // Dacă NextSibling este nul, atunci cel mai probabil aceasta este ultima Run din Paragraf. 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"); // Dacă NextSibling este nul, atunci cel mai probabil aceasta este ultima Run din Paragraf. 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);
Rulați codul

Cum să faci PDF în Java

  1. Instalați Aspose.Words for Java
  2. Adăugați o referință la bibliotecă (importați biblioteca) în proiectul dvs. Java
  3. Creați un nou PDF document
  4. Apelați metoda "save()", trecând numele fișierului
  5. Obțineți rezultatul ca fișier separat

Java bibliotecă pentru a crea PDF

Java pachetele noastre Java în depozitele Maven. 'Aspose.Words pentru Java' este un JAR obișnuit care conține byte-code. Vă rugăm să urmați instrucțiunile pas cu pas despre cum să îl instalați în mediul dumneavoastră de dezvoltator Java.

Cerințe de sistem

Java SE 7 și versiunile mai recente Java sunt acceptate. De asemenea, oferim un pachet separat pentru Java SE 6 în cazul în care sunteți obligat să utilizați acest JRE învechit.

Pachetul nostru Java este multiplataforma și rulează pe toate sistemele de operare cu JVM, inclusiv Microsoft Windows, Linux, macOS, Android și iOS.

Pentru informații despre dependențele opționale ale pachetelor, cum ar fi JogAmp JOGL, motorul de fonturi Harfbuzz Java Advanced Imaging JAI, vă rugăm să consultați Documentația produsului.

Alte formate de fișiere acceptate

Puteți crea documente în alte formate de fișiere:

5%

Abonați-vă la Actualizări de produse Aspose

Primiți buletine informative lunare și oferte livrate direct în căsuța dvs. poștală.

© Aspose Pty Ltd 2001-2024. Toate drepturile rezervate.