简体   繁体   English

动态PDF行样机

[英]Dynamic PDF Row Mockup

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. 我正在用c#在asp.net中动态生成PDF文件,而现在我只是为此做基础。 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) 这是一些代码(这是我第一次这样做,所以请不要对我大喊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). 那是一个问题,但可悲的是,我又遇到了一个问题(尽管愚蠢的人多了10倍,而愚蠢的人却多了10倍)。 The color I want to use is #c5c5c5, but it doesn't want to recognize the color code. 我要使用的颜色是#c5c5c5,但它不想识别颜色代码。

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): 这是我正在使用的ItextSharp的系统的列表(这是Visual Studio和SQLserver的标准系统旁边的,不,如果可能的话,我宁愿不添加更多系统):

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. 您正在使用PdfPRow ,但是您不应该这样做。 The PdfPRow class is for internal use only. PdfPRow类仅供内部使用。 You should work at the PdfPCell level. 您应该在PdfPCell级别上工作。 If you want to color complete rows, you can do so by using a PdfPTableEvent . 如果要为完整的行着色,可以使用PdfPTableEvent See for instance the colored rows in alternating.pdf . 见例如在有色行alternating.pdf They were colored in an AlternatingBackground table event. 它们在AlternatingBackground表事件中被着色。
  2. You have difficulties creating the color #c5c5c5 . 您很难创建颜色#c5c5c5 The hexadecimal value C5 equals 197, hence you want to create the following color object: new BaseColor(197, 197, 197); 十六进制值C5等于197,因此您要创建以下颜色对象: new BaseColor(197, 197, 197);

Your main mistake is that you create a PdfPRow by adding an array of PdfPCell objects. 您的主要错误是您通过添加一个PdfPCell对象数组来创建PdfPRow 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 . AddCell()方法会将字符串包装在Phrase Create a PdfPCell with this Phrase and apply all the properties you've defined for the DefaultCell to that cell. 使用此Phrase创建一个PdfPCell ,并将为DefaultCell定义的所有属性应用于该单元格。 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. 显然,如果您自己创建PdfPCell实例,则DefaultCell的属性将被忽略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM