ساخت اسناد PDF یا Word در Java

با استفاده از کتابخانه Java ما با وفاداری بالا، یک سند جدید تقریباً در هر قالبی ایجاد کنید

با استفاده از API برنامه نویسی ما، توسعه دهندگان Java می توانند به راحتی سندی را با فرمت های PDF، DOC، DOCX، HTML، EPUB و بسیاری دیگر تنها با چند خط کد ایجاد کنند.

مشاهده قطعه کد

با استفاده از Java یک سند بسازید

با API قدرتمند داده شده، توسعه دهندگان Java می توانند اسناد را تقریباً در هر قالبی ایجاد کنند. برای انجام این کار، باید چند مرحله را با استفاده از کتابخانه Java ما دنبال کنید:

  1. نام فایل را اضافه کنید
  2. شروع به ایجاد یک سند با استفاده از Java
  3. سند ایجاد شده را در قالب انتخاب شده ذخیره کنید

شایان ذکر است که یک سند خالی از نظر فنی باید شامل یک پاراگراف باشد، بنابراین وقتی به صورت برنامه‌نویسی یک سند را ایجاد می‌کنید، دقیقاً همان ساختار سند اولیه را دریافت خواهید کرد.

توجه داشته باشید که می توانید فوراً محتوا را به یک سند ایجاد شده اضافه کنید. بنابراین، شما نه تنها یک سند خالی، بلکه یک سند حاوی محتوای لازم را دریافت خواهید کرد. برای اطلاعات بیشتر در مورد نحوه ویرایش یک سند، به صفحه ویرایش مراجعه کنید.

یک سند در Java به صورت برنامه نویسی ایجاد کنید

کتابخانه Java به شما این امکان را می دهد که به صورت برنامه نویسی یک سند در هر فرمت پشتیبانی شده ایجاد کنید - PDF، DOCX، DOC، RTF، ODT، EPUB، HTML و غیره.

عملکرد قدرتمند ما را امتحان کنید و ببینید که چگونه با استفاده از مثال زیر یک سند در برخی از فرمت ها ایجاد کنید:

با استفاده از 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.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"); // اگر NextSibling تهی باشد، به احتمال زیاد این آخرین Run در پاراگراف است. 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"); // اگر NextSibling تهی باشد، به احتمال زیاد این آخرین 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.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"); // اگر NextSibling تهی باشد، به احتمال زیاد این آخرین Run در پاراگراف است. 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"); // اگر NextSibling تهی باشد، به احتمال زیاد این آخرین 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.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");
// اگر NextSibling تهی باشد، به احتمال زیاد این آخرین Run در پاراگراف است.
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"); // اگر NextSibling تهی باشد، به احتمال زیاد این آخرین Run در پاراگراف است. 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"); // اگر NextSibling تهی باشد، به احتمال زیاد این آخرین 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.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"); // اگر NextSibling تهی باشد، به احتمال زیاد این آخرین Run در پاراگراف است. 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"); // اگر NextSibling تهی باشد، به احتمال زیاد این آخرین 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.docx", saveOptions);
کد را اجرا کنید

نحوه ایجاد سند در Java

  1. Aspose.Words for Java نصب کنید
  2. یک مرجع کتابخانه (وارد کردن کتابخانه) به پروژه Java خود اضافه کنید
  3. یک سند جدید ایجاد کنید
  4. با ارسال نام فایل، روش "save()" را فراخوانی کنید
  5. نتیجه را در یک فایل جداگانه دریافت کنید

کتابخانه Java برای ایجاد اسناد

ما بسته های Java خود را در مخازن Maven میزبانی می کنیم. 'Aspose.Words برای Java' یک JAR رایج حاوی کد بایت است. لطفاً دستورالعمل های گام به گام نحوه نصب آن را در محیط توسعه دهنده جاوا خود دنبال کنید.

سیستم مورد نیاز

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. تمامی حقوق محفوظ است.