WANQIANG WANG


  • Home

  • About

  • Tags

  • Categories

  • Archives

  • Search

iText学习

Posted on 2020-01-05 | In iText

页眉与页脚

iText5中并没有之前版本HeaderFooter对象设置页眉和页脚,可以利用PdfPageEventHelper来完成页眉页脚的设置工作。PdfPageEventHelper中包含以下事件处理器。

1
2
3
4
5
onOpenDocument() — 当打开一个文档时触发,可以用于初始化文档的全局变量。
onStartPage() — 当一个页面初始化时触发,可用于初始化页面的设置参数,但是注意这个函数触发时,该页面并没有创建好,不用利用这个函数添加内容,最好利用onEndPage()处理页面的初始化。
onEndPage() — 在创建一个新页面完成但写入内容之前触发,是添加页眉、页脚、水印等最佳时机。
onCloseDocument() — 在文档关闭之前触发,可以用于释放一些资源。
onCloseDocument() — 在文档关闭之前触发,可以用于释放一些资源。

要想出发事件需要在程序中添加事件

如下

1
2
PdfReportM1HeaderFooter footer=new PdfReportM1HeaderFooter();
writer.setPageEvent(footer);

该类PdfReportM1HeaderFooter继承自PdfPageEventHelper所以可以直接添加、

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183

package demo;

/**
* Project Name:report
* File Name:PdfReportM1HeaderFooter.java
* Package Name:com.riambsoft.report.pdf
* Date:2013-9-16上午08:59:00
* Copyright (c) 2013, riambsoft All Rights Reserved.
*
*/

import java.io.IOException;

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.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;

/**
* ClassName:PdfReportM1HeaderFooter <br/>
* Function: TODO ADD FUNCTION. <br/>
* Reason: TODO ADD REASON. <br/>
* Date: 2013-9-13 上午08:59:00 <br/>
*
* @author 落雨
* @version 394263788(QQ)
* @since JDK 1.5
* @see http://hi.baidu.com/ae6623
*/
public class PdfReportM1HeaderFooter extends PdfPageEventHelper {

/**
* 页眉
*/
public String header = "";

/**
* 文档字体大小,页脚页眉最好和文本大小一致
*/
public int presentFontSize = 12;

/**
* 文档页面大小,最好前面传入,否则默认为A4纸张
*/
public Rectangle pageSize = PageSize.A4;

// 模板
public PdfTemplate total;

// 基础字体对象
public BaseFont bf = null;

// 利用基础字体生成的字体对象,一般用于生成中文文字
public Font fontDetail = null;

/**
*
* Creates a new instance of PdfReportM1HeaderFooter 无参构造方法.
*
*/
public PdfReportM1HeaderFooter() {

}

/**
*
* Creates a new instance of PdfReportM1HeaderFooter 构造方法.
*
* @param yeMei 页眉字符串
* @param presentFontSize 数据体字体大小
* @param pageSize 页面文档大小,A4,A5,A6横转翻转等Rectangle对象
*/
public PdfReportM1HeaderFooter(String yeMei, int presentFontSize, Rectangle pageSize) {
this.header = yeMei;
this.presentFontSize = presentFontSize;
this.pageSize = pageSize;
}

public void setHeader(String header) {
this.header = header;
}

public void setPresentFontSize(int presentFontSize) {
this.presentFontSize = presentFontSize;
}

/**
*
* TODO 文档打开时创建模板
*
* @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(50, 50);// 共 页 的矩形的长宽高
}

/**
*
* TODO 关闭每页的时候,写入页眉,写入'第几页共'这几个字。
*
* @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(com.itextpdf.text.pdf.PdfWriter,
* com.itextpdf.text.Document)
*/
public void onEndPage(PdfWriter writer, Document document) {

try {
if (bf == null) {
bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
}
if (fontDetail == null) {
fontDetail = new Font(bf, presentFontSize, Font.NORMAL);// 数据体字体
}
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

// 1.写入页眉
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, new Phrase(header, fontDetail),
document.left(), document.top() + 20, 0);

// 2.写入前半部分的 第 X页/共
int pageS = writer.getPageNumber();
String foot1 = "第 " + pageS + " 页 /共";
Phrase footer = new Phrase(foot1, fontDetail);

// 3.计算前半部分的foot1的长度,后面好定位最后一部分的'Y页'这俩字的x轴坐标,字体长度也要计算进去 = len
float len = bf.getWidthPoint(foot1, presentFontSize);

// 4.拿到当前的PdfContentByte
PdfContentByte cb = writer.getDirectContent();

// 自己增加的
if (pageS == 1) {
Phrase footerLeft = new Phrase("978-1-4799-0530-031.00 ©2013 IEEE", fontDetail);
ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, footerLeft, document.left(), document.bottom() - 20, 0);
}

// 5.写入页脚1,x轴就是(右margin+左margin + right() -left()- len)/2.0F
// 再给偏移20F适合人类视觉感受,否则肉眼看上去就太偏左了
// ,y轴就是底边界-20,否则就贴边重叠到数据体里了就不是页脚了;注意Y轴是从下往上累加的,最上方的Top值是大于Bottom好几百开外的。
ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, footer,
(document.rightMargin() + document.right() + document.leftMargin() - document.left() - len) / 2.0F
+ 20F,
document.bottom() - 20, 0);

