Wednesday, October 22, 2014

Java - Get IndexOf String of an Exact Word

Been a while since I posted but I thought this would be a good little piece of code to give you the index of an exact word in a large string. I found the need in this because I was parsing command-line output.

Pattern pattern = Pattern.compile("\\bbe\\b");
Matcher matcher = pattern.matcher("Today is a good day because I will be going to the park.");

if(!matcher.find())
{
    return -1;
}
int index = matcher.start();

The result will ensure not to give you the index of 'be' in because and actually give you the exact 'be' in the String. If not found, return -1. Hope this helps!

Thursday, February 20, 2014

PDF to SVG - Java

I have been working with SVG formats recently. Mainly, trying to find a decent swing component for creating and modifying SVGs. As a result of such escapade, I decided to write a PDF to SVG converter. Now, I haven't fully tested it, but with the PDFs I have tried, it converted successfully.

The libraries I used were Batik 1.7 and PDF-Renderer v1.0.5

Here is My Main

public class ConversionMain
{
    public ConversionMain()
    {
        File pdf = new File("ss12.pdf");
        PDFUtility.PDF_TO_SVG(pdf);
    }

    public static void main(String[] args)
    {
        new ConversionMain();
    }
}

Here is my PDFUtility function for taking PDF, reading it and then converting to SVG

public class PDFUtility
{
    public static void PDF_TO_SVG(File pdfFile)
    {
        try
        {
            RandomAccessFile raf = new RandomAccessFile(pdfFile, "r");
            FileChannel channel = raf.getChannel();
            ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
            PDFFile pdf = new PDFFile(buf);



            DOMImplementation domImp = GenericDOMImplementation.getDOMImplementation();
            for (int i = 1; i <= pdf.getNumPages(); i++)
            {
                PDFPage page = pdf.getPage(i);
                // image dimensions
                int width = (int) page.getWidth();  //Can be changed to whatever you like
                int height = (int) page.getHeight();

                Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight());
                Image image = page.getImage(width, height, rect, null, true, true);

                String svgNS = "http://www.w3.org/2000/svg";
                Document document = domImp.createDocument(svgNS, "svg", null);

                SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
                Graphics graphics = svgGenerator.create();
                graphics.drawImage(image, 0, 0, null);

                boolean userCSS = true;
                FileOutputStream fos = new FileOutputStream(new File("Page " + i + ".svg"));

                Writer out = new OutputStreamWriter(fos);

                svgGenerator.stream(out, userCSS);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }
}

Try it out and let me know what you think

Thanks!