Vytvárajte PDF dokumenty alebo Word v Java

Vytvorte nový dokument takmer v akomkoľvek formáte programovo pomocou našej knižnice Java s vysokou presnosťou

Pomocou nášho programovacieho rozhrania API môžu vývojári Java jednoducho vytvoriť dokument vo PDF formátoch, DOC, DOCX, HTML, EPUB a mnohých ďalších formátoch pomocou niekoľkých riadkov kódu.

Zobraziť útržok kódu

Vytvorte dokument pomocou Java

S daným výkonným API môžu vývojári Java vytvárať dokumenty takmer v akomkoľvek formáte. Ak to chcete urobiť, musíte vykonať niekoľko krokov pomocou našej knižnice Java:

  1. Pridajte názov súboru
  2. Začnite vytvárať dokument pomocou Java
  3. Uložte vytvorený dokument vo zvolenom formáte

Stojí za zmienku, že prázdny dokument má technicky obsahovať jeden odsek, takže keď programovo vytvoríte dokument, získate presne túto základnú štruktúru dokumentu.

Upozorňujeme, že do novo vytvoreného dokumentu môžete okamžite pridať obsah. Získate tak nielen prázdny dokument, ale aj dokument s potrebným obsahom. Ďalšie informácie o úprave dokumentu nájdete na stránke Úpravy.

Vytvorte dokument v Java programovo

Daná Java knižnica umožňuje programovo vytvárať dokument v akomkoľvek podporovanom formáte – PDF, DOCX, DOC, RTF, ODT, EPUB, HTML a iné.

Vyskúšajte naše výkonné funkcie a zistite, ako vytvoriť dokument v niektorých formátoch pomocou nasledujúceho príkladu:

Vytvorte nový dokument pomocou Java
Vyberte cieľový formát zo zoznamu
Spustiť kód
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"); // Ak je NextSibling null, potom je s najväčšou pravdepodobnosťou toto posledné Run v odseku. 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"); // Ak je NextSibling null, potom je s najväčšou pravdepodobnosťou toto posledné Run v odseku. 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"); // Ak je NextSibling null, potom je s najväčšou pravdepodobnosťou toto posledné Run v odseku. 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"); // Ak je NextSibling null, potom je s najväčšou pravdepodobnosťou toto posledné Run v odseku. 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");
// Ak je NextSibling null, potom je s najväčšou pravdepodobnosťou toto posledné Run v odseku.
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"); // Ak je NextSibling null, potom je s najväčšou pravdepodobnosťou toto posledné Run v odseku. 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"); // Ak je NextSibling null, potom je s najväčšou pravdepodobnosťou toto posledné Run v odseku. 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"); // Ak je NextSibling null, potom je s najväčšou pravdepodobnosťou toto posledné Run v odseku. 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"); // Ak je NextSibling null, potom je s najväčšou pravdepodobnosťou toto posledné Run v odseku. 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);
Spustiť kód

Ako vytvoriť dokument v Java

  1. Nainštalovať Aspose.Words for Java
  2. Pridajte odkaz na knižnicu (importujte knižnicu) do svojho Java projektu
  3. Vytvorte nový dokument
  4. Zavolajte metódu save() a odovzdajte názov súboru
  5. Získajte výsledok ako samostatný súbor

Knižnica Java na vytváranie dokumentov

Naše balíčky Java hostíme v úložiskách Maven. 'Aspose.Words for Java' je bežný JAR obsahujúci bajtový kód. Postupujte podľa pokynov krok za krokom, ako ho nainštalovať do vývojárskeho prostredia Java.

Požiadavky na systém

Podporovaná je Java SE 7 a novšie Java Poskytujeme tiež samostatný balík pre Java SE 6 pre prípad, že ste povinní používať toto zastarané JRE.

Náš Java je multiplatformový a beží na všetkých operačných systémoch s JVM, vrátane Microsoft Windows, Linux, macOS, Android a iOS.

Informácie o voliteľných závislostiach balíkov, ako napríklad JogAmp JOGL, Harfbuzz písma Java Advanced Imaging JAI, nájdete v dokumentácii k produktu.

Najpopulárnejšie formáty súborov

5%

Prihláste sa na odber aktualizácií produktov Aspose

Získajte mesačné bulletiny a ponuky priamo do vašej poštovej schránky.

© Aspose Pty Ltd 2001-2024. Všetky práva vyhradené.