// 6.写入页脚2的模板(就是页脚的Y页这俩字)添加到文档中,计算模板的和Y轴,X=(右边界-左边界 - 前半部分的len值)/2.0F + len , y
// 轴和之前的保持一致,底边界-20
cb.addTemplate(total,
(document.rightMargin() + document.right() + document.leftMargin() - document.left()) / 2.0F + 20F,
document.bottom() - 20); // 调节模版显示的位置

}

/**
*
* TODO 关闭文档时,替换模板,完成整个页眉页脚组件
*
* @see com.itextpdf.text.pdf.PdfPageEventHelper#onCloseDocument(com.itextpdf.text.pdf.PdfWriter,
* com.itextpdf.text.Document)
*/
public void onCloseDocument(PdfWriter writer, Document document) {
// 7.最后一步了,就是关闭文档的时候,将模板替换成实际的 Y 值,至此,page x of y 制作完毕,完美兼容各种文档size。
total.beginText();
total.setFontAndSize(bf, presentFontSize);// 生成的模版的字体、颜色
String foot2 = " " + (writer.getPageNumber() - 1) + " 页";
total.showText(foot2);// 模版显示的内容
total.endText();
total.closePath();
}
}

页边距

Isn’t it possible for you to use HtmlConverter#convertToElements method. It returns List as a result and then you can add its elements to a document with set margins:

1
2
3
4
5
6
7
Document document = new Document(pdfDocument);
List list = HtmlConverter.convertToElements(new FileInputStream(htmlSource));
for (IElement element : list) {
if (element instanceof IBlockElement) {
document.add((IBlockElement) element);
}
}

Another approach: in your html just introduce the @page rule which sets the margins you need, for example:

1
@page {margin: 0;}

Yet another solution: implement your own custom tag worker for tag and set margins on its level. For example, to set zero margins one could create tag the next worker:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class CustomTagWorkerFactory extends DefaultTagWorkerFactory {
public ITagWorker getCustomTagWorker(IElementNode tag, ProcessorContext context) {
if (TagConstants.HTML.equals(tag.name())) {
return new ZeroMarginHtmlTagWorker(tag, context);
}
return null;
}
}
public class ZeroMarginHtmlTagWorker extends HtmlTagWorker {
public ZeroMarginHtmlTagWorker(IElementNode element, ProcessorContext context) {
super(element, context);
Document doc = (Document) getElementResult();
doc.setMargins(0, 0, 0, 0);
}
}

and pass it as a ConverterProperties parameter to Htmlconverter:

converterProperties.setTagWorkerFactory(new CustomTagWorkerFactory());
HtmlConverter.convertToPdf(new File(htmlPath), new File(pdfPath), converterProperties);

表格跨页问题

代码的处理方式

pdfHtml的一种处理方式

I had a similar issue of trying to keep together content within a div. I applied the following css property and this kept everything together. This worked with itext7 pdfhtml.

1
page-break-inside: avoid;

PdfPage转换成图片

生成image对象

https://stackoverflow.com/questions/37809019/itext7-pdf-to-image

Please read the official documentation for iText 7, more specifically Chapter 6: Reusing existing PDF documents
In PDF, there’s the concept of _Form XObject_s. A Form XObject is a piece of PDF content that is stored outside the content stream of a page, hence XObject which stands for eXternal Object. The use of the word Form in Form XObject could be confusing, because people might be thinking of a form as in a fillable form with fields. To avoid that confusing, we introduced the term PdfTemplate in iText 5.
The class PdfImportedPage you refer to was a subclass of PdfTemplate: it was a piece of PDF syntax that could be reused in another page. Over the years, we noticed that people also got confused by the word PdfTemplate.
In iText 7, we returned to the basics. When talking about a Form XObject, we use the class PdfFormXObject. When talking about a page in a PDF file, we use the class PdfPage.
This is how we get a PdfPage from an existing document:

1
2
PdfDocument origPdf = new PdfDocument(new PdfReader(src));
PdfPage origPage = origPdf.getPage(1);

This is how we use that page in a new document:

1
2
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
PdfFormXObject pageCopy = origPage.copyAsFormXObject(pdf);

If you want to use that pageCopy as an Image, just create it like this:

1
Image image = new Image(pageCopy);

存成图片(验证了这种方法不可行)

