首页 > 编程知识 正文

Java生成PDF框架,java实现doc转换pdf

时间:2023-05-03 14:53:32 阅读:20381 作者:2060

首先在项目中引入MAVEN依赖关系

ependencygroupidcom.itext pdf/groupidartifactiditextpdf/artifactidversion5.5. 13/version/dependencydepencygrouptioup artifactidversion5.2.0/version/dependency在控制层中添加以下方法即可

公共语音下载(httpservletresponseresponse, http servlet request request throws exception (bytearrayoutputstreambaos=n nows/*解决中文无法显示: iTextAsian.jar包的字体*/base font base font=base font.create font (' stsong STD-light ',' UniGB-UCS2-H ',font font _ normal=new font (base font ) font font _ normal2=new font (基本字体,14,Font.NORMAL ); font font _ bold=new font (基字体,12,Font.BOLD ); /*时间* /日期日期=new date (; simpledateformatsimpledateformat=newsimpledateformat (' yyyy日MM月dd日'); 字符串格式=simple date format.format (date; /*要在第一步中创建文档实例并自定义页面大小,请*.com.itext pdf.text.document document=newcom.itext pdf.text.document /*步骤获取pdf写入器实例*/pdf写入器写入器=pdf写入器. getinstance (文档,baos ); //pdf writer.getinstance (文档,baos ); document.set margins (64、64、36、36 ); /*步骤3 (文档(*/document.open ); /*步骤4添加段落内容*/paragraph paragraph=new paragraph ('你想写的标题n ' ',new font (基本字体,25,字体. bold ) ); paragraph.set alignment (element.align _ center ); Paragraph.setspacingafter(10; document.add(paragraph; paragraph=new Paragraph (证书_XXX_ )身份证号码XXXXX )是我们的员工,其工作号码_XXXX_、所属公司_XXXX_,用于申请购车优惠

',font_normal; document.add(paragraph; Paragraph=newParagraph('n ' '有效期1个月',font_normal2); document.add(paragraph; String kg=' '; Paragraph=newParagraph((n(kg ) xxxx有限公司工会委员会),font_normal2); document.add(paragraph; Paragraph=newParagraph(kg '下载日期: " format,font _ normal2" document.add (paragraph; 创建image对象。 Image image=Image.getInstance ('要导入的图像路径) ); //图像//设置image对象的输出位置pa.getasnumber(pa.size(-1 ).floatValue )是此页面的pdf坐标轴的y轴的最大值image.setabsoluteposition //图像大小document.add(image; 输出流输出=null; try {浮动页面宽度=document.right () document.left ); 获取//pdf内容正文的页面宽度float pageHeight=document.top () document.bottom ); 获取//pdf内容正文页的高度用于设置//水印字体格式的base font base=base font.create font (' stsong-light ',' UniGB-UCS2-H) fontwatermarkfont=newfont(base,15,Font.BOLD,BaseColor.LIGHT_GRAY ); pdfcontentbytewatermarkpdfcontent=writer.getdirectcontentunder (; Phrase phrase=new Phrase ('我是水印',水印字体); //5行6列for(doubleI=0; i1; i=0.2 ) floatf=(float ) (0.1 i ); for(doublej=0; j1d; j=0.15 (浮动=)浮动) ) 0.2 j ); column text.show text aligned (watermarkpdfcontent,Element.ALIGN_CENTER,phrase,pageWidth*f,pageHeight*h, 45 }//2行3列column text.show text aligned (watermarkpdfcontent,Element.ALIGN_CENTER,phrase,pageWidth*0.25f, pageWidth*0.25f column text.show text aligned (watermarkpdfcontent,Element.ALIGN_CENTER,phrase,page width * 0.25 f ), page height * 0.5f column text.show text aligned (watermarkpdfcontent,Element.ALIGN_CENTER,phrase,pageWidth*0.25f, page height * 0.8f column text.show text aligned (watermarkpdfcontent,Element.ALIGN_CENTER,phrase,pageWidth*0.65f, page height * 0.2f column text.show text aligned (watermarkpdfcontent,Element.ALIGN_CENTER,phrase,pageWidth*0.65f, page height * 0.5f column text.show text aligned (watermarkpdfcontent,Element.ALIGN_CENTER,phrase,pageWidth*0.65f,) //请求回复类型response.setheader (content-disposition )、)附件; filename=' new String ('我想要你. pdf'.getBytes )、StandardCharsets.ISO_8859_1) ) response.set contentlengtent out=response.getOutputStream (; BAOS.writeto(out ); }catch(exceptione ) { e.printStackTrace ); } finally { out.flush (); out.close (; }以上是JAVA生成PDF的方法

转载: https://blog.csdn.net/weixin _ 48506585/article/details/107244063

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。