Image Extraction from a Image Field

275 views
Skip to first unread message

Gervais...@gmail.com

unread,
Oct 24, 2007, 6:53:54 PM10/24/07
to Adobe LiveCycle Developers
Hi, thanks to anyone who responds!

I've made a form in LiveCycle and in it I have an image field where a
user can insert an image into the form. When the submit the form, the
image is embedded in the xml file and when I import it back into the
form the image is still there, yay.

My problem is that I would like to be able to take that image out of
the form when it gets submitted back to me. Either have it attached
separately from the xml file or some other way.

Hope someone can help.

Gervaise

Howard@Avoka

unread,
Oct 25, 2007, 1:27:31 AM10/25/07
to Adobe LiveCycle Developers
Hi
You (or we) could write a tool to extract the data from teh XML, and
convert it back into a file.
This could be done on the client, or the server.
The easiest way would be to import the XML file back into the original
form using Acrobat, and then use the Snapshot tool to take a snapshot.
Very low tech, but the easiest/cheapest way to go.
You will potentially lose quality though...

Howard
http://www.avoka.com

On Oct 25, 8:53 am, "Gervaise.He...@gmail.com"

Gervais...@gmail.com

unread,
Oct 25, 2007, 1:43:56 AM10/25/07
to Adobe LiveCycle Developers
Hey,
The snapshot tool wouldn't be good enough as the form is a request for
reports and the user has the ability to enter a client logo for use in
the reports and there would be too much chance of loss of quality.

Isn't there some way of maybe not using an image field but maybe
placing a button which would prompt the user for a file which gets
added as an attachment along with the xml file when submitted?


PS
I found this post somewhere else:
http://www.acrobatusers.com/forums/ask_an_expert/questions/view/2226/

I don't know JavaScript but I tried to add
"event.target.browseForFileToSubmit();" to a text field and a button
as JavaScript for mouse up event and nothing happens when clicked.


On Oct 25, 12:27 am, "Howard@Avoka" <htreis...@gmail.com> wrote:
> Hi
> You (or we) could write a tool to extract the data from teh XML, and
> convert it back into a file.
> This could be done on the client, or the server.
> The easiest way would be to import the XML file back into the original
> form using Acrobat, and then use the Snapshot tool to take a snapshot.
> Very low tech, but the easiest/cheapest way to go.
> You will potentially lose quality though...
>

> Howardhttp://www.avoka.com

Howard@Avoka

unread,
Oct 25, 2007, 2:12:36 AM10/25/07
to Adobe LiveCycle Developers
Try this code under the Click event of a regular Button (in
Javascript)
var myDoc = event.target;
// This will prompt the user to import an external document and save
it to an attachment called "profile.xml"
myDoc.importDataObject("logo");

You will need to either have your customers use Acrobat, or Reader
Extend the form, as attachments are not supported by Reader.

We'd be happy to provide the Reader Extensions license if you're
intersted - email sa...@avoka.com

Howard
http://www.avoka.com

On Oct 25, 3:43 pm, "Gervaise.He...@gmail.com"

> > > Gervaise- Hide quoted text -
>
> - Show quoted text -

Gervais...@gmail.com

unread,
Oct 25, 2007, 2:35:11 AM10/25/07
to Adobe LiveCycle Developers
OK I finally feel like I'm getting somewhere!

I did that and the open file dialog came up, I selected the file, it
imported the file as an attachment to the pdf but when I submitted it,
there was no additional xml file and when I imported the outputted xml
file back into the form, the attachment wasn't in it.

On Oct 25, 1:12 am, "Howard@Avoka" <htreis...@gmail.com> wrote:
> Try this code under the Click event of a regular Button (in
> Javascript)
> var myDoc = event.target;
> // This will prompt the user to import an external document and save
> it to an attachment called "profile.xml"
> myDoc.importDataObject("logo");
>
> You will need to either have your customers use Acrobat, or Reader
> Extend the form, as attachments are not supported by Reader.
>
> We'd be happy to provide the Reader Extensions license if you're
> intersted - email sa...@avoka.com
>

Christoph Rooms

