Loading [MathJax]/jax/input/TeX/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >CSharp每日代码示例:使用iTextSharp创建PDF文件

CSharp每日代码示例:使用iTextSharp创建PDF文件

作者头像
程序你好
发布于 2018-07-20 08:41:22
发布于 2018-07-20 08:41:22
3.3K00
代码可运行
举报
文章被收录于专栏:程序你好程序你好
运行总次数:0
代码可运行

本文演示如何使用iTextSharp PDF库将文本文件转换为PDF文件。 iTextSharp是一个从JAVA项目iText衍生的.Net版本的开源项目,采用GPL许可证发布。这个项目在创建PDF文件方面功能相当强大,弱点是缺少对于现有PDF文件进行修改和分析的功能。

介绍:

ITextSharp中相关的概念:

一、Document

这个对象有三个构造函数:

隐藏行号 复制代码 ? 这是一段程序代码。

  1. public Document();
  2. public Document(Rectangle pageSize);
  3. public Document(Rectangle pageSize,
  4. int marginLeft,
  5. int marginRight,
  6. int marginTop,
  7. int marginBottom);

第一个构造函数以A4页面作为参数调用第二个构造函数,第二个构造函数以每边36磅页边距为参数调用调用第三个构造函数。

页面尺寸:

你可以通过指定的颜色和大小创建你自己的页面,示例代码0102创建一个细长的浅黄色背景的页面:

Rectangle pageSize = new Rectangle(144, 720);

pageSize.BackgroundColor = new Color(0xFF, 0xFF, 0xDE);

Document document = new Document(pageSize);

通常,你不必创建这样的页面,而可以从下面页面尺寸中选择:

A0-A10, LEGAL, LETTER, HALFLETTER, _11x17, LEDGER, NOTE, B0-B5, ARCH_A-ARCH_E, FLSA 和 FLSE

大多数情况下使用纵向页面,如果希望使用横向页面,你只须使用rotate()函数:

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

详细代码见示例代码0103。

页边距:

当创建一个文件时,你还可以定义上、下、左、右页边距:

Document document = new Document(PageSize.A5, 36, 72, 108, 180);

说明:

当创建一个矩形或设置边距时,你可能希望知道该用什么度量单位:厘米、英寸或象素,事实上,默认的度量系统以排版单位磅为基础得出其他单位的近似值,如1英寸=72磅,如果你想在A4页面的PDF中创建一个矩形,你需要计算以下数据:

21 厘米 / 2.54 = 8.2677 英寸

8.2677英寸* 72 = 595 磅

29.7 厘米 / 2.54 = 11.6929 英寸

11.6929英寸* 72 = 842 磅

默认边距为36磅即半英寸。

如果你修改了页面尺寸,仅仅影响到下一页,如果你修改了页边距,则影响到全部,故慎用。

二、Writer

一旦创建了document,我们可以创建该文档的多个Writer的实例,所有这些Writer实例均继承自抽象类“iTextSharp.text.DocWriter”。

同时还有另外一种情况,你可以用iTextSharp.text.pdf.PdfWriter产生文档PDF文件,如果你想创建一个TeX文档,你可以使用iTextSharp.text.TeX.TeXWriter包。

Writer类的构造函数是私有的,你只能通过下面的方法创建一个实例:

public static xxxWriter getInstance(Document document, Stream os);(xxx 是 Pdf 或 Xml)

你可以通过下面的方法创建一个实例:

PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap01xx.pdf"));

但是你几乎永远不会用到Writer实例(除非你想创建高级PDF或者希望用一些非常特殊的函数,如ViewerPreferences 或 Encryption)。所以通过下面的办法得到实例已经足够了: PdfWriter.getInstance(document, new FileStream("Chap01xx.pdf"));

