gpt4 book ai didi

c# - 使用 IText 创建 Pdf 文档#

转载 作者:太空狗 更新时间:2023-10-29 20:57:23 24 4
gpt4 key购买 nike

如何创建包含表格的 pdf 文档,如下所示。

enter image description here

最佳答案

添加命名空间:

using iTextSharp.text;
using iTextSharp.text.pdf;

使用 C# 编写的代码:

    Document doc = new Document(PageSize.A4);
var output = new FileStream(Server.MapPath("MyFirstPDF.pdf"), FileMode.Create);
var writer = PdfWriter.GetInstance(doc, output);


doc.Open();


var logo = iTextSharp.text.Image.GetInstance(Server.MapPath("~/ABsIS_Logo.jpg"));
logo.SetAbsolutePosition(430, 770);
logo.ScaleAbsoluteHeight(30);
logo.ScaleAbsoluteWidth(70);
doc.Add(logo);

PdfPTable table1 = new PdfPTable(2);
table1.DefaultCell.Border = 0;
table1.WidthPercentage = 80;

var titleFont = new Font(Font.FontFamily.UNDEFINED, 24);
var subTitleFont = new Font(Font.FontFamily.UNDEFINED, 16);

PdfPCell cell11 = new PdfPCell();
cell11.Colspan = 1;
cell11.AddElement(new Paragraph("ABC Traders Receipt", titleFont));

cell11.AddElement(new Paragraph("Thankyou for shoping at ABC traders,your order details are below", subTitleFont));


cell11.VerticalAlignment = Element.ALIGN_LEFT;

PdfPCell cell12 = new PdfPCell();


cell12.VerticalAlignment = Element.ALIGN_CENTER;


table1.AddCell(cell11);

table1.AddCell(cell12);


PdfPTable table2 = new PdfPTable(3);

//One row added

PdfPCell cell21 = new PdfPCell();

cell21.AddElement(new Paragraph("Photo Type"));

PdfPCell cell22 = new PdfPCell();

cell22.AddElement(new Paragraph("No. of Copies"));

PdfPCell cell23 = new PdfPCell();

cell23.AddElement(new Paragraph("Amount"));


table2.AddCell(cell21);

table2.AddCell(cell22);

table2.AddCell(cell23);


//New Row Added

PdfPCell cell31 = new PdfPCell();

cell31.AddElement(new Paragraph("Safe"));

cell31.FixedHeight = 300.0f;

PdfPCell cell32 = new PdfPCell();

cell32.AddElement(new Paragraph("2"));

cell32.FixedHeight = 300.0f;

PdfPCell cell33 = new PdfPCell();

cell33.AddElement(new Paragraph("20.00 * " + "2" + " = " + (20 * Convert.ToInt32("2")) + ".00"));

cell33.FixedHeight = 300.0f;



table2.AddCell(cell31);

table2.AddCell(cell32);

table2.AddCell(cell33);


PdfPCell cell2A = new PdfPCell(table2);

cell2A.Colspan = 2;

table1.AddCell(cell2A);

PdfPCell cell41 = new PdfPCell();

cell41.AddElement(new Paragraph("Name : " + "ABC"));

cell41.AddElement(new Paragraph("Advance : " + "advance"));

cell41.VerticalAlignment = Element.ALIGN_LEFT;

PdfPCell cell42 = new PdfPCell();

cell42.AddElement(new Paragraph("Customer ID : " + "011"));

cell42.AddElement(new Paragraph("Balance : " + "3993"));

cell42.VerticalAlignment = Element.ALIGN_RIGHT;


table1.AddCell(cell41);

table1.AddCell(cell42);


doc.Add(table1);

doc.Close();

关于c# - 使用 IText 创建 Pdf 文档#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5776090/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com