可以做平面设计兼职的网站,群辉docker安装wordpress,长沙营销网站设计,盘锦企业网站建设虽说网上有很多可以在线导出Pdf或者word或者转成png等格式的工具#xff0c;但是我觉得还是得了解知道是怎么实现的。一来#xff0c;在线免费转换工具#xff0c;是有容量限制的#xff0c;达到一定的容量时#xff0c;是不能成功导出的;二来#xff0c;业务需求#x… 虽说网上有很多可以在线导出Pdf或者word或者转成png等格式的工具但是我觉得还是得了解知道是怎么实现的。一来在线免费转换工具是有容量限制的达到一定的容量时是不能成功导出的;二来业务需求特别是OA方面的项目报表不单单只是在线通过浏览器登录对应的站点浏览还需有时导出Pdf格式(pdf格式为通用格式无论是浏览器还是其他工具都能打开因此特别是做项目实施的除了用word编写文档之外通常还导出一下pdf,这样一来保证给老板看时不会因为某种原因打不开文件看不到对应的实质内容。 直接开门见山: 一、导入Maven依赖 project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdsite.duanzy/groupIdartifactIdstudy_pdf/artifactIdversion0.0.1-SNAPSHOT/versionpackagingwar/packagingnamestudy_pdf/namedescription /propertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesdependenciesdependencygroupIdcom.itextpdf/groupIdartifactIditextpdf/artifactIdversion5.5.10/version/dependencydependencygroupIdcom.itextpdf/groupIdartifactIditext-asian/artifactIdversion5.2.0/version/dependencydependencygroupIdorg.bouncycastle/groupIdartifactIdbcprov-jdk15on/artifactIdversion1.54/version/dependencydependencygroupIdjavax/groupIdartifactIdjavaee-api/artifactIdversion7.0/versionscopeprovided/scope/dependencydependencygroupIdorg.glassfish.web/groupIdartifactIdjavax.servlet.jsp.jstl/artifactIdversion1.2.2/version/dependency/dependenciesbuildpluginspluginartifactIdmaven-compiler-plugin/artifactIdversion2.3.2/versionconfigurationsource1.8/sourcetarget1.8/target/configuration/pluginpluginartifactIdmaven-war-plugin/artifactIdversion2.2/versionconfigurationversion3.1/versionfailOnMissingWebXmlfalse/failOnMissingWebXml/configuration/plugin/plugins/build
/project 二、编写Java代码 package site.duanzy.pdf.demo;import java.io.FileNotFoundException;
import java.io.FileOutputStream;import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;public class TestPDFDemo1 {public static void main(String[] args) throws FileNotFoundException, DocumentException {// 1.新建document对象Document document new Document();// 2.建立一个书写器(Writer)与document对象关联通过书写器(Writer)可以将文档写入到磁盘中。// 创建 PdfWriter 对象 第一个参数是对文档对象的引用第二个参数是文件的实际名称在该名称中还会给出其输出路径。PdfWriter writer PdfWriter.getInstance(document, new FileOutputStream(E://Pdf//test.pdf));writer.flush();writer.close();// 3.打开文档document.open();// 4.添加一个内容段落document.add(new Paragraph(Hello World!));// 5.关闭文档document.close();}} package site.duanzy.pdf.demo;import java.io.FileNotFoundException;
import java.io.FileOutputStream;import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;public class TestPDFDemo2 {public static void main(String[] args) throws FileNotFoundException,DocumentException {// 创建文件Document document new Document();// 建立一个书写器PdfWriter writer PdfWriter.getInstance(document,new FileOutputStream(E://Pdf//test2.pdf));// 打开文件document.open();// 添加内容document.add(new Paragraph(Some content here));// 设置属性// 标题document.addTitle(this is a title);// 作者document.addAuthor(Mr You);// 主题document.addSubject(this is subject);// 关键字document.addKeywords(Keywords);// 创建时间document.addCreationDate();// 应用程序document.addCreator(hd.com);// 关闭文档document.close();// 关闭书写器writer.close();}
} package site.duanzy.pdf.demo;import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;public class TestPDFDemo3 {public static void main(String[] args) throws DocumentException,IOException {// 创建文件Document document new Document();// 建立一个书写器PdfWriter writer PdfWriter.getInstance(document,new FileOutputStream(E://Pdf//test3.pdf));// 打开文件document.open();// 添加内容document.add(new Paragraph(HD content here));// 图片1Image image1 Image.getInstance(E://Demo//workspace//java_pdf//src//main//webapp//images//test.png);// 设置图片位置的x轴和y周image1.setAbsolutePosition(100f, 550f);// 设置图片的宽度和高度image1.scaleAbsolute(200, 200);// 将图片1添加到pdf文件中document.add(image1);// 图片2Image image2 Image.getInstance(new URL(https://gss0.bdstatic.com/-4o3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D220/signbda2ad09277f9e2f74351a0a2f31e962/0b46f21fbe096b63ea0d41bf0c338744eaf8accc.jpg));// 将图片2添加到pdf文件中document.add(image2);// 关闭文档document.close();// 关闭书写器writer.close();}
} 以上值列举三个测试代码更多可以参考我的githubhttps://github.com/youcong1996/study_simple_demo.git 记住该示例代码在我的侧分支分支名为java-pdf。 三、运行测试代码的最后结果(结果最后是生成pdf一般控制台不报错就表示OK 全面示例运行后生成的文件如下图所示: