Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Thursday, June 10, 2010

Match Differences Between Two Strings

Hi friends,

Once i was asked to write an algorithm which matches the two strings and tell the difference between them.

In other words we have two sentences
1. Its raining cats and dogs since morning
2. Its raining monkey and dogs since morning.

than the algorithm should efficient enough to detect the difference between the two strings.
In the above example it should tell that there are two difference in the string.

i-e monkeys found at the place of cat.

In order to implement the algorithm i would recommend to use


Diff takes two texts and finds the differences. This implementation works on a character by character basis. The result of any diff may contain 'chaff', irrelevant small commonalities which complicate the output. A post-diff cleanup algorithm factors out these trivial commonalities.

Google Difference Match patch
It is an open source algorithm written in the Java, JavaScript, C++, C#, Lua and Python.
You can find it here
http://code.google.com/p/google-diff-match-patch/

The Diff Match and Patch libraries offer robust algorithms to perform the operations required for synchronizing plain text.

  1. Diff:
    • Compare two blocks of plain text and efficiently return a list of differences.
    • Diff Demo
  2. Match:
    • Given a search string, find its best fuzzy match in a block of plain text. Weighted for both accuracy and location.
    • Match Demo
  3. Patch:
    • Apply a list of patches onto plain text. Use best-effort to apply patch even when the underlying text doesn't match.
    • Patch Demo

Currently available in Java, JavaScript, C++, C#, Lua and Python. Regardless of language, each library features the same API and the same functionality. All versions also have comprehensive test harnesses.


Thanks
Vajahat Ali Niazi
Lahore Pakistan

Thursday, April 29, 2010

How to extract comments from pdf using Itext

Hi Friends,

Here is the code of extracting comments from the PDF file using the itext.This extracts the comments of each page one by one and print them on the console.


/******************************************************************************/
package pdftest;
import com.itextpdf.text.pdf.PdfArray;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfObject;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfString;
import java.util.ListIterator;

public class CommentsTest {

public static void main(String args[]) {
try {
System.out.println("Hi in the Comments Test");
PdfReader reader = new PdfReader("c:\\temp\\testattachments.pdf");

for (int i = 1; i <= reader.getNumberOfPages(); i++)
{

PdfDictionary page = reader.getPageN(i);
PdfArray annotsArray = null;

if(page.getAsArray(PdfName.ANNOTS)==null)
continue;

annotsArray = page.getAsArray(PdfName.ANNOTS);
for (ListIterator iter = annotsArray.listIterator(); iter.hasNext();)
{
PdfDictionary annot = (PdfDictionary) PdfReader.getPdfObject(iter.next());
PdfString content = (PdfString) PdfReader.getPdfObject(annot.get(PdfName.CONTENTS));
if (content != null) {
System.out.println(content);
}
}
}


} catch (Exception e) {
e.printStackTrace();
}

}
}

/******************************************************************************/


Regards,
Vajahat Ali

Wednesday, March 31, 2010

Remove Atatchments from PDF file using itext

Hi Friends:
Here is the code that will be used to unpack(remove) the attachments from the PDF using itext.


PdfReader reader = new PdfReader(filename);
PdfDictionary root = reader.getCatalog();
PdfDictionary documentnames = root.getAsDict(PdfName.NAMES);
PdfDictionary embeddedfiles = documentnames.getAsDict(PdfName.EMBEDDEDFILES);
PdfArray filespecs = embeddedfiles.getAsArray(PdfName.NAMES);
PdfDictionary filespec;
PdfDictionary refs;
FileOutputStream fos;
PRStream stream;
for (int i = 0; i < filespecs.size(); ) {
filespecs.getAsName(i++);
filespec = filespecs.getAsDict(i++);
refs = filespec.getAsDict(PdfName.EF);
for (PdfName key : refs.getKeys()) {
fos = new FileOutputStream(filespec.getAsString(key).toString());
stream = (PRStream) PdfReader.getPdfObject(refs.getAsIndirectObject(key));
fos.write(PdfReader.getStreamBytes(stream));
fos.flush();
fos.close();
}
}

Monday, August 3, 2009

Download Java2 Complete Refernece Book 5th Edition




Click here to download

The File has been uploaded for the sake of sharing the knowledge.
Don't Forget to inform your friends about this page.

Regards,
Vj Ali
SE
SInnovations

Tuesday, June 23, 2009

Using Urdu in Java

Other day someone asked me how to use Urdu fonts in Java. So for a sample java program that uses urdu font, you need to do following:

1. Download Nafees Nastaleeq Urdu font from CRULP.

2. Copy the font in C:\WINDOWS\Fonts folder

3. Create following java program. It should display "Usman" in Urdu.


import java.awt.*;
import javax.swing.*;

public class BasicDraw {
public static void main(String[] args) {
new BasicDraw();
}
BasicDraw() {
// Create a frame
JFrame frame = new JFrame();

// Add a component with a custom paint method
frame.getContentPane().add(new MyComponent());

// Display the frame
int frameWidth = 200;
int frameHeight = 200;
frame.setSize(frameWidth, frameHeight);
frame.setVisible(true);
}

class MyComponent extends JComponent {
// This method is called whenever the contents needs to be painted
public void paint(Graphics g) {
int style = Font.PLAIN;
int size = 24;
Font font = null;
font = new Font("Nafees Nastaleeq", style, size);
String text = "\u0639\u062b\u0645\u0627\u0646";
g.setFont(font);

// Draw a string such that its base line is at x, y
int x = 80;
int y = 40;
g.drawString(text, x, y);
}
}
}


4. The resulting image should look like:

Friday, May 29, 2009

Convert word,excel an power point to PDF using JAVA API

Today i will Let you guys now how to convert the word,powerpoint,excel formats to PDF.
Apache has a Java API, called POI, that allows you to access Microsoft formats from within Java. It looks handy, but I found some people complaining about the complexity of it. Some further searching turned up mention of something called JODConverter. This is a fantastic project.
JODConverter is essentially a Java library that utilizes OpenOffice’s conversion engine to convert to and from any format that OpenOffice supports. This includes Word, Excel, PowerPoint, RTF, Plain Text, etc.
I decided to see how easy it was going to be to use this library. Let’s face it, a lot of these open source projects claim to do all kinds of great things, but do not provide the greatest results. So, I created a small test project and wrote a simple class. Here’s what it looks like:
public class convertFile {
public static void main(String[] args) {
File inputFile = new File("resume-2.doc");
      File outputFile = new File("resume-2.pdf");
     OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
     try {
        connection.connect();
     } catch(Exception e) {
     }

DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
     converter.convert(inputFile, outputFile);

connection.disconnect();

}
}

Obviously, there’s error-checking that is lacking. For example, the convert() method should be wrapped to ensure that a failed conversion doesn’t blow up your application, and something should be done if an exception is caught on the connection. However, this is the basic skeleton. Pretty cool.
Basically, all this does is open a connection to OpenOffice, create a converter, and pass in the files to convert. JODConverter and OpenOffice handle everything else.
The only caveat to this is that OpenOffice must be installed on the server and must be running as a headless app (the JODConverter README file explains how to do this). While that may not be ideal, it’s probably worth it for the amazing results this library provides.
Great tool, highly recommended.

Regards,
Ali Vajahat
Software Engineer
Software Innovations
Lahore Pakistan