I am using this sample PDFBox code to encrypt and disable printing of a pdf file. Encryption happens successfully, but printing is not disabled.
What could be the issue?
Here's the dependencies section of my pom.xml
<dependencies>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15</artifactId>
<version>1.46</version>
</dependency>
</dependencies>
and below is the source code
import java.io.File;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.encryption.AccessPermission;
import org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy;
public class Test {
public static void main(String[] args) throws Exception {
PDDocument doc = PDDocument.load(new File("/tmp/Test.pdf"));
int keyLength = 128;
AccessPermission ap = new AccessPermission();
ap.setCanPrint(false);
StandardProtectionPolicy spp = new StandardProtectionPolicy("Admin", "Password", ap);
spp.setEncryptionKeyLength(keyLength);
spp.setPermissions(ap);
doc.protect(spp);
doc.save("/tmp/Test-Encrypted.pdf");
doc.close();
}
}
Related
I am getting the below error while trying to use the bouncy castle jars in my project.
java.lang.SecurityException: class "org.bouncycastle.asn1.ASN1Integer"'s signer information does not match signer information of other classes in the same package
These are the dependencies -
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>cloudfront</artifactId>
<version>2.19.15</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk18on</artifactId>
<version>1.72</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk18on</artifactId>
<version>1.72</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>regions</artifactId>
<version>2.19.15</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>ssm</artifactId>
<version>2.19.15</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>url-connection-client</artifactId>
<version>2.19.15</version>
</dependency>
</dependencies>
This is the part of code where I am using this-
import java.io.StringReader;
import java.security.KeyPair;
import java.security.PrivateKey;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
public class MyUtil {
public static PrivateKey convertMyPemStringToADerPrivateKey(String privKey) throws Exception {
PrivateKey derPrivateKey = null;
StringReader strReader = null;
PEMParser pemParser = null;
try {
//Converting string to reader
strReader = new StringReader(privKey);
//passing reader to PEMParser from Bouncycastle
pemParser = new PEMParser(strReader);
JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
KeyPair keyPair = converter.getKeyPair((PEMKeyPair)pemParser.readObject());
// Export DER encoded PKCS#8 private key
derPrivateKey = keyPair.getPrivate();
}catch (Exception exception) {
throw exception;
}finally {
if(null != strReader) {
strReader.close();
}
if(null != pemParser) {
pemParser.close();
}
}
return derPrivateKey;
}
What I am trying to do is convert a RSA private key String object which is in PKCS#1 to DER format, but getting the above exception, can someone please help me on this?
I was expecting this to work and convert the PEM key to DER format but getting the above exception.
I am trying to run a distcp command on my hadoop cluster using the Hadoop Java Library to move content from the HDFS to a Google Cloud Bucket. I am getting the error NoClassDefFoundError: Could not initialize class com.google.cloud.hadoop.fs.gcs.GoogleHadoopFileSystem
Below is my java code:
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.tools.DistCp;
import org.apache.hadoop.tools.DistCpOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class HadoopHelper {
private static Logger logger = LoggerFactory.getLogger(HadoopHelper.class);
private static final String FS_DEFAULT_FS = "fs.defaultFS";
private final Configuration conf;
public HadoopHelper(String hadoopUrl) {
conf = new Configuration();
conf.set(FS_DEFAULT_FS, "hdfs://" + hadoopUrl);
}
public void distCP(JsonArray files, String target) {
try {
List<Path> srcPaths = new ArrayList<>();
for (JsonElement file : files) {
String srcPath = file.getAsString();
srcPaths.add(new Path(srcPath));
}
DistCpOptions options = new DistCpOptions.Builder(
srcPaths,
new Path("gs://" + target)
).build();
logger.info("Using distcp to copy {} to gs://{}", files, target);
this.conf.set("fs.gs.impl", "com.google.cloud.hadoop.fs.gcs.GoogleHadoopFileSystem");
this.conf.set("fs.gs.auth.service.account.email", "my-svc-account#my-gcp-project.iam.gserviceaccount.com");
this.conf.set("fs.gs.auth.service.account.keyfile", "config/my-svc-account-keyfile.p12");
this.conf.set("fs.gs.project.id", "my-gcp-project");
DistCp distCp = new DistCp(this.conf, options);
Job job = distCp.execute();
job.waitForCompletion(true);
logger.info("Distcp operation success. Exiting");
} catch (Exception e) {
logger.error("Error while trying to execute distcp", e);
logger.error("Distcp operation failed. Exiting");
throw new IllegalArgumentException("Distcp failed");
}
}
public void createDirectory() throws IOException {
FileSystem fileSystem = FileSystem.get(this.conf);
fileSystem.mkdirs(new Path("/user/newfolder"));
logger.info("Done");
}
}
I have added the below dependencies in the pom.xml:
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-distcp</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>com.google.cloud.bigdataoss</groupId>
<artifactId>gcs-connector</artifactId>
<version>hadoop3-2.2.4</version>
</dependency>
<dependency>
<groupId>com.google.cloud.bigdataoss</groupId>
<artifactId>util</artifactId>
<version>2.2.4</version>
</dependency>
If I run the distcp command on the cluster itself like so: hadoop distcp /user gs://my_bucket_name/
The distcp operation works and the content is copied onto the Cloud Bucket.
Did you add the jar to the hadoop's classpath?
Add the connector jar to Hadoop's classpath
Placing the connector jar in the HADOOP_COMMON_LIB_JARS_DIR directory should be sufficient to have Hadoop load the jar. Alternatively, to be certain that the jar is loaded, you can add HADOOP_CLASSPATH=$HADOOP_CLASSPATH:</path/to/gcs-connector.jar> to hadoop-env.sh in the Hadoop configuration directory.
This needs to be done to DisctCp conf(in your code this.conf) before this line of code:
this.conf.set("HADOOP_CLASSPATH","$HADOOP_CLASSPATH:/tmp/gcs-connector-latest-hadoop2.jar")
DistCp distCp = new DistCp(this.conf, options);
If it helps there is a troubleshooting section for this.
I have a PDF form which is created by LiberOffice Draw 4.1.0.4.
The form contains text field, check box and radio button
After I set value to the fields and flatten the form with PDFBox(2.0.21), the field value cannot be displayed
I think it may be the problem of the appearance of the field's annotation but I have no idea how to make it right
Test PDF
import org.apache.commons.lang3.StringUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDField;
import org.apache.pdfbox.pdmodel.interactive.form.PDTextField;
import java.io.File;
public class PdfGenerationTest {
public static void main( String[] args ) throws Exception{
testFillValue();
}
public static void testFillValue() throws Exception{
PDDocument pdDocument = PDDocument.load(new File("C:\\temp\\test.pdf"));
PDAcroForm acroForm = pdDocument.getDocumentCatalog().getAcroForm();
String defaultAppearance = "/Helv 12 Tf 0 0 1 rg";
for(PDField field : acroForm.getFields()) {
if (field instanceof PDTextField) {
PDTextField textField = (PDTextField) field;
textField.setActions(null);
textField.setDefaultAppearance(defaultAppearance);
}
}
setFormValue(acroForm, "TextBox", "testvalue");
setFormValue(acroForm, "radioBtn", "Yes");
setFormValue(acroForm, "chkBox", "Yes");
acroForm.refreshAppearances();
acroForm.flatten();
pdDocument.save("C:\\temp\\test_filled.pdf");
pdDocument.close();
}
private static void setFormValue(PDAcroForm acroForm, String key, String value) throws Exception {
PDField f = acroForm.getField(key);
if (f != null) {
if (value != null && StringUtils.isNotEmpty(value.trim())) {
f.setValue(value);
}
}
}
}
PDAcroForm.flaten is work for me after using pdfbox 2.0.24
<dependencies>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.24</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>fontbox</artifactId>
<version>2.0.24</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox-examples</artifactId>
<version>2.0.24</version>
</dependency>
</dependencies>
I don't know why this code is wrong. I am using a code from itext, but is giving error evin with all dependencies imported. Below is the codes that I am using in the project. Plase help me, someone.
https://developers.itextpdf.com/examples/security/digital-signatures-white-paper/digital-signatures-chapter-5
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.Security;
import java.util.ArrayList;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import com.itextpdf.text.log.LoggerFactory;
import com.itextpdf.text.log.SysoLogger;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.security.PdfPKCS7;
public class PdfReaderExample {
public static final String EXAMPLE1 = "/opt/doc.pdf";
public PdfPKCS7 verifySignature(AcroFields fields, String name) throws GeneralSecurityException, IOException {
System.out.println("Signature covers whole document: " + fields.signatureCoversWholeDocument(name));
System.out.println("Document revision: " + fields.getRevision(name) + " of " + fields.getTotalRevisions());
PdfPKCS7 pkcs7 = fields.verifySignature(name);
System.out.println("Integrity check OK? " + pkcs7.verify());
return pkcs7;
}
public void verifySignatures(String path) throws IOException, GeneralSecurityException {
System.out.println(path);
PdfReader reader = new PdfReader(path);
AcroFields fields = reader.getAcroFields();
ArrayList<String> names = fields.getSignatureNames();
for (String name : names) {
System.out.println("===== " + name + " =====");
verifySignature(fields, name);
}
System.out.println();
}
public static void main(String[] args) throws IOException, GeneralSecurityException {
LoggerFactory.getInstance().setLogger(new SysoLogger());
BouncyCastleProvider provider = new BouncyCastleProvider();
Security.addProvider(provider);
PdfReaderExample app = new PdfReaderExample();
app.verifySignatures(EXAMPLE1);
}
}
===== Signature2 =====
Signature covers whole document: true
Document revision: 1 of 1
Exception in thread "main" java.lang.VerifyError: (class: org/bouncycastle/cms/CMSSignedHelper, method: <clinit> signature: ()V) Incompatible argument to function
at org.bouncycastle.cms.CMSSignedData.<clinit>(Unknown Source)
at org.bouncycastle.tsp.TimeStampToken.getSignedData(Unknown Source)
at org.bouncycastle.tsp.TimeStampToken.<init>(Unknown Source)
at com.itextpdf.text.pdf.security.PdfPKCS7.<init>(PdfPKCS7.java:402)
at com.itextpdf.text.pdf.AcroFields.verifySignature(AcroFields.java:2419)
at com.itextpdf.text.pdf.AcroFields.verifySignature(AcroFields.java:2372)
at PdfReaderExample.verifySignature(PdfReaderExample.java:20)
at PdfReaderExample.verifySignatures(PdfReaderExample.java:32)
at PdfReaderExample.main(PdfReaderExample.java:42)
Process finished with exit code 1
File POM is this.
<dependencies>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.12</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.58</version>
</dependency>
</dependencies>
I have a tibetan pdf file, and I want to extract its content. But I tried following three codes to read the file, I got code that isn't what I wanted.
code1:
import java.io.IOException;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
public class iTextReadDemo {
public static void main(String[] args) {
try {
PdfReader reader = new PdfReader("");
String page = PdfTextExtractor.getTextFromPage(reader, 1);
System.out.println("Page Content:\n\n" + page + "\n\n");
} catch (IOException e) {
e.printStackTrace();
}
}
}// - See more at:
// http://www.quicklyjava.com/read-pdf-file-in-java-using-itext/#sthash.iAhF00Kj.dpuf
code2 :
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
PdfReader reader = new PdfReader("");
byte[] bs = new byte[100];
byte[] streamBytes = reader.getPageContent(1);
for(byte b: streamBytes){
System.out.print((char)b);
}
}
}
code3:
package pdfBox;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;
public class PDFTest {
public static void main(String[] args) throws Exception {
PDDocument pd;
File input = new File("C:\\Users\\Administrator\\Desktop\\tibetan Dictionary pdf/藏英英藏词典 - 副本.pdf");
pd = PDDocument.load(input);
PDFTextStripper reader = new PDFTextStripper("utf-8");
String pageText = reader.getText(pd);
System.out.println(pageText);
}
}
and this is the part of the maven pom dependency
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.3</version>
</dependency>
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>4.2.1</version>
</dependency>
<dependency>
<groupId>org.swinglabs</groupId>
<artifactId>pdf-renderer</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>1.8.7</version>
</dependency>
what is wrong ?
is he said right?
https://answers.acrobatusers.com/Can-I-convert-PDF-Word-Doc-Tibetan-script-addition-English-language-q219757.aspx
The quality of exported content from a PDF is directly related to the quality of the PDF's "build" (what is under the hood, not what you "see"). Poor quality export indicates a poorly built PDF. Nothing you can do other that ask the originator of the PDF to do a better job.