package com.qoppa.sample; import java.awt.Color; import java.awt.geom.Rectangle2D; import java.util.List; import java.util.Vector; import com.qoppa.pdf.PDFException; import com.qoppa.pdf.actions.URLAction; import com.qoppa.pdf.annotations.Annotation; import com.qoppa.pdf.annotations.Link; import com.qoppa.pdfProcess.PDFDocument; import com.qoppa.pdfProcess.PDFPage; import com.qoppa.pdfProcess.content.FillColorSpan; import com.qoppa.pdfProcess.content.LineSpan; import com.qoppa.pdfProcess.content.TextContent; /** * This sample shows how to get the TextContent object and group text into spans by text fill color and as lines of text in the page. * It shows how to find text close to a specified color and change its color. * It also shows how to search for a string of text within a Span and change the color of a subset of that span. * */ public class ChangePDFTextColor { public static void main(String[] args) throws Exception { PDFDocument doc = new PDFDocument("C:/test/TestFile.pdf", null); for (int i = 0; i < doc.getPageCount(); i++) { PDFPage page = doc.getPage(i); TextContent textContent = page.getTextContent(); // Group text into spans by text fill color List spans = textContent.getSpans(FillColorSpan.class); for (FillColorSpan span : spans) { // look for spans of text with a fill color close to blue if (colorMatch(span.getColor(), 0, 10, 0, 10, 245, 255)) { // change color of entire span span.setColor(Color.green); // if a link doesn't already exist, add one if (!linkExists(page, span.getBounds())) { addLink(page, span.getBounds()); } } } // Group text into spans that contain a single line of text (the text may or may not have any attributes in common) List lineSpans = textContent.getSpans(LineSpan.class); for (LineSpan span : lineSpans) { String searchText = "Qoppa"; // find a string of text within the span int startIndex = span.getText().indexOf(searchText); if (startIndex > -1) { // change color of a subset of the span (where text was found) int endIndex = startIndex + searchText.length(); span.setColor(Color.red, startIndex, endIndex); } } } } /** * Returns true if the given Color's RGB components are within the given bounds. * @param color The Color to check for a match. * @param redMin The minimum acceptable value for the red component (inclusive). * @param redMax The maximum acceptable value for the red component (inclusive). * @param greenMin The minimum acceptable value for the green component(inclusive); * @param greenMax The maximum acceptable value for the green component (inclusive). * @param blueMin The minimum acceptable value for the blue component(inclusive). * @param blueMax The maximum acceptable value for the blue component (inclusive). * @return true if the given Color's RGB components are within the given bounds. */ public static boolean colorMatch(Color color, int redMin, int redMax, int greenMin, int greenMax, int blueMin, int blueMax) { return color.getRed() >= redMin && color.getRed() <= redMax && color.getGreen() >= greenMin && color.getGreen() <= greenMax && color.getBlue() >= blueMin && color.getBlue() <= blueMax; } }