We developed a PDF reader desktop app using iTextSharp
and we are now developing an android app. This is my C# code I want to know what can be use for StringComparison
in Java
public final void PDFReferenceGetter(String pSearch, StringComparison SC, String sourceFile, String destinationFile)
{
//
}
the full code
public final void PDFReferenceGetter(String pSearch, String SC, String sourceFile, String destinationFile)
{
PdfStamper stamper = null;
PdfContentByte contentByte;
Rectangle refRectangle = null;
int refPage = 0;
//this.Cursor = Cursors.WaitCursor;
if ((new java.io.File(sourceFile)).isFile())
{
PdfReader pReader = new PdfReader(sourceFile);
stamper = new PdfStamper(pReader, new FileOutputStream(destinationFile));
for (int page = 1; page <= pReader.getNumberOfPages(); page++)
{
LocationTextExtractionStrategy strategy = new LocationTextExtractionStrategy();
contentByte = stamper.getUnderContent(page);
//Send some data contained in PdfContentByte, looks like the first is always cero for me and the second 100, but i'm not sure if this could change in some cases
strategy._UndercontentCharacterSpacing = contentByte.getCharacterSpacing();
strategy._UndercontentHorizontalScaling = contentByte.getHorizontalScaling();
//It's not really needed to get the text back, but we have to call this line ALWAYS,
//because it triggers the process that will get all chunks from PDF into our strategy Object
String currentText = PdfTextExtractor.getTextFromPage(pReader, page, strategy);
//The real getter process starts in the following line
java.util.ArrayList<Rectangle> matchesFound = strategy.GetTextLocations("References", SC);
//Set the fill color of the shapes, I don't use a border because it would make the rect bigger
//but maybe using a thin border could be a solution if you see the currect rect is not big enough to cover all the text it should cover
contentByte.setColorFill(BaseColor.PINK);
//MatchesFound contains all text with locations, so do whatever you want with it, this highlights them using PINK color:s
for (Rectangle rect : matchesFound)
{
refRectangle = rect;
refPage = page;
}
contentByte.fill();
}
for (int page = 1; page <= pReader.getNumberOfPages(); page++)
{
LocationTextExtractionStrategy strategy = new LocationTextExtractionStrategy();
contentByte = stamper.getUnderContent(page);
//Send some data contained in PdfContentByte, looks like the first is always cero for me and the second 100, but i'm not sure if this could change in some cases
strategy._UndercontentCharacterSpacing = contentByte.getCharacterSpacing();
strategy._UndercontentHorizontalScaling = contentByte.getHorizontalScaling();
//It's not really needed to get the text back, but we have to call this line ALWAYS,
//because it triggers the process that will get all chunks from PDF into our strategy Object
String currentText = PdfTextExtractor.getTextFromPage(pReader, page, strategy);
String text = currentText;
String patternString = pSearch;
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(text);
boolean matches = matcher.matches();
if(matches == true)
{
ArrayList<String> mc;
mc.add(text);
//MatchCollection mc = Regex.Matches(currentText, pSearch);
java.util.ArrayList<Rectangle> matchesFound = new java.util.ArrayList<Rectangle>();
for (String m : mc)
{
matchesFound = strategy.getTextLocations(m.toString(), SC);
for (Rectangle rect : matchesFound)
{
contentByte.rectangle(rect.getLeft(), rect.getBottom(), rect.getWidth(), rect.getHeight());
PdfDestination pdfdest = new PdfDestination(PdfDestination.XYZ, refRectangle.LEFT, refRectangle.TOP, 0);
PdfAnnotation annot = PdfAnnotation.createLink(stamper.getWriter(), rect, PdfAnnotation.HIGHLIGHT_INVERT, refPage, pdfdest);
stamper.addAnnotation(annot, page);
}
}
//The real getter process starts in the following line
//Set the fill color of the shapes, I don't use a border because it would make the rect bigger
//but maybe using a thin border could be a solution if you see the currect rect is not big enough to cover all the text it should cover
contentByte.setColorFill(BaseColor.LIGHT_GRAY);
//MatchesFound contains all text with locations, so do whatever you want with it, this highlights them using PINK color:
contentByte.fill();
}
stamper.close();
pReader.close();
}
//this.Cursor = Cursors.Default;
}
}