How to use PDFBox 1.0 in .net / C# environment usi

2019-07-31 01:47发布

问题:

Id like to use PDFBox to generate PDF highlight files in my .net project. PDFBox states that it can be used in .net via IKVM http://www.pdfbox.org/userguide/dot_net.html

BUT running ikvmc (latest version) to generate the DLLs on PDFBOX.1.0.0.jar generates a whole lot of NoClassDefFound warnings.

How should I fix this, and what other DLLs do I need to include in my project? It seems as though file names have changed from the older documentation/articles I have read on the matter.

thanks in advance.

回答1:

I just came across this question while trying to figure out for myself how to get PDFBox 1.0 working. It appears that the NoClassFound errors are being generated because it can't find the referenced jars (which are in the folder titled "external"). Here's how I was finally able to get it working:

  1. Use the binaries from IKVM 0.30.00
  2. Put the pdfbox-1.0.0 jar file in the "external" folder (easier to reference)
  3. In command line, execute lkvmc.exe -target:library -out:"[Output Path]\netpdfbox.dll" "[PDFBox Path]\*.jar" <-- the *.jar tells IKVM to run include all jar files as one large assembly

You should be able to include the outputted .dll in your C# code. I was able to execute the following code without any issues:

PDDocument doc = PDDocument.load("test.pdf");
string output = new PDFTextStripper().getText(doc);


回答2:

It depends on what version of PDFBox you are using. The latest releaesd version (0.7.3) uses IKVM 0.30.00. There are other versions of IKVM out there as well, and not all are backwards compatible. There is also another beta PDFBox that I believe uses a newer version of IKVM.

I'm pretty sure that the IKVM libraries that are required for each versioned release of PdfBox are in the release zip file. Be sure you use the one that comes with the release, and not the latest version of IKVM because they may not be compatible.