-2

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;
    }
}
Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44

3 Answers3

2

The StringComparison enum is described in more detail here.

The short answer is that there is, unfortunately, no suitable type in the java libraries.

The easy solution

Create your own Java enum mirroring the c#. You also have to create your own string comparison method taking the StringComparison into account, e.g. ignoring case, etc, depending on the value of the StringComparison.

The best solution

I would avoid using the StringComparison in the interface of a method. Instead search for usages of the method. I'm guessing it is only used to sometimes ignore case and others not. Or that it is completely unused. For the later case - Simply remove it and you're done! For the former case just pass in a bool to the interface instead! Remember to update the c# code to keep the ports somewhat in sync.

vidstige
  • 12,492
  • 9
  • 66
  • 110
1

If you have one string:

String myString = "somestring";

And another one:

String anotherString = "somestringelse";

You can do use the built in equals() function like this:

if(myString.equals(anotherString)) {
 //Do code
}
PKlumpp
  • 4,913
  • 8
  • 36
  • 64
Shawnic Hedgehog
  • 2,353
  • 1
  • 15
  • 20
0

You can use basic Java string comparison methods

  1. If you wanna compare two strings completely I mean as a whole string

String string1="abcd", string2="abcd";

if(string1.equals(string2)) ----> returns true as they are equal else it returns false.

  1. If you wanna compare two strings completely ignoring their cases you can use the following method

String string1="abcd", string2="AbCd";

if(string1.equalsIgnorecase(string2)) -- > returns true as they are equal though their cases are different else it returns false.

If you don't wanna compare whole strings you can use following methods

check the following link for all the string comparison methods in Java

http://docs.oracle.com/javase/tutorial/java/data/comparestrings.html

Sid
  • 691
  • 6
  • 9