在第一步中创建一个文档时,第一个参数意义不大,第二个参数可以是任何一种流,到目前为止我们一直使用System.IO.FileStream将Document写入文件中,示例代码0105用到了System.IO.MemoryStream(这不是一个独立的例子,你必须在Servlet Engine中测试这些代码。

文档加密:

public void setEncryption(boolean strength, String userPassword, String ownerPassword, int permissions);

· strength 是下面两个常量之一:

o PdfWriter.STRENGTH40BITS: 40 位

o PdfWriter.STRENGTH128BITS: 128位 (Acrobat Reader 5.0及以上版本支持)

· UserPassword和ownerPassword 可以为空或零长度, 这种情况下, ownerPassword 将被随机的字符串代替

· Permissions 为下列常量之一:

o PdfWriter.AllowPrinting

o PdfWriter.AllowModifyContents

o PdfWriter.AllowCopy

o PdfWriter.AllowModifyAnnotations

o PdfWriter.AllowFillIn

o PdfWriter.AllowScreenReaders

o PdfWriter.AllowAssembly

PdfWriter.AllowDegradedPrinting

三、块(Chunk)

块(Chunk)是能被添加到文档的文本的最小单位,块可以用于构建其他基础元素如短句、段落、锚点等,块是一个有确定字体的字符串,要添加块到文档中时,其他所有布局变量均要被定义。

四、短句(Phrases)

短句(Phrases)是一系列以特定间距(两行之间的距离)作为参数的块,一个短句有一个主字体,但短句中的一些块具有不同于主字体的字体,你有更多的选择去创建短句。

五、段落

段落是一系列块和(或)短句。同短句一样,段落有确定的间距。用户还可以指定缩排;在边和(或)右边保留一定空白,段落可以左对齐、右对齐和居中对齐。添加到文档中的每一个段落将自动另起一行。

说明:一个段落有一个且仅有一个间距,如果你添加了一个不同字体的短句或块,原来的间距仍然有效,你可以通过SetLeading来改变间距,但是段落中所有内容将使用新的中的间距。

更改分割符

通常,当文本不能放在一行时,文本将被分割成不同的部分,iText首先会查找分割符,如果没有找到,文本将在行尾被截断。有一些预定的分割符如“ ”空格和“-”连字符,但是你可以使用setSplitCharacter方法来覆盖这些默认值。

以使用IndentationLeft和IndentationRight,FirstLineIndent属性设置缩排;

六、锚点(Anchor)

如果你想在文档中添加一个外部链接(例如使用URL链接到WEB上的其他文档),你可以简单地使用Anchor对象,它派生于Phrase对象,使用方法相同。只有两种额外方法定义两种额外变量:setName和 setReference。

外部链接示例:

隐藏行号 复制代码 ? 这是一段程序代码。

  1. Anchor anchor = new Anchor("website", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.UNDERLINE, new Color(0, 0, 255)));
  2. anchor.Reference = http://itextsharp.sourceforge.net;
  3. anchor.Name = "website";

如果你想添加内部链接,你需要选择该链接不同的名称,就象你相位在HTML中利用名称作为锚点一样。为达到该目的,你需要添加一个“#”。

内部链接示例:

隐藏行号 复制代码 ? 这是一段程序代码。

  1. Anchor anchor1 = new Anchor("This is an internal link");
  2. anchor1.Name = "link1";
  3. Anchor anchor2 = new Anchor("Click here to jump to the internal link");
  4. anchor.Reference = "#link1";

七、列表(List,ListItem)

通过类List 和ListItem,你可以添加列表到PDF文件中,对于列表你还可以选择是否排序。

排序列表示例:

隐藏行号 复制代码 ? 这是一段程序代码。

  1. List list = new List(true, 20);
  2. list.Add(new ListItem("First line"));
  3. 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?"));
  4. list.Add(new ListItem("Third line"));

结果如下:

1. First line

2. The second line is longer to see what happens once the end of the line is reached. Will it start on a new line?

3. Third line

不排序示例如下:

隐藏行号 复制代码 ? 这是一段程序代码。

  1. List overview = new List(false, 10);
  2. overview.Add(new ListItem("This is an item"));
  3. overview.Add("This is another item");

结果如下:

· This is an item

· This is another item

