Skip to main content
Skip table of contents

pdfOffice + iText Core Annotations Example

Background:

With the addition of pdfOffice to the iText 7 Suite portfolio comes a wide range of potential synergies and new frontiers for your company's document workflow, thanks to its capabilities for accurate conversion of MS Office documents to PDF.

In this example, we will demonstrate how a potential client may utilize pdfOffice + iText Core in order to convert a .docx file into a PDF, then apply a comment as a PDF annotation. This is an especially relevant use-case for organizations dealing with contract work, invoicing, and potentially receipts.

Code Snippet:

In the following code snippet, we take the first three paragraphs from Lewis Carroll's classic novel Alice's Adventures in Wonderland (commonly known as simply Alice in Wonderland) in a .docx as input. After using pdfOffice to convert our Microsoft Word document into a PDF, we then utilize iText Core to apply a simple text annotation onto our favorite section of the reading.

JAVA:

JAVA

JAVA
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfString;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
import com.itextpdf.kernel.pdf.annot.PdfTextAnnotation;
import com.itextpdf.layout.Document;
import com.itextpdf.licensekey.LicenseKey;
import com.itextpdf.pdfoffice.OfficeConverter;

import java.io.*;

public class PdfOfficeAnnotationExample {

	public static String DEST = "output_annotated.pdf";
	public static String KEY = "pdfOffice_commercial_key.xml";

    public static void main(String args[]) throws IOException {
		
		LicenseKey.loadLicenseFile(KEY);

        ByteArrayOutputStream baos = new ByteArrayOutputStream(0);
		
        OfficeConverter.convertOfficeDocumentToPdf(new FileInputStream("Alice in Wonderland Excerpt.docx"), baos);

        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());

        PdfDocument pdfDoc = new PdfDocument(new PdfReader(bais), new PdfWriter(new FileOutputStream(DEST)));
        Document doc = new Document(pdfDoc);

        Rectangle rect = new Rectangle(80, 400, 0, 0);
        PdfAnnotation ann = new PdfTextAnnotation(rect);

        ann.setColor(ColorConstants.GREEN);
        ann.setContents("I love this part!");
        ann.setTitle(new PdfString("My favorite part."));

        pdfDoc.getFirstPage().addAnnotation(ann);

        doc.close();
        baos.close();
    }
}


Resources:

Right-click the link below and select "Save link as..." to download

pdfOffice_annotation.zip

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.