Vistas pdf

En este paquete vamos a crear las clases que generan nuestras vistas pdf ya que el proyecto contempla imprimir numero de expediente para que lo presente el paciente cada vez que vaya a consulta esto en caso que no tengan tarjeta para que puedan buscar su expediente y la otra vista es la generación de la receta para que la pueda imprimir el medico y darsela a su paciente para que surta su medicamento.

Creamos en src/main/java el paquete com.tutosoftware.ecemexico.pdf.
Generamos la clase AbstractITextPdfView
 
   
  package com.tutosoftware.ecemexico.pdf;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.view.AbstractView;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;

public abstract class AbstractITextPdfView extends AbstractView{
	
	public AbstractITextPdfView() {
		setContentType("application/pdf");
	}

	@Override
	protected boolean generatesDownloadContent() {
		return true;
	}
		
	@Override
	protected void renderMergedOutputModel(Map<String, Object> model,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		// IE workaround: write into byte array first.
		ByteArrayOutputStream baos = createTemporaryOutputStream();

		// Apply preferences and build metadata.
		Document document = newDocument();
		PdfWriter writer = newWriter(document, baos);
		prepareWriter(model, writer, request);
		buildPdfMetadata(model, document, request);

		// Build PDF document.
		document.open();
		buildPdfDocument(model, document, writer, request, response);
		document.close();

		// Flush to HTTP response.
		writeToResponse(response, baos);
	}

	protected Document newDocument() {
		return new Document(PageSize.A4);
	}
	
	protected PdfWriter newWriter(Document document, OutputStream os) throws DocumentException {
		return PdfWriter.getInstance(document, os);
	}
	
	protected void prepareWriter(Map<String, Object> model, PdfWriter writer, HttpServletRequest request)
			throws DocumentException {

		writer.setViewerPreferences(getViewerPreferences());
	}
	
	protected int getViewerPreferences() {
		return PdfWriter.ALLOW_PRINTING | PdfWriter.PageLayoutSinglePage;
	}
	
	protected void buildPdfMetadata(Map<String, Object> model, Document document, HttpServletRequest request) {
	}
	
	protected abstract void buildPdfDocument(Map<String, Object> model, Document document, PdfWriter writer,
			HttpServletRequest request, HttpServletResponse response) throws Exception;

}
  
  
  
  

La clase PDFExpedienteBuilder
 
   
   package com.tutosoftware.ecemexico.pdf;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;


import com.tutosoftware.ecemexico.entity.Paciente;
public class PDFExpedienteBuilder extends AbstractITextPdfView{
	
	
	@Override
	protected void buildPdfDocument(Map<String, Object> model, Document doc,
			PdfWriter writer, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// get data model which is passed by the Spring container
		@SuppressWarnings("unchecked")
		List<Paciente> listPaciente = (List<Paciente>) model.get("listPaciente");
		
		doc.add(new Paragraph("Presentar este documento cada vez que vayas a consulta"));
		
		PdfPTable table = new PdfPTable(2);
		table.setWidthPercentage(100.0f);
		table.setWidths(new float[] {2.0f,3.0f});
		table.setSpacingBefore(10);
		
		// define font for table header row
		Font font = FontFactory.getFont(FontFactory.HELVETICA);
		font.setColor(BaseColor.WHITE);
		
		// define table header cell
		PdfPCell cell = new PdfPCell();
		cell.setBackgroundColor(BaseColor.BLUE);
		cell.setPadding(5);
		
		// write table header 
		cell.setPhrase(new Phrase("Nombre", font));
		table.addCell(cell);
		
		cell.setPhrase(new Phrase("Numero Expediente", font));
		table.addCell(cell);

		
		
		// write table row data
		for (Paciente paciente : listPaciente) {
			table.addCell(paciente.getNombre()+" "+paciente.getApellidoPaterno()+" "+paciente.getApellidoMaterno());
			table.addCell(paciente.getIdUsuario());
			
		}
		
		doc.add(table);
		
	}

}
   
   
   
   
  

La clase PDFRecetaBuilder
 
   
  package com.tutosoftware.ecemexico.pdf;


import java.util.List;
import java.util.Map;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.tutosoftware.ecemexico.model.RecetaExp;

public class PDFRecetaBuilder extends AbstractITextPdfView{
	
	
	@Override
	protected void buildPdfDocument(Map<String, Object> model, Document doc,
			PdfWriter writer, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		@SuppressWarnings("unchecked")
		List<RecetaExp> listReceta = (List<RecetaExp>) model.get("listReceta");
		
		doc.addTitle("Receta Médica");
		
		doc.add(new Paragraph("                             ECEMEXICO              "));
		
		// get data model which is passed by the Spring container
		
		doc.add(new Paragraph("Número de receta: "+listReceta.get(0).getNumeroReceta()));
		doc.add(new Paragraph("Médico: "+listReceta.get(0).getNombreDoctor()));
		doc.add(new Paragraph("Fecha: "+listReceta.get(0).getFecha()));
		
		
		
		
		PdfPTable table = new PdfPTable(5);
		table.setWidthPercentage(100.0f);
		table.setWidths(new float[] {1.0f,3.0f,2.0f,2.0f,2.0f});
		table.setSpacingBefore(10);
		
		// define font for table header row
		Font font = FontFactory.getFont(FontFactory.HELVETICA);
		font.setColor(BaseColor.WHITE);
		
		// define table header cell
		PdfPCell cell = new PdfPCell();
		cell.setBackgroundColor(BaseColor.BLUE);
		cell.setPadding(5);
		
		// write table header 
		cell.setPhrase(new Phrase("No.", font));
		table.addCell(cell);
		
		cell.setPhrase(new Phrase("Medicamento", font));
		table.addCell(cell);
        
		cell.setPhrase(new Phrase("Dosis", font));
		table.addCell(cell);
		
		cell.setPhrase(new Phrase("Duración", font));
		table.addCell(cell);
		
		
		cell.setPhrase(new Phrase("Notas", font));
		table.addCell(cell);
		
		
		List<String> medicamento = listReceta.get(0).getMedicamento();
		
		List<String> dosis = listReceta.get(0).getDosis();
		
		
		List<String> duracion = listReceta.get(0).getDuracion();
		
		
		List<String> notas = listReceta.get(0).getNotas();
		
		// write table row data
		for (int i=0;i<medicamento.size();i++) {
			table.addCell(String.valueOf(i+1));
			table.addCell(medicamento.get(i));
			table.addCell(dosis.get(i));
		    table.addCell(duracion.get(i));
			table.addCell(notas.get(i));
		}
		
		doc.add(table);
		
		
		doc.add(new Paragraph(" "));
		doc.add(new Paragraph(" "));
		doc.add(new Paragraph(" "));
		doc.add(new Paragraph(" "));
		doc.add(new Paragraph("                     FIRMA Y SELLO"));
		doc.add(new Paragraph(" "));
		doc.add(new Paragraph(" "));
		doc.add(new Paragraph(" "));
		doc.add(new Paragraph(" "));
		doc.add(new Paragraph("                     ----------------------------"));
		doc.add(new Paragraph("                     Nombre del Médico/Doctor"));
		
		
		
		
	}
	
	
	
	

} 
   
   
    
  

Hasta este momento asi se veria el proyecto.
java project