Jack Du

Greenhorn
+ Follow
since Dec 17, 2018
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
5
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Jack Du

I suggest that you can try Spire.Office.jar to accomplish this job. Here is the code snippet for your reference.

2 years ago
Try Spire.Doc for Java. Here is the sample code snippet for your reference.
2 years ago
I know it's a bit late to provide a solution in such an old post, but I hope that my answer can help someone who finds this post by searching.

This scenario is based on Spire.PDF for Java, which a class library for PDF document. Before running the following code snippet to create PDF from text files, you'll need to add a dependency to Spire.Pdf.jar in your program.

2 years ago
Hi, if you use Spire.PDF for Java, which is a class library for processing PDFs from Java code. You can specify the coordinate of a digital signature using the following code snippet.

More details can be found here.



2 years ago
Overview

Excel headers or footers are visible in Page Layout view or the printed pages. We can add the headers or footers at the top or bottom of a printed worksheet to display page number, worksheet name, current data, text or a picture. This article will demonstrates how to programmatically add headers or footers to a worksheet by using a free java API for Excel.

Introduction

This solution relies on Free Spire.XLS for Java, which provides a PageSetup class to deal with all page setup settings in Excel. Particularly, it offers setLeftHeader method, setLeftHeaderImage method, etc. to handle text and image in header or footer. In addtion, some special script commands are provided for you to display dynamic content like page number and current date, or to format the text to be showed.

Here is a list of scripts and their usage.  
ScriptDescription
&PThe current page number
&NThe total number of pages
&DThe current date
&TThe current time
&GA picture
&AThe worksheet name
&FThe file name
&BMake text bold
&IItalicize text
&UUnderline text
&”font name”Represents a font name, for example, &”Aril”
&font sizeRepresents font size, for example, &12
&K<HEX color>Represents font color, for example, &KFF0000


Installation

Download Free Spire.XLS for Java pack, unzip it and you’ll get Spire.Xls.jar file from the “lib” folder. Import the jar file in your project as a dependency. If you are creating a Maven project, you can easily add the jar dependency by adding the following configurations to the pom.xml.



Example 1. Insert image and page number in header

It’s very commen that we add our company logo or another meanful image to a document at the header space. The following code snippets show you how to insert an image as well as page number in the Excel header.



Example 2. Insert formatted text in footer

It is easy to insert plain text in header or footer by assigning values to header or footer. To change the look of the text, you can use the script commends listed in the table, mentioned above to format the text.




Example 3. Set different header and footer for the first page

In some cases, the first page will be used as the cover of your printed document. As a result, you may want the header and footer of the fisrt page to be different.


3 years ago
Example 3. How to create a line chart

A line chart is a graph that shows a series of data points connected by straight lines and is generally used to show changes over time.

4 years ago
Excel charts can display data in the form of visual graphs, so that we can easily find the essential information behind the data, such as the proportion of men and women in a department, data trends over time, market share, etc. In this article, I am going to show you how to create clustered column charts, pie charts and line charts in an Excel document, by using Free Spire.XLS for Java.

Add Spire.Xls.jar as dependency

Method 1: Download Free Spire.XLS for Java pack, unzip it and you’ll get Spire.Doc.jar file from the “lib” folder. Import the jar file in your project as a denpendency.

Method 2:  If you are creating a Maven project, you can easily add the jar dependency by adding the following configurations to the pom.xml.

Example 1. How to create a clustered column chart

A column chart displays a series as a set of vertical bars that are grouped by category.



Example 2. How to create a pie chart

A pie chart helps show proportions and percentages between categories, by dividing a circle into proportional segments.


4 years ago
Adding tiled watermarks to PDF documents is an effective way to prevent documents from being pirated. A tiled watermark lets you have tiles of your watermark all over a PDF page. You can specify either a text to be used as a tile, or you can specify a picture for that. This article will demonstrate how to add a tiling watermark effect to PDF documents using Free Spire.PDF for Java.

