cancel
Showing results for 
Search instead for 
Did you mean: 

Sample project how to use Itext (pdf) in webdynpro

Former Member
0 Kudos

Hi all,

Have someone a sample how to use Itext in a webdynpro.

Richard

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Bala,

I want to create a look and feel and think it is easer to do this with Itext than with XML.

Richard

roberto_tagliento
Active Contributor
0 Kudos

Did not understood your last post.

By the way from mine and Bala post i guess you have the answer.

Good job.

Former Member
0 Kudos

Hi Roberto and Bala,

Sorry, I was thinking the wrong way!!

Please can you check my source, I a'm get an error by opening the new window. the error : D:\PDFfromJava.pdf (The system cannot find the file specified)

this my source:

//@@begin wdDoInit()

wdContext.currentPersonalDataElement().setFirstName("Richard");

wdContext.currentPersonalDataElement().setLastName("Middelburg");

Document document = new Document();

try {

PdfWriter.getInstance(document, new FileOutputStream("D:
PDFfromJava.pdf"));

document.open();

document.add(new Paragraph("Hi, this is demo PDF file from JAVA!"));

}

catch(DocumentException de) {

// wdComponentAPI.getMessageManager().reportException(de.getMessage(),false);

wdContext.currentContextElement().setTest("fout1");

}catch(IOException ioe) {

// wdComponentAPI.getMessageManager().reportException(ioe.getMessage(),false);

wdContext.currentContextElement().setTest("fout2");

}

File file = new File("D:
PDFfromJava.pdf");// from iText library

try

{

IWDResource wr = WDResourceFactory.createCachedResource(getBytesFromFile(file), "PDF Report", WDWebResourceType.PDF);

IWDWindow w = wdComponentAPI.getWindowManager().createNonModalExternalWindow(wr.getUrl(0), "PDF Report");

w.show();

}

catch (Exception e)

{

wdComponentAPI.getMessageManager().reportException(e.getLocalizedMessage(), false);

wdContext.currentContextElement().setTest("fout3");

}

//@@end

//@@begin others

public static byte[] getBytesFromFile(File file) throws IOException {

InputStream is = new FileInputStream(file);

// Get the size of the file

long length = file.length();

// You cannot create an array using a long type.

// It needs to be an int type.

// Before converting to an int type, check

// to ensure that file is not larger than Integer.MAX_VALUE.

if (length > Integer.MAX_VALUE) {

// File is too large

}

// Create the byte array to hold the data

byte[] bytes = new byte[(int)length];

// Read in the bytes

int offset = 0;

int numRead = 0;

while (offset < bytes.length

&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {

offset += numRead;

}

// Ensure all the bytes have been read in

if (offset < bytes.length) {

throw new IOException("Could not completely read file "+file.getName());

}

// Close the input stream and return bytes

is.close();

return bytes;

}

//@@end

Richard

roberto_tagliento
Active Contributor
0 Kudos

Noooo

this:


File file = new File("D:PDFfromJava.pdf");// from iText library

probably isn´t right.

I don´t know iText, from its library you have to find the way of get the PDF FILE created.

or i guess that could be right, but you never SAVED the PDF FILE.

after:


document.open();
document.add(...);

see there is something like:


document.SAVE();
or
document.CLOSE();

Former Member
0 Kudos

Hi Roberto and Bala,

Problem is solved. Thanks for you help!!!!

Richard

Former Member
0 Kudos

Hi Richard,

In my WebDynpro Application I want to generate a PDF (using ITEXT API's) out of the data retrieved from back end system .

I used this source code.

Document document = new Document(PageSize.A4);
document.open();
PdfPTable table = new PdfPTable(1);
PdfPCell cell;
cell = new PdfPCell(new Paragraph("ONE"));
table.addCell(cell);
cell = new PdfPCell(new Paragraph("TWO")); 	
table.addCell(cell);

document.add(table);
document.close();

byte[] b = new byte[100 * 1024];
b =  document.toString().getBytes("UTF-8");
		
IWDCachedWebResource pdfRes = WDWebResource.getPublicCachedWebResource(b, WDWebResourceType.PDF, WDScopeType.CLIENTSESSION_SCOPE, 	wdThis.wdGetAPI().getComponent().getDeployableObjectPart(),"FileNameHelloText"));

I have used Window Manager to create a external window with the URL from pdfRes.getUrl() method.

After execution i get a pop up window with out PDF document.

Please let me know where i am making the mistake ?

Thanks

Senthil

Answers (3)

Answers (3)

Former Member
0 Kudos

Richard,

You may refer <a href="/people/prakash.singh4/blog/2005/04/05/create-a-pdf-file-using-java a PDF file using JAVA</a> for the usage of the IText APIs.

But, why do you want to use IText APIs? You can use the WD APIs for generating a PDF provided you have the data in the form of bytes. A pointer code will look like this.

try
{
    IWDResource wr = WDResourceFactory.createCachedResource(<i>YourByteArray/i], "PDF Report", WDWebResourceType.PDF);
    IWDWindow w = wdComponentAPI.getWindowManager().createNonModalExternalWindow(wr.getUrl(0), "PDF Report");
    w.show();
}
catch (Exception e)
{
    wdComponentAPI.getMessageManager().reportException(e.getLocalizedMessage(), false);
}

Bala

Former Member
0 Kudos

Hi Roberto,

Thanks for your quick response. I want to show the PDF file in new window!. I looking for a sample that I can use. I'am not a die hard developer.

Richard

roberto_tagliento
Active Contributor
0 Kudos

Thanks to you 😄

now i know iText!

I used the FOP library, the idea is different but for how to use must be the same.

With iText you can create physically the PDF on your server, after done it you can bind the binary file to a UIDownload element of a webdyndro, so the user can download it!

roberto_tagliento
Active Contributor
0 Kudos

Can use also UILinkToUrl element.



	// Deserialize from a file
	File file = new File("EquipmentFromFo.pdf");// from iText library

	// Get some byte array data
	pdfFile = getBytesFromFile(file);
	
	
	//pdfFoXMLFile = this.toFoXML().getBytes("UTF-8");
	// create a cached Web Dynpro XLS Resource for the given byte array and filename
	
	cachedPdfResource = this.getCachedWebResource(pdfFile, fileName, WDWebResourceType.PDF);



	// Store URL and filename of cached excel resource in context. 
	if (cachedPdfResource != null) {
	  wdContext.currentContextElement().setPdfFileURL(cachedPdfResource.getURL());
	  wdContext.currentContextElement().setPdfFileName(cachedPdfResource.getResourceName());

	} else {
	  ret = 1;
	}

where the method are:


   public static byte[] getBytesFromFile(File file) throws IOException {
		   InputStream is = new FileInputStream(file);
    
		   // Get the size of the file
		   long length = file.length();
    
		   // You cannot create an array using a long type.
		   // It needs to be an int type.
		   // Before converting to an int type, check
		   // to ensure that file is not larger than Integer.MAX_VALUE.
		   if (length > Integer.MAX_VALUE) {
			   // File is too large
		   }
    
		   // Create the byte array to hold the data
		   byte[] bytes = new byte[(int)length];
    
		   // Read in the bytes
		   int offset = 0;
		   int numRead = 0;
		   while (offset < bytes.length
				  && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
			   offset += numRead;
		   }
    
		   // Ensure all the bytes have been read in
		   if (offset < bytes.length) {
			   throw new IOException("Could not completely read file "+file.getName());
		   }
    
		   // Close the input stream and return bytes
		   is.close();
		   return bytes;
	   }


   private IWDCachedWebResource getCachedWebResource(byte[] file, String name, WDWebResourceType type) {
	 IWDCachedWebResource cachedWebResource = null;
	 if (file != null) {
	   cachedWebResource = WDWebResource.getWebResource(file, type);
	   cachedWebResource.setResourceName(name);
	 }
	 return cachedWebResource;
   }