@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);
            }
        }
    }

enter image description here

The pdf which is downloaded in system enter image description here

using the pdfbox version as个 Org.apache.pdfbox Pdfbox 3.0.1

PDF downloaded is attached

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和@Tilman感谢你们的支持.这是我更新的最终解决方案,它可能会帮助正在寻找解决方案的人.

    @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);
            }
        }

Java相关问答推荐

BiPredicate和如何使用它

RxJava PublishSubject缓冲区元素超时

Quarkus keycloat配置不工作.quarkus. keycloak. policy—enforcer. enable = true在. yaml表示中不工作

如何调用Firebase Realtime Database中的子图像列表到android studio中的回收器视图?

Java记录的不同序列化/反序列化

我需要生成一个文件来整合每个特性执行的所有JSON结果

如何才能使我的程序不会要求两次输入?

如何获得执行人?

第二次按下按钮后,我需要将按钮恢复到其原始状态,以便它可以再次工作

如何从JNI方法正确调用NSOpenPanel以在正确的线程上运行?

Jenv-相同的Java版本,但带有前缀

在JDK 1.8源代码中,为什么使用A-B 0来确定哪个更大,而不是A B?

使SLF4J在Android中登录到Logcat,在测试中登录到控制台(Gradle依赖问题)

将PNG转换为位图自定义十六进制字符串

如何将Java文档配置为在指定的项目根目录中生成?

一对多关系和ID生成

为什么我不能建立输入/输出流?Java ServerSocket

Java 17与Java 8双重表示法

HBox内部的左对齐按钮(如果重要的话,在页码内)

spring 数据Elastic search 与 spring 启动数据Elastic search 之间的区别是什么?