Loading
PDF creation

Java Quick Notes

Refresh Your Java - Before Java Interview

We are Agile, believe in less Documentation - Only Quick notes (Java Interview Questions) of Java/J2ee Read more....


Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet

Go to SiteMap

Q)  How to create PDF with Spring ?


Ans)

 

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

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

    <bean name="*.pdf" class="common.PDFView">
    </bean>

</beans>

 

import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
import org.springframework.web.servlet.view.document.AbstractPdfView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.util.Map;

public class PDFView extends AbstractPdfView {
  private String filename;

  public String getFilename() {
    return filename;
  }

  public void setFilename(String filename) {
    this.filename = filename;
  }

  protected void buildPdfDocument(
          Map model,
          Document doc,
          PdfWriter writer,
          HttpServletRequest req,
          HttpServletResponse resp)
          throws Exception {

    PdfContentByte cb = writer.getDirectContent();
    PdfReader reader = new PdfReader(new FileInputStream(filename));
    for (int i = 1; i <= reader.getNumberOfPages(); i++) {
      doc.newPage();
      PdfImportedPage page = writer.getImportedPage(reader, i);
      cb.addTemplate(page, 0, 0);
    }
  }


  protected Document newDocument() {
    return new Document();
  }

}

 

 



Back to top

------------------------- We hope you got necessary Info On -----------------------------------------

PDF creation


PDF creation

-------------------------------------------------------------------------------------------------------



Face Book
Request for a Mock Interview/Training

Get a PDF

Face Book
Same look (Read) on any device, this is Ads free