【问题标题】:print an icard in pdf using itextsharp使用 itextsharp 以 pdf 格式打印 icard
【发布时间】:2012-06-15 01:45:09
【问题描述】:

我想创建一个显示 Icard 的 PDF 文件。

以下是代码:

iTextSharp.text.Font custFont = new iTextSharp.text.Font() { Size=6f};
int borderSize = 1;
Document IcardDoc = new Document(PageSize.HALFLETTER);
PdfWriter.GetInstance(IcardDoc, Response.OutputStream);
IcardDoc.Open();

PdfPTable icardTable = new PdfPTable(2);
iTextSharp.text.Image logoImage = iTextSharp.text.Image.GetInstance(Server.MapPath("~/images/logo.jpg"));
logoImage.WidthPercentage = 15f;
PdfPCell cell = new PdfPCell(logoImage, false);
cell.PaddingLeft = 10f;
cell.PaddingTop = 10f;
cell.PaddingBottom = 10f;
cell.PaddingRight = 10f;
cell.Colspan = 2;
cell.Border = 0;
cell.HorizontalAlignment = 1;
icardTable.AddCell(cell);
icardTable.AddCell(new PdfPCell(new Phrase("Name:", custFont)) { Border = borderSize });
icardTable.AddCell(new PdfPCell(new Phrase(student.firstname + " " + student.lastname, custFont)) { Border = borderSize });

打印文档的尺寸为高 8.5 厘米,宽 5.4 厘米。

所以需要这样调整,是否有任何解决方案,因为上面的代码不适合页面上的表格并且大小不合适?

任何其他格式也可以像 word 等一样。

【问题讨论】:

    标签: c# asp.net pdf itextsharp


    【解决方案1】:
    var pgSize = new iTextSharp.text.Rectangle(1042, 651);
    var doc = new iTextSharp.text.Document(pgSize, 0, 0, 0, 0);
    

    【讨论】:

    • 1042x651 文档与问题中要求的 8.5 厘米高和 5.4 厘米宽有何关系?
    【解决方案2】:

    想出了解决办法:

    Document IcardDoc = new Document(new Retangele(200f, 315f),35f,35f,0f,0f);
    

    并相应地调整表格的宽度:

    PdfPTable icardTable = new PdfPTable(2);
    icardTable.WidthPercentage = 140f;
    

    为了使表格完全适合页面,如果减小矩形width,则增加表格WidthPercentage,反之亦然。 您还需要注意Document 类构造函数中定义为 35f 的左右边距

    这对我有用。

    【讨论】:

      猜你喜欢
      • 2016-11-07
      • 2011-08-31
      • 1970-01-01
      • 2014-06-07
      • 2015-01-01
      • 2015-09-23
      • 2018-02-24
      • 1970-01-01
      • 2012-06-04
      相关资源
      最近更新 更多