当前位置: 首页 > news >正文

现代通信技术专业学什么抖音seo代理

现代通信技术专业学什么,抖音seo代理,西城网站建设公司,美国做南京做网站5.1 使用表和单元格事件装饰表 实现PdfPTableEvent 接口 实现PdfPCellEvent 接口 合并表格和单元格事件 5.2 基本构建块的事件 通用块(Chunk)功能 段落(Paragraph)事件 章节(Chapter)和 区域(…

5.1 使用表和单元格事件装饰表

实现PdfPTableEvent 接口

实现PdfPCellEvent 接口

合并表格和单元格事件

5.2 基本构建块的事件

通用块(Chunk)功能

段落(Paragraph)事件

章节(Chapter)和 区域(Section)事件

页面顺序和空白页面

5.3 页面边界概述

媒体盒

作物箱

其他页面边界

5.4 添加页码事件到 PdfWriter

PdfPageEvent 接口

onOpenDocument():当文档打开后触发,初始化变量整个文档可使用

onStartPage():当开始一个新页面触发,初始化页面级变量,不要在这个方法内添加内容

onEndPage():开始一个新页面并在文档关闭前触发,这里添加header、footer、watermark等

onCloseDocument():文档关闭时触发,这里释放资源

添加页头与页尾

import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;public class MovieHistory2 {/** The resulting PDF file. */public static final String RESULT= "results/part1/chapter05/movie_history2.pdf";/** Inner class to add a header and a footer. */class HeaderFooter extends PdfPageEventHelper {/** Alternating phrase for the header. */Phrase[] header = new Phrase[2];/** Current page number (will be reset for every chapter). */int pagenumber;/*** Initialize one of the headers.* @see com.itextpdf.text.pdf.PdfPageEventHelper#onOpenDocument(*      com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)*/public void onOpenDocument(PdfWriter writer, Document document) {header[0] = new Phrase("Movie history");}/*** Initialize one of the headers, based on the chapter title;* reset the page number.* @see com.itextpdf.text.pdf.PdfPageEventHelper#onChapter(*      com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document, float,*      com.itextpdf.text.Paragraph)*/public void onChapter(PdfWriter writer, Document document,float paragraphPosition, Paragraph title) {header[1] = new Phrase(title.getContent());pagenumber = 1;}/*** Increase the page number.* @see com.itextpdf.text.pdf.PdfPageEventHelper#onStartPage(*      com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)*/public void onStartPage(PdfWriter writer, Document document) {pagenumber++;}/*** Adds the header and the footer.* @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(*      com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)*/public void onEndPage(PdfWriter writer, Document document) {Rectangle rect = writer.getBoxSize("art");switch(writer.getPageNumber() % 2) {case 0:ColumnText.showTextAligned(writer.getDirectContent(),Element.ALIGN_RIGHT, header[0],rect.getRight(), rect.getTop(), 0);break;case 1:ColumnText.showTextAligned(writer.getDirectContent(),Element.ALIGN_LEFT, header[1],rect.getLeft(), rect.getTop(), 0);break;}ColumnText.showTextAligned(writer.getDirectContent(),Element.ALIGN_CENTER, new Phrase(String.format("page %d", pagenumber)),(rect.getLeft() + rect.getRight()) / 2, rect.getBottom() - 18, 0);}}/** The different epochs. */public static final String[] EPOCH ={ "Forties", "Fifties", "Sixties", "Seventies", "Eighties","Nineties", "Twenty-first Century" };/** The fonts for the title. */public static final Font[] FONT = new Font[4];static {FONT[0] = new Font(FontFamily.HELVETICA, 24);FONT[1] = new Font(FontFamily.HELVETICA, 18);FONT[2] = new Font(FontFamily.HELVETICA, 14);FONT[3] = new Font(FontFamily.HELVETICA, 12, Font.BOLD);}/*** Creates a PDF document.* @param filename the path to the new PDF document* @throws    DocumentException * @throws    IOException* @throws    SQLException*/public void createPdf(String filename)throws IOException, DocumentException, SQLException {// step 1Document document = new Document(PageSize.A4, 36, 36, 54, 54);// step 2PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));HeaderFooter event = new HeaderFooter();writer.setBoxSize("art", new Rectangle(36, 54, 559, 788));writer.setPageEvent(event);// step 3document.open();// step 4Chapter chapter = null;// 初始化数据...document.add(chapter);// step 5document.close();}/*** Main method.** @param    args    no arguments needed* @throws DocumentException * @throws IOException * @throws SQLException*/public static void main(String[] args)throws IOException, DocumentException, SQLException {new MovieHistory2().createPdf(RESULT);}
}

解决 page X of Y 问题

利用XObject特性,iText 真正将PdfTemplate 写入 OutputStream 是在调用releaseTemplate() 时。

import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;public class MovieCountries1 {/** The resulting PDF file. */public static final String RESULT= "D:/data/iText/inAction/chapter05/movie_countries1.pdf";/*** Inner class to add a table as header.*/class TableHeader extends PdfPageEventHelper {/** The header text. */String header;/** The template with the total number of pages. */PdfTemplate total;/*** Allows us to change the content of the header.* @param header The new header String*/public void setHeader(String header) {this.header = header;}/*** Creates the PdfTemplate that will hold the total number of pages.* @see com.itextpdf.text.pdf.PdfPageEventHelper#onOpenDocument(*      com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)*/public void onOpenDocument(PdfWriter writer, Document document) {total = writer.getDirectContent().createTemplate(30, 16);}/*** Adds a header to every page* @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(*      com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)*/public void onEndPage(PdfWriter writer, Document document) {PdfPTable table = new PdfPTable(3);try {table.setWidths(new int[]{24, 24, 2});table.setTotalWidth(527);table.setLockedWidth(true);table.getDefaultCell().setFixedHeight(20);table.getDefaultCell().setBorder(Rectangle.BOTTOM);table.addCell(header);table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);table.addCell(String.format("Page %d of", writer.getPageNumber()));PdfPCell cell = new PdfPCell(Image.getInstance(total));cell.setBorder(Rectangle.BOTTOM);table.addCell(cell);table.writeSelectedRows(0, -1, 34, 803, writer.getDirectContent());}catch(DocumentException de) {throw new ExceptionConverter(de);}}/*** Fills out the total number of pages before the document is closed.* @see com.itextpdf.text.pdf.PdfPageEventHelper#onCloseDocument(*      com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)*/public void onCloseDocument(PdfWriter writer, Document document) {ColumnText.showTextAligned(total, Element.ALIGN_LEFT,new Phrase(String.valueOf(writer.getPageNumber() - 1)),2, 2, 0);}}/*** Creates a PDF document.* @param filename the path to the new PDF document* @throws    DocumentException * @throws    IOException* @throws    SQLException*/public void createPdf(String filename)throws IOException, DocumentException, SQLException {// step 1Document document = new Document(PageSize.A4, 36, 36, 54, 36);// step 2PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));TableHeader event = new TableHeader();writer.setPageEvent(event);// step 3document.open();// step 4// 新建页面数据...document.add(new Phrase("Hello Page 1."));document.newPage();document.add(new Phrase("Hello Page 2."));document.newPage();document.add(new Phrase("Hello Page 3."));document.newPage();document.add(new Phrase("Hello Page 4."));// step 4document.close();}/*** Main method.** @param    args    no arguments needed* @throws DocumentException * @throws IOException * @throws SQLException*/public static void main(String[] args)throws IOException, DocumentException, SQLException {new MovieCountries1().createPdf(RESULT);}
}

在页尾添加页码

import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;/*** TODO 在此写上类的相关说明.<br>* @author gongqiang <br>* @version 1.0.0 2023年9月18日<br>* @see * @since JDK 1.5.0*/
public class MovieCountries {/** The resulting PDF file. */public static final String RESULT= "D:/data/iText/inAction/chapter05/movie_countries.pdf";/*** Inner class to add a table as header.*/class TableHeader extends PdfPageEventHelper {/** The header text. */String header;/** The template with the total number of pages. */PdfTemplate total;BaseFont baseFont;int fontSize = 14;/*** Allows us to change the content of the header.* @param header The new header String*/public void setHeader(String header) {this.header = header;}/*** Creates the PdfTemplate that will hold the total number of pages.* @see com.itextpdf.text.pdf.PdfPageEventHelper#onOpenDocument(*      com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)*/public void onOpenDocument(PdfWriter writer, Document document) {total = writer.getDirectContent().createTemplate(30, 16);try {baseFont = BaseFont.createFont("D:/data/iText/fonts/simfang.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);} catch (Exception e) {throw new ExceptionConverter(e);}}/*** Adds a header to every page* @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(*      com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)*/public void onEndPage(PdfWriter writer, Document document) {// 新建获得用户页面文本和图片内容位置的对象PdfContentByte pdfContentByte = writer.getDirectContent();// 保存图形状态pdfContentByte.saveState();String text = "页" + writer.getPageNumber() + "/";//String text = "页 99/";// 获取点字符串的宽度float textSize = baseFont.getWidthPoint(text, fontSize);pdfContentByte.beginText();// 设置随后的文本内容写作的字体和字号pdfContentByte.setFontAndSize(baseFont, fontSize);// 定位'X/'
//            float x = (document.right() + document.left()) / 2;float x = document.right() - 50;float y = 20f;pdfContentByte.setTextMatrix(x, y);pdfContentByte.showText(text);pdfContentByte.endText();// 将模板加入到内容(content)中- // 定位'Y'pdfContentByte.addTemplate(total, x + textSize, y);pdfContentByte.restoreState();}/*** Fills out the total number of pages before the document is closed.* @see com.itextpdf.text.pdf.PdfPageEventHelper#onCloseDocument(*      com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)*/public void onCloseDocument(PdfWriter writer, Document document) {total.beginText();total.setFontAndSize(baseFont, fontSize);total.setTextMatrix(0, 0);// 设置总页数的值到模板上,并应用到每个界面total.showText(String.valueOf(writer.getPageNumber()));//total.showText(String.valueOf("99"));total.endText();}}/*** Creates a PDF document.* @param filename the path to the new PDF document* @throws    DocumentException * @throws    IOException* @throws    SQLException*/public void createPdf(String filename)throws IOException, DocumentException, SQLException {// step 1Document document = new Document(PageSize.A4, 36, 36, 54, 36);// step 2PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));TableHeader event = new TableHeader();writer.setPageEvent(event);// step 3document.open();// step 4// 新建页面数据...document.add(new Phrase("Hello Page 1."));document.newPage();document.add(new Phrase("Hello Page 2."));document.newPage();document.add(new Phrase("Hello Page 3."));document.newPage();document.add(new Phrase("Hello Page 4."));// step 4document.close();}/*** Main method.** @param    args    no arguments needed* @throws DocumentException * @throws IOException * @throws SQLException*/public static void main(String[] args)throws IOException, DocumentException, SQLException {new MovieCountries().createPdf(RESULT);}
}

添加水印

如果水印是图片,可以通过PdfContentByte.addImage()、或将图片包装成ColumnText对象,或者添加到表格的cell里。

注意:Image对象需要在onOpenDocument()方法内初始化,避免重复添加图片的字节流。

import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.GrayColor;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;public class MovieCountries2 extends MovieCountries1 {/** The resulting PDF file. */public static final String RESULT= "D:/data/iText/inAction/chapter05/movie_countries2.pdf";/*** Inner class to add a watermark to every page.*/class Watermark extends PdfPageEventHelper {Font FONT = new Font(FontFamily.HELVETICA, 52, Font.BOLD, new GrayColor(0.75f));public void onEndPage(PdfWriter writer, Document document) {ColumnText.showTextAligned(writer.getDirectContentUnder(),Element.ALIGN_CENTER, new Phrase("FOOBAR FILM FESTIVAL", FONT),297.5f, 421, writer.getPageNumber() % 2 == 1 ? 45 : -45);}}/*** Creates a PDF document.* @param filename the path to the new PDF document* @throws    DocumentException * @throws    IOException* @throws    SQLException*/public void createPdf(String filename)throws IOException, DocumentException, SQLException {// step 1Document document = new Document(PageSize.A4, 36, 36, 54, 36);// step 2PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));TableHeader event = new TableHeader();writer.setPageEvent(event);writer.setPageEvent(new Watermark());// step 3document.open();// step 4// 新建页面数据...document.add(new Phrase("Hello Page 1."));document.newPage();document.add(new Phrase("Hello Page 2."));document.newPage();document.add(new Phrase("Hello Page 3."));document.newPage();document.add(new Phrase("Hello Page 4."));// step 5document.close();}/*** Main method.** @param    args    no arguments needed* @throws DocumentException * @throws IOException * @throws SQLException*/public static void main(String[] args)throws IOException, DocumentException, SQLException {new MovieCountries2().createPdf(RESULT);}
}

创建幻灯片

http://www.dinnco.com/news/45672.html

相关文章:

  • 网站漏洞扫描工具公司个人怎么做网络推广
  • 如何用爬虫做网站监控html网页制作软件
  • 青海网站制作哪家好推广seo网站
  • 赣州做网站的公司有哪家彩虹云商城网站搭建
  • 销售型网站怎么做seo关键词优化推广
  • 贵阳中国建设银行招聘信息网站seo批量建站
  • 怎么做网站底部备案号百度关键词推广网站
  • 龙虎和网站开发aso关键词优化计划
  • 网站建设推荐信息seo sem是啥
  • 我不想找之前做网站的续费电商培训机构哪家强
  • 网站目录有什么意义北京网站seo设计
  • 加盟平台响应网站建设百度云官网入口
  • 一个网站里有两个网页怎么做百度网盘资源免费搜索引擎入口
  • 日本网站制作搜索引擎优化缩写
  • 网站推广服务费会计分录怎么做seo报价单
  • 网站开发w亿玛酷1负责sem分析
  • 四川网站建设套餐搜索引擎优化的基本方法
  • 西安大网站建设公司排名seo营销怎么做
  • 香港主机网站速度营销必备十大软件
  • 教做吃的网站推广平台排行榜
  • 自己电脑做网站域名备案域名注册平台哪个好
  • 阿里巴巴网站今日的新闻
  • 青岛网站制作工作室企业网站建设的作用
  • 青岛网络建站公司整站seo技术
  • 一个公司主体可以在多个网站做备案杭州百度推广代理商
  • 做外贸网站美国服务器要多少钱中国搜索网站排名
  • 海珠营销型网站建设公司深圳全网推广效果如何
  • 哪个全球购网站做的好处百度关键词刷排名软件
  • 做网站选关键词产品运营推广方案
  • 做教育的网站需要资质吗软文撰写案例