动态PDF行样机 [英] Dynamic PDF Row Mockup

查看:167
本文介绍了动态PDF行样机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题,答案很可能是真的很明显,简单的,但我想我已经在搜索互联网的答案失败了,所以我再次来到你们。

我动态生成用C#在asp.net中的PDF文件,而现在我只是做它的基础。其中一个它产生的东西是在一个购物车的内容应披露表(是我说的发票),我想给的表中的一些样机,但模拟为上排会有所不同比其他人。 (其中列被定义页眉(数量,名称,单价,折扣和Total)

下面是一些code(这是我第一次这样做,所以不要在我的xD喊)

  PdfPCell数量=新PdfPCell(新词(数量));
PdfPCell标题=新PdfPCell(新词(标题));
PdfPCell UniPr =新PdfPCell(新词(单价));
PdfPCell光盘=新PdfPCell(新词(折扣));
PdfPCell总=新PdfPCell(新词(合计));
PdfPCell [] = cartheaderc {数量,标题,UniPr,光盘,总};
PdfPRow cartheader =新PdfPRow(cartheaderc);

所以,我已经尝试过这种方式,然后说:

  PdfPRow.BackgroundColor =新BaseColor(255,0,0);

由于该工程细胞,我想这可能是有意义的,但显然事实并非如此。我大概可以做到这一点,当我把每一个细胞分开,而是应该有一个更简单的方法,对吧?

这是一个问题,但可悲的是,我有一个更多(尽管10倍的愚蠢和10倍更容易)。颜色我想用的是#c5c5c5,但它并不想承认的颜色code。

下面是系统iTextSharp的我正在使用的列表(这是从Visual Studio和SQLServer标准的系统旁边,不,我宁可不希望添加更多的系统如果可能的话):

 使用System.IO;
使用iTextSharp.text;
使用iTextSharp.text.pdf;


解决方案

您有两个问题:


  1. 您正在使用 PdfPRow ,但你不应该这样做。在 PdfPRow 类仅供内部使用。您应该在 PdfPCell 一级的工作。如果你想要的颜色完全行,您可以通过使用 PdfPTableEvent 这样做。见例如在 alternating.pdf 有色行。他们在 AlternatingBackground 表事件进行着色。

  2. 您已经创建的颜色#c5c5c5 困难。十六进制值 C5 等于197,因此,您要创建下列颜色对象:新BaseColor(197,197,197);

您主要错误是通过添加 PdfPCell 对象的数组创建一个 PdfPRow 。你从哪里得到的灵感这样做呢?如果您发现有人谁写了这样一个例子,请让我知道,如果他靠近,我会亲自揍他; - )

表是这样创建:

  PdfPTable表=新PdfPTable(5);
PdfPCell数量=新PdfPCell(新词(数量));
table.AddCell(数量);
PdfPCell标题=新PdfPCell(新词(标题));
table.AddCell(标题);
PdfPCell UniPr =新PdfPCell(新词(单价));
table.AddCell(UniPr);
PdfPCell光盘=新PdfPCell(新词(折扣));
table.AddCell(光盘);
PdfPCell总=新PdfPCell(新词(合计));
table.AddCell(总计);

还有就是要做到这一点更简单的方法。这更简单的方法,您还可以定义每个单元格的背景颜色:

  PdfPTable表=新PdfPTable(5);
table.DefaultCell.BackgroundColor =新BaseColor(197,197,197);
table.AddCell(数量);
table.AddCell(标题);
table.AddCell(单价);
table.AddCell(折扣);
table.AddCell(合计);

AddCell()方法将一个包裹短语里面的字符串。创建一个 PdfPCell 短语并应用您为 DefaultCell中定义的所有属性该小区。通过这种方式,可以确保所有的细胞具有相同的回地面的颜色(或边界,或......)。显然,属性的 DefaultCell 如果你自己创建的 PdfPCell 实例将被忽略。

I have a small problem and the answer is probably really obvious and simple, but I guess I have failed in searching the internet for an answer, so once again I came to you guys.

I'm dynamically generating a PDF file in asp.net with c#, and right now I'm just making the base for it. One of the things it generates is a table in which a cart content should be revealed (yes I'm talking about an invoice) and I'm trying to give the table some mockup, but the mock up for the upper row will be different than the rest. (the header in which the columns are defined (Quantity, Title, Unit Price, Discount and Total)

Here's some code (it's the first time I did this so don't yell at me xD)

PdfPCell Quantity = new PdfPCell(new Phrase("Quantity"));
PdfPCell Title = new PdfPCell(new Phrase("Title"));
PdfPCell UniPr = new PdfPCell(new Phrase("Unit Price"));
PdfPCell Disc = new PdfPCell(new Phrase("Discount"));
PdfPCell Total = new PdfPCell(new Phrase("Total"));
PdfPCell[] cartheaderc = { Quantity, Title, UniPr, Disc, Total };
PdfPRow cartheader = new PdfPRow(cartheaderc);

So I've tried it this way and then say:

PdfPRow.BackgroundColor = new BaseColor(255,0,0);

Since that works for cells, I thought this might make sense, but apparently it didn't. I probably can do it when I take each cell apart, but there should be an easier way, right?

That's one problem, but sadly enough, I've got one more (although 10x more stupid and 10x easier). The color I want to use is #c5c5c5, but it doesn't want to recognize the color code.

Here is a list of the systems for ItextSharp I'm using (this is beside the standard systems from Visual Studio and SQLserver and no, I rather not want to add more systems if possible):

using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

解决方案

You have two questions:

  1. You are using PdfPRow, but you're not supposed to do that. The PdfPRow class is for internal use only. You should work at the PdfPCell level. If you want to color complete rows, you can do so by using a PdfPTableEvent. See for instance the colored rows in alternating.pdf. They were colored in an AlternatingBackground table event.
  2. You have difficulties creating the color #c5c5c5. The hexadecimal value C5 equals 197, hence you want to create the following color object: new BaseColor(197, 197, 197);

Your main mistake is that you create a PdfPRow by adding an array of PdfPCell objects. Where did you get inspiration to do so? If you find somebody who wrote such an example, please let me know and if he's near, I'll personally spank him ;-)

Tables are created like this:

PdfPTable table = new PdfPTable(5);
PdfPCell Quantity = new PdfPCell(new Phrase("Quantity"));
table.AddCell(Quantity);
PdfPCell Title = new PdfPCell(new Phrase("Title"));
table.AddCell(Title);
PdfPCell UniPr = new PdfPCell(new Phrase("Unit Price"));
table.AddCell(UniPr);
PdfPCell Disc = new PdfPCell(new Phrase("Discount"));
table.AddCell(Disc);
PdfPCell Total = new PdfPCell(new Phrase("Total"));
table.AddCell(Total);

There is an even easier way to do this. This easier way also allows you to define the background color for every cell:

PdfPTable table = new PdfPTable(5);
table.DefaultCell.BackgroundColor = new BaseColor(197, 197, 197);
table.AddCell("Quantity");
table.AddCell("Title");
table.AddCell("Unit Price");
table.AddCell("Discount");
table.AddCell("Total");

The AddCell() method will wrap the strings inside a Phrase. Create a PdfPCell with this Phrase and apply all the properties you've defined for the DefaultCell to that cell. This way, you can make sure that all the cell have the same back ground color (or border, or...). Obviously, the properties of the DefaultCell will be ignored if you create PdfPCell instances yourself.

这篇关于动态PDF行样机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