unread,
Oct 25, 2007, 4:41:12 AM10/25/07
to Adobe LiveCycle Developers
Hi,

When working with Attachments, you would have to submit the PDF ...
not the XML file. The PDF acts like a container for all the elements.
On the server you can extract the attachments from the PDF file.

Christoph

On Oct 25, 8:35 am, "Gervaise.He...@gmail.com"

Duane Nickull

unread,
Oct 25, 2007, 5:36:03 AM10/25/07
to Adobe LiveCycle Developers
If you use the API in forms, it can be done. You could also do it with
XPAAJ (if you happened to have this library). I think we still allow XPAAJ
to be used by people who have bought LC.

The code to do it is below. I also have more specialized examples on my
blog at http://technoracle.blogspot.com


import java.io.InputStream;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.BufferedWriter;

import java.io.FileWriter;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.util.List;

import java.util.Iterator;

import java.util.ListIterator;

import java.awt.image.DataBuffer;

import com.adobe.pdf.*;

public class PDFExtractData {

public static void main(String[] args)

throws FileNotFoundException, IOException

{

// get pdf filename

String inPdfName;

if(args.length != 1 ) {

System.out.println("\nCommand line format: java PDFExtractData
pdf-file");

return;

}

else {

// message

System.out.println("\nPDF data extraction using Adobe XPAAJ.");

inPdfName = new String(args[0]);

PDFExtract(inPdfName);

}

}

/**

Method to extract data from a PDF file

*/

public static void PDFExtract(String inPdfName)

throws FileNotFoundException, IOException

{

// open PDF

System.out.println("\nOpen PDF Document ... ");

PDFDocument doc = null;

FileInputStream inPdfFile = new FileInputStream(inPdfName);

try {

doc = PDFFactory.openDocument(inPdfFile);

} catch (IOException e) {

System.out.println("Error opening PDF file :" + inPdfName);

System.out.println(e);

}

if(doc == null)

System.out.println("Cannot open PDF file : " + inPdfName);

else

System.out.println( inPdfName + " was successfully opened.");

// PDF info.

System.out.println ("PDF data extraction started ...");

// Export the XMP metadata

System.out.println("\nExtracting document metadata ...");

String DocMetadataFile = "DocMetadata.xml";

boolean b = false;

InputStream inputStream;

inputStream = doc.exportXMP();

if(inputStream == null)

System.out.println("No document level metadata was exported.");

else {

System.out.println("Document metadata was exported.");

try {

b = saveFile(inputStream, DocMetadataFile);

} catch (Exception e) {

System.out.println("Error saving metadata file.");

System.out.println(e);

}

if(b == true)

System.out.println ("Document metadata was saved to file : "
+ DocMetadataFile);

else

System.out.println("Document metadata was not saved.");

}

// Export image metadata

System.out.println("\nExtracting image metadata ...");

List images = doc.getImages();

if(images.size() > 0)

System.out.println("Images were extracted. Number of images = "
+ images.size());

else

System.out.println("No images where found in the PDF file.");

// Iterate over all of the images and export their metadata.

int imageNumber = 0;

Iterator iterator = images.iterator();

while(iterator.hasNext()) {

imageNumber++;

PDFImage image = (PDFImage)iterator.next();

try {

// export image metadata

inputStream = image.exportXMP();

if(inputStream == null) {

System.out.println("No metadata found for image #" +
imageNumber);

continue;

}

System.out.println("Image #" + imageNumber + " - " +
"metadata was exported.");

String imageFilename = "ImageMetadata" + imageNumber +
".xml";

b = saveFile(inputStream, imageFilename);

System.out.println("Image metadata was saved to file : " +
imageFilename);

} catch (IOException e) {

System.out.println("Error exporting image metadata.");

System.out.println(e);

} catch (Exception e) {

System.out.println("Error saving image metadata.");

System.out.println(e);

}

}

// Export the Annotations

System.out.println("\nExtracting annotaion data ...");

inputStream = doc.exportAnnotations();

if(inputStream == null)

System.out.println("No annotations were exported.");

else {

System.out.println("Annotations were exported.");

String annotFile = "Annotations.xml";

b = false;

try {

b = saveFile(inputStream, annotFile);

} catch (Exception e) {

System.out.println("Error saving annotation file.");

System.out.println(e);

}

if(b == true)

System.out.println ("Annotations were saved to file : " +
annotFile);

else

System.out.println("Annotations were not saved to file.");

}

// Get the form type

System.out.println("\nExtracting form data ...");

FormType formType = doc.getFormType();

// Export the form data

String formDataFile = "FormData.xml";

if(formType == FormType.XML_FORM)

{

System.out.println("The PDF has an XML form.");

inputStream = doc.exportFormData(FormDataFormat.XFA);

if(inputStream == null)

System.out.println("No XML form data was exported.");

else {

System.out.println("XML form data was exported.");

try {

b = saveFile(inputStream, formDataFile);

System.out.println("XML form data was saved to file : "
+ formDataFile);

} catch (Exception e) {

System.out.println("Error saving XML Form Data.");

System.out.println(e);

}

}

}

// Export the form data

else if(formType == FormType.ACROFORM)

{

System.out.println("The PDF has an Acro form.");

inputStream = doc.exportFormData(FormDataFormat.XFDF);

if(inputStream == null)

System.out.println("No XFDF form data was exported.");

else {

System.out.println("XFDF data was exported.");

try {

b = saveFile(inputStream, formDataFile);

System.out.println("XFDF form data was saved to file : "
+ formDataFile);

} catch (Exception e) {

System.out.println("Error saving Acro Form Data.");

System.out.println(e);

}

}

}

else {

System.out.println("The PDF does not have a form.");

}

// export file attachments

System.out.println("\nExporting file attachments ...");

byte[][] names = doc.getFileAttachmentNames();

if(names != null) {

for(int i = 0; i < names.length; i++) {

FileAttachment embeddedFile =
doc.exportFileAttachment(names[i]);

String fileName = embeddedFile.getFilename();

String MimeType = embeddedFile.getMimetype();

inputStream = embeddedFile.getData();

try {

b = saveFile(inputStream, fileName);

} catch (Exception e) {

System.out.println("Error saving file attachment.");

System.out.println(e);

}

if(b == true){

System.out.println ("File attachment " + ( i + 1) + "
was saved to file : " + fileName);

System.out.println ("The MIME type is : " + MimeType);

}

else

System.out.println("File attachment was not saved to
file.");

}

}

else {

System.out.println("The PDF has no file attachments.");

}

// save the pdf with same name plus "_saved"

System.out.println ("\nSaving document ... ");

int j = inPdfName.lastIndexOf(".");

String outPdfName = inPdfName.substring(0, j) + "_saved" + ".pdf";

inputStream = doc.save();

b = false;

try {

b = saveFile(inputStream, outPdfName);

} catch (Exception e) {

System.out.println("Error saving PDF file.");

System.out.println(e);

}

if(b == true)

System.out.println ("Document was saved to file : " +
outPdfName);

else

System.out.println("Document was not saved to file.");

// save the pdf as an XDP file

String outXdpName = inPdfName.substring(0, j) + "_saved" + ".xdp";

inputStream = doc.saveAsXDP();

try {

b = saveFile(inputStream, outXdpName);

} catch (Exception e) {

System.out.println("Error saving XDP file.");

System.out.println(e);

}

if(b == true)

System.out.println ("XDP file was saved to file : " +
outXdpName);

else

System.out.println("XDP file was not saved to file.");

// extract words

System.out.println("\nExtracting words ...");

ListIterator wordIterator = doc.getWords();

int pN = 0;

int cPageN = 0;

String text = "";

while(wordIterator.hasNext()) {

PDFWord word = (PDFWord) wordIterator.next();

pN = word.getPageNumber();

// append text if in the same page

if(cPageN == pN) {

// append text in same page.

text = text + word.getString() + " ";

}

// write text to a file for each page

else {

String outTextFilename = inPdfName.substring(0, j) + "_page"
+ ( pN+1 ) + ".txt";

saveStringToFile(text, outTextFilename);

System.out.println("Page " + ( pN+1 ) + " : " + "text was
saved to file.");

text = "";

text = text + word.getString();

cPageN = pN;

}

}

// write the last page

if(cPageN == pN && text.length() >0) {

String outTextFilename = inPdfName.substring(0, j) + "_page" + (
pN + 1 ) + ".txt";

saveStringToFile(text, outTextFilename);

System.out.println("Page " + (pN+1) + " : " + "text was saved to
file.");

}

System.out.println("\nExecution of PDFExtractData has finished.");

}

/**

method to save InputStream to a file.

*/

public static boolean saveFile(InputStream is, String filePath)

throws Exception

{

boolean retVal=false;

byte[] buffer = new byte[10240];

FileOutputStream outStream = null;

try

{

outStream = new FileOutputStream(filePath);

int len=0;

while (true)

{

len = is.read(buffer);

if (len == -1)

break;

outStream.write(buffer, 0, len);

}

outStream.close();

retVal = true;

}

catch (IOException io)

{

System.out.println("Writing the array of bytes into the file "

+ filePath + " failed.");

throw new Exception(

"Writing the array of bytes into the file "+ filePath +

" failed in saveFile");

}

return retVal;

}

// save text string to a file.

public static boolean saveStringToFile(String text, String filePath)

{

boolean b = false;

try {

BufferedWriter outTxtFile = new BufferedWriter(new
FileWriter(filePath));

outTxtFile.write(text, 0, text.length());

outTxtFile.close();

b = true;

} catch (IOException e) {

System.out.println("Error saving text file.");

}

return b;

}

}

/******* End of file *********************************/


On 10/24/07 3:53 PM, "Gervais...@gmail.com" <Gervais...@gmail.com>
wrote:

>
> Hi, thanks to anyone who responds!
>
> I've made a form in LiveCycle and in it I have an image field where a
> user can insert an image into the form. When the submit the form, the
> image is embedded in the xml file and when I import it back into the
> form the image is still there, yay.
>
> My problem is that I would like to be able to take that image out of
> the form when it gets submitted back to me. Either have it attached
> separately from the xml file or some other way.
>
> Hope someone can help.
>
> Gervaise
>
>
> >

--
**********************************************************************
"Speaking only for myself"
Blog - http://technoracle.blogspot.com
Community Music - http://www.mix2r.com
My Band - http://www.myspace.com/22ndcentury
Adobe MAX 2008 - http://technoracle.blogspot.com/2007/08/adobe-max-2008.html
**********************************************************************

Howard@Avoka

unread,
Oct 25, 2007, 8:25:42 AM10/25/07
to Adobe LiveCycle Developers
Apparently XPAAJ is deprecated, and unsupported.
A pity.
You can still presumably use it, at your own risk, if you've purchased
LiveCycle. Or Cold Fusion?
Howard

On Oct 25, 7:36 pm, Duane Nickull <dnick...@adobe.com> wrote:
> If you use the API in forms, it can be done. You could also do it with
> XPAAJ (if you happened to have this library). I think we still allow XPAAJ
> to be used by people who have bought LC.
>
> The code to do it is below. I also have more specialized examples on my

> blog athttp://technoracle.blogspot.com

> ...
>
> read more »

Duane Nickull

unread,
Oct 25, 2007, 8:28:50 AM10/25/07
to Adobe LiveCycle Developers
XPAAJ will always live in the hearts of those who found it useful

;-)

I think if can be requested but only for people who have purchased other LC
components. Not sure about CF but I would like to propose that it makes
sense.

Duane

--

Gervais...@gmail.com

unread,
Oct 25, 2007, 12:19:06 PM10/25/07
to Adobe LiveCycle Developers
ok, Y'all lost me with talk of API and XPAAJ. Thats alright, I used
the importDataObject and set the form to submit as pdf which kept the
attatchments in the file. Perfect, that'll do. Thanks all

On Oct 25, 7:28 am, Duane Nickull <dnick...@adobe.com> wrote:
> XPAAJ will always live in the hearts of those who found it useful
>
> ;-)
>
> I think if can be requested but only for people who have purchased other LC
> components. Not sure about CF but I would like to propose that it makes
> sense.
>
> Duane
>

> ...
>
> read more »

Reply all
Reply to author
Forward
0 new messages