`
8792321
  • 浏览: 39191 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

iText创建PDF文件

阅读更多
		//左、右、上、下页边距
		Document doc =new Document(PageSize.A4, 50, 50, 10, 10); 
		//A4横向 
//		Document doc = new Document(PageSize.A4.rotate());
		PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream("d:/test.pdf"));
		
		
		//添加footer需要在document.open之前
//页眉
		HeaderFooter header = new HeaderFooter(new Phrase("This is a header without a page number"), false);
		header.setAlignment(Element.ALIGN_CENTER);
		header.setBorder(Rectangle.BOTTOM);//下边框
		doc.setHeader(header);
//		页脚
		HeaderFooter footer = new HeaderFooter(new Phrase("This is page: "), true);
		footer.setAlignment(Element.ALIGN_RIGHT);
		footer.setBorder(Rectangle.NO_BORDER);//不要边框
		doc.setFooter(footer);
//标题、主题、作者、关键字、装订方式、创建者、生产者、创建日期
		doc.addTitle("标题");
		doc.addSubject("主题");
		doc.addKeywords("关键字");
		doc.addAuthor("作者");
		doc.addCreator("");
		doc.addProducer();
		doc.addCreationDate();
		doc.addHeader("html_header", "内容");//其中方法addHeader对于PDF文档无效,addHeader仅对html文档有效,用于添加文档的头信息
		
				
		doc.open();
		PdfContentByte cb = writer.getDirectContent();
		
		
		// 标题字体
		BaseFont bfTitle = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
		com.lowagie.text.Font titleFont = new com.lowagie.text.Font(bfTitle, 18, com.lowagie.text.Font.NORMAL);

		// 内容字体
		BaseFont bfComic = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
		com.lowagie.text.Font font = new com.lowagie.text.Font(bfComic, 9, com.lowagie.text.Font.NORMAL);

		Paragraph titleP = new Paragraph("儿童信息 Child Information",titleFont);
		titleP.setAlignment(Paragraph.ALIGN_CENTER);		
		doc.add(titleP);
		
//		生成条形码
		BarcodeEAN codeEAN = new BarcodeEAN();
		codeEAN.setCodeType(BarcodeEAN.EAN13);
		codeEAN.setCode("9780201615883");
		codeEAN.setAltText("9780201615883");
		Image imageEAN = codeEAN.createImageWithBarcode(cb, Color.black, Color.blue);
		doc.add(new Phrase(new Chunk(imageEAN, 10, 10)));
		
		Barcode128 code = new Barcode128();
		code.setCode("9780201615883");		
		Image code128 =code.createImageWithBarcode(cb, Color.black, Color.blue);
		doc.add(new Phrase(new Chunk(code128, 5, -25)));
		
		// 生成4列的表格
		PdfPTable table = new PdfPTable(4);
		table.setSpacingBefore(10f);//防止与上面文字重叠
		table.setWidthPercentage(100);
		
		table.addCell(new Paragraph("Children-id", font));
		PdfPCell cell = new PdfPCell(new Paragraph("09140800002", font));
		cell.setColspan(3);
		table.addCell(cell);
		// 添加第二行
		table.addCell(new Paragraph("Name(CN)", font));
		table.addCell(new Paragraph("党宁生", font));
		table.addCell(new Paragraph("Name(EN)", font));
		table.addCell(new Paragraph("DANG NING SHENG", font));

		// 添加第三行
		table.addCell(new Paragraph("Sex(CN)", font));
		table.addCell(new Paragraph("男", font));
		table.addCell(new Paragraph("Sex(EN)", font));
		table.addCell(new Paragraph("MALE", font));
		// 添加第四行
		table.addCell(new Paragraph("Note", font));
		cell = new PdfPCell(new Paragraph("儿童资料", font));
		cell.setColspan(3);
		table.addCell(cell);

		// 添加第五行
		table.addCell(new Paragraph("Pictures", font));
		Image photo = Image.getInstance("f:/eclipse09.gif");
		photo.setAlignment(Image.UNDERLYING);
		photo.scaleAbsolute(194,202);
		photo.scalePercent(30f);

		cell = new PdfPCell(photo);
//		cell.addElement(new Paragraph("文字", font));
		cell.setColspan(3);
		table.addCell(cell);
		
//		 添加第六行
		table.addCell(new Paragraph("Barcode", font));
		BarcodeDatamatrix bar = new BarcodeDatamatrix(); 
		bar.setOptions(BarcodeDatamatrix.DM_AUTO); 
		bar.generate("HEnSh0701003-2V1");
		cell = new PdfPCell(bar.createImage());
		cell.setColspan(3);
		table.addCell(cell);	

		for (PdfPRow row : (ArrayList<PdfPRow>) table.getRows()) {
			for (PdfPCell cells : row.getCells()) {
				if (cells != null) {
					cells.setPadding(10.0f);
				}
			}
		}

		doc.add(table);
		
//		内容为“hello World”、红色、斜体、COURIER字体、尺寸20的一个块
		Chunk chunk = new Chunk("Hello World", FontFactory.getFont(FontFactory.COURIER, 20, com.lowagie.text.Font.ITALIC, new Color(255, 0, 0)));
		doc.add(chunk);
		//一个段落有一个且仅有一个间距,如果你添加了一个不同字体的短句或块,原来的间距仍然有效,你可以通过SetLeading来改变间距,但是段落中所有内容将使用新的中的间距
		Paragraph p1 = new Paragraph(new Chunk("This is my first paragraph.", FontFactory.getFont(FontFactory.HELVETICA, 12)));
		p1.add("you can add strings, "); p1.add(new Chunk("you can add chunks ")); p1.add(new Phrase("or you can add phrases."));
		doc.add(p1);
		//如果我们使用FontFactory来创建字体,字体风格不会被延续,因为FontFactory使用了另外的技术构建一个字体:
		Phrase myPhrase = new Phrase("Hello 1bis! ", FontFactory.getFont(FontFactory.TIMES_BOLD, 8, com.lowagie.text.Font.BOLD));
		myPhrase.add(new Phrase("some other font ", FontFactory.getFont(FontFactory.HELVETICA, 8, com.lowagie.text.Font.ITALIC)));
		myPhrase.add(new Phrase("This is the end of the sentence.\n", FontFactory.getFont(FontFactory.TIMES_ROMAN, 8, com.lowagie.text.Font.ITALIC)));
		doc.add(myPhrase);
		//锚点
		Anchor anchor = new Anchor("website", FontFactory.getFont(FontFactory.HELVETICA, 12, com.lowagie.text.Font.UNDERLINE, new Color(0, 0, 255)));
		anchor.setReference("http://itextsharp.sourceforge.net");
		anchor.setName("website");
		doc.add(anchor);
		//如果你想添加内部链接,你需要选择该链接不同的名称,就象你相位在HTML中利用名称作为锚点一样。为达到该目的,你需要添加一个“#”。
		Anchor anchor1 = new Anchor("This is an internal link");
		anchor1.setName("link1");
		Anchor anchor2 = new Anchor("Click here to jump to the internal link");
		anchor2.setName("#link1");
		doc.add(anchor1);
		doc.add(anchor2);

//		列表
//		通过类List 和ListItem,你可以添加列表到PDF文件中,对于列表你还可以选择是否排序。
		List list = new List(true, 20);
		list.add(new ListItem("First line"));
		list.add(new ListItem("The second line is longer to see what happens once the end of the line is reached. Will it start on a new line?"));
		list.add(new ListItem("Third line"));
		doc.add(list);
//		文本注释
		Annotation an = new Annotation("authors","Maybe it's because I wanted to be an author myself that I wrote iText.");
		doc.add(an);

//章节和区域
		Paragraph cTitle = new Paragraph("This is chapter 1");
		Chapter chapter = new Chapter(cTitle, 1);
		Paragraph sTitle = new Paragraph("This is section 1 in chapter 1");
		Section section = chapter.addSection(sTitle, 1);
		doc.add(section);
		//创建了一个4行4列的表格然后添加一些单元格到随机的位置上
		Table aTable = new Table(4,4);		
//		aTable.setSpacing(10);
		aTable.setAutoFillEmptyCells(true);//将AutoFillEmptyCells属性设置为true,这将自动、默认的单元格布局填充空的单元格
		aTable.addCell("2.2", new Point(2,2));
		aTable.addCell("3.3", new Point(3,3));
		aTable.addCell("2.1", new Point(2,1));
		Cell cell2 = new Cell("1.3");
		//对齐方式
		cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell2.setBorderColor(new Color(255, 0, 0));
		cell2.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
		aTable.addCell(cell2, new Point(1,3));
		doc.add(aTable);
		//本地转向
		Chunk localgoto = new Chunk("this word", FontFactory.getFont(FontFactory.HELVETICA, 12, com.lowagie.text.Font.NORMAL, new Color(0, 0, 255))).setLocalGoto("test");
		Chunk destination = new Chunk("local destination", FontFactory.getFont(FontFactory.HELVETICA, 12, com.lowagie.text.Font.NORMAL, new Color(0, 255, 0))).setLocalDestination("test");
		doc.add(localgoto);
		doc.add(destination);
		//异地转向
		Chunk chunk2 = new Chunk("anchor", FontFactory.getFont(FontFactory.HELVETICA, 12)).setAnchor(new URL("http://www.lowagie.com/iText/"));
		doc.add(chunk2);
		
		Chunk chunk3 = new Chunk("jump", FontFactory.getFont(FontFactory.HELVETICA, 12, com.lowagie.text.Font.ITALIC)).setRemoteGoto("test.pdf", 3);
		doc.add(chunk3);

//		有几种办法可以缩放图片:
//		public void scaleAbsolute(int newWidth, int newHeight)
//		public void scalePercent(int percent)
//		public void scalePercent(int percentX, int percentY)
//		public void scaleToFit(int fitWidth, int fitHeight)
//		旋转
//		可以通过下面的方法旋转图片
//		public void setRotation(double r)
//		使用包含图片信息的数组来得到图片的实例
//		public static Image getInstance(byte[] img)

		//新的页面
		doc.newPage();
		/**细长的浅黄色背景的页面**/
		Rectangle pageSize = new Rectangle(595, 842);
		pageSize.setBackgroundColor(new Color(0xFF, 0xFF, 0xDE));
		doc.setPageSize(pageSize);

		// 插入图片
		doc.newPage();
		Image image1 = Image.getInstance("f:/forecolor.gif");
		image1.setAlignment(Image.ALIGN_CENTER);
		image1.scaleToFit(PageSize.A4.getHeight(), PageSize.A4.getWidth());
		doc.add(image1);

		doc.close();

 

分享到:
评论
1 楼 bobbell 2014-11-23  
GOOD.非常好的博客,赞一个。我是一个java barcode generator sdk的开发人员,你这篇关于iText创建PDF的文章写的很好,希望以后多多交流关于javad的技术问题。

相关推荐

    itext 生成pdf 目录

    itext 生成pdf 目录,itext 生成pdf 目录,itext 生成pdf 目录

    iText PDF中文字体文件.rar

    Java使用iText PDF插件导出PDF文档时,需引入中文字体文件,并创建不同大小风格的中文字体

    itext解析pdf全能最新jar

    通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。如果PDF是标记的且包含一个结构树,就可以借助于iText将PDF文档转换成XML文档(这往往取决于PDF文档是如何创建的)。另外还可以从页面...

    itextpdf 导出pdf 表格 自动分页中文 目录

    包含 itext-asian-5.2.0.jar itextpdf-5.5.5.jar

    权威Itext生成pdf

    利用iText五步创建一个PDF文件:helloword。 第一步,创建一个 iTextSharp.text.Document对象的实例: Document document = new Document(); 第二步,为该Document创建一个Writer实例: PdfWriter.getInstance...

    Java实现PDF读写(Itext)与解析XML读写(Dom4j)

    生成PDF文件,引用到的JAR包是itext5.5.1 项目文件为PDFText.java 主要实现功能: 1、创建一个PDF文件:HelloWorld.pdf,给文档设置加密,密码为zhouyl或111 2、将PDF文件压缩成ZIP文件 3、生成一个PDF文件D:\\...

    iText2.1.7资料大全(制作PDF的java开源框)

    iText是一个能够快速产生PDF文件的java类库。iText的java类对于那些要产生包含文本,表格,图形的只读文档是很有用的。它的类库尤其与javaServlet有很好的给合。使用iText与PDF能够使你正确的控制Servlet的输出。 相关...

    Java 使用iText7生成带页码的PDF文件(同时生成目录,但是不会合并两个PDF)

    gradle项目,首次使用请先下载jar包(右键项目→Gradle→Refresh),不然报错!

    MyPdf:Android使用iText生成pdf文件并读取pdf内容

    MyPdf:Android使用iText生成pdf文件并读取pdf内容

    itext-5.3.4 PDF中文語言包

    itext-5.3.4 PDF中文语言包,在利用itext-5.3.4创建PDF文件时,可导入此文件来支持中文显示。

    iText中文帮助文档

    利用iText五步创建一个PDF文件:helloword。 第一步,创建一个 iTextSharp.text.Document对象的实例: Document document = new Document(); 第二步,为该Document创建一个Writer实例: PdfWriter.getInstance...

    itext5pdf表格行变色实现过程源码+doc文档

    内含所有测试代码,java源码,两个java文件即可实现行变色,所到之处,所向披靡。代码下载下来,导入eclipse即可运行。实例呼之欲出。 配合博客: 1.http://blog.csdn.net/ae6623/article/details/8987645 2....

    itext文档组件,生成PDF

    创建PDF文件,可由HTML代码生成PDF文件

    iText7 dll文件

    使用iText7 创建PDF; 设置:Project Settings——&gt;Player——&gt;Api Compatibility Level 设置为 .Net 4.

    OpenPDF是用于创建和编辑PDF文件的开源Java库

    OpenPDF是用于创建和编辑PDF文件的开源Java库,基于 iText 4实现。

    使用poi+itext将excel转为pdf

    看到现在网上excel转pdf的代码很少,在csdn上找到一个还不能用,只能做简单的转换,只好自己写了一个,代码是一个maven工程,用eclipse创建,支持单元格合并等复杂的excel,同时能同步单元格样式到pdf中。...

    poi创建word、生成html、itext将html转换成pdf。pd4ml将jsp转成pdf

    2.通过读取数据库更改原始的.doc文件、读取数据库生成pdf。 -然后更改webRoot下面的.doc文件。然后调用ReadWriteAndDownloadDocAction生成一 个新的word。 -读取数据库文件生成pdf调用createDBPdf类。 ...

    iText中文教程.pdf

    利用iText五步创建一个PDF文件:helloword 。第一步,创建一个iTextSharp.text.Document对象的实例: Document document = new Document(); 第二步,为该Document创建一个 Writer 实例: PdfWriter.getInstance...

    The ABC of ITextPdf

    所以,当你需要你的ASP.Net Web应用程序中包含创建或与PDF文件交互的部分时,就不得不去找可用的第三方组件.使用谷歌可以搜索到在你预算之内的收费组件,当然同时也有一些开源组件。其中之一就是iTextSharp,这个程序...

    itext:通过itext批处理操作更改PDF

    目录路径:它将以递归方式扫描所有查找.pdf文件的目录(警告:这是一个缓慢的操作) 一般流程 生成要以上述任一方式处理的文件的列表 处理每个文件 检查注解链接,进行适当的更改 如果文件至少具有一个更新的链接: ...

Global site tag (gtag.js) - Google Analytics