Flatten signatures in pdf with PDFBOX java - java

I want to flatten a pdf with signatures from a form but I am using this code and when I generate the final pdf I can still delete the signature. What I want is that when I generate the final pdf, I cannot delete anything at all from the pdf
private static void flattenPDF(String src, String dst) throws IOException {
PDDocument doc = null;
try {
doc = PDDocument.load(new File(src));
} catch (IOException e) {
System.out.println("Exception: " + e.getMessage());
}
PDDocumentCatalog catalog = doc.getDocumentCatalog();
PDAcroForm acroForm = catalog.getAcroForm();
if(acroForm == null) acroForm = new PDAcroForm(PDDocument.load(new File(src)));
PDResources resources = new PDResources();
List<PDField> fields = new ArrayList<>(acroForm.getFields());
processFields(fields, resources);
acroForm.setDefaultResources(resources);
try {
acroForm.flatten();
doc.save(dst);
doc.close();
} catch (IOException e) {
System.out.println("Exception: " + e.getMessage());
}
}
private static void processFields(List<PDField> fields, PDResources resources) {
fields.stream().forEach(f -> {
f.setReadOnly(true);
COSDictionary cosObject = f.getCOSObject();
String value = cosObject.getString(COSName.DV) == null ?
cosObject.getString(COSName.V) : cosObject.getString(COSName.DV);
System.out.println("Setting " + f.getFullyQualifiedName() + ": " + value);
try {
f.setValue(value);
} catch (IOException e) {
if (e.getMessage().matches("Could not find font: /.*")) {
String fontName = e.getMessage().replaceAll("^[^/]*/", "");
System.out.println("Adding fallback font for: " + fontName);
resources.put(COSName.getPDFName(fontName), PDType1Font.HELVETICA);
try {
f.setValue(value);
} catch (IOException e1) {
e1.printStackTrace();
}
} else {
e.printStackTrace();
}
}
if (f instanceof PDNonTerminalField) {
processFields(((PDNonTerminalField) f).getChildren(), resources);
}
});
}

Related

Java Unable to load arabic character U+062C into PDF on PDFBox even though it exists on the font

