How to render password-protected PDF - pdf-generation

I am using Flying Saucer to generate PDF from XHTML. Rendering works nice, but any attempt to password-protect the PDF document fails with an Exception.
I am using code recommended by Ajit Soman in https://stackoverflow.com/a/46024498/4517737. A copy of code is shown below.
If you un-comment the commented line, Exception is thrown in line "renderer.createPDF(os, false)".
The Exception is
"Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException"
(for JavaFX application).
For plain Java, the Exception is "Exception in thread "main" java.lang.NoClassDefFoundError: org/bouncycastle/asn1/ASN1Encodable
at com.itextpdf.text.pdf.PdfEncryption.(PdfEncryption.java:147).....
Caused by: java.lang.ClassNotFoundException:org.bouncycastle.asn1.ASN1Encodable"
final File outputFile = File.createTempFile(fileName, ".pdf");
FileOutputStream os = new FileOutputStream(outputFile);
PDFEncryption pdfEncryption = new PDFEncryption();
String password= "password#123";
pdfEncryption.setUserPassword(password.getBytes());
ITextRenderer renderer = new ITextRenderer();
// the following line causes Exception
// renderer.setPDFEncryption(pdfEncryption);
renderer.setDocumentFromString(htmlContent);
renderer.layout();
renderer.createPDF(os, false);
renderer.finishPDF();
Please help.

Related

docx4j SerializationHandler classnotfoundexception

I am using docx4j to run through my document and extract the text. I am using the code below to check if I got the texts right
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(tempName));
String XPATH_TO_SELECT_TEXT_NODES = "//w:t";
List<Object> texts;
texts = wordMLPackage.getMainDocumentPart().getJAXBNodesViaXPath(XPATH_TO_SELECT_TEXT_NODES,true);
for (Object obj : texts) {
Text text = (Text) ((JAXBElement) obj).getValue();
System.out.println("line "+ i+": "+text.getValue());
i++;
}
However, I am getting an error below
Caused by: java.lang.ClassNotFoundException: org.apache.xml.serializer.SerializationHandler.
I already imported the important dependencies for this to work:
dependency 'org.docx4j:docx4j:6.1.2'
Thank you! Hope you can help me.

Bufferedreader and InputStreamReader

I am having trouble getting my java program to read any text files.
public static void main(String[] args) throws java.io.IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
try {
long base = Long.parseLong(args[0]);
String input = br.readLine(); //read first line till the end of file
long list = Long.parseLong(input);
convertBase(base, list);
}
finally {
br.close();
}
}
The program works when I manually type the values into the command line, but when I try to use a text file it throws exceptions:
Exception in thread "main" java.lang.NumberFormatException: For input string: "baseconverion.txt"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:589)
at java.lang.Long.parseLong(Long.java:631)
at FromDecimal.main(FromDecimal.java:46)
Not sure what I am doing wrong/missing.
when I try to use a text file
You mean 'when I enter a filename instead of a number'. You aren't 'try[ing]' to use a text file' at all. All you're doing is entering a filename at the console. There is nothing here that opens or reads a text file whatsoever. Java is not magic: it won't magically realize that's a filename and magically open it for you.
You need to read about file I/O in the Java Tutorial and the Javadoc.
You also shouldn't be using Long.parseLong() if your objective is to convert decimal into another base. You need to pass the original line to your conversion method.

FlattenFields is not working as expected

We have been working on a POC with IText7 and getting an error when we try to FlattenFields. All we are trying to do is load a pdf template and inject values. The template which we are using was working fine with IText5.
Here is the exception message:
An exception of type 'iText.Kernel.PdfException' occurred in itext.kernel.dll but was not handled in user code
Additional information: unbalanced.begin.end.marked.content.operators
using (PdfDocument pdfDoc = new PdfDocument(new PdfReader(fileName), new PdfWriter(outputStream)))
{
PdfAcroForm stamper = PdfAcroForm.GetAcroForm(pdfDoc, true);
stamper.FlattenFields();
stamper.SetGenerateAppearance(true);
}
Regards
Shreenidhi B.R
Followed up with IText support folks and they said that this issue has been fixed in IText 7.0.1. I haven't had a chance to test it myself though.

Conversion exceptions while using docx4j (From Docx to PDF)

I would like to know why this code:
String inputfilepath = "D:\\DFADFADSF";
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath + ".docx"));
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
wordMLPackage.setFontMapper(new IdentityPlusMapper());
FOSettings foSettings = Docx4J.createFOSettings();
foSettings.setWmlPackage(wordMLPackage);
String outputfilepath = "D:\\OUT_FontContent.pdf";
OutputStream os = new java.io.FileOutputStream(outputfilepath);
Docx4J.toPDF(wordMLPackage,os);
Throws this exception:
org.docx4j.openpackaging.exceptions.Docx4JException: Exception exporting package
org.docx4j.openpackaging.exceptions.Docx4JException: Exception executing transformer: org.apache.fop.fo.ValidationException: "fo:flow" is missing child elements. Required content model: marker* (%block;)+
Although there are similar posts, I haven't seen one about this exception...
Maybe I should add aditional code to configure the conversion...

JavaCV capture a frame using FFmpeg

I create a class that capture frame from a video. When it capture a frame, it's saved as a picture. When the video is .avi, application works ok. When format is .avi.
public static void main(String[] args) {
FFmpegFrameGrabber grabber = new FFmpegFrameGrabber("C:/Users/Ioanna/Desktop/video1.avi");
try {
IplImage img;
//Start grabber to capture video
grabber.start();
//grab video frame to IplImage
img = grabber.grab();
if (img != null) {
//save video frame as a picture
cvSaveImage("capture.jpg", img);
}
}catch (Exception e) {
}
}
The error is
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.googlecode.javacv.FFmpegFrameGrabber.<init>(FFmpegFrameGrabber.java:106)
at Video.main(Video.java:75)
Caused by: java.lang.IllegalStateException: Can't overwrite cause with java.lang.UnsatisfiedLinkError: no avcodec in java.library.path
at java.lang.Throwable.initCause(Throwable.java:457)
at com.googlecode.javacpp.Loader.load(Loader.java:581)
at com.googlecode.javacpp.Loader.load(Loader.java:532)
at com.googlecode.javacv.cpp.avcodec.<clinit>(avcodec.java:39)
... 2 more
Caused by: java.lang.UnsatisfiedLinkError: no jniavcodec in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1837)
Do anyone know what is the problem?
Thanks in advance
After searching the web for quite some time, I came to the following solution:
Step 1: Download the .zip file "javacv-0.6-cppjars.zip" from
https://code.google.com/p/javacv/downloads/list and unzip it.
Step 2: Add "ffmpeg-20130915-git-7ac6c63-windows-x86_64.jar" file to your Java project!

Resources