精华内容
下载资源
问答
  • java操作PDF

    2018-07-01 12:31:50
    java操作PDF提取PDF标题作者文档时间以及各种详细信息
  • Java操作PDF

    2013-03-18 13:08:20
    Java操作PDF 成功的DEMO 每一行都有注释,有jar包,适合刚接触的!可以直接运行,不会让你失望!
  • Java操作pdf

    2015-03-04 11:30:45
    首先,记录一下Java操作pdf文件。 Java操作pdf文件,我用的是iText,iText是一个开源的PDF工具,小巧而又便捷。虽然虽然jacob中也有关于PDF的方法,但是过于复杂,不便于使用。 文章最后附件中是我自己下载的...
    最近在研究用Java来操作文件的技术,在这里做一下记录。首先,记录一下Java操作pdf文件。

    Java操作pdf文件,我用的是iText,iText是一个开源的PDF工具,小巧而又便捷。虽然虽然jacob中也有关于PDF的方法,但是过于复杂,不便于使用。
    文章最后附件中是我自己下载的iText的jar包,方便大家使用。

    iText:
    官网:http://www.lowagie.com/iText/
    下载:https://github.com/itext/itextpdf/releases
    最新版:iText 5.5.4

    以下为测试代码:
    package com.lafecat;

    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 HelloPdf {

    /**
    * @param args
    */
    public static void main(String[] args) {

    Document doc = new Document();
    try {
    PdfWriter.getInstance(doc,new FileOutputStream("D:\\Hello.pdf"));
    doc.open();
    doc.add(new Paragraph("Hello,itext"));
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (DocumentException e) {
    e.printStackTrace();
    }finally{
    doc.close();
    }
    System.out.println("运行成功了!快去看看吧");
    }

    }

    代码偏于简单,复杂的操作还在研究中。
    展开全文
  • Java 操作PDF

    2019-08-06 01:36:57
    NULL 博文链接:https://tianqiushi.iteye.com/blog/2092134
  • JAVA操作PDF

    2015-08-03 02:27:36
    Java操作PDF文档(PDFBox) ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 ...
     Java操作PDF文档(PDFBox)

    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    package com.leixinhui.test;
     
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.io.OutputStreamWriter;
    import java.io.Writer;
     
    import org.apache.pdfbox.pdmodel.PDDocument;
    import org.apache.pdfbox.util.PDFTextStripper;
     
    public class Test {
     
        /**
         * @param args
         * @throws Exception
         * @throws SQLException
         */
        public static void main(String[] args) throws Exception {
            String strFile = "F:\\代码\\Java\\Java操作PDF文档\\PDFbox使用教程.pdf";
            new Test().getText(strFile);
        }
     
        /**
         * 打印pdf文档文本内容
         * @param strFile pdf文件
         * @throws Exception
         */
        private void getText(String strFile) throws Exception{
            boolean sort = false;
            int startPage = 1;
            int endPage = Integer.MAX_VALUE;
             
            InputStream inputStream = null;
            Writer outWriter = null;
            PDFTextStripper textStripper = null;
            PDDocument document = null;
            try {
                inputStream = new FileInputStream(strFile);
                outWriter = new OutputStreamWriter( System.out );
                 
                textStripper = new PDFTextStripper();
                document = PDDocument.load(inputStream);
                 
                textStripper.setSortByPosition( sort );
                textStripper.setStartPage( startPage );
                textStripper.setEndPage( endPage );
                 
                textStripper.writeText(document, outWriter);
            catch (Exception e) {
                e.printStackTrace();
                throw e;
            finally {
                inputStream.close();
                outWriter.flush();
                outWriter.close();
            }
        }
    }

    备注:使用Apache PDFBox 1.6.0

    展开全文
  • java 操作 PDF

    千次阅读 2018-07-12 14:09:57
    类似于制作这样的发票技术选型我选择java在网上寻找了一些操作PDF的框架决定用iText制作, 因为它比较活跃, 而且后期做签章和插入图片二维码都有着现成的接口.大体想了下难点都在什么地方, 插入图片,商品太多时候...

    近来收到一个需求, 制作 PDF 制作发票.

    类似于制作这样的发票


    技术选型我选择java

    在网上寻找了一些操作PDF的框架决定用iText制作, 因为它比较活跃, 而且后期做签章和插入图片二维码都有着现成的接口.

    大体想了下难点都在什么地方, 插入图片,商品太多时候的分页问题,数字签章........

    于是我就开始尝试着使用 iText的新建操作pdf,

    新建maven工程

    加入依赖

    <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
    <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.20</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.3.2</version>
    </dependency>

    起初准备自己画这个table, 后来花了几个都很丑于是参考网上发可以先制作模板,预设表单值再往里面填入表单数据.

    具体可以通过Adobe Acrobat Pro去制作


    制作好的模板



    JAVA操作PDF

    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    import com.boss.hmc.bean.Commodity;
    import com.boss.hmc.bean.Ticket;
    import com.itextpdf.text.DocumentException;
    import com.itextpdf.text.Image;
    import com.itextpdf.text.Rectangle;
    import com.itextpdf.text.pdf.AcroFields;
    import com.itextpdf.text.pdf.BaseFont;
    import com.itextpdf.text.pdf.PdfContentByte;
    import com.itextpdf.text.pdf.PdfReader;
    import com.itextpdf.text.pdf.PdfStamper;
    
    public class PDFTemplet2Ticket {
    //	public static String modelPath = "D:\\work\\pdf\\发票模板123_bak.pdf";
    	public static String modelPath = "D:\\work\\pdf\\模板1.pdf";
    	public static String modelPath2 = "D:\\work\\pdf\\模板2.pdf";
    	private static String imagePath = "D:\\study\\practice\\chapter.png";
    
    	public void ticketInPDF(Ticket ticket, File target) throws IOException, DocumentException {
    		if (target == null)
    			target.createNewFile();
    		PdfReader reader = new PdfReader(modelPath);
    		ByteArrayOutputStream bos = new ByteArrayOutputStream();
    		// 读pdf
    		PdfStamper ps = new PdfStamper(reader, bos);
    		AcroFields s = ps.getAcroFields();
    
    		ArrayList<BaseFont> fontList = new ArrayList<BaseFont>();
    //        BaseFont bf = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
    		BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_CACHED);
    
    		insertImage(s, "quickMark", ps);
    
    		fontList.add(bf);
    		s.setSubstitutionFonts(fontList);
    
    		s.setField("machineIdentificNumber", ticket.getMachineIdentificNumber());
    		s.setField("invoiceNumber", ticket.getInvoiceNumber());
    		s.setField("invoiceCode", ticket.getInvoiceCode());
    		s.setField("consumerPassword", ticket.getConsumerPassword());
    		String date = ticket.getInvoiceDate();
    		s.setField("invoiceDate",
    				new StringBuilder(date.substring(0, 4)).append("    ").append(date.substring(4, 6)).append("    ")
    						+ date.substring(6));
    		s.setField("checkCode", ticket.getCheckCode());
    		s.setField("consumerName", ticket.getConsumerName());
    		s.setField("consumerIdentityNumber", ticket.getConsumerIdentityNumber());
    		s.setField("consumerAddTel", ticket.getConsumerAddTel());
    		s.setField("consumerAccount", ticket.getConsumerAccount());
    		s.setField("figure", "¥"+ticket.getFigure() + " .");
    		s.setField("tax", "¥"+ticket.getTax() + " .");
    		s.setField("sumFigureTax", "¥"+ticket.getSumFigureTax() + " ");
    		s.setField("chineseSumFigureTax", ticket.getChineseSumFigureTax());
    
    		List<Commodity> list = ticket.getCommodities();
    		if (list != null && list.size() >= 5) {
    			s.setField("name0", " (详见销货清单)");
    			addDetailItems(list);
    		} else if (list != null && list.size() < 5) {
    			addItems(list, s);
    		}
    
    		s.setField("merchantName", ticket.getMerchantName());
    		s.setField("merchantIdentityNumber", ticket.getMerchantIdentityNumber());
    
    		s.setField("merchantAddTel", ticket.getMerchantAddTel());
    		s.setField("merchantAccount", ticket.getMerchantAccount());
    		s.setField("receiver", ticket.getReceiver());
    		s.setField("proofreader", ticket.getProofreader());
    		s.setField("drawer", ticket.getDrawer());
    
    		ps.setFormFlattening(true);
    		ps.close();
    
    		FileOutputStream fos = new FileOutputStream(target);
    		fos.write(bos.toByteArray());
    		fos.close();
    	}
    
    	private void addDetailItems(List<Commodity> list) throws IOException, DocumentException {
    		int size = list.size();
    		//每页最多的条数
    		int pageSize = 20;
    		//总页数
    		int total = size/pageSize + (size%pageSize > 0 ? 1 : 0);
    		for (int i = 0; i < total; i++) {
    			int nowPageSize = pageSize;
    			//末页条数
    			if(i == total-1) nowPageSize = (size%pageSize == 0 ? pageSize : size % pageSize);
    			printEveryDetails(list,i*pageSize,i,nowPageSize,total);
    		}
    	}
    	
    	private void printEveryDetails(List<Commodity> list, int start, int page, int size, int total) throws IOException, DocumentException {
    		PdfReader reader = new PdfReader(modelPath2);
    		ByteArrayOutputStream bos = new ByteArrayOutputStream();
    		// 读pdf
    		PdfStamper ps = new PdfStamper(reader, bos);
    		AcroFields s = ps.getAcroFields();
    		s.addSubstitutionFont(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_CACHED));
    		for (int i = start,j = 1; i < start+size; i++,j++) {
    			Commodity c = list.get(i);
    			s.setField("no", j+"");
    			s.setField("name" + i, c.getName());
    			s.setField("modelType" + i, c.getModelType());
    			s.setField("unit" + i, c.getUnit());
    			s.setField("count" + i, c.getCount() + "");
    			s.setField("price" + i, c.getPrice().toString());
    			s.setField("figure" + i, c.getFigure().toString());
    			s.setField("taxRate" + i, c.getTaxRate());
    			s.setField("taxMoney" + i, c.getTaxMoney() + "");
    		}
    		ps.setFormFlattening(true);
    		ps.close();
    		FileOutputStream fos = new FileOutputStream(new File("detail"+page));
    		fos.write(bos.toByteArray());
    		fos.close();
    	}
    
    	private void addItems(List<Commodity> list, AcroFields s) throws IOException, DocumentException {
    		for (int i = 0; i < list.size(); i++) {
    			Commodity c = list.get(i);
    			s.setField("name" + i, c.getName());
    			s.setField("modelType" + i, c.getModelType());
    			s.setField("unit" + i, c.getUnit());
    			s.setField("count" + i, c.getCount() + "");
    			s.setField("price" + i, c.getPrice().toString());
    			s.setField("figure" + i, c.getFigure().toString());
    			s.setField("taxRate" + i, c.getTaxRate());
    			s.setField("taxMoney" + i, c.getTaxMoney() + "");
    		}
    	}
    
    	public static void generatePDF() {
    		 PDFMergerUtility PDFmerger = new PDFMergerUtility();
    		 
    	}
    	
    	public static void insertImage(AcroFields form, String fieldName, PdfStamper stamper)
    			throws DocumentException, IOException {
    //		form.addSubstitutionFont(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED));
    		// 通过域名获取所在页和坐标,左下角为起点
    		int pageNo = form.getFieldPositions(fieldName).get(0).page;
    		Rectangle signRect = form.getFieldPositions(fieldName).get(0).position;
    		float x = signRect.getLeft();
    		float y = signRect.getBottom();
    		// 读图片
    		Image image = Image.getInstance(imagePath);
    		// 获取操作的页面
    		PdfContentByte under = stamper.getOverContent(pageNo);
    		// 根据域的大小缩放图片
    		image.scaleToFit(signRect.getWidth(), signRect.getHeight());
    		// 添加图片
    		image.setAbsolutePosition(x, y);
    		under.addImage(image);
    	}
    
    	public static void main(String[] args) throws IOException, DocumentException {
    		// 读模板
    		Ticket ticket = Xml2Ticket.xml2ticket(new File("example.txt"));
    		File file = new File("D:\\work\\pdf\\target.pdf");
    		// 打印pdf
    		new PDFTemplet2Ticket().ticketInPDF(ticket, file);
    	}
    }

    制作好的初期效果,细节慢慢调整




    展开全文
  • java操作pdf

    2018-08-08 09:50:15
    新项目里有个pdf的生成文件,折腾了两天,终于可以 用了,现在 记下来,方便后来者。 有些代码重复的地方,大家凑合着看了,项目太急,也不想优化了。 里面大部分地方都做了注释,一看就会明白了。 废话不多说,...

    新项目里有个pdf的生成文件,折腾了两天,终于可以 用了,现在 记下来,方便后来者。

    有些代码重复的地方,大家凑合着看了,项目太急,也不想优化了。

    里面大部分地方都做了注释,一看就会明白了。

    废话不多说,直接上代码

    先添加依赖

    <itextpdf.version>5.5.12</itextpdf.version>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itextpdf</artifactId>
        <version>${itextpdf.version}</version>
    </dependency>
    
     
    import com.itextpdf.text.*;
    import com.itextpdf.text.pdf.PdfPCell;
    import com.itextpdf.text.pdf.PdfPTable;
    import com.itextpdf.text.pdf.PdfWriter;
    import org.springframework.util.ResourceUtils;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
     
    public class PdfUtil {
        
        public static final Document document = new Document(PageSize.A4, 36, 72, 58, 100);
    
        public void createPdf() {
            try {
                PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("b.pdf"));
                document.open();
                document.newPage();
                // 标题
                document.add(new Paragraph("Customer Name and DL Number in header"));
                // 中间段落
                document.add(midelParagraph());
                document.add(getParagraph2("1801W Jefferson"));
                document.add(getParagraph2("Phonenix AZ 85007"));
                document.add(new Paragraph("\n"));
                //第一个table
                document.add(firstTable(null, null, null, null, null, null));
                document.add(new Paragraph("\n"));
                //第二个table
                document.add(secondTable(null, null, null, null, null, null, null));
                document.add(new Paragraph("\n"));
                //第三个table
                document.add(thirdTable(null, null, null, null, null,
                        null, null, null, null, null, null, null, null,
                        null, null, null));
                document.add(new Paragraph("\n"));
                document.newPage();
                document.add(new Paragraph("Customer Name and DL Number in header"));
                document.add(new Paragraph("\n"));
                document.add(fourth(null, null, null, null, null, null));
                document.add(new Paragraph("\n"));
                document.add(new Paragraph("\n"));
                document.add(fifthTitle());
                document.add(fifth());
                document.add(new Paragraph("\n"));
                document.add(new Paragraph("\n"));
                document.add(sixthTitle());
                document.add(sixth());
                document.add(new Paragraph("\n"));
                document.add(new Paragraph("\n"));
                document.add(seventhTitle("Saturday,July 15,2017"));
                document.add(sixth());
                document.add(new Paragraph("\n"));
                document.add(new Paragraph("\n"));
                document.add(seventhTitle("Thursday,Auguest17,2017"));
                List<String> strs = new ArrayList<>();
                strs.add("Event Row");
                strs.add("Time");
                strs.add("Description");
                strs.add("BAC");
                document.add(titleListFour(strs));
    
    
                document.close();
                writer.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    
        /**
         * 设置下划线
         *
         * @return
         */
        public static Chunk getChunk(String str) {
            // 创建Chunk对象,设置下划线的厚度为0.1
            Chunk underline = new Chunk(str);
            underline.setUnderline(0.1f, -1f);
            // 创建Chunk对象,设置下划线的厚度为1
            Chunk strike = new Chunk(str);
            strike.setUnderline(1f, 3f);
            return underline;
        }
    
        /**
         * 设置段落居中
         *
         * @return
         */
        public static Paragraph getParagraph(String str) {
            Paragraph pg = new Paragraph(str, FontFactory.getFont(FontFactory.COURIER, 16, Font.BOLD));
            pg.setAlignment(1);
            return pg;
        }
    
        /**
         * 设置下划线段落居中
         *
         * @param str
         * @return
         */
        public static Paragraph getParagraph2(String str) {
            Paragraph pg = new Paragraph(getChunk(str));
            pg.setAlignment(1);
            return pg;
        }
    
        /**
         * 设置下划线段落居中加大字体
         *
         * @param str
         * @return
         */
        public static Paragraph getParagraph3(String str) {
            Paragraph pg = new Paragraph(getChunk(str));
            pg.setFont(FontFactory.getFont(FontFactory.COURIER, 18, Font.BOLD));
            pg.setAlignment(1);
            return pg;
        }
    
    
        /**
         * 中间段落
         *
         * @return
         * @throws
         */
        public static Paragraph midelParagraph() {
            Paragraph manufacturer = getParagraph2("Manufacturer");
            manufacturer.setSpacingBefore(25); // 设置行距
            return manufacturer;
        }
    
    
        /**
         * 照片
         *
         * @throws
         * @throws
         * @throws DocumentException
         */
        public static Document headPicDocument() {
            try {
                Image jpg = Image.getInstance("/home/acer/upload/1c98260a-1e10-44ba-8d62-cde4b782d8cb.png");
                jpg.setAlignment(Image.ALIGN_CENTER);
                document.add(jpg);
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return document;
    
    
        }
    
    
        //第getChunk一个table
        public static PdfPTable firstTable(String name, String dl, String address, String phoneNumber, String orderingAgency, String pic) {
    
    
            // PDFTable类似于html的表格文件,但是只有列的概念,定义列的数量,不能定义行的数量。
            // 创建一个两列的表格
            PdfPTable table = new PdfPTable(2);
            int width11[] = {70, 30};
            try {
                table.setWidths(width11);
            } catch (DocumentException e) {
                e.printStackTrace();
            }
            // 3、创建左边数据表格PdfPTable iTable,划分为N列
            PdfPTable leftTable = new PdfPTable(1);// 创建左边表格
            // 4、往左边表格中写入数据,加入iTable中
            leftTable.setWidthPercentage(10f);
            Paragraph customer_Information = new Paragraph();
            customer_Information.add(getChunk("Customer Information:"));
            PdfPCell leftCell = new PdfPCell(customer_Information);
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            leftCell = new PdfPCell(new Paragraph("Name:" + name));
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            leftCell = new PdfPCell(new Paragraph("DL#:" + dl));
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            leftCell = new PdfPCell(new Paragraph("Address:" + address));
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            leftCell = new PdfPCell(new Paragraph("Phone Number:" + phoneNumber));
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            leftCell = new PdfPCell(new Paragraph("Ordering Agency:" + orderingAgency));
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            // 将表格加入到第一列中
            PdfPCell cell = new PdfPCell(leftTable);
            table.getDefaultCell().setBorder(0);  //去除边框
            cell.setBorder(0);
            table.addCell(cell);
            int count = pic.indexOf(".");
            // 5、创建图片对象,加入headerTable中,此处写入图片路径
            try {
                Image image = null;
                if (-1 != count) {
                    image = Image.getInstance(pic);
                } else {
                    String url = ResourceUtils.getURL("classpath:").getPath();
                    image = Image.getInstance(url + "/headPic/headPic.jpg");
                }
                table.addCell(image);
            } catch (BadElementException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            // 将主要的表格headerTable加入document文档对象中
            return table;
        }
    
    
        //第二个table
        public static PdfPTable secondTable(String name, String mpi, String address,
                                            String phoneNumber, String technician_Information, String name2, String id) {
            // PDFTable类似于html的表格文件,但是只有列的概念,定义列的数量,不能定义行的数量。
            // 创建一个两列的表格
            PdfPTable table2 = new PdfPTable(2);
            int width11[] = {50, 50};
            try {
                table2.setWidths(width11);
            } catch (DocumentException e) {
                e.printStackTrace();
            }
            // 3、创建左边数据表格PdfPTable iTable,划分为N列
            PdfPTable leftTable = new PdfPTable(1);// 创建左边表格
            // 4、往左边表格中写入数据,加入iTable中
            leftTable.setWidthPercentage(10f);
            Paragraph ServiceCenter = new Paragraph();
            ServiceCenter.add(getChunk("ServiceCenter:"));
            PdfPCell leftCell = new PdfPCell(ServiceCenter);
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            leftCell = new PdfPCell(new Paragraph("Name:" + name));
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            leftCell = new PdfPCell(new Paragraph("MPI :" + mpi));
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            leftCell = new PdfPCell(new Paragraph("Address: " + address));
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            leftCell = new PdfPCell(new Paragraph("Phone Number: " + phoneNumber));
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            // 将表格加入到第一列中
            PdfPCell cell = new PdfPCell(leftTable);
            table2.getDefaultCell().setBorder(0);  //去除边框
            cell.setBorder(0);
            table2.addCell(cell);
            PdfPTable rightTable = new PdfPTable(1);// 创建左边表格
            rightTable.setWidthPercentage(10f);
            Paragraph technicianInformation = new Paragraph();
            technicianInformation.add(getChunk("Technician Information:" + technician_Information));
            PdfPCell rightCell = new PdfPCell(technicianInformation);
            rightCell.setBorder(0);
            rightTable.addCell(rightCell);
            rightCell = new PdfPCell(new Paragraph("(For Callbration Checks/Installs/Removals only)"));
            rightCell.setBorder(0);
            rightTable.addCell(rightCell);
            rightCell = new PdfPCell(new Paragraph("Name:" + name2));
            rightCell.setBorder(0);
            rightTable.addCell(rightCell);
            rightCell = new PdfPCell(new Paragraph("ID#:" + name2));
            rightCell.setBorder(0);
            rightTable.addCell(rightCell);
            PdfPCell cell2 = new PdfPCell(rightTable);
            table2.getDefaultCell().setBorder(0);  //去除边框
            cell2.setBorder(0);
            table2.addCell(rightTable);
            // 将主要的表格headerTable加入document文档对象中
            return table2;
        }
    
    
        /**
         * 第三个table
         *
         * @return
         */
        public static Element thirdTable(String device_Model, String device_Firmware_Version, String Device_software_Version,
                                         String install_Date, String date_of_Service, String removal_Date, String report_Period,
                                         String next_Service_Date, String as_Found, String reference_Check, String reference_Check2, String handset_or_Relay_In,
                                         String handset_or_relay_Out, String year_Make_Model, String plate, String vin) {
            PdfPTable table3 = new PdfPTable(2);
            int width11[] = {50, 50};
            // 3、创建左边数据表格PdfPTable iTable,划分为N列
            PdfPTable leftTable = new PdfPTable(1);// 创建左边表格
            // 4、往左边表格中写入数据,加入iTable中
            leftTable.setWidthPercentage(10f);
            Paragraph ServiceCenter = new Paragraph();
            ServiceCenter.add(getChunk("Device Information:"));
            PdfPCell leftCell = new PdfPCell(ServiceCenter);
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            leftCell = new PdfPCell(new Paragraph("Device/Model:" + device_Model));
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            leftCell = new PdfPCell(new Paragraph("Device Firmware Version :" + device_Firmware_Version));
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            leftCell = new PdfPCell(new Paragraph("Device software Version: " + Device_software_Version));
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            leftCell = new PdfPCell(new Paragraph("Install Date: " + install_Date));
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            leftCell = new PdfPCell(new Paragraph("Date of Service : " + date_of_Service));
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            leftCell = new PdfPCell(new Paragraph("Removal Date: " + removal_Date));
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            leftCell = new PdfPCell(new Paragraph("Report Period: " + report_Period));
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            leftCell = new PdfPCell(new Paragraph("Next Service Date: " + next_Service_Date));
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            leftCell = new PdfPCell(new Paragraph("As Found: " + as_Found));
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            leftCell = new PdfPCell(new Paragraph("Reference Check: " + reference_Check));
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            leftCell = new PdfPCell(new Paragraph("reference Check: " + reference_Check2));
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            leftCell = new PdfPCell(new Paragraph("Handset or Relay# In: " + handset_or_Relay_In));
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            leftCell = new PdfPCell(new Paragraph("Handset or Relay# Out: " + handset_or_relay_Out));
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            // 将表格加入到第一列中
            PdfPCell cell = new PdfPCell(leftTable);
            table3.getDefaultCell().setBorder(0);  //去除边框
            cell.setBorder(0);
            table3.addCell(cell);
    
            PdfPTable rightTable = new PdfPTable(1);// 创建左边表格
            rightTable.setWidthPercentage(10f);
            Paragraph technicianInformation = new Paragraph();
            technicianInformation.add(getChunk("Vehicle Information:"));
            PdfPCell rightCell = new PdfPCell(technicianInformation);
            rightCell.setBorder(0);
            rightTable.addCell(rightCell);
            rightCell = new PdfPCell(new Paragraph("Year/Make/Model :" + year_Make_Model));
            rightCell.setBorder(0);
            rightTable.addCell(rightCell);
            rightCell = new PdfPCell(new Paragraph("Plate:" + plate));
            rightCell.setBorder(0);
            rightTable.addCell(rightCell);
            rightCell = new PdfPCell(new Paragraph("Vin:" + vin));
            rightCell.setBorder(0);
            rightTable.addCell(rightCell);
            PdfPCell cell2 = new PdfPCell(rightTable);
            table3.getDefaultCell().setBorder(0);  //去除边框
            rightCell.setBorder(0);
            table3.addCell(rightTable);
            // 将主要的表格headerTable加入document文档对象中
            return table3;
        }
    
    
        //第四个table
        public static Element fourth(String reportable_Violations, String bypass_Code, String temportary_Lock_Out,
                                     String recall_Lock_Out, String failed_Rolling_Retest, String aborts) {
    
            // PDFTable类似于html的表格文件,但是只有列的概念,定义列的数量,不能定义行的数量。
            // 创建一个两列的表格
            PdfPTable table4 = new PdfPTable(1);
            // 3、创建左边数据表格PdfPTable iTable,划分为N列
            PdfPTable leftTable = new PdfPTable(1);// 创建左边表格
            // 4、往左边表格中写入数据,加入iTable中
            leftTable.setWidthPercentage(10f);
            Paragraph customer_Information = new Paragraph();
            customer_Information.add(getChunk("Summary of Events:"));
            PdfPCell leftCell = new PdfPCell(customer_Information);
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            leftCell = new PdfPCell(new Paragraph("Reportable Violations: " + reportable_Violations));
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            leftCell = new PdfPCell(new Paragraph("Bypass_Code:" + bypass_Code));
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            leftCell = new PdfPCell(new Paragraph("Temportary Lock out:" + temportary_Lock_Out));
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            leftCell = new PdfPCell(new Paragraph("Recall Lock Out:" + recall_Lock_Out));
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            leftCell = new PdfPCell(new Paragraph("Failed Rolling Retest:" + failed_Rolling_Retest));
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            leftCell = new PdfPCell(new Paragraph("Aborts:" + aborts));
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            // 将表格加入到第一列中
            PdfPCell cell = new PdfPCell(leftTable);
            cell.setBorder(0); //去除边框
            table4.addCell(cell);
            return table4;
        }
    
        /**
         * 第五个表title
         *
         * @return
         */
        public static Element fifthTitle() {
            PdfPTable table5 = new PdfPTable(1);
            PdfPTable leftTable = new PdfPTable(1);// 创建左边表格
            leftTable.setWidthPercentage(10f);
            Paragraph customer_Information = new Paragraph();
            customer_Information.add(getChunk("Summary of Violations:"));
            PdfPCell leftCell = new PdfPCell(customer_Information);
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            PdfPCell cell = new PdfPCell(leftTable);
            cell.setBorder(0);
            table5.getDefaultCell().setBorder(0);  //去除边框
            table5.addCell(cell);
            return table5;
        }
    
        /**
         * 第五个表内容
         *
         * @return
         */
    
        public static Element fifth() {
            PdfPTable table5 = new PdfPTable(6);
            int width3[] = {30, 20, 20, 20, 30, 20};
            try {
                table5.setWidths(width3);
            } catch (DocumentException e) {
                e.printStackTrace();
            }
            PdfPCell cell1 = new PdfPCell(new Paragraph("Event Row"));
            PdfPCell cell2 = new PdfPCell(new Paragraph("Evemt ID"));
            PdfPCell cell3 = new PdfPCell(new Paragraph("Date"));
            PdfPCell cell4 = new PdfPCell(new Paragraph("Time"));
            PdfPCell cell5 = new PdfPCell(new Paragraph("Description"));
            PdfPCell cell6 = new PdfPCell(new Paragraph("BAC"));
            cell1.setBorder(0);
            cell2.setBorder(0);
            cell3.setBorder(0);
            cell4.setBorder(0);
            cell5.setBorder(0);
            cell6.setBorder(0);
            table5.addCell(cell1);
            table5.addCell(cell2);
            table5.addCell(cell3);
            table5.addCell(cell4);
            table5.addCell(cell5);
            table5.addCell(cell6);
    
            for (int i = 0; i < 120; i++) {
                cell1 = new PdfPCell(new Paragraph("data"));
                cell1.setBorder(0);
                table5.addCell(cell1);
            }
            return table5;
        }
    
    
        /**
         * 第六个表title
         *
         * @return
         */
        public static Element sixthTitle() {
            PdfPTable table6 = new PdfPTable(1);
            PdfPTable leftTable = new PdfPTable(1);// 创建左边表格
            leftTable.setWidthPercentage(10f);
            Paragraph customer_Information = new Paragraph();
            customer_Information.add(getChunk("Detailed Events: "));
            PdfPCell leftCell = new PdfPCell(customer_Information);
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            leftCell = new PdfPCell(new Paragraph("Thursday,June 29,2017"));
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            PdfPCell cell = new PdfPCell(leftTable);
            cell.setBorder(0);
            table6.getDefaultCell().setBorder(0);  //去除边框
            table6.addCell(cell);
            return table6;
        }
    
        /**
         * 第六个表
         *
         * @return
         */
    
        public static Element sixth() {
            PdfPTable table6 = new PdfPTable(6);
            int width3[] = {30, 20, 30, 20, 20, 40};
            try {
                table6.setWidths(width3);
            } catch (DocumentException e) {
                e.printStackTrace();
            }
            PdfPCell cell1 = new PdfPCell(new Paragraph("Event Row"));
            PdfPCell cell2 = new PdfPCell(new Paragraph("Time"));
            PdfPCell cell3 = new PdfPCell(new Paragraph("Description"));
            PdfPCell cell4 = new PdfPCell(new Paragraph("BAC"));
            PdfPCell cell5 = new PdfPCell(new Paragraph("Photo"));
            PdfPCell cell6 = new PdfPCell(new Paragraph("GPS Coordinates"));
            cell1.setBorder(0);
            cell2.setBorder(0);
            cell3.setBorder(0);
            cell4.setBorder(0);
            cell5.setBorder(0);
            cell6.setBorder(0);
            table6.addCell(cell1);
            table6.addCell(cell2);
            table6.addCell(cell3);
            table6.addCell(cell4);
            table6.addCell(cell5);
            table6.addCell(cell6);
    
            for (int i = 0; i < 120; i++) {
                cell1 = new PdfPCell(new Paragraph("data"));
                cell1.setBorder(0);
                table6.addCell(cell1);
            }
            return table6;
        }
    
        /**
         * 第七个表
         *
         * @return
         */
        public static Element seventhTitle(String str) {
            PdfPTable table7 = new PdfPTable(1);
            PdfPTable leftTable = new PdfPTable(1);// 创建左边表格
            PdfPCell leftCell = new PdfPCell(new Paragraph(str));
            leftCell.setBorder(0);
            leftTable.addCell(leftCell);
            table7.getDefaultCell().setBorder(0);  //去除边框
            table7.addCell(leftTable);
            return table7;
    
        }
    
    
        public static Element titleListFour(List<String> strs) {
            PdfPTable table6 = new PdfPTable(4);
            try {
                int width3[] = {30, 20, 30, 20};
                PdfPCell cell = null;
                try {
                    table6.setWidths(width3);
                } catch (DocumentException e) {
                    e.printStackTrace();
                }
                for (String str : strs) {
                    cell = new PdfPCell(new Paragraph(str));
                    cell.setBorder(0);
                    table6.addCell(cell);
                }
                for (int i = 0; i < 40; i++) {
                    cell = new PdfPCell(new Paragraph("data"));
                    cell.setBorder(0);
                    Image jpg = Image.getInstance("/home/acer/upload/1c98260a-1e10-44ba-8d62-cde4b782d8cb.png");
                    jpg.setAlignment(Image.ALIGN_CENTER);
                    table6.addCell(jpg);
                }
            } catch (IOException i) {
                i.printStackTrace();
            } catch (BadElementException b) {
                b.printStackTrace();
            }
    
            return table6;
        }
    
    }

     

     

     

    展开全文
  • java 操作pdf_java操作pdf

    2021-02-12 10:26:45
    新项目里有个pdf的生成文件,折腾了两天,终于可以 用了,现在 记下来,方便后来者。有些代码重复的地方,大家凑合着看了,项目太急,也不想优化了。里面大部分地方都做了注释,一看就会明白了。废话不多说,直接上...
  • 包含了主流的Java操作pdf所需要的jar,包含icepdf.jar、pdfbox.jar等
  • java操作pdf制作电子签章

    万次阅读 热门讨论 2017-10-07 21:40:32
    java操作pdf制作电子签章电子签章简介电子签章,与我们所使用的数字证书一样,是用来做为身份验证的一种手段,泛指所有以电子形式存在,依附在电子文件并与其逻辑关联,可用以辨识电子文件签署者身份,保证文件的...
  • java操作pdf,pdf-demo

    2010-11-30 18:24:06
    pdf-demo,java操作pdf,从网上下载后,能跑放上网上备份
  • JAVA操作pdf——创建表格 一、前言 在实习的时候遇到了需要将查询到的数据构建成为PDF的情况,于是在网上查找到了相关的Java操作pdf的教程,看到大部分的文章都是使用ITextPdf操作的,于是边借此机会写个笔记记录...
  • java 操作pdf的jar包

    2013-06-01 20:22:58
    java操作pdf的jar包,很实用!生成pdf文件,很简单
  • java有很多可以操作pdf的框架,pdfbox和itext就是其中的两种pdfbox有如下作用提取文本,包括Unicode字符。和Jakarta Lucene等文本搜索引擎的整合过程十分简单。加密/解密PDF文档。从PDF和XFDF格式中导入或导出表单...
  • java操作PDF表单

    2018-02-03 19:57:01
    通过java填写pdf表单,压缩文件里面都要相关的jar包,源码也在里面

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 3,229
精华内容 1,291
关键字:

java操作pdf

java 订阅