可以通过SetListSymbol方法来更改列表符号,可以使用图片或其它对象作为列表符号。

隐藏行号 复制代码 ? 这是一段程序代码。

  1. // 用字符串作为列表符号
  2. list1.ListSymbol = "*";
  3. // 用Chunk 作为列表符号(包含“•”字符)
  4. list2.ListSymbol = new Chunk("\u2022", FontFactory.getFont(FontFactory.HELVETICA, 20));
  5. //用图片作为列表符号
  6. list3.ListSymbol = new Chunk(Image.getInstance("myBullet.gif"), 0, 0);

还可以使用IndentationLeft和IndentationRight属性设置缩排,列表符号的缩排使用SymbolIndent属性,也可以在构造函数中设置。

八、注释

你可以添加一小段文本到你的文档中,但它并非文档内容的一部分,注释有标题和内容:

Annotation a = new Annotation(

"authors",

"Maybe it's because I wanted to be an author myself that I wrote iText.");

外部链接注释:

你需要指定一个可点击的矩形和一个字符串(URL描述)或URL对象:

Annotation annot = new Annotation(100f, 700f, 200f, 800f, new URL("http://www.lowagie.com"));

Annotation annot = new Annotation(100f, 700f, 200f, 800f, "http://www.lowagie.com");

外部PDF文件链接注释:

你需要指定一个可点击的矩形和一个字符串(文件名称)和目的文件或页码。

Annotation annot = new Annotation(100f, 700f, 200f, 800f, "other.pdf", "mark");

Annotation annot = new Annotation(100f, 700f, 200f, 800f, "other.pdf", 2);

指定行为链接注释

你需要指定一个可点击的矩形和一个指定的行为:

Annotation annot = new Annotation(100f, 700f, 200f, 800f, PdfAction.FIRSTPAGE);

u 应用程序链接注释:

你需要指定一个可点击的矩形和一个应用程序:

Annotation annot = new Annotation(300f, 700f, 400f, 800f, "C://winnt/notepad.exe", null, null, null);

我们无须在页面上指定一个位置,iText会内部处理。你能够看到iText添加文本注释在页面上当前位置下面,第一个在段后第一行下面,第二个在短句结束处的下面。

所有其他注释需要指定想匹配的矩形区域,在示例代码0304中,我们画了一些正方形(使用的函数将在第十章中介绍),为每个正方形添加了一些链接注释。

九、页眉页脚

在旧版本中,有HeaderFooter对象就可以设置页眉页脚,但是新版本中,已经不存在这个对象。

新版本中,使用新的对象PdfWriter中有一个对象:PdfEvent对象,它实现了如下接口:

隐藏行号 复制代码 ? 这是一段程序代码。

  1. public interface IPdfPageEvent
  2. {
  3. void OnChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title);
  4. void OnChapterEnd(PdfWriter writer, Document document, float paragraphPosition);
  5. void OnCloseDocument(PdfWriter writer, Document document);
  6. void OnEndPage(PdfWriter writer, Document document);
  7. void OnGenericTag(PdfWriter writer, Document document, Rectangle rect, string text);
  8. void OnOpenDocument(PdfWriter writer, Document document);
  9. void OnParagraph(PdfWriter writer, Document document, float paragraphPosition);
  10. void OnParagraphEnd(PdfWriter writer, Document document, float paragraphPosition);
  11. void OnSection(PdfWriter writer, Document document, float paragraphPosition, int depth, Paragraph title);
  12. void OnSectionEnd(PdfWriter writer, Document document, float paragraphPosition);
  13. void OnStartPage(PdfWriter writer, Document document);
  14. }

可以在这里面实现。

十、章节(Chapter)和区域(Section)

章节的使用就比较少了,并且不太好控制,这就不作说明

十一、书签

简单创建书签,使用如下代码:

隐藏行号 复制代码 ? 这是一段程序代码。

  1. protected PdfOutline SetDestination(PdfOutline root, Chunk chk, string name, string destination)
  2. { chk.SetLocalDestination(destination); return new PdfOutline(root, PdfAction.GotoLocalPage(destination, false), name); }

