Twórz PDF dokumenty lub Word w Java

Utwórz programowo nowy dokument w prawie dowolnym formacie, korzystając z naszej Java biblioteki o wysokiej wierności

Korzystając z naszego programowania API, programiści Java mogą z łatwością tworzyć dokumenty w PDF formatach, DOC, DOCX, HTML, EPUB i wielu innych za pomocą zaledwie kilku linijek kodu.

Zobacz fragment kodu

Utwórz dokument za pomocą Java

Dzięki podanemu potężnemu interfejsowi API programiści Java mogą tworzyć dokumenty w prawie każdym formacie. Aby to zrobić, musisz wykonać kilka kroków, korzystając z naszej biblioteki Java:

  1. Dodaj nazwę pliku
  2. Zacznij tworzyć dokument za pomocą Java
  3. Zapisz utworzony dokument w wybranym formacie

Warto zauważyć, że pusty dokument technicznie powinien zawierać jeden akapit, więc gdy programowo utworzysz dokument, otrzymasz dokładnie taką podstawową strukturę dokumentu.

Pamiętaj, że możesz natychmiast dodać zawartość do nowo utworzonego dokumentu. W ten sposób otrzymasz nie tylko pusty dokument, ale dokument zawierający niezbędną treść. Aby uzyskać więcej informacji na temat edycji dokumentu, zobacz stronę Edycja.

Programowe tworzenie dokumentu w Java

Podana Java biblioteka pozwala programowo stworzyć dokument w dowolnym obsługiwanym formacie – PDF, DOCX, DOC, RTF, ODT, EPUB, HTML i inne.

Wypróbuj naszą zaawansowaną funkcjonalność i zobacz, jak utworzyć dokument w niektórych formatach, korzystając z następującego przykładu:

Utwórz nowy dokument za pomocą Java
Wybierz format docelowy z listy
Uruchom kod
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.docx");
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"); // Jeśli NextSibling ma wartość null, najprawdopodobniej jest to ostatnie Run w akapicie. if (secondRun.getNextSibling() != null) builder.moveTo(secondRun.getNextSibling()); else builder.moveTo(secondRun.getParentParagraph()); builder.endBookmark("Aspose bookmark"); doc.save("Output.docx"); 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"); // Jeśli NextSibling ma wartość null, najprawdopodobniej jest to ostatnie Run w akapicie. 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.docx", 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.docx");
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"); // Jeśli NextSibling ma wartość null, najprawdopodobniej jest to ostatnie Run w akapicie. if (secondRun.getNextSibling() != null) builder.moveTo(secondRun.getNextSibling()); else builder.moveTo(secondRun.getParentParagraph()); builder.endBookmark("Aspose bookmark"); doc.save("Output.docx"); 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"); // Jeśli NextSibling ma wartość null, najprawdopodobniej jest to ostatnie Run w akapicie. 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.docx", 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");
// Jeśli NextSibling ma wartość null, najprawdopodobniej jest to ostatnie Run w akapicie.
if (secondRun.getNextSibling() != null)
    builder.moveTo(secondRun.getNextSibling());
else
    builder.moveTo(secondRun.getParentParagraph());
builder.endBookmark("Aspose bookmark");

doc.save("Output.docx");
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"); // Jeśli NextSibling ma wartość null, najprawdopodobniej jest to ostatnie Run w akapicie. if (secondRun.getNextSibling() != null) builder.moveTo(secondRun.getNextSibling()); else builder.moveTo(secondRun.getParentParagraph()); builder.endBookmark("Aspose bookmark"); doc.save("Output.docx"); 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"); // Jeśli NextSibling ma wartość null, najprawdopodobniej jest to ostatnie Run w akapicie. 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.docx", 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.docx");
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"); // Jeśli NextSibling ma wartość null, najprawdopodobniej jest to ostatnie Run w akapicie. if (secondRun.getNextSibling() != null) builder.moveTo(secondRun.getNextSibling()); else builder.moveTo(secondRun.getParentParagraph()); builder.endBookmark("Aspose bookmark"); doc.save("Output.docx"); 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"); // Jeśli NextSibling ma wartość null, najprawdopodobniej jest to ostatnie Run w akapicie. 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.docx", saveOptions);
Uruchom kod

Jak zrobić dokument w Java

  1. Zainstaluj Aspose.Words for Java
  2. Dodaj odniesienie do biblioteki (zaimportuj bibliotekę) do swojego Java projektu
  3. Utwórz nowy dokument
  4. Wywołaj metodę "save()", przekazując nazwę pliku
  5. Uzyskaj wynik jako osobny plik

Java biblioteka do tworzenia dokumentów

Nasze Java hostujemy w repozytoriach Maven. 'Aspose.Words for Java' to wspólny JAR zawierający kod bajtowy. Postępuj zgodnie z instrukcjami krok po kroku, jak zainstalować go w swoim środowisku programisty Java.

wymagania systemowe

Obsługiwana jest Java SE 7 i nowsze Java Udostępniamy również osobny pakiet dla Java SE 6 na wypadek, gdybyś musiał używać tego przestarzałego JRE.

Nasz Java jest wieloplatformowy i działa na wszystkich systemach operacyjnych z JVM, w tym Microsoft Windows, Linux, macOS, Android i iOS.

Aby uzyskać informacje na temat dodatkowych zależności między pakietami, takich jak JogAmp JOGL, Harfbuzz silnika czcionki, Java Zaawansowane obrazowanie JAI, zapoznaj się z dokumentacją produktu.

Najpopularniejsze formaty plików

5%

Subskrybuj aktualizacje produktów Aspose

Otrzymuj comiesięczne biuletyny i oferty dostarczane bezpośrednio do Twojej skrzynki pocztowej.

© Aspose Pty Ltd 2001-2024. Wszelkie prawa zastrzeżone.