0% found this document useful (0 votes)
39 views8 pages

Output 2

Uploaded by

Shampa Nasrin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views8 pages

Output 2

Uploaded by

Shampa Nasrin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1.

iTextPDF
iTextPDF is a popular Java library for creating and manipulating PDF documents. It provides
functionalities for generating PDF files dynamically, adding text, images, tables, and various other
elements to PDF documents. iTextPDF is widely used in enterprise applications for generating
reports, invoices, and other types of documents programmatically. It offers both open-source and
commercial versions, with the open-source version being licensed under the Affero General Public
License (AGPL).

2. iTextPDF Features
• PDF Splitter: This feature allows you to split a PDF document into two documents, either
by specifying page ranges or by other criteria. User can choose the page where you want
to split the PDF.

• PDF Merger: With the PDF merger feature, you can combine two PDF documents into a
single document. You can choose to reorder the files, or select which pages to merge from
each PDF. The default option is to merge "All" pages, but you can choose to merge the
PDFs from specific pages if you wish.

• PDF Rotator: The PDF rotator feature enables you to rotate pages within a PDF document.
You can then choose the rotation direction and angle you want. If you have uploaded a
PDF with multiple pages, you can choose if you want to rotate a specific page.

• PDF Page Deleter: This feature allows you to delete specific pages from a PDF document,
useful for removing unnecessary or confidential content.

• PDF Protector: With the PDF protector feature, you can apply encryption and permissions
to PDF documents. You can choose an Owner Password to protect the PDF with, and select
the Encryption options you want. You can also configure permissions for the PDF, for
example if you want to allow printing without entering the password. If you wish, you can
also set a User Password to allow restricted access to the PDF.

• PDF Unlocker: The PDF unlocker feature allows you to enter the Owner Password for the
PDF and providing access to restricted content.

• Image Converter: iTextPDF supports converting images to PDF format. You can choose
the page size and margin settings for the resulting PDF. You can also choose if you want
to rotate the image and set image position settings.
• SVG Converter: This feature allows you to convert Scalable Vector Graphics (SVG) files
to PDF format, preserving vector graphics quality and scalability.

• HTML to PDF Creator: With this feature, you can convert HTML content, such as web
pages or HTML documents, into PDF format, facilitating document generation from web-
based sources.

• XFA Flattener: iTextPDF supports flattening XFA (XML Forms Architecture) forms
within PDF documents. If you want to update the XFA data, or just flatten the XFA to a
static PDF. If you choose Update XFA Data you can edit the XML directly and download
the updated unflattened XFA form. To view the updated XFA data you'll need to use a PDF
viewer that supports XFA-based PDF forms.

• PDF Optimizer: The PDF optimizer feature allows you to reduce the file size of PDF
documents by compressing images, removing redundant data, and optimizing the
document structure. You can select an optimization profile to use. Each optimization
profile uses different types of compression to optimize a PDF, including image quality,
scaling, stream compression, font duplication, and font subsetting. The available profiles
range from Low to High compression, with a Lossless compression profile which
preserves image quality while applying all other optimizations.

• Render PDFs to Images: With this feature, you can render individual pages or entire PDF
documents as image files, such as JPEG or PNG, for various purposes, including display
or further processing. If you want to render all pages or a custom range. It is required to
select your preferred output format. You can select a scaling percentage value or enter a
custom number. The default is 100%.

3 Installing iText for Java


iText can be installed via Maven or directly by downloading the jar files.

3.1 Installing via Jar


Link To download Jar: https://jar-download.com/artifacts/com.itextpdf/itextpdf/5.5.9/source-
code
you can use this jar file directly in your project.

3.2 Installing via Maven


iText Core is available via Maven on The Central repository. You will need to choose whether you
want to add all modules or just the ones you require.
3.2.1 Adding all modules
If you need all iText modules, then you don't need to add the individual modules and you can just
add this XML snippet in your pom.xml. Maven will then do all the heavy lifting for you and
download the required modules from The Central Repository. Simply add iText as a dependency
to your pom.xml:
<properties>
<itext.version>8.0.2</itext.version>
</properties>
<dependencies>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-core</artifactId>
<version>${itext.version}</version>
<type>pom</type>
</dependency>
</dependencies>