Before start, you should learn about two important concepts in Spire.PDF – tiling brush and coordinate system. A tiling brush, represented by PdfTitlingBrush class, is used to define the contents of the tile that you use to paint a region. A coordinate system is used to determine where the contents (text or/and images) shoule be placed on a tile.

How is the coordinate system defined?

  • The origin of the coordinate system (0, 0) represents the top-left corner of a graphics in Spire.PDF.
  • The x-axis extends to the right and the y-axis extends downward.

  • How to create a tiling brush?

  • Step 1. Create a PdfTilingBrush object. Set its width and height to half of the width and height of a PDF page respectively. You can change the ratio according to the actual situation.
  • Step 2: Translate coordinate system to the lower right.
  • Step 3: Rotate coordinate system 45 degrees. Step 2 and step 3 enable that the text watermark lies in the center of the brush and is tilted at 45 degrees.
  • Step 4: Draw text from the starting point (0, 0) of the new coordinate system.



  • If you want to place an image at the center of a brush, you do not need to transform the coordinate system but calculate the X, Y coordinates directly. After a tiling brush is created, you can use it to paint the whole area of a page to get a tiling background effect.



    The following sections provide detailed code snippets showing you how to add text and image watermarks to PDF.

    Adding tiled text watermark
    Output:


    Adding tiled image watermark

    Output:

    4 years ago
    Overview

    With the continuous development of e-commerce, electronic invoices are adopted by more and more businesses, among which PDF invoices are one of the most popular ones. In this article, I’ll share with you a free and brilliant solution of creating PDF invoices in Java.

    A typical invoice contains the names and addresses of customer and supplier, the invoice number, the descriptions of items purchased, the payment amount, etc. These information varies from one document to another. To generate invoices dynamically, I created a template with MS Word, in which it is easy to have your desired layout pre-defined. Then I modified the template document and saved it as a PDF file using Free Spire.Doc for Java 2.0.0.

    Create a Template in Word

    As shown in the figure 1, the Word invoice template consists of three tables. Table one and table two display the seller information and buyer information. Table three specify the products or services the seller had provided the buyer. What we need to do is replace the text in the tables that starts with # and fill the third table with the customer’s purchase list.

    In order to calculate the total amount automatically, some formulas have been added to the particular cells. For example, the cell D2 contains a formula of “=B2*C2”, which calculates the total price of the item that will be written in the cell A2. While we add more items (rows) to the table, we need to update the formulas of some cells dynamically.


                                                                                      Figure 1. Invoice Template

    Download Inovice-Template.docx

    How to Replace Text in Word

    Spire.Doc has a method IBodyRegion.replace(java.lang.String given, java.lang.String replace, boolean caseSensitive, boolean wholeWord) that we can use to replace a certain string in the document with a new string. For instance, to replace “#InvoiceNum” with “17854”, use the following line of code, where doc is a Document object.

    How to Update an Existing Table in Word

    Now, let’s see how to add rows to the existing table, how to fill the table with data and how to update formulas dynamically. To make the logic clearer, I customized three functons and made the below graph (Figure 2) to demonstrate how they function and the invoke relationship between them.

                                                                  Figure 2. Custom Functions for Updating Table Three

    Here come the code snippets and detailed instuctions for these three custom functions.

  • addRows()

  • This function actually duplicates the second row of the existing table and adds the duplicated rows successively next to the second row. The new rows inherit the cell format, font style as well as formula of the second row. Therefore, we need to update the formulas in the new rows in turn, and the following formulas that change as the number of rows increases.

  • fillTableWithData()

  • As its name suggests, this fucntion is simply used to write data from String[][] to the table, from the first cell of the second row.

  • writeDataToDocument()

  • Since the invoice template already has a row (the second row) for displaying the information of one item, we need to determine if it is necessary to add more rows. If the customer only purchased one item, we’re not required to do anything before inserting the pruchase data. Otherwise, we need to add rows to accommodate more items and update the formulas dynamically so as to get the correct payment amount.

    The writeDataToDocument() method takes a parameter of an String[][] object, which stores one customer’s purchase information. Each element of it is a String array that can be set up like this:

    The length of the String[][] is acutally the number of the items a customer purchased. If the length is greater than one, we’ll need to add rows at the number of length – 1.

    Generating Invoice

    Here's the code for generating PDF invoices in the main method.

    The result document is showing below.


                                                                                  Figure 3. Invoice for Multiple Items

    If you have the purchase data like this, then you’ll get the output shown in Figure 4.


                                                                                 Figure 4. Invoice for One Item

    P.S. Free Spire.Doc for Java is capable of loading a Word document having no more than 500 paragraphs or 25 tables, and converting the first 3 pages into PDF. Most PDF invoices have only one or two pages. So, feel free to enjoy this solution, it is completely free.
    4 years ago
    Overview

    With the continuous development of e-commerce, electronic invoices are adopted by more and more businesses, among which PDF invoices are one of the most popular ones. In this article, I’ll share with you a free and brilliant solution of creating PDF invoices in Java.

    A typical invoice contains the names and addresses of customer and supplier, the invoice number, the descriptions of items purchased, the payment amount, etc. These information varies from one document to another. To generate invoices dynamically, I created a template with MS Word, in which it is easy to have your desired layout pre-defined. Then I modified the template document and saved it as a PDF file using Free Spire.Doc for Java 2.0.0.

    Create a Template in Word

    As shown in the figure 1, the Word invoice template consists of three tables. Table one and table two display the seller information and buyer information. Table three specify the products or services the seller had provided the buyer. What we need to do is replace the text in the tables that starts with # and fill the third table with the customer’s purchase list.

    In order to calculate the total amount automatically, some formulas have been added to the particular cells. For example, the cell D2 contains a formula of “=B2*C2”, which calculates the total price of the item that will be written in the cell A2. While we add more items (rows) to the table, we need to update the formulas of some cells dynamically.


                                                                                      Figure 1. Invoice Template

    Download Inovice-Template.docx

    How to Replace Text in Word

    Spire.Doc has a method IBodyRegion.replace(java.lang.String given, java.lang.String replace, boolean caseSensitive, boolean wholeWord) that we can use to replace a certain string in the document with a new string. For instance, to replace “#InvoiceNum” with “17854”, use the following line of code, where doc is a Document object.

    How to Update an Existing Table in Word

    Now, let’s see how to add rows to the existing table, how to fill the table with data and how to update formulas dynamically. To make the logic clearer, I customized three functons and made the below graph (Figure 2) to demonstrate how they function and the invoke relationship between them.

                                                                  Figure 2. Custom Functions for Updating Table Three

    Here come the code snippets and detailed instuctions for these three custom functions.

  • addRows()

  • This function actually duplicates the second row of the existing table and adds the duplicated rows successively next to the second row. The new rows inherit the cell format, font style as well as formula of the second row. Therefore, we need to update the formulas in the new rows in turn, and the following formulas that change as the number of rows increases.

  • fillTableWithData()

  • As its name suggests, this fucntion is simply used to write data from String[][] to the table, from the first cell of the second row.

  • writeDataToDocument()

  • Since the invoice template already has a row (the second row) for displaying the information of one item, we need to determine if it is necessary to add more rows. If the customer only purchased one item, we’re not required to do anything before inserting the pruchase data. Otherwise, we need to add rows to accommodate more items and update the formulas dynamically so as to get the correct payment amount.

    The writeDataToDocument() method takes a parameter of an String[][] object, which stores one customer’s purchase information. Each element of it is a String array that can be set up like this:

    The length of the String[][] is acutally the number of the items a customer purchased. If the length is greater than one, we’ll need to add rows at the number of length – 1.

    Generating Invoice

    Here's the code for generating PDF invoices in the main method.

    The result document is showing below.


                                                                                  Figure 3. Invoice for Multiple Items

    If you have the purchase data like this, then you’ll get the output shown in Figure 4.


                                                                                 Figure 4. Invoice for One Item

    P.S. Free Spire.Doc for Java is capable of loading a Word document having no more than 500 paragraphs or 25 tables, and converting the first 3 pages into PDF. Most PDF invoices have only one or two pages. So, feel free to enjoy this solution, it is completely free.
    4 years ago
    In this post, I’ll give you an example of how to create a simple PDF file containing title, paragraph, image, numbered list and table, and how to convert PDF to other file formats using Free Spire.PDF with Java.

    Table of Contents


    Overview

    Free Spire.PDF for Java is a class library that allows you to create and process PDF documents directly from your Java programs without having to install any additional software. PDF files can be easily created using this library. Simply create a PdfDocument object, create as many PdfPageBase objects as necessary, add strings, images, tables, lists and any other elements to the pages and then save the document.

    Before Start

    Download Free Spire.PDF for Java from this link and import the Spire.Pdf.jar file in your Java project.

    Coordinate System

    A page generated by Spire.PDF consists of margins and content area. The coordinate system exists only in the content area, allowing users to add content to the specified coordinates. The coordinate system in Spire.PDF follows the following rules.

  • The origin of the coordinate system (0, 0) represents the top-left corner of the content area.
  • The x-axis extends to the right and the y-axis extends downward.



  • Creating Custom Functions

    Spire.PDF itself provides useful classes and methods, such as PdfPageBase class, drawString method and drawImage method, which work with the page settings, writing text and adding images according to the specific coordinates. In order to make the code easier to read, I pre-defined five custom functions below to perform different tasks.

  • drawTitle

  • When drawing a title on PDF, you probably need to align text. The drawTitle function created based on drawString is such a function that allows to horizontally align text, for instance, to center.

  • drawParagraph

  • The drawParagraph function is different from drawTitle, though they’re taking similar parameters. This function is created based on PdfTextWidget.draw method and returns an object of PdfLayoutResult class which contains the bounds information of the element being drawing. By doing so, you’ll be able to know where the current paragraph ends and where to start drawing the next piece of content.

  • drawImage

  • The drawImage function is a simply overloading of the original method. This function won’t returns a PdfLayoutResult object. In order to know where the content below begins, you'll need to get the image height through PdfImage object and then calculate the Y coordinate.

  • drawTable

  • When using the drawTable function to draw tables on a PDF page, you need to specify the table data in String[][] array. This function also returns a PdfLayoutResult object that helps us get the position and size of the table.

  • drawSortedList

  • The last function drawSortedList is quite similar with drawTable, except that it takes some unique parameters like list content, order marker and list indent.

    Using the Code

    Invoke these custom functions and you’ll be able to draw elements at the appropriate positions of a PDF page.

    The resulting PDF file looks like the one below.


    Convert PDF to Other Formats

    Instead of saving the newly generated PDF (PdfDocument object) to a PDF file, you can also save it to other file formats like Doc, Docx, XPS, SVG and HTML directly through saveToFile method. In the code snippets below, I’d like to show you how to load an existing PDF document and do the file format conversion.

    Apart from the file formats mentioned above, Spire.PDF also supports to convert PDF to popular image formats such as PNG, JPEG, EMF and TIFF. The code is a bit tricky, but far from complicated.

    Other Useful Features

    Below is a list of some other useful features supported by Free Spire.PDF.

  • Add Header and Footer to PDF
  • Add Links to PDF
  • Add Text/Image Watermark to PDF
  • Add Attachments to PDF
  • Add Image/Dynamic Stamps to PDF
  • Apply Different Fonts in PDF
  • Create Fillable Form Fields in PDF
  • Create Grid in PDF
  • Draw Barcode in PDF
  • Draw Shapes in PDF
  • Extract Text/Image from PDF
  • Encrypt and Decrypt PDF
  • Merge/Split PDF
  • Print PDF

  • Conclusion

    With the easy-to-use APIs provided by Free Spire.PDF, you can quickly create PDF from scratch and convert PDF to other file formats without spending a lot of learning time. This is undoubtedly a good news for developers. No matter whether your project needs a PDF component or not, no matter whether you are a senior or a rookie programmer, you are welcome to download the library and gain some wonderful experience in processing PDF documents.  

    If you have any questions, feel free to comment below or contact us at support@e-iceblue.com.
    4 years ago
    In this post, I’ll give you an example of how to create a simple PDF file containing title, paragraph, image, numbered list and table, and how to convert PDF to other file formats using Free Spire.PDF with Java.

    Table of Contents


    Overview

    Free Spire.PDF for Java is a class library that allows you to create and process PDF documents directly from your Java programs without having to install any additional software. PDF files can be easily created using this library. Simply create a PdfDocument object, create as many PdfPageBase objects as necessary, add strings, images, tables, lists and any other elements to the pages and then save the document.

    Before Start

    Download Free Spire.PDF for Java from this link and import the Spire.Pdf.jar file in your Java project.

    Coordinate System

    A page generated by Spire.PDF consists of margins and content area. The coordinate system exists only in the content area, allowing users to add content to the specified coordinates. The coordinate system in Spire.PDF follows the following rules.

  • The origin of the coordinate system (0, 0) represents the top-left corner of the content area.
  • The x-axis extends to the right and the y-axis extends downward.



  • Creating Custom Functions

    Spire.PDF itself provides useful classes and methods, such as PdfPageBase class, drawString method and drawImage method, which work with the page settings, writing text and adding images according to the specific coordinates. In order to make the code easier to read, I pre-defined five custom functions below to perform different tasks.

  • drawTitle

  • When drawing a title on PDF, you probably need to align text. The drawTitle function created based on drawString is such a function that allows to horizontally align text, for instance, to center.

  • drawParagraph

  • The drawParagraph function is different from drawTitle, though they’re taking similar parameters. This function is created based on PdfTextWidget.draw method and returns an object of PdfLayoutResult class which contains the bounds information of the element being drawing. By doing so, you’ll be able to know where the current paragraph ends and where to start drawing the next piece of content.

  • drawImage

  • The drawImage function is a simply overloading of the original method. This function won’t returns a PdfLayoutResult object. In order to know where the content below begins, you'll need to get the image height through PdfImage object and then calculate the Y coordinate.

  • drawTable

  • When using the drawTable function to draw tables on a PDF page, you need to specify the table data in String[][] array. This function also returns a PdfLayoutResult object that helps us get the position and size of the table.

  • drawSortedList

  • The last function drawSortedList is quite similar with drawTable, except that it takes some unique parameters like list content, order marker and list indent.

    Using the Code

    Invoke these custom functions and you’ll be able to draw elements at the appropriate positions of a PDF page.

    The resulting PDF file looks like the one below.


    Convert PDF to Other Formats

    Instead of saving the newly generated PDF (PdfDocument object) to a PDF file, you can also save it to other file formats like Doc, Docx, XPS, SVG and HTML directly through saveToFile method. In the code snippets below, I’d like to show you how to load an existing PDF document and do the file format conversion.

    Apart from the file formats mentioned above, Spire.PDF also supports to convert PDF to popular image formats such as PNG, JPEG, EMF and TIFF. The code is a bit tricky, but far from complicated.

    Other Useful Features

    Below is a list of some other useful features supported by Free Spire.PDF.

  • Add Header and Footer to PDF
  • Add Links to PDF
  • Add Text/Image Watermark to PDF
  • Add Attachments to PDF
  • Add Image/Dynamic Stamps to PDF
  • Apply Different Fonts in PDF
  • Create Fillable Form Fields in PDF
  • Create Grid in PDF
  • Draw Barcode in PDF
  • Draw Shapes in PDF
  • Extract Text/Image from PDF
  • Encrypt and Decrypt PDF
  • Merge/Split PDF
  • Print PDF

  • Conclusion

    With the easy-to-use APIs provided by Free Spire.PDF, you can quickly create PDF from scratch and convert PDF to other file formats without spending a lot of learning time. This is undoubtedly a good news for developers. No matter whether your project needs a PDF component or not, no matter whether you are a senior or a rookie programmer, you are welcome to download the library and gain some wonderful experience in processing PDF documents.  

    If you have any questions, feel free to comment below or contact us at support@e-iceblue.com.
    5 years ago