https://stackoverflow.com/questions/24940813/saving-com-itextpdf-text-image-as-a-image-file
Just convert the Barcode39 itext image into an AWT image using createAwtImage):

1
java.awt.Image awtImage = code39.createAwtImage(Color.BLACK, Color.WHITE);

Then convert it to a BufferedImage and store it:

1
2
3
4
5
6
BufferedImage bImage= new BufferedImage(awtImage.getWidth(), awtImage.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = bImage.createGraphics();
g.drawImage(awtImage, 0, 0, null);
g.dispose();
File outputfile = new File("saved.png");
ImageIO.write(bImage, "jpg", new File("code39.jpg"));

blog复活

Posted on 2020-01-04

回顾

之前在阿里云上花钱买了一个网页空间,用了好几年,刚开始还挺好的,但最近半年,完全不能用。频繁刷新都能被关黑屋。还遇到有人恶意访问。

今天终于花时间弄清楚了 github + hexo + next + travis-ci 的blog组合。

搞清楚了hexo 的 tags 和 categories 的启用方法。

从目前的情况来说,与之前的 wordpress 对比,显示完美,当然写作没有之前的方便。

还支持 https,并且 github 技术加持,不担心被人恶意黑掉。自己的域名也能完美支持。

总之,blog 重新复活。

如何快速创建一个github个人网站?

克隆一个已经存在的网站:),比如我这个网站。然后把source目录中的内容删除掉。在调整一下自己的配置就可以了。

hexo blog 如何创建404页面

有两种方法:

  1. 可以自己创建一个 404.html 页面直接放到 source 目录下面。

  2. 也可以在 source 目录下面创建一个 404 的文件夹, 然后面在里面创建一个 index.md 的文件。 可以使用这个命令创建 hexo new page "404"

图床如何用?

  1. 七牛的不错,可以绑定自己的域名。做好备份,如果那天免费不能用了,换地方应该也不需要在文章里面改链接。

  2. 还有一个自动上传的工作可用。 MPic官方网址:http://mpic.lzhaofu.cn

Thinkpad 小红点恢复Win10中键滚动功能

Posted on 2020-01-04

问题

在Windows 10下面,按住中键,拔动小红点,页面滚动的同时,光标也会一起跑。控制面板中也找不到设置的地方。

解决方案

需要修改注册表:
在HKEY_CURRENT_USER\Software\Synaptics\SynTPEnh\UltraNavPS2中,把TrackPointModeFunction的值修改一下即可。
原为:”TrackPointModeFunction”=dword:00000011
改为:”TrackPointModeFunction”=dword:00000010
这样就跟以前使用完全相同了,中键点击是小手或指针,滚动是鼠标中键滚动的图标,一切完美。

QNAP安装transmission

Posted on 2019-07-16

docker create \ –name=transmission \ -e PUID=1000 \ -e PGID=1000 \ -e TZ=Europe/London \ -e TRANSMISSION_WEB_HOME=/combustion-release/ #optional \ -p 9091:9091 \ -p 51413:51413 \ -p 51413:51413/udp \ -v /share/homes/docker/trans/data:/config \ -v /share/homes/docker/trans/downloads:/downloads \ -v /share/homes/docker/trans/watch:/watch \ –restart unless-stopped \ linuxserver/transmission

xslt keep-together.within-page 在Fop中的形为

Posted on 2019-07-16
1
keep-together.within-page
  1. auto是默认形为, 写不写都是一样的。
  1. always是强制在一页中。如果当前限制的内容超过一页时,会从页面下方溢出。
  1. 指定一个数字。测试 1 的形为是这样的:会换页将当前内容保持在一页中。但如果当前内容超过一页时,也会将多余的内容显示到下一页中,保证不会将内容溢出到纸的外面。

https://www.w3.org/TR/xsl11/#keep-together

xslt调试技巧

Posted on 2019-07-09

https://www.ibm.com/developerworks/cn/xml/x-tipxslmsg.html

qnap国外商店

Posted on 2019-06-21

https://qnapclub.eu/index.php/en

Linux Lab

Posted on 2019-06-14

linux 内核学习资源,暂时收集。

还没有详细研究。 http://tinylab.org/linux-lab/

树莓派docker运行报错

Posted on 2019-06-07

解决方案

This is because the docker service is not automatically started after an install. You can start the docker service in Ubuntu and its derivatives (looking at you Linux Mint) by typing: sudo service docker start To check that docker started, this file should exist: ls -la /var/run/docker.sock

树莓派的Web ssh

Posted on 2019-06-03

https://www.instructables.com/id/Raspberry-Pi-online-SSH-easy-way/

123…10

Wanqiang

95 posts
39 categories
40 tags
GitHub E-Mail
© 2021 Wanqiang
Powered by Hexo
|
Theme — NexT.Pisces v5.1.4
|
粤ICP备15047376号