3.2.2 Adding only the modules you need


If you do not require some of the functionality in iText, then you can add only the modules you
need. Please be aware you will always need kernel, io and layout. You will need to add the
following XML snippets to your pom.xml. Simply add iText as a dependency to your pom.xml.
<properties>
<itext.version>8.0.2</itext.version>
</properties>
<dependencies>
<!-- always needed -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>kernel</artifactId>
<version>${itext.version}</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>io</artifactId>
<version>${itext.version}</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>layout</artifactId>
<version>${itext.version}</version>
</dependency>
<!-- only needed for forms -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>forms</artifactId>
<version>${itext.version}</version>
</dependency>
<!-- only needed for PDF/A -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>pdfa</artifactId>
<version>${itext.version}</version>
</dependency>
<!-- only needed for digital signatures -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>sign</artifactId>
<version>${itext.version}</version>
</dependency>
<!-- only needed for barcodes -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>barcodes</artifactId>
<version>${itext.version}</version>
</dependency>
<!-- only needed for Asian fonts -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>font-asian</artifactId>
<version>${itext.version}</version>
</dependency>
<!-- only needed for hyphenation -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>hyph</artifactId>
<version>${itext.version}</version>
</dependency>
</dependencies>

The source code of iText Java is publically available on GitHub: https://github.com/itext/itext-


java

4 Connecting iText with Laravel


iText is built on Java and we can't directly connect it to Laravel. There are multiple ways to
connect iText with Laravel. Those are:
1. Using Jar File
2. Using RESTful API
3. Using Java Bridge
4.1 Connecting Via Jar File
The shell_exec() function is an inbuilt function in PHP that is used to execute the commands via
shell and return the complete output as a string. We can use this shell_exec() to perform iText
functionality in Laravel.
First, we need to build a specific jar that does our required function.
Then execute the jar functionality using the shell_exec() function.
Code: shell_exec("java -jar iTextJar.jar");

4.2 Connecting Via RESTful APIs:

5 Adding an image to an existing file


To add an image to an existing PDF file using iTextPDF library in Java, you can follow the steps
below:

Step1: Open the existing PDF file using PdfReader.


Step2: Create a PdfStamper to modify the existing PDF.
Step3: Add the image to the PDF using Image class.
Step4: Set the position and size of the image on the page.
Step5: Close the PdfStamper and save the modified PDF.

Here's a sample Java code demonstrating how to achieve this:

import com.itextpdf.text.DocumentException;

import com.itextpdf.text.Image;

import com.itextpdf.text.pdf.PdfContentByte;

import com.itextpdf.text.pdf.PdfImage;

import com.itextpdf.text.pdf.PdfIndirectObject;

import com.itextpdf.text.pdf.PdfName;

import com.itextpdf.text.pdf.PdfReader;

import com.itextpdf.text.pdf.PdfStamper;import java.io.File;


import java.io.FileOutputStream;

import java.io.IOException;

public class PdfEditorClass {

public static final String SRC = "D:\\Task1.pdf";

public static final String DEST = "D:\\task2.pdf";

public static final String IMG = "D:\\as.PNG";

int newWidth = 200;

int newHeight = 100;

int absoluteX = 36;

int absoluteY = 200;

public static void main(String[] args) throws IOException, DocumentException {

File file = new File(DEST);

file.getParentFile().mkdirs();

new PdfEditorClass().manipulatePdf(SRC, DEST);

public void manipulatePdf(String src, String dest) throws IOException,


DocumentException {

PdfReader reader = new PdfReader(src);

PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));

Image image = Image.getInstance(IMG);

PdfImage stream = new PdfImage(image, "", null);

stream.put(new PdfName("ITXT_SpecialId"), new PdfName("123456789"));

PdfIndirectObject ref = stamper.getWriter().addToBody(stream);


image.setDirectReference(ref.getIndirectReference());

image.scaleAbsolute(newWidth, newHeight);

image.setAbsolutePosition(absoluteX, absoluteY);

int totalPages = reader.getNumberOfPages();

for (int i = 1; i <= totalPages; i++) {

PdfContentByte over = stamper.getOverContent(i);

over.addImage(image);

stamper.close();

reader.close();

You might also like