Java generate pdf file

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Using Java to generate PDF file

xiang-lee/pdf-generator

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Using Java to generate PDF file

It’s flexible to create your HTML+CSS in freemarkter tempalte to generate the PDF.

alt tag

It supports for Bookmarks in navigation bar. In the example, users data is from User.java object

If you are using eclipse, you can import «Existing Maven Project», set the PDF output path in Runner.java and run it. Enjoy!

//The output path of your PDF file String outputFilePath = "C:\\PDFSample\\samplePDF.pdf"; //The path of your freemarker template String tampleFile = "freemarker_template.html"; //you can put any variables you want, so that you can use them in freemarker template Map variables = new HashMap(); variables.put("users",users); String htmlStr = HtmlGenerator.generate(tampleFile, variables); PdfGenerator.generate(htmlStr, new FileOutputStream(outputFilePath)); 

You can easily display the data like HTML + CSS, the users variable is from variables.put(«users»,users) in the example

Читайте также:  Javascript range slider with input

Источник

Creating a PDF in Java: A Step-by-Step Guide

Create PDF Documents using Java

As a programmer, you may need to create PDF files in Java applications at some stage. For example, to generate receipts, invoices, business reports, etc. Various Java PDF libraries are available that let you generate PDF files from scratch. However, this article is going to provide you with a powerful, high-speed, and high-quality Java library to create PDF files, which requires you to write minimum lines of code.

So let’s see how to create PDF files from scratch in Java. Also, let’s have a look at how to insert text, images, tables, and other elements in a PDF document programmatically.

Java PDF Library#

To create and manipulate PDF files, we will use Aspose.PDF for Java. It is a PDF file manipulation library to create rich PDF files without writing complex code. Using the library, you can create simple as well as complex PDF documents. Also, you can get a free license and generate the desired PDF files.

Aspose.PDF for Java can either be downloaded as JAR or installed within Maven-based applications.

 AsposeJavaAPI Aspose Java API https://repository.aspose.com/repo/  
 com.aspose aspose-pdf 22.11 jdk17  

How to Create a PDF in Java#

The following are the steps to create a simple PDF file from scratch and insert a piece of text in it.

  • Create an instance of Document class.
  • Add a new page using Document.getPages().add() method and get its reference in Page object.
  • Add text to the page using Page.getParagraphs().add(new TextFragment(“Hello World!”)) method.
  • Save the PDF file using Document.save(String) method.

The following code sample shows how to create a PDF file in Java.

Java PDF Generation with Text Formatting#

Let’s now check out how to apply formatting to the text in the PDF document. The text formatting could include font, font size, color, position, etc. The following are the steps to perform this operation.

  • Use Document class to create a new PDF document or load an existing one.
  • Access the Page you want to place the text on.
  • Create an object of TextFragment to set the text and other formatting options such as position, font, color, size, etc.
  • Use the TextBuilder class to add the TextFragment object to the page.
  • Use the Document.save(String) method to save the PDF document.
Читайте также:  Java comparator not sorting

The following code sample shows how to generate a PDF file and apply text formatting using Java.

Generate PDF Files with Image in Java#

The following are the steps to insert an image in a PDF file using Aspose.PDF for Java.

  • Create a Document object to create or open the PDF document.
  • Get the page you want to add an image to in the Page object.
  • Add the image into the Page.getResources() collection.
  • Use operators to place the image on the page:
    • GSave operator to save the current graphical state.
    • ConcatenateMatrix operator to specify where the image is to be placed.
    • Do operator to draw the image on the page.
    • Finally, use the GRestore operator to save the updated graphical state.

    The following code sample shows how to insert image in a PDF file in Java.

    Create a Table in PDF File in Java#

    The table is a fine way of organizing the data in the form of rows and columns. Aspose.PDF lets you insert a table in a PDF document quite easily.

    The following are the steps to create a PDF table in Java.

    • Load the source file using Document class.
    • Initialize a Table and set its columns and rows.
    • Set table’s setting (e.g. borders).
    • Populate the table by adding rows to it.
    • Add the table to a page.
    • Save the PDF file.

    The following code sample shows how to insert a table in a PDF in Java.

    Create Form in a PDF in Java#

    The PDF format also supports adding forms to collect input from the users. The form may contain various types of fields such as text boxes, radio buttons, etc. The following steps demonstrate how to insert a simple form in the PDF file.

    • Load the PDF document or create a new one using Document class.
    • Create the form field that you want to add such as TextBoxField.
    • Set properties of the field.
    • Add the field to the Form of the PDF using pdfDocument.getForm().add(Field, Int) method.
    • Save the PDF document.

    The following code sample shows how to create a form in a PDF file in Java.

    Read more about the form fields using here.

    Java: Generate PDF having Bookmarks#

    The following are the steps to add bookmarks in PDF file.

    • Open a PDF document using the Document object.
    • Add documents outlines to an OutlineItemCollection collection.
    • Set the destination page number.
    • Add the bookmark to the document’s outline collection.
    • Save the PDF file.

    The following code sample shows how to insert bookmarks in PDF using Java.

    Free Java PDF Generation Library#

    You can get a free temporary license and generate PDF files from scratch without any limitations.

    Explore Java PDF Library#

    You can explore more about the Java PDF API using documentation.

    Conclusion#

    In this article, you have learned how to create PDF files from scratch in Java. Furthermore, the Java code samples have demonstrated how to insert text, images, tables, forms, and bookmarks in new or existing PDF files. You can easily install the library in your application and generate your desired PDF files.

    See Also#

    Источник

    Java generate pdf file

    Learn Latest Tutorials

    Splunk tutorial

    SPSS tutorial

    Swagger tutorial

    T-SQL tutorial

    Tumblr tutorial

    React tutorial

    Regex tutorial

    Reinforcement learning tutorial

    R Programming tutorial

    RxJS tutorial

    React Native tutorial

    Python Design Patterns

    Python Pillow tutorial

    Python Turtle tutorial

    Keras tutorial

    Preparation

    Aptitude

    Logical Reasoning

    Verbal Ability

    Company Interview Questions

    Artificial Intelligence

    AWS Tutorial

    Selenium tutorial

    Cloud Computing

    Hadoop tutorial

    ReactJS Tutorial

    Data Science Tutorial

    Angular 7 Tutorial

    Blockchain Tutorial

    Git Tutorial

    Machine Learning Tutorial

    DevOps Tutorial

    B.Tech / MCA

    DBMS tutorial

    Data Structures tutorial

    DAA tutorial

    Operating System

    Computer Network tutorial

    Compiler Design tutorial

    Computer Organization and Architecture

    Discrete Mathematics Tutorial

    Ethical Hacking

    Computer Graphics Tutorial

    Software Engineering

    html tutorial

    Cyber Security tutorial

    Automata Tutorial

    C Language tutorial

    C++ tutorial

    Java tutorial

    .Net Framework tutorial

    Python tutorial

    List of Programs

    Control Systems tutorial

    Data Mining Tutorial

    Data Warehouse Tutorial

    Javatpoint Services

    JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.

    • Website Designing
    • Website Development
    • Java Development
    • PHP Development
    • WordPress
    • Graphic Designing
    • Logo
    • Digital Marketing
    • On Page and Off Page SEO
    • PPC
    • Content Development
    • Corporate Training
    • Classroom and Online Training
    • Data Entry

    Training For College Campus

    JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
    Duration: 1 week to 2 week

    Like/Subscribe us for latest updates or newsletter RSS Feed Subscribe to Get Email Alerts Facebook Page Twitter Page YouTube Blog Page

    Источник

Оцените статью