惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
CXSECURITY Database RSS Feed - CXSecurity.com
K
Kaspersky official blog
A
Arctic Wolf
Attack and Defense Labs
Attack and Defense Labs
L
LINUX DO - 热门话题
N
News | PayPal Newsroom
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
L
Lohrmann on Cybersecurity
PCI Perspectives
PCI Perspectives
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
The Last Watchdog
The Last Watchdog
B
Blog RSS Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
W
WeLiveSecurity
Know Your Adversary
Know Your Adversary
博客园 - Franky
T
Tenable Blog
T
Tailwind CSS Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Help Net Security
Help Net Security
WordPress大学
WordPress大学
T
The Exploit Database - CXSecurity.com
www.infosecurity-magazine.com
www.infosecurity-magazine.com
博客园 - 司徒正美
阮一峰的网络日志
阮一峰的网络日志
D
Darknet – Hacking Tools, Hacker News & Cyber Security
H
Heimdal Security Blog
TaoSecurity Blog
TaoSecurity Blog
S
Security Affairs
J
Java Code Geeks
小众软件
小众软件
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Apple Machine Learning Research
Apple Machine Learning Research
NISL@THU
NISL@THU
O
OpenAI News
The Cloudflare Blog
月光博客
月光博客
Google Online Security Blog
Google Online Security Blog
V
V2EX
罗磊的独立博客
美团技术团队
博客园 - 三生石上(FineUI控件)
Security Latest
Security Latest
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
Cyber Attacks, Cyber Crime and Cyber Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Cyberwarzone
Cyberwarzone
L
LINUX DO - 最新话题
Hacker News - Newest:
Hacker News - Newest: "LLM"
大猫的无限游戏
大猫的无限游戏

博客园 - EWang

如何在MS Project中进行挣值分析 - EWang - 博客园 API函数大全 - EWang - 博客园 Bugzilla, CVS, ClearQuest, ClearCase安装心得(转) - EWang SN大全 - EWang - 博客园 SQL INNER JOIN/OUTER JOIN - EWang Linux下时间的修改 - EWang - 博客园 Asp.net调试的一些问题 - EWang - 博客园 SQL字符串处理函数大全 - EWang - 博客园 crontab 格式 - EWang - 博客园 SQL 函数 函数大全 - EWang c# ToString() 格式化字符串 - EWang 常用CSS样式 - EWang - 博客园 GridView空间的DataFormatString - EWang - 博客园 asp.net 学习资源列表 - EWang - 博客园 (转)Visual Studio .NET已检测到指定的WEB服务运行的不是ASP.NET 1.1版。你将无法运行ASP.NET WEB应用程序或服务 - EWang 一个困扰我半天的问题解决了,java环境真的很难配置 - EWang - 博客园 Liunx下常用程序(转) - EWang - 博客园 liunx下的一些常用命令 - EWang - 博客园 Eclipse 运行命令行参数大全 - EWang - 博客园
Java中iText使用技巧 - EWang - 博客园
EWang · 2008-03-26 · via 博客园 - EWang

要生成pdf文件,在网上查了下资料,首选iText,跟着大家走,我也iText一把。
1:把字型编程A4横向

Document document = new Document(PageSize.A4.rotate());
            

2:在PDF文件中加入表格

    float[] widths = {0.05f,0.29f, 0.05f, 0.05f, 0.14f, 0.03f, 0.04f,0.04f, 0.05f, 0.05f,0.08f,0.08f, 0.05f};
            //new 一个13列的table
            PdfPTable table = new PdfPTable(13);
            //设置table每一列的宽度,widths里写的是百分比,他们加和需要是1
            table.setWidths(widths);
            //设置表格在页面上的宽度,设成100表示可以表格填满页面,但是要去掉页面margin
            table.setWidthPercentage(100);
            //设置表格上端的空白距离,类似css中的margin-top:xxpx;这样在给表格加上标题后,标题就不会跟表格重叠在一起了。
            table.setSpacingBefore(3f);
            

3:向表格里填数据, 例子

    for(int i = 0; i<26; i++)
            {
            table.table.addCell(i+"");
            }
            

这样就会往表格里填上2行数据,这个api比较简单,不用向jxl/poi 那里那样还有明确写出到底要往那个cell填
4:标题和表格组合

document.add(new Paragraph(titleWorkhour, FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD, new Color(0, 0, 0))));
            //由于设置了table.setSpacingBefore(3f);所以table跟标题不会重合。
            document.add(table);
            

5:分页

document.newPage();
            

6:合并PDF文件,由于往一个document里加内容只能是顺序往下加,而我的summary页要最后才能算出来,但是summary页又要放在第一页
所以我不得不先把body生成一个pdf文件,然后summary在生成一个文件,然后把两个文件合并成同一个文件。

private void concatenateSummary(String[] args, String finalFile)
            {
            try {
            int pageOffset = 0;
            ArrayList master = new ArrayList();
            int f = 0;
            String outFile = finalFile;
            Document document = null;
            PdfCopy  writer = null;
            while (f < args.length) {
            // we create a reader for a certain document
            PdfReader reader = new PdfReader(args[f]);
            reader.consolidateNamedDestinations();
            // we retrieve the total number of pages
            int n = reader.getNumberOfPages();
            List bookmarks = SimpleBookmark.getBookmark(reader);
            if (bookmarks != null) {
            if (pageOffset != 0)
            SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null);
            master.addAll(bookmarks);
            }
            pageOffset += n;
            if (f == 0) {
            // step 1: creation of a document-object
            document = new Document(reader.getPageSizeWithRotation(1));
            // step 2: we create a writer that listens to the document
            writer = new PdfCopy(document, new FileOutputStream(outFile));
            // step 3: we open the document
            document.open();
            }
            // step 4: we add content
            PdfImportedPage page;
            for (int i = 0; i < n; ) {
            ++i;
            page = writer.getImportedPage(reader, i);
            writer.addPage(page);
            }
            PRAcroForm form = reader.getAcroForm();
            if (form != null)
            writer.copyAcroForm(reader);
            f++;
            }
            if (!master.isEmpty())
            writer.setOutlines(master);
            // step 5: we close the document
            document.close();
            }
            catch(Exception e) {
            e.printStackTrace();
            }
            }