VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > c#教程 >
  • C#教程之C#教程之Pdf File Writer 中文应用(PDF文件编写器C#(5)

本站最新发布   C#从入门到精通
试听地址  
https://www.xin3721.com/eschool/CSharpxin3721/

方法是订单输入表单或发票的示例。它是数据表支持的一个示例。它演示使用的PdfTablePdfTableCell以及PdfTableStyle类。

复制代码
// Draw example of order form
private void DrawBookOrderForm()
{
// Define constants to make the code readable
const Double Left = 4.35;
const Double Top = 4.65;
const Double Bottom = 1.1;
const Double Right = 7.4;
const Double FontSize = 9.0;
const Double MarginHor = 0.04;
const Double MarginVer = 0.04;
const Double Border = 0.015;
const Double Grid = 0.01;

// column widths
Double ColWidthPrice = ArialNormal.TextWidth(FontSize, "9999.99") + 2.0 * MarginHor;
Double ColWidthQty = ArialNormal.TextWidth(FontSize, "Qty") + 2.0 * MarginHor;
Double ColWidthDesc = Right - Left - Border - 3 * Grid - 2 * ColWidthPrice - ColWidthQty;

// define table
PdfTable Table = new PdfTable(Page, Contents, ArialNormal, FontSize);
Table.TableArea = new PdfRectangle(Left, Bottom, Right, Top);
Table.SetColumnWidth(new Double[] {ColWidthDesc, ColWidthPrice, ColWidthQty, ColWidthPrice});

// define all borders
Table.Borders.SetAllBorders(FrameWidth, GridWidth);

// margin
PdfRectangle Margin = new PdfRectangle(MarginHor, MarginVer);

// default header style
Table.DefaultHeaderStyle.Margin = Margin;
Table.DefaultHeaderStyle.BackgroundColor = Color.FromArgb(255, 196, 255);
Table.DefaultHeaderStyle.Alignment = ContentAlignment.MiddleRight;

// private header style for description
Table.Header[0].Style = Table.HeaderStyle;
Table.Header[0].Style.Alignment = ContentAlignment.MiddleLeft;

// table heading
Table.Header[0].Value = "Description";
Table.Header[1].Value = "Price";
Table.Header[2].Value = "Qty";
Table.Header[3].Value = "Total";

// default style
Table.DefaultCellStyle.Margin = Margin;

// description column style
Table.Cell[0].Style = Table.CellStyle;
Table.Cell[0].Style.MultiLineText = true;

// qty column style
Table.Cell[2].Style = Table.CellStyle;
Table.Cell[2].Style.Alignment = ContentAlignment.BottomRight;

Table.DefaultCellStyle.Format = "#,##0.00";
Table.DefaultCellStyle.Alignment = ContentAlignment.BottomRight;

Contents.DrawText(ArialBold, FontSize, 0.5 * (Left + Right), Top + MarginVer + Table.DefaultCellStyle.FontDescent,
    TextJustify.Center, DrawStyle.Normal, Color.Purple, "Example of PdfTable support (new for 1.10.0)");

// reset order total
Double Total = 0;

// loop for all items in the order
// Order class is a atabase simulation for this example
foreach(Order Book in Order.OrderList)
    {
    Table.Cell[0].Value = Book.Title + ". By: " + Book.Authors;
    Table.Cell[1].Value = Book.Price;
    Table.Cell[2].Value = Book.Qty;
    Table.Cell[3].Value = Book.Total;
    Table.DrawRow();

    // accumulate total
    Total += Book.Total;
    }
Table.Close();

// save graphics state
Contents.SaveGraphicsState();

// form line width 0.01"
Contents.SetLineWidth(FrameWidth);
Contents.SetLineCap(PdfLineCap.Square);

// draw total before tax
Double[] ColumnPosition = Table.ColumnPosition;
Double TotalDesc = ColumnPosition[3] - MarginHor;
Double TotalValue = ColumnPosition[4] - MarginHor;
Double PosY = Table.RowTopPosition - 2.0 * MarginVer - Table.DefaultCellStyle.FontAscent;
Contents.DrawText(ArialNormal, FontSize, TotalDesc, PosY, TextJustify.Right, "Total before tax");
Contents.DrawText(ArialNormal, FontSize, TotalValue, PosY, TextJustify.Right, Total.ToString("#.00"));

// draw tax (Ontario Canada HST)
PosY -= Table.DefaultCellStyle.FontLineSpacing;
Contents.DrawText(ArialNormal, FontSize, TotalDesc, PosY, TextJustify.Right, "Tax (13%)");
Double Tax = Math.Round(0.13 * Total, 2, MidpointRounding.AwayFromZero);
Contents.DrawText(ArialNormal, FontSize, TotalValue, PosY, TextJustify.Right, Tax.ToString("#.00"));

// draw total line
PosY -= Table.DefaultCellStyle.FontDescent + 0.5 * MarginVer;
Contents.DrawLine(ColumnPosition[3], PosY, ColumnPosition[4], PosY);

// draw final total
PosY -= Table.DefaultCellStyle.FontAscent + 0.5 * MarginVer;
Contents.DrawText(ArialNormal, FontSize, TotalDesc, PosY, TextJustify.Right, "Total payable");
Total += Tax;
Contents.DrawText(ArialNormal, FontSize, TotalValue, PosY, TextJustify.Right, Total.ToString("#.00"));

PosY -= Table.DefaultCellStyle.FontDescent + MarginVer;
Contents.DrawLine(ColumnPosition[0], Table.RowTopPosition, ColumnPosition[0], PosY);
Contents.DrawLine(ColumnPosition[0], PosY, ColumnPosition[4], PosY);
Contents.DrawLine(ColumnPosition[4], Table.RowTopPosition, ColumnPosition[4], PosY);

// restore graphics state
Contents.RestoreGraphicsState();
return;
}
复制代码

