CSharp: iText-donet 8.0 in donet 4.8.1

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebAppPdfDemo.WebForm1" ResponseEncoding="utf-8" %>
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="renderer" content="webkit" />
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no"/>
    <title>geovindu</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
        </div>
    </form>
</body>
</html>

  

 

 

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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using iText.IO.Font;
using iText.IO.Image;
using iText.Kernel.Font;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas.Draw;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using iText.Pdfa;
using iText.IO;
using iText.Kernel.Pdf.Xobject;
using iText.Kernel.Utils;
using iText.Kernel.Events;
using iText.Kernel.Colors;
using iText.Kernel.Geom;
using iText.Kernel.Pdf.Canvas;
using iText.Layout.Borders;
 
 
 
namespace WebAppPdfDemo
{
 
 
    /// <summary>
    /// iText 8.0
    ///
    /// </summary>
    public partial class WebForm1 : System.Web.UI.Page
    {
 
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
 
 
            try
            {
 
                //字体设定,c:\windows\fonts\kaiu.ttf,中文楷体
                string filePath = Server.MapPath(@"sRGB_CS_profile.icm");
                string fontFile = @"c:\windows\fonts\kaiu.ttf";
                string fontFile2 = Server.MapPath(@"font\\MHeiHK-Light.TTF");
                string fileName = Server.MapPath(@"sample\\" + DateTime.Now.ToString("yyyyMMHHmmss") + ".pdf");
 
 
                List<Orders> orders = new List<Orders>();
                Orders order = new Orders();
                order.productId = 1;
                order.product = "中國電器";
                order.qty = 50;
                order.price = 52.0;
                orders.Add(order);
 
                order = new Orders();
                order.productId = 2;
                order.product = "日本電器";
                order.qty = 150;
                order.price = 252.0;
                orders.Add(order);
 
 
                //PdfADocument pdf = new PdfADocument(
                //new PdfWriter(fileName),
                //PdfAConformanceLevel.PDF_A_1B,
                //new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1",
                // new FileStream(filePath, FileMode.Open, FileAccess.Read)));
 
                PdfDocument pdf = new PdfDocument(new PdfWriter(new FileStream(fileName, FileMode.Create, FileAccess.Write)));
 
 
                //FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                //PdfReader reader=new PdfReader(stream);
                //PdfADocument pdf = new PdfADocument(reader, new PdfWriter(fileName));    
 
 
                //中文 字體和文字要相適應
                //FontProgramFactory.CreateFont(fontFile);
                PdfFont font = PdfFontFactory.CreateFont(fontFile2, PdfEncodings.IDENTITY_H);//PdfFontFactory.EmbeddingStrategy.FORCE_EMBEDDED
                Document document = new Document(pdf, iText.Kernel.Geom.PageSize.A4);
 
                document.SetFont(font);
 
 
                //指定位置添加图片  images\luflog20180410145504.png
                //PdfContentByte pcb1 = writer.DirectContent;
 
                //image1.SetAbsolutePosition(12, 18); //底部
                PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.Create(Server.MapPath("images/luflog20180410145504.png")));
                iText.Layout.Element.Image image1 = new iText.Layout.Element.Image(xObject, 200);
                // ColumnText ct = new ColumnText(pcb1);
                image1.ScaleAbsolute(148, 50);
                //pcb1.AddImage(image1);    
                //Resize image depend upon your need
                image1.ScaleToFit(140f, 120f);
 
                //p = new Paragraph(new Chunk(image1, 10f, 10f));
                iText.Layout.Element.Table datatable = new iText.Layout.Element.Table(5, true); //5列               
 
                Cell pCTitle = new Cell(1, 5).SetBorder(iText.Layout.Borders.Border.NO_BORDER);//无边
                Paragraph p = new Paragraph().SetTextAlignment(TextAlignment.CENTER);
                p.Add(image1.SetTextAlignment(TextAlignment.CENTER));
                pCTitle.Add(p);
                datatable.AddCell(pCTitle);
                document.Add(datatable);
 
 
 
 
 
                Paragraph header = new Paragraph("ORDER DETAIL 顧客塗聚文訂單詳情").SetTextAlignment(TextAlignment.CENTER).SetFontSize(20);
                document.Add(header);
 
                Paragraph subheader = new Paragraph("C# WINDOWS FORM CREATE PDF DOCUMENT USING iTEXT7 LIBRARY").SetTextAlignment(TextAlignment.CENTER).SetFontSize(10);
                document.Add(subheader);
 
                LineSeparator ls = new LineSeparator(new SolidLine());
                document.Add(ls);
 
                Paragraph sellerHeader = new Paragraph("Sold by:").SetBold().SetTextAlignment(TextAlignment.LEFT);
                Paragraph sellerDetail = new Paragraph("Seller Company").SetTextAlignment(TextAlignment.LEFT);
                Paragraph sellerAddress = new Paragraph("Shenzhen city, Guangdong China").SetTextAlignment(TextAlignment.LEFT);
                Paragraph sellerContact = new Paragraph("+91 1000000000").SetTextAlignment(TextAlignment.LEFT);
 
                document.Add(sellerHeader);
                document.Add(sellerDetail);
                document.Add(sellerAddress);
                document.Add(sellerContact);
 
                Paragraph customerHeader = new Paragraph("Customer details:").SetBold().SetTextAlignment(TextAlignment.RIGHT);
                Paragraph customerDetail = new Paragraph("Customer ABC").SetTextAlignment(TextAlignment.RIGHT);
                Paragraph customerAddress1 = new Paragraph("Buxin 1008, shuibei Jewellery Builder").SetTextAlignment(TextAlignment.RIGHT);
                Paragraph customerAddress2 = new Paragraph("Shenzhen 518019, Guangdong China").SetTextAlignment(TextAlignment.RIGHT);
 
