itextSharp 使用模板(PdfTemplate)不规则分栏(ColumnText)

  1   public static void Main()
  2     {
  3
  4         Document document = new Document();
  5         BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA , BaseFont.CP1252 , BaseFont.NOT_EMBEDDED);
  6         Font font = new Font(bf , 11 , Font.NORMAL);
  7         Color green=new Color(0 , 131 , 34);
  8
  9         try
 10         {
 11
 12             PdfWriter writer = PdfWriter.getInstance(document , new FileStream("tpl.pdf" , FileMode.Create));
 13
 14             document.Open();
 15
 16             PdfContentByte cb = writer.DirectContent;
 17             PdfTemplate tpl = cb.createTemplate(600 , 800);
 18             PdfTemplate tplCount = cb.createTemplate(50 , 50);
 19             Image caesar = Image.getInstance("caesar_coin.jpg");
 20
 21             //Graphic grx = new Graphic();
 22             //grx.setHorizontalLine(2 , 90 , green);
 23             //grx.stroke();
 24             //tpl.Add(grx);
 25
 26
 27             tpl.GrayFill = 0.75f;
 28             tpl.moveTo(310 , 112);
 29             tpl.lineTo(280 , 60);
 30             tpl.lineTo(340 , 60);
 31             tpl.closePath();
 32             tpl.moveTo(310 , 790);
 33             tpl.lineTo(310 , 710);
 34             tpl.moveTo(310 , 580);
 35             tpl.lineTo(310 , 122);
 36             tpl.stroke();
 37
 38
 39             ColumnText ct = new ColumnText(cb);
 40             ct.addText(new Phrase("\r\nPhrase Title\r\n" , FontFactory.getFont(FontFactory.HELVETICA , 22,Font.BOLD,green)));
 41             ct.addText(new Phrase("GALLIA est omnis divisa in partes tres, quarum unam incolunt Belgae, aliam Aquitani, tertiam qui ipsorum lingua Celtae, nostra Galli appellantur.  Hi omnes lingua, institutis, legibus inter se differunt. Gallos ab Aquitanis Garumna flumen, a Belgis Matrona et Sequana dividit. Horum omnium fortissimi sunt Belgae, propterea quod a cultu atque humanitate provinciae longissime absunt, minimeque ad eos mercatores saepe commeant atque ea quae ad effeminandos animos pertinent important, proximique sunt Germanis, qui trans Rhenum incolunt, quibuscum continenter bellum gerunt.  Qua de causa Helvetii quoque reliquos Gallos virtute praecedunt, quod fere cotidianis proeliis cum Germanis contendunt, cum aut suis finibus eos prohibent aut ipsi in eorum finibus bellum gerunt.\n" , FontFactory.getFont(FontFactory.HELVETICA , 12)));
 42
 43             ct.addText(new Phrase("\r\nPhrase Title\r\n" , FontFactory.getFont(FontFactory.HELVETICA , 22 , Font.BOLD , green)));
 44             ct.addText(new Phrase("Eorum una, pars, quam Gallos obtinere dictum est, initium capit a flumine Rhodano, continetur Garumna flumine, Oceano, finibus Belgarum, attingit etiam ab Sequanis et Helvetiis flumen Rhenum, vergit ad septentriones. Belgae ab extremis Galliae finibus oriuntur, pertinent ad inferiorem partem fluminis Rheni, spectant in septentrionem et orientem solem. Aquitania a Garumna flumine ad Pyrenaeos montes et eam partem Oceani quae est ad Hispaniam pertinet; spectat inter occasum solis et septentriones.\n" , FontFactory.getFont(FontFactory.HELVETICA , 12)));
 45
 46
 47
 48
 49
 50
 51
 52             ct.addText(new Phrase("\r\nPhrase Title:: jpg\r\n" , FontFactory.getFont(FontFactory.HELVETICA , 22 , Font.BOLD , green)));
 53             var ph=new Phrase("Apud Helvetios longe nobilissimus fuit et ditissimus Orgetorix.  Is M. Messala, [et P.] M.  Pisone consulibus regni cupiditate inductus coniurationem nobilitatis fecit et civitati persuasit ut de finibus suis cum omnibus copiis exirent:  perfacile esse, cum virtute omnibus praestarent, totius Galliae imperio potiri.  Id hoc facilius iis persuasit, quod undique loci natura Helvetii continentur:  una ex parte flumine Rheno latissimo atque altissimo, qui agrum Helvetium a Germanis dividit; altera ex parte monte Iura altissimo, qui est inter Sequanos et Helvetios; tertia lacu Lemanno et flumine Rhodano, qui provinciam nostram ab Helvetiis dividit.  His rebus fiebat ut et minus late vagarentur et minus facile finitimis bellum inferre possent; qua ex parte homines bellandi cupidi magno dolore adficiebantur.  Pro multitudine autem hominum et pro gloria belli atque fortitudinis angustos se fines habere arbitrabantur, qui in longitudinem milia passuum CCXL, in latitudinem CLXXX patebant.\n" , FontFactory.getFont(FontFactory.HELVETICA , 12));
 54             Image image = Image.getInstance("caesar_coin.jpg");
 55             //image.Alignment = Image.RIGHT | Image.TEXTWRAP;
 56             //Chunk bx=new Chunk(image,0,0,true);
 57             //ph.addSpecial(bx);
 58             //ct.addText(ph);
 59
 60             cb.addImage(image,100 , 0 , 0 , 100 ,350 , 300);
 61
 62             ct.addText(new Phrase("\r\nPhrase Title\r\n" , FontFactory.getFont(FontFactory.HELVETICA , 22 , Font.BOLD , green)));
 63             ct.addText(new Phrase("His rebus adducti et auctoritate Orgetorigis permoti constituerunt ea quae ad proficiscendum pertinerent comparare, iumentorum et carrorum quam maximum numerum coemere, sementes quam maximas facere, ut in itinere copia frumenti suppeteret, cum proximis civitatibus pacem et amicitiam confirmare.  Ad eas res conficiendas biennium sibi satis esse duxerunt; in tertium annum profectionem lege confirmant.  Ad eas res conficiendas Orgetorix deligitur.  Is sibi legationem ad civitates suscipit.  In eo itinere persuadet Castico, Catamantaloedis filio, Sequano, cuius pater regnum in Sequanis multos annos obtinuerat et a senatu populi Romani amicus appellatus erat, ut regnum in civitate sua occuparet, quod pater ante habuerit; itemque Dumnorigi Haeduo, fratri Diviciaci, qui eo tempore principatum in civitate obtinebat ac maxime plebi acceptus erat, ut idem conaretur persuadet eique filiam suam in matrimonium dat.  Perfacile factu esse illis probat conata perficere, propterea quod ipse suae civitatis imperium obtenturus esset:  non esse dubium quin totius Galliae plurimum Helvetii possent; se suis copiis suoque exercitu illis regna conciliaturum confirmat.  Hac oratione adducti inter se fidem et ius iurandum dant et regno occupato per tres potentissimos ac firmissimos populos totius Galliae sese potiri posse sperant.\n" , FontFactory.getFont(FontFactory.HELVETICA , 12)));
 64
 65
 66             ct.addText(new Phrase("\r\nPhrase Title\r\n" , FontFactory.getFont(FontFactory.HELVETICA , 22 , Font.BOLD , green)));
 67             ct.addText(new Phrase("Ea res est Helvetiis per indicium enuntiata.  Moribus suis Orgetoricem ex vinculis causam dicere coegerunt; damnatum poenam sequi oportebat, ut igni cremaretur.  Die constituta causae dictionis Orgetorix ad iudicium omnem suam familiam, ad hominum milia decem, undique coegit, et omnes clientes obaeratosque suos, quorum magnum numerum habebat, eodem conduxit; per eos ne causam diceret se eripuit.  Cum civitas ob eam rem incitata armis ius suum exequi conaretur multitudinemque hominum ex agris magistratus cogerent, Orgetorix mortuus est; neque abest suspicio, ut Helvetii arbitrantur, quin ipse sibi mortem consciverit." , FontFactory.getFont(FontFactory.HELVETICA , 12)));
 68
 69
 70             float[] left1  = { 70 , 790 , 70 , 60 };
 71             float[] right1 = { 300 , 790 , 300 , 700 , 240 , 700 , 240 , 590 , 300 , 590 , 300 , 106 , 270 , 60 };
 72             float[] left2  = { 320 , 790 , 320 , 700 , 380 , 700 , 380 , 590 , 320 , 590 , 320 , 106 , 350 , 60 };
 73             float[] right2 = { 550 , 790 , 550 , 60 };
 74
 75             int status = 0;
 76             int column = 0;
 77
 78             var header=new Rectangle(50 , 795 , 570 , 815);
 79             header.BackgroundColor=green;
 80             header.BorderColor=green;
 81             cb.rectangle(header);
 82             cb.stroke();
 83
 84             var footer=new Rectangle(50 , 35 , 570 , 55);
 85             footer.BackgroundColor=green;
 86             footer.BorderColor=green;
 87             cb.rectangle(footer);
 88             cb.stroke();
 89
 90             cb.setRGBColorFill(255 , 255 , 255);
 91
 92             cb.beginText();
 93             cb.setFontAndSize(bf , 12);
 94             cb.setTextMatrix(70 ,800);
 95             cb.showText("U als ondernemer");
 96             cb.setTextMatrix(550-bf.getWidthPoint("Resultaten" , 12) , 800);
 97             cb.showText("Resultaten");
 98             cb.endText();
 99
100             String pageText = "Page " + writer.PageNumber + " of ";
101             float pageLen = bf.getWidthPoint(pageText , 12);
102             cb.beginText();
103             cb.setFontAndSize(bf , 12);
104             cb.setTextMatrix(280 , 40);
105             cb.showText(pageText);
106             cb.endText();
107
108             cb.addTemplate(tplCount , 280 + pageLen , 40);
109             cb.resetRGBColorFill();
110
111
112             cb.addTemplate(tpl , 0 , 0);
113             cb.addImage(caesar , 100 , 0 , 0 , 100 , 260 , 595);
114
115             while((status & ColumnText.NO_MORE_TEXT) == 0)
116             {
117                 if(column == 0)
118                 {
119                     ct.setColumns(left1 , right1);
120                     column = 1;
121                 }
122                 else
123                 {
124                     ct.setColumns(left2 , right2);
125                     column = 0;
126                 }
127                 status = ct.go();
128                  ct.YLine = 790;
129                  ct.Alignment = Element.ALIGN_JUSTIFIED;
130                 status = ct.go();
131
132                 if((column == 0) && ((status & ColumnText.NO_MORE_COLUMN) != 0))
133                 {
134                     document.newPage();
135
136                     cb.rectangle(header);
137                     cb.stroke();
138
139                     cb.rectangle(footer);
140                     cb.stroke();
141
142                     cb.setRGBColorFill(255 , 255 , 255);
143
144
145
146                     cb.beginText();
147                     cb.setFontAndSize(bf , 12);
148                     cb.setTextMatrix(70 , 800);
149                     cb.showText("U als ondernemer");
150                     cb.setTextMatrix(550-bf.getWidthPoint("Resultaten" , 12) , 800);
151                     cb.showText("Resultaten");
152                     cb.endText();
153
154                     pageText = "Page " + writer.PageNumber + " of ";
155                     pageLen = bf.getWidthPoint(pageText , 12);
156
157                     cb.beginText();
158                     cb.setFontAndSize(bf , 12);
159                     cb.setTextMatrix(280 , 40);
160                     cb.showText(pageText);
161                     cb.endText();
162
163                     cb.addTemplate(tplCount , 280 + pageLen , 40);
164                     cb.resetRGBColorFill();
165
166
167                     cb.addTemplate(tpl, 0 , 0);
168                     cb.addImage(caesar , 100 , 0 , 0 , 100 , 260 , 595);
169
170
171
172
173                 }
174             }
175
176
177
178             tplCount.beginText();
179             tplCount.setFontAndSize(bf , 12);
180             tplCount.showText((writer.PageNumber).ToString());
181             tplCount.endText();
182
183             document.Close();
184         }
185         catch(DocumentException de)
186         {
187             Console.Error.WriteLine(de.Message);
188         }
189         catch(IOException ioe)
190         {
191             Console.Error.WriteLine(ioe.Message);
192         }
193     }