复杂的书签就要使用Pdfaction,PdfOutline,PdfDestination三个对象来创建了。

十二、中文语言支持

中文语言支持,要加入一些扩展dll,加入方法如下所示:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public static void RegisterFont()
        {
            if (!_isRegisterFont)
            {
                lock (typeof(TextSharpHelper))
                {                    if (!_isRegisterFont)
                    {                        BaseFont.AddToResourceSearch("iTextAsian.dll");                        BaseFont.AddToResourceSearch("iTextAsianCmaps.dll");                        FontFactory.Register(Environment.GetFolderPath(Environment.SpecialFolder.System) +                                             @"\..\Fonts\STSONG.ttf");                        FontFactory.Register(Environment.GetFolderPath(Environment.SpecialFolder.System) +                                             @"\..\Fonts\simhei.ttf");                        FontFactory.Register(Environment.GetFolderPath(Environment.SpecialFolder.System) +                                             @"\..\Fonts\simsun.ttc");
                        _isRegisterFont = true;
                    }
                }
            }

        }

上面的两个dll是注册中文语言支持,后面是注册系统下的一些中文字体文件。

十三、文字、表格、图像混排

在进行文字、表格、图像混排中,有时比较难控制位置,最好是把文字、表格、图像分别放到不同的段落中,这样才能很好控制位置。

十四、表单写入

读取表单中的域:

隐藏行号 复制代码 ? 这是一段程序代码。

  1. public static Dictionary<string, string> ReadForm(string pdfTemplate)
  2. {
  3. Dictionary<string, string> dic = new Dictionary<string, string>();
  4. PdfReader pdfReader = null;
  5. try
  6. {
  7. pdfReader = new PdfReader(pdfTemplate);
  8. AcroFields pdfFormFields = pdfReader.AcroFields;
  9. foreach (KeyValuePair<string, AcroFields.Item> de in pdfFormFields.Fields)
  10. {
  11. dic.Add(de.Key, "");
  12. }
  13. }
  14. finally
  15. {
  16. if (pdfReader != null)
  17. {
  18. pdfReader.Close();
  19. }
  20. }
  21. return dic;
  22. }

对表单中的域进行填充:

隐藏行号 复制代码 ? 这是一段程序代码。

  1. public static void FillForm(string pdfTemplate, string newFile, Dictionary<string, string> dic)
  2. {
  3. PdfReader pdfReader = null;
  4. PdfStamper pdfStamper = null;
  5. try
  6. {
  7. pdfReader = new PdfReader(pdfTemplate);
  8. pdfStamper = new PdfStamper(pdfReader, new FileStream(
  9. newFile, FileMode.Create));
  10. AcroFields pdfFormFields = pdfStamper.AcroFields;
  11. foreach (KeyValuePair<string, string> de in dic)
  12. {
  13. pdfFormFields.SetField(de.Key, de.Value);
  14. }
  15. pdfStamper.FormFlattening = true;
  16. }
  17. finally
  18. {
  19. if (pdfReader != null)
  20. {
  21. pdfReader.Close();
  22. }
  23. if (pdfStamper != null)
  24. {
  25. pdfStamper.Close();
  26. }
  27. }
  28. }

简单示例:多个文本文件转换一个PDF

关于使用的类:

StreamReader类使用流(比如文本文件)提供读取访问。

Document类允许创建一个新的PDF文件。

PdfWriter类提供了从文档类对象编写PDF文档的即时访问。

引用的命名空间

  • System.IO,
  • iTextSharp,
  • iTextSharp.text,
  • iTextSharp.text.pdf,
  • iTextSharp.text.pdf.draw

有到的控件:

  1. TextBox Control (txtOutput)
  2. Button Control (btnSelect, btnCreatePDF, btnClear)
  3. ListBox Control (lbInputs)
  4. RadioButton Control (rbCombined, rbIndividual)