                Paragraph customerContact = new Paragraph("+85 0000000000").SetTextAlignment(TextAlignment.RIGHT);
 
                document.Add(customerHeader);
                document.Add(customerDetail);
                document.Add(customerAddress1);
                document.Add(customerAddress2);
                document.Add(customerContact);
 
                Paragraph orderNo = new Paragraph("Order No:15484659").SetBold().SetTextAlignment(TextAlignment.LEFT);
                Paragraph invoiceNo = new Paragraph("Invoice No:MH-MU-1077").SetTextAlignment(TextAlignment.LEFT);
                Paragraph invoiceTimestamp = new Paragraph("Date: 30/01/2024 04:25:37 PM").SetTextAlignment(TextAlignment.LEFT);
 
                document.Add(orderNo);
                document.Add(invoiceNo);
                document.Add(invoiceTimestamp);
 
                iText.Layout.Element.Table table = new iText.Layout.Element.Table(5, true);
 
                table.SetFontSize(9);
                Cell headerProductId = new Cell(1, 1).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph("ID"));
                Cell headerProduct = new Cell(1, 1).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph("產品名稱"));
                Cell headerProductPrice = new Cell(1, 1).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph("價格"));
                Cell headerProductQty = new Cell(1, 1).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph("數量"));
                Cell headerTotal = new Cell(1, 1).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph("合計"));
 
                table.AddCell(headerProductId);
                table.AddCell(headerProduct);
                table.AddCell(headerProductPrice);
                table.AddCell(headerProductQty);
                table.AddCell(headerTotal);
 
                double grandTotalVal = 0;
                foreach (Orders c in orders)
                {
                    Cell productid = new Cell(1, 1).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph(c.productId.ToString()));
                    Cell product = new Cell(1, 1).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph(c.product));
                    Cell price = new Cell(1, 1).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph(c.price.ToString()));
                    Cell qty = new Cell(1, 1).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph(c.qty.ToString()));
                    var value = c.price * c.qty;
                    Cell total = new Cell(1, 1).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph(value.ToString()));
 
                    grandTotalVal += value;
                    table.AddCell(productid);
                    table.AddCell(product);
                    table.AddCell(price);
                    table.AddCell(qty);
                    table.AddCell(total);
                }
 
                Cell grandTotalHeader = new Cell(1, 4).SetTextAlignment(TextAlignment.RIGHT).Add(new Paragraph("合計: "));
                Cell grandTotal = new Cell(1, 1).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph(" " + grandTotalVal.ToString()));
 
                table.AddCell(grandTotalHeader);
                table.AddCell(grandTotal);
 
                document.Add(table);
 
 
                //底部
                float[] tableWidth = { 445, 50F };
                iText.Layout.Element.Table footerTable = new iText.Layout.Element.Table(5).SetFixedPosition(20F, 25F, 780F).SetBorder(iText.Layout.Borders.Border.NO_BORDER);
 
                PdfImageXObject xObject2 = new PdfImageXObject(ImageDataFactory.Create(Server.MapPath("images/lufaddress_20180410145531.png")));
                iText.Layout.Element.Image image2 = new iText.Layout.Element.Image(xObject2, 500);
                //image2.SetMarginBottom(18); //底部
                //image2.ScaleAbsolute(585, 40);
                image2.SetTextAlignment(TextAlignment.LEFT);
                Paragraph p2 = new Paragraph().SetTextAlignment(TextAlignment.LEFT);
                p2.Add(image2.SetTextAlignment(TextAlignment.LEFT));
 
                Cell cfoot = new Cell(1, 5).SetBorder(iText.Layout.Borders.Border.NO_BORDER); //离得远
                cfoot.Add(p2);
                footerTable.AddCell(cfoot).SetTextAlignment(TextAlignment.LEFT);
                document.Add(footerTable);
 
 
                // document.Add(image2);
 
                table.Flush();
                table.Complete();
                document.Close();
                Response.Charset = "utf-8";//设置输出流的字符集-中文
                Response.ContentType = "application/pdf";
                // Response.Write(fileName);
 
                MemoryStream m = new MemoryStream();
                FileStream fs = new FileStream(fileName, FileMode.Open);
                fs.CopyTo(m);
                Response.OutputStream.Write(m.GetBuffer(), 0, m.GetBuffer().Length);
                Response.OutputStream.Flush();
                Response.OutputStream.Close();
 
                //自动下载
                //Response.Clear();
                //Response.ContentType = "application/pdf";
                //Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
                //Response.TransmitFile(fileName);
                //Response.End();
 
 
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
                Response.Write(ex.Message.ToString());
            }
 
 
        }
    }
 
 
    /// <summary>
    /// 订单实体类
    /// </summary>
    public class Orders
    {
        /// <summary>
        /// ID
        /// </summary>
        public int productId { get; set; }
        /// <summary>
        /// 产品名称
        /// </summary>
        public string product { get; set; }
        /// <summary>
        /// 数量
        /// </summary>
        public int qty { get; set; }
        /// <summary>
        /// 价格
        /// </summary>
        public double price { get; set; }
    }
}

  

 

 

 

 

posted @   ®Geovin Du Dream Park™  阅读(59)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2018-01-30 JqGrid: paging int asp.net
2013-01-30 Csharp: 打印設置字符之間的間距
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示