itextSharp 使用模板(PdfTemplate)不规则分栏(ColumnText)

时间: 2024-10-05 05:04:57

itextSharp 使用模板(PdfTemplate)不规则分栏(ColumnText)的相关文章

利用套打和分栏巧妙来做商品价签

商品价签,非常多人都会想到用PS.Ai等工具,然后各种的填写数据.各种的调整位置,各种的浪费时间啊有木有!.! 忽然间想到FineReport具有的强大功能,套打.非常多人都看了帮助文档,我也不例外,苦心研究,经历了两天的时间.最终有了成果. 甚是欣慰啊! 首先扫描模板(十张价签拼接成一张不规则的An纸张).然后保存在WebReport\WEB-INF\reportlets\doc\目录下,格式JPG.图片导入进去了.那么问题来了. 怎么样吧数据搞进去.然后各种測试,開始时候想着把价签单独搞出来

ActiveReports 报表应用教程 (4)---分栏报表

原文:ActiveReports 报表应用教程 (4)---分栏报表 在 ActiveReports 中可以实现分栏报表布局样式,可以设置横向分栏.纵向分栏,同时进行分栏和分组设置,统计分栏分组的小计.合计等.在商业报表系统中常见的分栏报表有商品标签.员工工卡.条码打印等.本文主要讲解如何在 ActiveReports 中实现横向分栏.纵向分栏和分组分栏报表. 1.横向分栏报表 1.1.在 ASP.NET 应用程序中添加一个名为 rptAcrossDown.cs 的 ActiveReports

