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>
Related
This question already has answers here:
non-static method toString() cannot be referenced from a static context
(5 answers)
Closed 5 years ago.
import com.google.zxing.WriterException;
import org.apache.commons.*;
import sun.misc.IOUtils;
import sun.nio.ch.IOUtil;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.*;
class HtmlWriter {
private final PrintWriter out;
private final File dir;
private int iota=1;
public HtmlWriter(File dir) throws IOException {
this.dir = dir;
this.out = new PrintWriter(new File(dir,"out.html"));
InputStream x = HtmlWriter.class.getResourceAsStream("/badges.html");
// out.println(IOUtils.toString(HtmlWriter.class.getResourceAsStream("/badges.html")));
System.out.print(IOUtils.toString(HtmlWriter.class.getResourceAsStream("/badges.html")));
}
public void add(String firstName, String lastName, String company, String email, String tel) throws IOException, WriterException {
VCardBuilder vc = new VCardBuilder();
vc.with("N",firstName+" "+lastName)
.with("ORG", company)
.with("TEL", tel)
.with("EMAIL", email);
System.out.println(vc.toString());
vc.writeQRCode(new File(dir,String.format("qr%04d.png",iota)));
out.printf(
"<div class='badge'>\n" +
" <img class='qrcode' src='qr%04d.png'>\n" +
" <div class='cblogo'></div>\n"+
" <div class='firstName'>%s</div>\n" +
" <div class='lastName'>%s</div>\n" +
" <div class='company'>%s</div>\n" +
"</div>\n", iota, firstName, lastName, company
);
iota++; }
public void close() {
out.println("</body></html>");
out.close();
}
}
You have imported sun.misc.IOUtils, but you probably wanted org.apache.commons.io.IOUtils. Apache IOUtils has the toString() method you want.
To use it in a maven project, include the following dependency:
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
Or download the jar-file from project homepage: https://commons.apache.org/proper/commons-io/download_io.cgi
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();
}
}
I took example from here. But my code below gives me "The method setOAuth2Credentials(GoogleCredential) is undefined for the type SpreadsheetService".
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.gdata.client.spreadsheet.SpreadsheetService;
import com.google.gdata.data.spreadsheet.SpreadsheetEntry;
import com.google.gdata.data.spreadsheet.SpreadsheetFeed;
import com.google.gdata.util.ServiceException;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.List;
public class Main{
public static void main(String[] args) throws MalformedURLException, GeneralSecurityException, IOException, ServiceException {
URL SPREADSHEET_FEED_URL;
SPREADSHEET_FEED_URL = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full");
File p12 = new File("./key.p12");
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
String[] SCOPESArray = {"https://spreadsheets.google.com/feeds", "https://spreadsheets.google.com/feeds/spreadsheets/private/full", "https://docs.google.com/feeds"};
final List SCOPES = Arrays.asList(SCOPESArray);
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId("cliend_ID")
.setServiceAccountScopes(SCOPES)
.setServiceAccountPrivateKeyFromP12File(p12)
.build();
SpreadsheetService service = new SpreadsheetService("Test");
service.setOAuth2Credentials(credential); /////// Error occurs here "The method setOAuth2Credentials(GoogleCredential) is undefined for the type SpreadsheetService"
SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL, SpreadsheetFeed.class);
List<SpreadsheetEntry> spreadsheets = feed.getEntries();
if (spreadsheets.size() == 0) {
System.out.println("No spreadsheets found.");
}
SpreadsheetEntry spreadsheet = null;
for (int i = 0; i < spreadsheets.size(); i++) {
if (spreadsheets.get(i).getTitle().getPlainText().startsWith("ListOfSandboxes")) {
spreadsheet = spreadsheets.get(i);
System.out.println("Name of editing spreadsheet: " + spreadsheets.get(i).getTitle().getPlainText());
System.out.println("ID of SpreadSheet: " + i);
}
}
}
}
I simply copied the code and imported the libraries as mentioned but I don't understand why I'm getting such error as below.
check that you use correct versions of dependencies:
<dependency>
<groupId>com.google.gdata</groupId>
<artifactId>core</artifactId>
<version>1.47.1</version>
</dependency>
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.20.0</version>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-jackson</artifactId>
<version>1.20.0</version>
</dependency>
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.
i'm using a maven project with following dependency :
<dependency>
<groupId>com.google.api.client</groupId>
<artifactId>google-api-client-googleapis-auth-clientlogin</artifactId>
<version>1.2.3-alpha</version>
</dependency>
when i run following code:
import java.io.IOException;
import com.google.api.client.googleapis.GoogleTransport;
import com.google.api.client.googleapis.auth.clientlogin.ClientLogin;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpResponseException;
import com.google.api.client.http.HttpTransport;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args ) throws IOException
{
HttpTransport transport = GoogleTransport.create();
// transport.addParser(new JsonCParser());
try {
// authenticate with ClientLogin
ClientLogin authenticator = new ClientLogin();
authenticator.authTokenType = "ndev";
authenticator.username = "....";
authenticator.password = "....";
authenticator.authenticate().setAuthorizationHeader(transport);
// make query request
HttpRequest request = transport.buildGetRequest();
request.setUrl("https://www.googleapis.com/bigquery/v1/query");
request.url.put(
"q", "select count(*) from [bigquery/samples/shakespeare];");
System.out.println(request.execute().parseAsString());
} catch (HttpResponseException e) {
System.err.println(e.response.parseAsString());
throw e;
}
}
}
i get below exception:
Exception in thread "main" java.lang.IllegalStateException: Missing required low-level HTTP transport package.
Use package "com.google.api.client.javanet".
at com.google.api.client.http.HttpTransport.useLowLevelHttpTransport(HttpTransport.java:129)
at com.google.api.client.http.HttpTransport.<init>(HttpTransport.java:187)
at com.google.api.client.googleapis.GoogleTransport.create(GoogleTransport.java:58)
at com.example.clientlogin.App.main(App.java:18)
what is the problem with GoogleTransport class?
Quick googeling resulted in maven for com.google.api.client.javanet.nethttpresponse Try adding
<dependency>
<groupId>com.google.api.client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.2.3-alpha</version>
</dependency>
or
<dependency>
<groupId>com.google.api.client</groupId>
<artifactId>google-api-client-javanet</artifactId>
<version>1.2.3-alpha</version>
</dependency>
to your POM file
this question is pretty old, but I've added some updates to our Java Google Client Lib + BigQuery samples (here: http://code.google.com/p/google-bigquery-tools/source/browse/samples/java/gettingstarted/BigQueryJavaGettingStarted/).