1

I'm working on removing Protected View from a series of PDFs, and am trying to use the iText library within VBA. My main issue at this point is that I have no idea what method to use, and the iText documentation is pretty dense.

I'm also feeling my way forward on calling the iText library from VBA, so any help on syntax to do this is also appreciated, though I'm sure I could get there myself if I knew which method to call...

Currently, I've got:

Dim program As WshExec
program = Shell("Java.exe -jar " & mypath & "\itext-5.5.6\itextpdf-5.5.6.jar")
'Debug.print program returns a value here, so this line works.

'I'm thinking I need something like:
'Set program = RunProgram("Java.exe -jar " & mypath & "\itext-5.5.6\itextpdf-5.5.6.jar", & _
methodName, param1)

I've been using the following questions to get me this far...

Calling Java library (JAR) from VBA/VBScript/Visual Basic Classic

Microsoft Excel Macro to run Java program

Desired functionality is to have an unprotected PDF sitting in a folder on mypath.

Community
  • 1
  • 1
nwhaught
  • 1,562
  • 1
  • 15
  • 36
  • Please clarify the following two points: (1.) What is protected view? Are you referring to encrypted PDFs? (2.) Why are you using the Java version of iText if you are working in VB? Are you sure you have a JVM? Wouldn't you rather use iTextSharp, which is the .NET port of iText? itextpdf-5.5.6.jar is **not** an *executable* jar, so it is unclear why you'd want to execute it from VBA: there's nothing to execute... – Bruno Lowagie May 12 '15 at 16:31

2 Answers2

4

The jar you are trying to run is not an executable jar. iText is a library that be used in a Java application by adding itextpdf-5.5.6.jar to the CLASSPATH. If you don't write any Java code, then the jar won't do a thing, hence your Shell() and your RunProgram() methods are useless: there is nothing to execute.

Moreover: from your question, it is far from certain that you have a Java environment on your machine. You are working in a VBA environment, which makes one wonder why you'd use the Java version of iText. Have you tried using iTextSharp, which is the .NET version of iText (written in C#)?

Take a look at this tutorial: Programmatically Complete PDF Form Fields using Visual Basic and the iTextSharp DLL

In this tutorial, we take an existing PDF, we fill out a form, and we get another PDF based on the original PDF, but with extra data. You can easily adapt the code so that it takes an existing PDF, doesn't add anything to the PDF, but saves the original PDF without its passwords, as is explained in my answer to How can I decrypt a PDF document with the owner password?

If you combine what you can learn from my Java code:

public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
    PdfReader.unethicalreading = true;
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    stamper.close();
    reader.close();
}

with what you learn from the form filling tutorial, you get something like this (provided that you use the iTextSharp DLL instead of the iText jar):

Dim pdfTemplate As String = "c:\Temp\PDF\encrypted.pdf"
Dim newFile As String = "c:\Temp\PDF\decrypted.pdf"
PdfReader.unethicalreading = true
Dim pdfReader As New PdfReader(pdfTemplate)
Dim pdfStamper As New PdfStamper(pdfReader, New FileStream(
    newFile, FileMode.Create))
pdfStamper.Close()
pdfReader.Close()

IMPORTANT: this will only remove the password if the file is only protected with an owner password (which is what I assume when you talk about protected view). If the file is protected in any other way, you'll have to clarify. Also note that the parameter unethicalreading is not without meaning: make sure that you're not doing unethical by removing the protection.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Thank you Bruno--I ran across another of your posts while researching and was beginning to head down the path you recommended here: http://support.itextpdf.com/node/52 (with the .NET version). By "Protected View" I mean the sandboxed view with most of the functionality disabled (similar to protected view on an Office document). The PDFs I'm working with come from emails and open in this Protected View by default. There are no passwords involved. – nwhaught May 12 '15 at 16:50
  • I'm pretty sure there *is* a password involved: the files are probably protected by an owner password. – Bruno Lowagie May 12 '15 at 17:09
0

I was having to manipulate protected PDF files using iText. I just put in my pom.xml the following dependency and nothing more.

<!-- https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk15on -->
<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcprov-jdk15on</artifactId>
    <version>1.59</version>
</dependency>
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
  • You might want to check whether the BouncyCastle version you use is the version your itext version was built against. BouncyCastle unfortunately is known for a very wild release policy, they change their API in seemingly minor version updates resulting in compatibility issues. You can check this by inspecting the pom file of your itext release. – mkl May 24 '18 at 05:56
  • Yes I have tested before. This is the last version of this package. – Cristiano Firmino Rodrigues May 25 '18 at 16:01
  • The last version is not necessarily the right one. Bouncycastle has had some really bad versioning ideas. – mkl May 25 '18 at 19:32