分栏报表-物品清单报表实现

提起分栏报表,它的数据组织形式就如同小时候使用的数学作业本一样,将数据分为多栏显示,当然写作业时有的人习惯横向写,有的人则喜欢竖着写完,在写另一栏.所有的电子技术都是采用隐喻的手法,从物理世界而来,分栏报表也是源于生活,分栏报表也可以行分栏(竖向分栏)和列分栏(横向分栏).分栏数可以分为两栏或多栏.行分栏数据流从上往下的方向显示数据,超过固定的行数就另起一栏显示,列分栏的数据的流向是从左往右,超过固定的列数就另起一栏显示. 在商业报表系统中常见的分栏报表有商品标签.员工工卡.条码打印等.由此可见

ListView分栏--制作分栏音乐列表

之前我遇到过这样的需求,要求在ListView中按时间对数据分栏,当时的做法是在每个ListView的item中加入时间栏的布局,然后在代码中控制时间栏 的显示与隐藏. 但其实重写Adapter两个方法后就可以完成这个任务,当ListView中带有不同布局的时候,可以根据itemType来加载不同的布局. int getItemViewType(int position) 返回指定position的itemView的viewType,用于加载不同布局.此方法必须返回0到getViewTypeCo

Sublime Text 3运行程序以及分栏快捷键的设置