4.  参考文献

  • Adobe PDF文件规范文档可从Adobe网站获得:“PDF参考,第六版,Adobe便携式文档格式版本1.7,2006年11月”
  • 有关OpenType字体规范的信息可以在Microsoft Typography - OpenType Specification找到
  • “PDF文件分析器与C#解析类”一文中提供了与PDF Deflate压缩类匹配的解压缩类的源
  • 压缩和解压缩类的原始源代码取自Uzi Granot在CodeProject.com网站上发表的“使用C#压缩/解压缩类处理标准ZIP文件”一文。

5.  其他开源软件由这个作者

  • Android Color Selector for Programmers是Google Play上的免费应用程式。
  • Google Play应用内结算演示应用本文是Google Play应用内结算版本3的一个示例。

6.  历史

  • 2013/04/01:版本1.0原始版本。
  • 2013/04/09:版本1.1支持除句点之外的小数分隔符的国家/地区。
  • 2013/07/21:版本1.2原始版本支持的图像资源只有jpeg文件格式。版本1.2支持Bitmap类可接受的所有图像文件。请参阅ImageFormat类。程序测试与:Bmp,Gif,图标,Jpeg,Png和Tiff。参见上面的2.3节和3.8节。
  • 2014/02/07:版本1.3修正PdfContents.DrawBezierNoP2(PointD P1,PointD P3)的错误。
  • 2014/03/01:1.4版改进了对字符替换的支持。改进对图像包含的支持。一些修复与PdfXObject相关。
  • 2014/05/05:版本1.5不使用字体的条形码支持。包括四个条形码:Code-128,Code-39,UPC-A和EAN-13。参见第2.5节和第3.7节。
  • 2014/07/09:版本1.6(1)创建文件后,CreateFile方法将PdfDocument重置为初始条件。(2)PdfFont对象正确地释放非托管代码资源。
  • 2014/08/25:版本1.7支持文档加密,网络链接和QR码。
  • 2014/09/12:版本1.8支持书签。
  • 2014/10/06:版本1.9支持制图,PrintDocument和图像元文件。
  • 2014/10/12:版本1.9.1修正为ChartExample。解析除句点之外的具有小数分隔符的区域中的数字字段。
  • 2014/12/02:1.10.0版本支持数据表。添加源代码文档。增加每个文档的最大图像数量。
  • 2015/01/12:版本1.11.0支持视频,声音和附件文件。添加对Interleave 2 of 5条码的支持。
  • 2015/04/13:版本1.12.0支持重新排序页面和增强数据表边框线支持。
  • 2015/05/05:1.13.0版本PDF文档输出到流。PDF表插入分页符。图像质量增强。支持标准128(RC4)加密。
  • 2015/06/08:1.14.0版支持PDF表中的长文本块或TextBox。
  • 2015/06/09:版本1.14.1一行改为PdfTableStyle类的复制方法。
  • 2015/06/17:版本1.15.0文档信息字典。PdfImage重写。其他图像保存选项。
  • 2015/06/18:版本1.15.1从解决方案资源管理器中删除未使用的源。
  • 2015/07/27:版本1.16.0 Unicode支持。提交页面方法。
  • 2015/08/07:版本1.16.1。修正小(<0.0001)实数转换为字符串。
  • 2015/09/01:版本1.16.2。修复未定义的字符。所选字体不支持使用的字符。 
  • 2015/09/22:版本1.16.3。PdfTable构造函数使用当前页面大小来计算默认表区域矩形。当PdfTable启动新页面时,从上一页获取页面类型和方向。
  • 2015/09/30:Version 1.16.4一致使用IDisposable接口释放非托管资源。
  • 2016/01/26:版本1.17.0 WPF图形,透明度,颜色混合,椭圆弧和二次贝塞尔曲线。
  • 2016/02/29:Version 1.17.1当第一列标题是TextBox时,PdfTable将正确显示标题。
  • 2016/03/22:版本1.17.2 PdfInfo PDF文档属性将正确显示。
  • 2016/04/14:版本1.17.3修正了将非整数字体大小在将小数分隔符定义为非句点(逗号)的区域中的问题。
  • 2016/05/24:版本1.18.0命名目的地和PdfFont资源的创建。
  • 2016/06/02:版本1.18.1重新申请1.17.3修复。
  • 2016/06/13:版本1.19.0文档链接。命名目标的更改。交互功能支持TextBox和PdfTable。
  • 2016/07/27:版本1.19.1 Fix:AddLocationMarker修复了具有小数分隔符而不是句点的区域。

7.  执照

  本文以及任何相关的源代码和文件均已获得代码项目开放许可证(CPOL)许可。

8.  关于原作者

  加拿大 Uzi格兰诺   格兰 科技有限公司

 

该文由小居工作室原创 翻译并提供解答支持,原文地址:Pdf File Writer 中文应用(PDF文件编写器C#类库):

 

相关教程