The font I am using is an arial.tff. Aside from this specific character (U+062C) all the other characters seem to be working fine.
Somehow when it reaches this point it just throws:
java.lang.IllegalArgumentException: U+062C ('afii57420') is not available in this font Helvetica (generic: ArialMT) encoding: StandardEncoding with differences
Code below:
PDFont font = PDType0Font.load(pdfDocumentTemplate, this.resourceLoader.getResource("classpath:arial.ttf").getInputStream(), false);
PDAcroForm pdAcroForm = pdAcroFormOptional.get();
String fontName = pdAcroForm.getDefaultResources()
.add(font)
.getName();
for (GenerateDocumentPlaceholder placeholder : command.getPlaceholderList()) {
PDTextField field = (PDTextField) pdAcroForm.getField(placeholder.getName());
try {
if (field != null) {
field.setDefaultAppearance("/" + fontName + " 0 Tf 0 g");
field.setValue(placeholder.getValue());
}
} catch (Exception ex) {
log.error("Error while updating field {}", placeholder.getValue(), ex);
}
}
And here's the font with the character:
Any ideas?
Fixed by replacing the embedded resources within pdf by doing this:
private String getPdfFontName(PDAcroForm pdAcroForm, PDDocument pdfDocumentTemplate, String resource) throws ConstraintViolatedException {
String fontName = "";
try {
PDResources pdResources = new PDResources();
pdAcroForm.setDefaultResources(pdResources);
PDFont font = PDType0Font.load(pdfDocumentTemplate, this.resourceLoader.getResource(resource).getInputStream());
pdResources.put(COSName.getPDFName("Helv"), font);
fontName = pdResources.add(font).getName();
} catch (Exception ex) {
log.warn("Error while adding arabic font", ex);
}
return fontName;
}
And then using it like this:
String fontName = this.getPdfFontName(pdAcroForm, pdfDocumentTemplate, "classpath:Arial.ttf");
for (GenerateDocumentPlaceholder placeholder : command.getPlaceholderList()) {
PDTextField field = (PDTextField) pdAcroForm.getField(placeholder.getName());
try {
if (field != null) {
field.setDefaultAppearance("/" + fontName + " 0 Tf 0 g");
field.setValue(placeholder.getValue());
} catch (Exception ex) {
log.error("Error while updating field {}", placeholder.getValue(), ex);
}
}

Extracting inline images coming in mail body using java Mail API

I have written two methods one for attachments and one email body content. I need to extract the images from email body. These two methods are working fine but when images are coming in email body it is should be saved in database. So it can be used later.
For attachmnents:-
public static List getAttachments(MimeMultipart multipart, List existingAttachments) {
if (multipart != null) {
try {
if (existingAttachments == null) {
existingAttachments = new ArrayList<MimeBodyPart>();
}
for (int i = 0; i < multipart.getCount(); i++) {
if (multipart.getBodyPart(i) instanceof MimeBodyPart) {
MimeBodyPart currentPart = (MimeBodyPart) multipart.getBodyPart(i);
if (Part.ATTACHMENT.equalsIgnoreCase(currentPart.getDisposition())) {
if (!existingAttachments.contains(currentPart)) {
existingAttachments.add(currentPart);
System.out.println(currentPart.getFileName());
}
} else if (currentPart.getContent() instanceof MimeMultipart) {
existingAttachments = getAttachments((MimeMultipart) currentPart.getContent(), existingAttachments);
}
}
}
} catch (MessagingException ex) {
LoggerFactory.getLogger(EmailUtil.class.getName()).error(ex.getMessage());
} catch (IOException ex) {
LoggerFactory.getLogger(EmailUtil.class.getName()).error(ex.getMessage());
}
}
return existingAttachments;
}
for email Body ContentThis method is extracting email body content
public static String getContent(MimeMultipart multipart) {
String emailContent = null;
if (multipart != null) {
try {
for (int i = 0; i < multipart.getCount(); i++) {
if (multipart.getBodyPart(i) instanceof MimeBodyPart) {
MimeBodyPart currentPart = (MimeBodyPart) multipart.getBodyPart(i);
if (Part.INLINE.equalsIgnoreCase(currentPart.getDisposition())) {
LoggerFactory.getLogger(EmailUtil.class.getName()).info("Content dispo is inline");
emailContent = (String) currentPart.getContent();
} else if (currentPart.getDisposition() == null && currentPart.getContentType().toLowerCase().contains("text")) {
LoggerFactory.getLogger(EmailUtil.class.getName()).info("Content dispo is null and type is text/*");
try {
emailContent = (String) currentPart.getContent();
} catch (ClassCastException ex) {
LoggerFactory.getLogger(EmailUtil.class.getName()).warn("Classcast exception caught and managed");
try {
InputStream is = currentPart.getInputStream();
emailContent = IOUtils.toString(is, currentPart.getEncoding());
Document doc=Jsoup.parse(emailContent);
Elements elements =doc.getElementsByTag("img");
System.out.println(elements);
int htmlCloseIndex = emailContent.indexOf("</html>");
emailContent = emailContent.substring(0, htmlCloseIndex);
emailContent+="</html>";
} catch (Exception e) {
LoggerFactory.getLogger(EmailUtil.class.getName()).error("Exception rebound caught and managed, email content will not read");
//emailContent = "Unable to read email content";
e.printStackTrace();
}
}
}else if (currentPart.getDisposition() == null && currentPart.getContentType().contains("TEXT")) {
LoggerFactory.getLogger(EmailUtil.class.getName()).info("Content dispo is null and type is TEXT/*");
try {
emailContent = (String) currentPart.getContent();
} catch (ClassCastException ex) {
LoggerFactory.getLogger(EmailUtil.class.getName()).warn("Classcast exception caught and managed");
try {
InputStream is = currentPart.getInputStream();
emailContent = IOUtils.toString(is, currentPart.getEncoding());
int htmlCloseIndex = emailContent.indexOf("</html>");
emailContent = emailContent.substring(0, htmlCloseIndex);
emailContent+="</html>";
} catch (Exception e) {
LoggerFactory.getLogger(EmailUtil.class.getName()).error("Exception rebound caught and managed, email content will not read");
//emailContent = "Unable to read email content";
e.printStackTrace();
}
}
}
else if (currentPart.getContent() instanceof MimeMultipart) {
emailContent = getContent((MimeMultipart) currentPart.getContent());
}
}
}
} catch (MessagingException ex) {
LoggerFactory.getLogger(EmailUtil.class.getName()).error(ex.getMessage());
LoggerFactory.getLogger(EmailUtil.class.getName()).warn("email content will not read");
//emailContent = "Unable to read email content";
} catch (IOException ex) {
LoggerFactory.getLogger(EmailUtil.class.getName()).error(ex.getMessage());
LoggerFactory.getLogger(EmailUtil.class.getName()).warn("email content will not read");
// emailContent = "Unable to read email content";
} catch (ClassCastException ex) {
LoggerFactory.getLogger(EmailUtil.class.getName()).warn("Classcast exception caught and managed");
// emailContent = "Unable to read email content";
}
}
return emailContent;
}
Okay one starts with the <img src="..."> tags, you already took:
Elements elements = doc.getElementsByTag("img");
An img tag for an embedded image looks like:
<img src="data:image/jpeg;base64,..." ...>
So having the src attribute do:
String src = ...
if (src.startsWith("data:")) { // Embedded image data.
int p = src.indexOf(';'); // Or better ";base64,"
if (p == -1) {
throw new IllegalArgumentException();
}
String mimeType = src.substring(5, p);
String base64Data = src.substring(p + 1 + 6 + 1); // ";base64,"
byte[] data = Base64.getDecoder().decode(base64Data);
String file = "test." + mimeType.replaceFirst("^.*/(.*)$", "$1");
Path path = Paths.get(file);
Files.write(path, data);
}

Converting Scanned Image In PDF to Text using Tesseract OCR

PDF document is loaded and get scanned page content as a BufferedImage. When I am doing OCR of this image it is showing empty in result.
Code is pasted below
public static void main(String[] args) {
PDDocument document = null;
try {
//mini-cog.pdf Optometry.pdf
document = PDDocument.load(new File("D:\\McLaren\\Optometry.pdf"));
PDPageTree pages = document.getPages();
Iterator iter = pages.iterator();
while (iter.hasNext()) {
PDPage page = (PDPage) iter.next();
PDResources resources = page.getResources();
for (COSName c : resources.getXObjectNames()) {
PDXObject o = resources.getXObject(c);
if (o instanceof PDImageXObject) {
BufferedImage image = ((PDImageXObject) o).getImage();
System.out.println("Width ====>> "+image.getWidth());
System.out.println("Height ====>> "+image.getHeight());
ocr(image);
}
}
} // end while loop
}
catch (IOException ex) {
System.out.println("" + ex);
}
}
public static void ocr(BufferedImage image) {
try {
System.setProperty("sun.java2d.cmm", "sun.java2d.cmm.kcms.KcmsServiceProvider");
System.load("C:\\Program Files\\Tesseract-OCR\\gsdll64.dll");
Tesseract tessInst = new Tesseract();
tessInst.setDatapath("D:\\tesseract\\");
tessInst.setLanguage("eng");
String result = tessInst.doOCR(image);
System.out.println(result);
}
catch (TesseractException e) {
e.printStackTrace();
}
}
BufferedImage is showing empty in result after converting Image into Text using OCR.

Extracting files in .iso using jSevenZip

I'm trying to extract the files in an .iso file I have. There are no errors and the console prints out all the files inside. However, none of the files get extracted. Any help is greatly appreciated!
I was able to find this link but I can't seem to integrate it in to my project.
Link to code example
class DownloadWorker extends SwingWorker<String, Object> {
private String flocation;
private JLabel out;
public DownloadWorker(String location, JLabel fout)
{
this.flocation = location;
this.out = fout;
}
#Override
public String doInBackground() {
//download here
out.setText("Beginning download. This may take a few minutes.");
try {
//ProgressBar/Install
System.out.println("FILELOCATION:\n----------");
System.out.println(flocation);
String URL_LOCATION = "http://www.futureretrogaming.tk/gamefiles/ProfessorPhys.iso";
String LOCAL_FILE = (flocation + "\\ProfessorPhys\\");
File localfile = new File(LOCAL_FILE);
if (localfile.exists()) {
System.out.println("Directory exists!");
}
else {
System.out.println("Directory doesn't exist! Creating...");
localfile.mkdir();
if (localfile.exists())
System.out.println("Directory created!");
}
System.out.println("LOCALFILE:\n-------");
System.out.println(LOCAL_FILE);
URL website = new URL(URL_LOCATION);
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(LOCAL_FILE+"\\ProfessorPhys.iso\\");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
System.out.println("--------\nDone Downloading\n---------");
RandomAccessFile randomAccessFile = null;
ISevenZipInArchive inArchive = null;
try {
randomAccessFile = new RandomAccessFile(localfile +"\\ProfessorPhys.iso\\", "r");
inArchive = SevenZip.openInArchive(null, // autodetect archive type
new RandomAccessFileInStream(randomAccessFile));
// Getting simple interface of the archive inArchive
ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface();
System.out.println(" Hash | Size | Filename");
System.out.println("----------+------------+---------");
for (ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) {
final int[] hash = new int[] { 0 };
if (!item.isFolder()) {
ExtractOperationResult result;
final long[] sizeArray = new long[1];
result = item.extractSlow(new ISequentialOutStream() {
public int write(byte[] data) throws SevenZipException {
hash[0] ^= Arrays.hashCode(data); // Consume data
sizeArray[0] += data.length;
return data.length; // Return amount of consumed data
}
});
if (result == ExtractOperationResult.OK) {
System.out.println(String.format("%9X | %10s | %s", //
hash[0], sizeArray[0], item.getPath()));
} else {
System.err.println("Error extracting item: " + result);
}
}
}
} catch (Exception e) {
System.err.println("Error occurs: " + e);
System.exit(1);
} finally {
if (inArchive != null) {
try {
inArchive.close();
} catch (SevenZipException e) {
System.err.println("Error closing archive: " + e);
}
}
if (randomAccessFile != null) {
try {
randomAccessFile.close();
} catch (IOException e) {
System.err.println("Error closing file: " + e);
}
}
}
} catch (Exception e) {
System.out.println(e);
}
return "done";
}
#Override
protected void done() {
//done
out.setText(out.getText() + "\n Operation Done");
}
}
Code I'm trying to implement:
public class ExtractorUtil {
private static Logger logger = Logger.getLogger(ExtractorUtil.class.getCanonicalName());
public static void extract(File file, String extractPath) throws Exception {
ISevenZipInArchive inArchive = null;
RandomAccessFile randomAccessFile = null;
randomAccessFile = new RandomAccessFile(file, "r");
inArchive = SevenZip.openInArchive(null, new RandomAccessFileInStream(randomAccessFile));
inArchive.extract(null, false, new MyExtractCallback(inArchive, extractPath));
if (inArchive != null) {
inArchive.close();
}
if (randomAccessFile != null) {
randomAccessFile.close();
}
}
public static class MyExtractCallback implements IArchiveExtractCallback {
private final ISevenZipInArchive inArchive;
private final String extractPath;
public MyExtractCallback(ISevenZipInArchive inArchive, String extractPath) {
this.inArchive = inArchive;
this.extractPath = extractPath;
}
#Override
public ISequentialOutStream getStream(final int index, ExtractAskMode extractAskMode) throws SevenZipException {
return new ISequentialOutStream() {
#Override
public int write(byte[] data) throws SevenZipException {
String filePath = inArchive.getStringProperty(index, PropID.PATH);
FileOutputStream fos = null;
try {
File dir = new File(extractPath);
File path = new File(extractPath + filePath);
if (!dir.exists()) {
dir.mkdirs();
}
if (!path.exists()) {
path.createNewFile();
}
fos = new FileOutputStream(path, true);
fos.write(data);
} catch (IOException e) {
logger.severe(e.getLocalizedMessage());
} finally {
try {
if (fos != null) {
fos.flush();
fos.close();
}
} catch (IOException e) {
logger.severe(e.getLocalizedMessage());
}
}
return data.length;
}
};
}

ExceptionConverter: java.io.IOException: The document has no pages

I want to create 4 pdf files using iText. While running the code the first two of are successfully created. The remaning two throw an Exception "ExceptionConverter: java.io.IOException: The document has no pages."
Here is the code for pdf creation part:
public static Boolean createPDFFile(String filename, int headerType, List<DremelitemDetails> itemDetails) {
try {
path = new File(".").getCanonicalPath();
column = 0;
// create the pdf file
props.load(new FileInputStream("Properties\\eng.properties"));
COLUMNS = new float[][] {
{ MARGIN_LEFT, MARGIN_BOTTOM, (PageSize.LETTER.rotate().getWidth() - MARGIN_ENTER_COLUMNS) / 2,
PageSize.LETTER.rotate().getTop(MARGIN_TOP) },
{ (PageSize.LETTER.rotate().getWidth() + MARGIN_ENTER_COLUMNS) / 2, MARGIN_BOTTOM,
PageSize.LETTER.rotate().getRight(MARGIN_RIGHT), PageSize.LETTER.rotate().getTop(MARGIN_TOP) } };
String fontPath = "font\\arialuni.TTF";
unicodeFont = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
mmPDFDocument = new Document(PageSize.LETTER.rotate(), MARGIN_LEFT, MARGIN_RIGHT, MARGIN_TOP, MARGIN_BOTTOM);
pdfWriter = PdfWriter.getInstance(mmPDFDocument, new FileOutputStream(filename));
mmPDFDocument.setPageSize(PageSize.LETTER.rotate());
/*String[] footer = { props.getProperty("header"), "fdftfdbfbug",
"\u00a9" + props.getProperty("footer1"), props.getProperty("footer2")};*/
String[] footer = { "SUMMARY OF PROJECTS", "Home Décor",
"\u00a92011 Robert Bosch Tool Corporation. All Rigths Reserved.",
"Web content and services - on demand, on your printer www.eprintcentre.com" };
pdfWriter.setPageEvent(new HeaderFooter(mmPDFDocument.right() - mmPDFDocument.left(), footer, path, headerType, unicodeFont));
mmPDFDocument.open();
COLUMNS[0][3] = mmPDFDocument.top();
COLUMNS[1][3] = mmPDFDocument.top();
pdfColumn = new ColumnText(pdfWriter.getDirectContent());
pdfColumn.setSimpleColumn(COLUMNS[column][0], COLUMNS[column][1], COLUMNS[column][2], COLUMNS[column][3]);
pdfColumn.setSpaceCharRatio(PdfWriter.NO_SPACE_CHAR_RATIO);
/* For generating Pdf */
addItemDetails(itemDetails);
mmPDFDocument.close();
} catch (FileNotFoundException e) {
System.out.println("FileNotFoundException-------"+e);
closePDFFile();
return false;
} catch (DocumentException e) {
System.out.println("DocumentException-------"+e);
closePDFFile();
return false;
} catch (Exception e) {
System.out.println("Exception-------"+e);
return false;
}
return true;
}
public static void main(String[] args) {
DremelData dremelData = new DremelData();
DremelPDFBulid dremelPDFBulid = new DremelPDFBulid();
Document doc = dremelData.readXml("C:\\eng.xml");
ArrayList<DremelitemDetails> homeDeco = dremelData.parseNode(1, 11, doc);
ArrayList<DremelitemDetails> artsAndCrafts = dremelData.parseNode(12, 24, doc);
ArrayList<DremelitemDetails> seasonalIdeas = dremelData.parseNode(25, 36, doc);
ArrayList<DremelitemDetails> DIYRestoration = dremelData.parseNode(37, 49, doc);
try {
dremelPDFBulid.createPDFFile("c:\\sam1.pdf", 0, homeDeco);
dremelPDFBulid.createPDFFile("c:\\sam2.pdf", 1, artsAndCrafts);
dremelPDFBulid.createPDFFile("c:\\sam3.pdf", 2, seasonalIdeas);
dremelPDFBulid.createPDFFile("c:\\sam4.pdf", 3, DIYRestoration);
} catch (Exception e) {
System.out.println("Error in readXml : " + e.toString());
}
}
In the above code the content of sam3 and samp4 pdf's are empty and showing "The document has no pages." exception

Categories

Resources