Sublime Text 3软件由于每次运行程序都要执行:Tools -> SublimeREPL -> Python -> RUN current file 菜单有点麻烦,所以现在可以考虑给他创建个快捷键 依次点击:perferences ------ key bindings user,将下列代码粘贴 {"keys":["f2"],"caption": "SublimeREPL: Python - RUN curr

导航状态栏(UINavigationBar)和分栏状态栏(UITabBar)

//*************导航状态栏(UINavigationController)*************** 1.在导航控制器中,所有的子视图控制器共用一个导航状态栏 UINavigationBar *navBar = self.navigationController.navigationBar 2.可设置导航栏的显示风格 navBar.barStyle = UIBarStyleDefault; 3.可设置导航栏的不透明度,默认为YES(不透明) //如果设置为透明,则子视图的左上角

JavaScript(二)--实现分栏

Javascript中讲到了分栏的小实例,这篇文章就写一个小的例子吧,从这个例子中看看我们都学到什么? (一)源代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtm

javascript有用小技巧—实现分栏显示

记得给师哥师姐測试考试系统的时候,看到他们的考试页面能够实现隐藏左边的考生信息部分,当时认为好高大上.好人性化. 如今学了javascript,我也能实现这个功能了,以下来显摆一下. 1.页面设计: (1).html代码: <title>js分栏</title> <style type="text/css"> .alignment{ text-align: center; } </style> </head> <scri

WORD里怎样能做到局部“分栏”就是一页里有的分有的不分

选中你要分的部分再分栏如果不想分的部分也被分了,那就可以选中不想分的那部分,选择“分栏”->“一栏” 转自:http://zhidao.baidu.com/question/9873268.html?qbl=relate_question_0&word=word%20%B7%D6%C0%B8%20%D6%D0%B6%CF&optimi=4