@RequestMapping(value = "/displaypdf.action", method = RequestMethod.GET)
public void displaypdf(HttpServletRequest p_objRequest, HttpServletResponse p_objResponse,
@RequestParam("filename") String p_sFilename) throws Exception {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(ClmSrDebugConstant.DISPLAYPDF);
}
String l_sReportName;
Blob l_bContent = null;
String l_sTcn = (String) p_objRequest.getSession().getAttribute(ClmSrConstant.TCN);
ServletOutputStream l_objServletOutputStream = p_objResponse.getOutputStream();
Map<String, Object> l_mapConfigFile = new HashMap<String, Object>();
Document document = null;
try {
l_mapConfigFile = m_objClinicalService.retrieveMap(l_sTcn);
Iterator<String> it = l_mapConfigFile.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
if (key.contains(p_sFilename)) {
l_bContent = (Blob) l_mapConfigFile.get(key);
}
}
l_sReportName = p_sFilename;
if (l_bContent != null) {
p_objResponse.setContentType("application/pdf");
byte[] l_bytes = l_bContent.getBytes(1, (int) l_bContent.length());
InputStream l_inptStrm = l_bContent.getBinaryStream();
PDDocument doc = Loader.loadPDF(l_bytes);
PDPage page = new PDPage();
doc.addPage(page);
String fileName = "C:\\Users\\dhs\\new.pdf";
PDFTextStripper stripper = new PDFTextStripper();
String text = stripper.getText(doc);
PDPageContentStream contentStream = new
PDPageContentStream(doc, page);
contentStream.beginText();
contentStream.setFont(new PDType1Font(Standard14Fonts.FontName.TIMES_ROMAN), 12);
contentStream.setLeading(14.5f);
contentStream.newLineAtOffset(25, 500);
text = text.replace("\n", "").replace("\r", "");
contentStream.showText(text);
contentStream.endText();
contentStream.close();
doc.save(fileName);
}
else {
p_objResponse.setContentType(ClmSrConstant.TXT_CNTN_TYP);
p_objResponse.getOutputStream().print(ClmSrConstant.NLP_REPORT_ERROR);
}
l_objServletOutputStream.flush();
if (doc != null) {
doc.close();
}
l_objServletOutputStream.close();
} catch (Exception exExp) {
if (LOGGER.isDebugEnabled()) {
LOGGER.error(ClmSrDebugConstant.EXCEP_DISPLAYPDF, exExp);
}
}
}
The pdf which is downloaded in system
using the pdfbox version as org.apache.pdfbox pdfbox 3.0.1
Two chalenges i am facing with pdfbox is
1. i just only need to show the bytes data to pdf, no addition or updation to the pdf required, just the exact pdf which i get through the bytes data
2. i am able to save the pdf in pdfbox , which is through doc.save() but , requirement is to display it in UI, not to save... looked for solution but no help as of now
@mkl and @Tilman Thanks for the support .. this is the final solution which i updated that it might help someone who's looking for solution.
@RequestMapping(value = "/displaypdf.action", method = RequestMethod.GET)
public void displaypdf(HttpServletRequest p_objRequest, HttpServletResponse p_objResponse,
@RequestParam("filename") String p_sFilename) throws Exception {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(ClmSrDebugConstant.DISPLAYPDF);
}
String l_sReportName;
Blob l_bContent = null;
String l_sTcn = (String) p_objRequest.getSession().getAttribute(ClmSrConstant.TCN);
ServletOutputStream l_objServletOutputStream = p_objResponse.getOutputStream();
Map<String, Object> l_mapConfigFile = new HashMap<String, Object>();
try {
l_mapConfigFile = m_objClinicalService.retrieveMap(l_sTcn);
Iterator<String> it = l_mapConfigFile.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
if (key.contains(p_sFilename)) {
l_bContent = (Blob) l_mapConfigFile.get(key);
}
}
l_sReportName = p_sFilename;
PDDocument doc = null;
if (l_bContent != null) {
p_objResponse.setContentType("application/pdf");
byte[] l_bytes = l_bContent.getBytes(1, (int) l_bContent.length());
//InputStream l_inptStrm = l_bContent.getBinaryStream();
//text added by pdfbox changes begining
doc = Loader.loadPDF(l_bytes);
PDDocumentInformation pdd = doc.getDocumentInformation();
pdd.setTitle(l_sReportName);
doc.save(l_objServletOutputStream);
//text added by pdfbox changes end
// if we are not using pdfbox we can directly pass the bytes to the outputstream
//l_objServletOutputStream.write(l_bytes);
} else {
p_objResponse.setContentType(ClmSrConstant.TXT_CNTN_TYP);
l_objServletOutputStream.print(ClmSrConstant.NLP_REPORT_ERROR);
}
l_objServletOutputStream.flush();
if (doc != null) {
doc.close();
}
l_objServletOutputStream.close();
} catch (Exception exExp) {
LOGGER.error(ClmSrDebugConstant.EXCEP_DISPLAYPDF, exExp);
}
}