Creați DOCX folosind Java biblioteca

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

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

Vizualizați fragmentul de cod

Faceți DOCX în Java în mod programatic

Cu biblioteca noastră Java, dezvoltatorii pot face cu ușurință DOCX 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 DOCX document folosind Java
  3. Salvați DOCX 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 DOCX 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 DOCX 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 DOCX în Java

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

Creați un nou DOCX 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.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"); // 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.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"); // 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.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"); // 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.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"); // 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.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");
// 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.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"); // 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.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"); // 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.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"); // 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.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"); // 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.docx", saveOptions);
Rulați codul

Cum să faci DOCX î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 DOCX 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 DOCX

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.