在下面,我实现了将多个文本文档转换为单个组合PDF和使用iTextSharp工具转换为单个PDF的代码

The Code 1. Variable Declaration Document doc; //Create a New instance on Document Class

Listing 1 2. Select the Input Text Files (code for the "Select Files" Button); see: using (OpenFileDialog file = new OpenFileDialog()) { //Allow to select multiple files file.Multiselect = true;

//Allow to select only *.txt Files file.Filter = "Only Text Documents|*.txt";

//Show the Dialog box to selcet file(s) file.ShowDialog(); //Add the input file names to ListBox lbInputs.Items.AddRange(file.FileNames); }

Listing 2 3. Function to Create a New instance of the PDFWriter Class: void GetInstance(String str) { //Create a New instance of PDFWriter Class for Output File PdfWriter.GetInstance(doc, new FileStream(str, FileMode.Create)); }

Listing 3 4. Convert Text Files into PDF File(s) (code for "Create PDF" Button):

//For Combined PDF Option is Selected

//Create a New instance on Document Class doc = new Document();

//Create a new instance of PDFWriter Class for output file GetInstance(txtOutput.Text);

//Open the Document doc.Open(); foreach (String var in lbInputs.Items) { //Access Each file in ListBox using StreamReader Class using (StreamReader rdr = new StreamReader(var)) { //Add New Page to the Output file doc.NewPage();

//Add the File name of text file to PDF File doc.Add(new Paragraph("File Name : " + var + "\n\n"));

//Add the content of Text File to PDF File doc.Add(new Paragraph(rdr.ReadToEnd())); } }

//Close the Document doc.Close();

MessageBox.Show("Conversion Successful....");

//Open the Converted PDF File" System.Diagnostics.Process.Start(txtOutput.Text); Listing 4 //For Individual PDFs Option is Selected

//Create a Output Directory for storing individual PDF files Directory.CreateDirectory(txtOutput.Text foreach (String var in lbInputs.Items) { //Create a New instance on Document Class doc = new Document();

//Create a New Instance of FileInfo Class //to get the Extension of selected file FileInfo file = new FileInfo(var);

//Create a new instance of PDFWriter Class for output file GetInstance(txtOutput.Text + file.Name.Replace(file.Extension, ".pdf")); using (StreamReader rdr = new StreamReader(var)) { //Open the Document doc.Open();

//Add the content of Text File to PDF File doc.Add(new Paragraph(rdr.ReadToEnd().ToString())); //Close the Document doc.Close(); } } MessageBox.Show("Conversion Successful...."); Listing 5

现在执行应用程序并查看结果

本文:https://www.cnblogs.com/xcj26/articles/5688236.html

iTextSharp使用可参考博客:

http://www.cnblogs.com/CareySon/archive/2011/11/09/2243496.html

中的系列教程。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-06-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 程序你好 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
iText的使用
首先要getInstance并open一个Document对象,该对象也就代表了这个文件:
LeoXu
2018/08/15
2.4K0
iText 制作PDF
由于在MVC项目中需要使用PDF,所以自己抽空也来看看itext,以便于丰富自己的知识吧。在此也简单的记录一下,说不定以后可能还用的到。
aehyok
2018/09/11
2.4K0
iText 制作PDF
Springboot输出PDF文件
有个人(死需求)跑过来跟你说,这些都给我输出成报告,pdf格式的,所以就有了下面这个,做一下笔记,以后有用直接过来拿。在网上找了一下,发现大家都是在用itext。iText是著名的开放项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。
用户3467126
2019/09/27
2.9K2
Springboot输出PDF文件
SpringBoot + ITextPdf:高效生成 PDF 预览文件
其实公司之前的项目里是用到了帆软报表的,然而最近接了一个新项目,这个项目独立部署在甲方的独立环境中,组长的意思是不用再单独部署一套帆软报表,成本太大,用其他方式实现一下。虽然我不太理解成本大在哪儿,不过身为助理工程师,别管那么多,照着干就完事了。
程序员皮皮林
2024/10/08
8990
SpringBoot + ITextPdf:高效生成 PDF 预览文件
iText5实现Java生成PDF文件完整版
最近项目中使用到Java实现导出PDF文件,经过一番参考研究最终决定使用itextpdf来实现,当然也可以参考 PDF Java类库:Spire.PDF for Java(https://www.e-iceblue.cn/spirepdfjava/create-pdf-in-java.html)。本文是使用第一种来实现的。
全栈程序员站长
2022/07/02
6.3K0
iText5实现Java生成PDF文件完整版
文档在线预览的实现
最近在研究企业文档管理,这个是基本上所有企业都需要的软件,当然也是有很多种解决方案。对于企业文档来说,最基本的需求就是独立存储,共享。这种需求只需要建立一个Windows共享文件夹或者架一个Samba服务器即可实现,无法做复杂的权限管理,统计等。另一种方案就是架一个Web应用,比如SharePoint,就可以实现。
深蓝studyzy
2022/06/16
3.8K0
itext实现合同尾部签章部分自动添加,定位签名
使用的pom <!-- pdf处理 start--> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version>5.2.0</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <vers
老梁
2019/09/10
1.6K0
itext实现合同尾部签章部分自动添加,定位签名
Java组件生成PDF文件
最近和一位朋友聊自己微信小程序的事情,朋友的建议将小程序的文章给去掉,增加一个新的功能,他说我写的文章很鸡肋。自己听完之后内心及其犹豫,一方面自己也很清楚自己的文字的功底确实不行,写的技术文章没多少人看,但是一直想这个能锻炼出来。另一方面虽然文章没人看,但是文章的功能是自己一点一点写出来的,让我隐藏起来,我确实有点不想接受。
每天学Java
2020/06/02
4.9K0
itext7知识点研究(PDF编辑)
static class MyEventListener implements IEventListener { private List<Rectangle> rectangles = new ArrayList<>(); @Override public void eventOccurred(IEventData data, EventType type) { if (type == EventType.RENDER_TEXT) { TextRenderInfo renderInfo = (TextRenderInfo) data; Vector startPoint = renderInfo.getDescentLine().getStartPoint(); Vector endPoint = renderInfo.getAscentLine().getEndPoint(); float x1 = Math.min(startPoint.get(0), endPoint.get(0)); float x2 = Math.max(startPoint.get(0), endPoint.get(0)); float y1 = Math.min(startPoint.get(1), endPoint.get(1)); float y2 = Math.max(startPoint.get(1), endPoint.get(1)); rectangles.add(new Rectangle(x1, y1, x2 - x1, y2 - y1)); } } @Override public Set<EventType> getSupportedEvents() { return new LinkedHashSet<>(Collections.singletonList(EventType.RENDER_TEXT)); } public List<Rectangle> getRectangles() { return rectangles; } public void clear() { rectangles.clear(); } } static class MyCharacterEventListener extends MyEventListener { @Override public void eventOccurred(IEventData data, EventType type) { if (type == EventType.RENDER_TEXT) { TextRenderInfo renderInfo = (TextRenderInfo) data; for (TextRenderInfo tri : renderInfo.getCharacterRenderInfos()) { super.eventOccurred(tri, type); } } } }
老梁
2019/09/10
2.8K0
itext7知识点研究(PDF编辑)
Python 自动化指南(繁琐工作自动化)第二版:十五、使用 PDF 和 WORD 文档
幸运的是,有 Python 模块可以让您轻松地与 PDF 和 Word 文档进行交互。本章将介绍两个这样的模块:PyPDF2 和 Python-Docx。
ApacheCN_飞龙
2023/04/04
3.8K0
Python 自动化指南(繁琐工作自动化)第二版:十五、使用 PDF 和 WORD 文档
给PDF添加水印(Python+C#)
   以下是用PDFlib给pdf添加水印的速记,另外PDFStamp是个很好用的pdf水印工具。PDFlib功能比较多、杂;PDFStamp功能单一,更方便使用。据walker测试,PDFlib会比PDFStamp快一些。
py3study
2020/01/08
1K0
itext实现pdf自动定位合同签订
需求 需要实现如下效果(最终效果) 思考 需求方的要求就是实现签订合同,实现方法不限,但过程中又提出需要在签章的过程中把签订日期的文字也打上去,这就有点坑了~ 一开始的想法是想办法定位需要签名的位
老梁
2019/09/10
2.5K1
itext实现pdf自动定位合同签订
基于iTextSharp的PDF文档操作
  公司是跨境电商,需要和各种物流打交道,需要把东西交给物流,让他们发到世界各地。其中需要物流公司提供一个运单号,来追踪货物到达哪里?!   最近在和DHL物流公司(应该是个大公司)对接,取运单号的方式是调用对方提供的API,简单说,就是我们传一些发货地址和客户信息,要发的货物等,对方返回一个运单号和物流面单(就是我们淘宝快递上的面单)。过程呢,还是比较顺利的,经过一系列沟通,最终还是实现了功能   下面还是说说没有实现的功能,如果都实现了,也不用写这篇博客了。不足之处在于DHL提供的面单,没有提供要拣哪些
用户1219352
2018/02/01
1.9K0
基于iTextSharp的PDF文档操作
PDF批量加水印 与 去除水印实践
本文主要目标是尝试去除水印,但是为了准备测试‍数据,我们需要先准备好有水印的pdf测试文件。
可以叫我才哥
2024/06/18
2670
PDF批量加水印 与 去除水印实践
Itext根据模板生成pdf
public class PdfUtils { // 利用模板生成pdf public static void pdfout(Map<String,Object> o,String newPDFPath){ // 模板路径 String templatePath = "C:/Users/pc/Desktop/990696 list -1.pdf"; // 生成的新文件路径 PdfReader reader; F
故久
2019/09/29
3.2K0
Itext根据模板生成pdf
Java输出Pdf(2021)集成springboot
1.首先,新建一个word文档,内容如下,另存为pdf格式,我的命名:mytest.pdf。
知识浅谈
2021/05/10
1.1K0
Java输出Pdf(2021)集成springboot
itext根据模板生成pdf(支持分页)
// 利用模板生成pdf public static void pdfout(Map<String,Object> o,String newPDFPath){ // 模板路径 File file = new File(""); String filePath = null; try { filePath = file.getCanonicalPath(); } catch (IOException e) { e.printStackTrace(); } System.out.println(filePath); String templatePath = "C:/Users/pc/Desktop/990696 list -1.pdf"; String templatePaths = "C:/Users/pc/Desktop/990696 list -2.pdf"; // 生成的新文件路径 PdfReader reader; PdfReader readers; FileOutputStream out;
故久
2019/09/29
4K0
itext根据模板生成pdf(支持分页)
itext7史上最全实战总结
最近有个需求需要我用Java手动写一份PDF报告,经过考察几种pdf开源代码,最终选取了itext7,此版本为7.1.11,由于发现网上关于该工具的博文比较少,特别是实战博文几乎没有,在我踩完各种坑,最终把PDF成型后,打算把经验分享出来,本文通过摘录解释来说明,内容来自本人GitHub itext-pdf
老梁
2020/06/02
7.2K1
java导出pdf模板_java模板导出PDF[通俗易懂]
一对一,点对点的给对应的地方写值,比如模板里面放了个name标识,在程序里把“张三”赋给name,那么输出的pdf里面name的地方就变成了张三,准确方便快捷
全栈程序员站长
2022/08/25
2.6K0
java导出pdf模板_java模板导出PDF[通俗易懂]
ITextPDF7
document 元素只能添加 AreaBreak 、 Image 对象和 IBlockElement 接口的实现类对象 IBlockElement 的实现类如下图:
全栈程序员站长
2022/09/07
1.6K0
相关推荐
iText的使用
更多 >
领券
💥开发者 MCP广场重磅上线!
精选全网热门MCP server,让你的AI更好用 🚀
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档