cancel
Showing results for 
Search instead for 
Did you mean: 

Reading a XML file AND Creating a Pdf file through Java Mapping

0 Kudos

Hi Experts,

I am reading a xml file and creating a Pdf file through Java Mapping.However I am able to create the Pdf file

but not being able to read the values from the XMl file and writes that into the Pdf file accordingly.Can you please provide some

good link or ideas as to how populate the values from XML to the pdf files?

Regards

Atanu Mazumdar

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Try Aspose.PDF for Java i use this API to create pdf file online and it also convert xml file to pdf in java language so u dnt have to do all the coding yourself this API can do this for you. i hope you will ike this API too like me.

Former Member
0 Kudos

Hi.

Have you checked this link?

http://scn.sap.com/message/13316490#13316490

Regards

Lucho

0 Kudos

Hi Luis,

I have gone through this link but this link donot actually meet my needs.

According to my need ,the xml file that I am reading is as given below:

<?xml version="1.0" encoding="UTF-8" ?> 

- <EmployeeDetails>

- <Employee>

<EMPNO>034391</EMPNO>

<Name>X</Name>

<Department>JRI</Department>

<Designation>ApplicationDeveloper</Designation>

</Employee>

- <Employee>

<EMPNO>51407523</EMPNO>

<Name>Y</Name>

<Department>XI</Department>

<Designation>Consultant</Designation>

</Employee>

</EmployeeDetails

My Java code that I have written is as given below:

import java.io.File;


//import com.lowagie.text.Document;
import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

//import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfTable;
import com.lowagie.text.pdf.PdfWriter;

public class XMLtoPDF {
public void generateExcel(File xmlDocument)
{
  
 
  com.lowagie.text.Document doc= new com.lowagie.text.Document();
 
  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  try {
   factory.newDocumentBuilder();
   DocumentBuilder builder = factory.newDocumentBuilder();
   org.w3c.dom.Document document = (org.w3c.dom.Document) builder.parse(xmlDocument);
   NodeList nodeList = document.getElementsByTagName("Employee");
  
  
    OutputStream file = new FileOutputStream(new File("C:\\Users\\atanu.mazumdar\\Desktop\\employeedetails.pdf"));
 
   PdfWriter.getInstance(doc, file);
 
   doc.open();
   PdfPTable table=new PdfPTable(4);
  
   PdfPCell cell = new PdfPCell(new Paragraph("Employee Details for XYZ Organization"));

   cell.setColspan(4);
  
            cell.setBackgroundColor(Color.red);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);

            cell = new PdfPCell(new Paragraph("EMP NO"));
            cell.setBackgroundColor(Color.blue);
            table.addCell(cell);

            cell = new PdfPCell(new Paragraph("NAME"));
            cell.setBackgroundColor(Color.blue);
            table.addCell(cell);

            cell = new PdfPCell(new Paragraph("DEPARTMENT"));
            cell.setBackgroundColor(Color.blue);
            table.addCell(cell);

            cell = new PdfPCell(new Paragraph("DESIGNATION"));
            cell.setBackgroundColor(Color.blue);
            table.addCell(cell);
           
            doc.add(table);
           
           
           
           
  
  
    doc.close();
   
             file.close();

  } catch (Exception e) {
  
   System.out.println(e);
  
  }
  
  

}

public static void main(String[] argv) {

       File xmlDocument = new File("C:\\Users\\atanu.mazumdar\\Desktop\\EmployeeDetails.xml");

       XMLtoPDF xmltopdf = new XMLtoPDF();
       xmltopdf.generateExcel(xmlDocument);
    }

}

The output that I am getting in pdf file is as given below:

                                       Employee details of XYZ orgainisation

Employee No                          Name                      Department                          Designation

What I want to have is to read the corresponding field values from the above xml and populate those accordingly.

Regards

Atanu Mazumdar

Former Member
0 Kudos

Hi Atanu, please refer to SAP help or search for Java mapping on SCN and you will find information about the prerequisites for Java mappings and plenty of examples for how to create a mapping class and how to extract XML field values.

For example provides a java mapping class template which is a good starting point

Best regards, Martin

Message was edited by: Martin Sommer

anupam_ghosh2
Active Contributor
0 Kudos

Hi Atanu,

                 Pls go thorugh your previous thread http://scn.sap.com/thread/3173071

Watch how a XML field value are obtained from input XML.

Apply same technique here too.

Regards

Anupam