pdfbox is used to implement the pdf text extraction and merge function example
- 2020-10-31 21:44:04
- OfStack
Sometimes we need to do some processing on PDF files, extract text, merge, etc. We used to use the A-ES3en Text Extractor free tool, so why not write one yourself?
Now we can use the open source library PDFBox-0.7.3.
PDFBox-0.7.3.dll
IKVM.GNU.Classpath.dll
Create a new project. The code is simple:
public static string ParseToTxtStringUsingPDFBox(string filename){
PDDocument doc = PDDocument.load(filename);
PDFTextStripper stripper = new PDFTextStripper();
return stripper.getText(doc);
}
Get the textString and write it as a disk file, like this:
public static void WriteToTextFile(string str,string txtpath)
{
if (string.IsNullOrEmpty(txtpath))
throw new ArgumentNullException("Output file path should not be Null");
using (var txtWriter = new StreamWriter(txtpath))
{
txtWriter.Write(str);
txtWriter.Close();
}
}
You are free to do the rest. This library currently supports:
PDF to text extraction
Merge PDF Documents
PDF Document Encryption/Decryption
Lucene Search Engine Integration
Fill in form data FDF and XFDF
Create a PDF from a text file
Create images from PDF pages
Print a PDF