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

Double BaseLine = ArialBold.DescentPlusLeading(FontSize); // the overall pattern box (we add text height value as left and right text margin) Double BoxWidth = TextWidth + 2 * TextHeight; Double BoxHeight = 4 * TextHeight; WaterMark.SetTileBox(BoxWidth, BoxHeight); // save graphics state WaterMark.SaveGraphicsState(); // fill the pattern box with background light blue color WaterMark.SetColorNonStroking(Color.FromArgb(230, 244, 255)); WaterMark.DrawRectangle(0, 0, BoxWidth, BoxHeight, PaintOp.Fill); // set fill color for water mark text to white WaterMark.SetColorNonStroking(Color.White); // draw PdfFileWriter at the bottom center of the box WaterMark.DrawText(ArialBold, FontSize, BoxWidth / 2, BaseLine, TextJustify.Center, Mark); // adjust base line upward by half height BaseLine += BoxHeight / 2; // draw the right half of PdfFileWriter shifted left by half width WaterMark.DrawText(ArialBold, FontSize, 0.0, BaseLine, TextJustify.Center, Mark); // draw the left half of PdfFileWriter shifted right by half width WaterMark.DrawText(ArialBold, FontSize, BoxWidth, BaseLine, TextJustify.Center, Mark); // restore graphics state WaterMark.RestoreGraphicsState(); return;}
复制代码

3.4  画与背景样式的框架

  该DrawFrameAndBackgroundWaterMark方法在具有背景水印图案的示例区域周围绘制框架。模式资源在前面的小节中定义。

 

复制代码
// Draw frame around example area
private void DrawFrameAndBackgroundWaterMark()
{
// save graphics state
Contents.SaveGraphicsState();

// Draw frame around the page
// Set line width to 0.02"
Contents.SetLineWidth(0.02);

// set frame color dark blue
Contents.SetColorStroking(Color.DarkBlue);

// use water mark tiling pattern to fill the frame
Contents.SetPatternNonStroking(WaterMark);

// rectangle position: x=1.0", y=1.0", width=6.5", height=9.0"
Contents.DrawRectangle(1.0, 1.0, 6.5, 9.0, PaintOp.CloseFillStroke);

// restore graphics sate
Contents.RestoreGraphicsState();

// draw article name under the frame
// Note: the \u00a4 is character 164 that was substituted during Font resource definition
// this character is a solid circle it is normally unicode 9679 or \u25cf in the Arial family
Contents.DrawText(ArialNormal, 9.0, 1.1, 0.85, "PdfFileWriter \u25cf PDF File Writer C# Class Library \u25cf Author: Uzi Granot");

// draw web link to the article
Contents.DrawWebLink(Page, ArialNormal, 9.0, 7.4, 0.85, TextJustify.Right, DrawStyle.Underline, Color.Blue, "Click to view article",
    "http://www.codeproject.com/Articles/570682/PDF-File-Writer-Csharp-Class-Library-Version");
return;
}
复制代码

3.5  画两条标题

复制代码
// Draw heading
private void DrawTwoLinesOfHeading()
{
// page heading
// Arguments: Font: ArialBold, size: 36 points, Position: X = 4.25", Y = 9.5"
// Text Justify: Center (text center will be at X position)
// Stoking color: R=128, G=0, B=255 (text outline)
// Nonstroking color: R=255, G=0, B=128 (text body)
Contents.DrawText(Comic, 40.0, 4.25, 9.25, TextJustify.Center, 0.02, Color.FromArgb(128, 0, 255), Color.FromArgb(255, 0, 128), "PDF FILE WRITER");

// save graphics state
Contents.SaveGraphicsState();

// change nonstroking (fill) color to purple
Contents.SetColorNonStroking(Color.Purple);

// Draw second line of heading text
// arguments: Handwriting font, Font size 30 point, Position X=4.25", Y=9.0"
// Text Justify: Center (text center will be at X position)
Contents.DrawText(Comic, 30.0, 4.25, 8.75, TextJustify.Center, "Example");

// restore graphics sate (non stroking color will be restored to default)
Contents.RestoreGraphicsState();
return;
}
复制代码

3.6  画愉快的面孔

  该DrawHappyFace方法是从线和贝塞尔曲线绘制椭圆和构造路径的示例。

 

复制代码
// Draw Happy Face
private void DrawHappyFace()
{
// save graphics state
Contents.SaveGraphicsState();

// translate coordinate origin to the center of the happy face
Contents.Translate(4.25, 7.5);

// change nonstroking (fill) color to yellow
Contents.SetColorNonStroking(Color.Yellow);

// draw happy face yellow oval
Contents.DrawOval(-1.5, -1.0, 3.0, 2.0, PaintOp.Fill);

// set line width to 0.2" this is the black circle around the eye
Contents.SetLineWidth(0.2);

// eye color is white with black outline circle
Contents.SetColorNonStroking(Color.White);
Contents.SetColorStroking(Color.Black);

// draw eyes
Contents.DrawOval(-0.75, 0.0, 0.5, 0.5, PaintOp.CloseFillStroke);
Contents.DrawOval(0.25, 0.0, 0.5, 0.5, PaintOp.CloseFillStroke);

// mouth color is black
Contents.SetColorNonStroking(Color.Black);

// draw mouth by creating a path made of one line and one Bezier curve 
Contents.MoveTo(-0.6, -0.4);
Contents.LineTo(0.6, -0.4);
Contents.DrawBezier(0.0, -0.8, 0, -0.8, -0.6, -0.4);

// fill the path with black color
Contents.SetPaintOp(PaintOp.Fill);

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

3.7  绘制条形码

  该DrawBarcode方法是绘制两个条形码EAN-13和Code-128的示例

  该DrawTwoLinesOfHeading方法在页面中心绘制两个标题线。第一行是用大纲特殊效果绘制文本。

复制代码
// Draw Barcode
private void DrawBarcode()
{
// save graphics state
Contents.SaveGraphicsState();

BarcodeEAN13 Barcode1 = new BarcodeEAN13("1234567890128");
Contents.DrawBarcode(1.3, 7.05, 0.012, 0.75, Barcode1, ArialNormal, 8.0);

PdfQRCode QRCode = new PdfQRCode(Document, "http://www.codeproject.com/Articles/570682/PDF-File-Writer-Csharp-Class-Library-Version", ErrorCorrection.M);
Contents.DrawQRCode(QRCode, 6.0, 6.8, 1.2);

// define a web link area coinciding with the qr code
Page.AddWebLink(6.0, 6.8, 7.2, 8.0, "http://www.codeproject.com/Articles/570682/PDF-File-Writer-Csharp-Class-Library-Version");

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

3.8  绘制与圆角的矩形和填充砖模式

  该DrawBrickPattern方法绘制具有圆角的矩形。该区域填充有砖图案。PdfTillingPattern类是一般类定义划分模式。该类有两个静态方法来创建特定模式。

  SetBrickPattern绘制砖墙图案并SetWeavePattern绘制织造图案。

 

复制代码
// Draw rectangle with rounded corners and filled with brick pattern
private void DrawBrickPattern()
{
// Define brick tilling pattern resource
// Arguments: PdfDocument class, Scale factor (0.25), Stroking color (lines between bricks), Nonstroking color (brick)
// Return value: tilling pattern resource
PdfTilingPattern BrickPattern = PdfTilingPattern.SetBrickPattern(Document, 0.25, Color.LightYellow, Color.SandyBrown);

// save graphics state
Contents.SaveGraphicsState();

// set outline width 0.04"
Contents.SetLineWidth(0.04);

// set outline color to purple
Contents.SetColorStroking(Color.Purple);

// set fill pattern to brick
Contents.SetPatternNonStroking(BrickPattern);

// draw rounded rectangle filled with brick pattern
Contents.DrawRoundedRectangle(1.1, 5.0, 1.4, 1.5, 0.2, PaintOp.CloseFillStroke);

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

3.9  绘制图像并剪切它

  在Draw法师方法是绘制一图象的一个例子。PdfFileWriter存储在支持的所有图像文件的支持图纸图片Bitmap类和Metafile类。ImageFormat类定义所有图像类型。JPEG图像文件类型是PDF文件的本机图像格式。如果PdfImage使用JPEG文件调用构造函数,程序将该文件按原样复制到PDF文件中。如果你调用PdfImage任何其他类型的图像文件构造函数,程序将其转换为JPEG文件。为了保持PDF文件的大小尽可能小,确保您的图像文件分辨率不是不合理的高。

  在PdfImage类加载图像和计算可以适合用户坐标给定的图像大小和保留原始宽高比最大尺寸。在绘制图像之前,我们创建一个椭圆形剪切路径以剪切图像。

复制代码
// Draw image and clip it
private void DrawImage()
{
// define local image resources
// resolution 96 pixels per inch, image quality 50%
PdfImage Image1 = new PdfImage(Document, "TestImage.jpg", 96.0, 50);

// save graphics state
Contents.SaveGraphicsState();

// translate coordinate origin to the center of the picture
Contents.Translate(2.6, 5.0);

// adjust image size and preserve aspect ratio
ImageSizePos NewSize = Image1.ImageSizePosition(1.75, 1.5, ContentAlignment.MiddleCenter);

// clipping path
Contents.DrawOval(NewSize.DeltaX, NewSize.DeltaY, NewSize.Width, NewSize.Height, PaintOp.ClipPathEor);

// draw image
Contents.DrawImage(Image1, NewSize.DeltaX, NewSize.DeltaY, NewSize.Width, NewSize.Height);

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

3.10  绘制心

  该DrawHeart方法通过定义中心线的两端来显示心形。DrawHeart方法PdfContents是绘制形成路径的两个对称贝塞尔曲线的特殊情况。

复制代码
// Draw heart
private void DrawHeart()
{
// save graphics state
Contents.SaveGraphicsState();

// draw heart
// The first argument are the coordinates of the centerline of the heart shape.
// The DrawHeart is a special case of DrawDoubleBezierPath method.
Contents.SetColorNonStroking(Color.HotPink);
Contents.DrawHeart(new LineD(new PointD(4.98, 5.1), new PointD(4.98, 6.0)), PaintOp.CloseFillStroke);

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

3.11  绘制饼图

  该DrawChart方法是定义图表并将其绘制到PDF文档的示例。

复制代码
/ Draw pie chart
private void DrawChart()
{
// save graphics state
Contents.SaveGraphicsState();

// create chart
Chart PieChart = PdfChart.CreateChart(Document, 1.8, 1.5, 300.0);

// create PdfChart object with Chart object
PdfChart PiePdfChart = new PdfChart(Document, PieChart);

// make sure we have good quality image
PieChart.AntiAliasing = AntiAliasingStyles.All;

// set colors
PieChart.BackColor = Color.FromArgb(220, 220, 255);
PieChart.Palette = ChartColorPalette.BrightPastel;

// default font
Font DefaultFont = MyPdfChart.CreateFont("Verdana", FontStyle.Regular, 0.05, FontSizeUnit.UserUnit);
Font TitleFont = MyPdfChart.CreateFont("Verdana", FontStyle.Bold, 0.07, FontSizeUnit.UserUnit);

// title (font size is 0.25 inches)
Title Title1 = new Title("Pie Chart Example", Docking.Top, TitleFont, Color.Purple);
PieChart.Titles.Add(Title1);

// legend
Legend Legend1 = new Legend();
PieChart.Legends.Add(Legend1);
Legend1.BackColor = Color.FromArgb(230, 230, 255);
Legend1.Docking = Docking.Bottom;
Legend1.Font = DefaultFont;

// chart area
ChartArea ChartArea1 = new ChartArea();
PieChart.ChartAreas.Add(ChartArea1);

// chart area background color
ChartArea1.BackColor = Color.FromArgb(255, 200, 255);

// series 1
Series Series1 = new Series();
PieChart.Series.Add(Series1);
Series1.ChartType = SeriesChartType.Pie;
Series1.Font = DefaultFont;
Series1.IsValueShownAsLabel = true;
Series1.LabelFormat = "{0} %";

// series values
Series1.Points.Add(22.0);
Series1.Points[0].LegendText = "Apple";
Series1.Points.Add(27.0);
Series1.Points[1].LegendText = "Banana";
Series1.Points.Add(33.0);
Series1.Points[2].LegendText = "Orange";
Series1.Points.Add(18.0);
Series1.Points[3].LegendText = "Grape";

Contents.DrawChart(MyPdfChart, 5.6, 5.0);

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

3.12  绘制文本框

DrawTextBox方法是使用TextBox的示例TextBox类格式文本,以适应列中。可以使用字体的样式和大小的真实性来绘制文本。

复制代码
// Draw example of a text box
private void DrawTextBox()
{
// save graphics state
Contents.SaveGraphicsState();

// translate origin to PosX=1.1" and PosY=1.1" this is the bottom left corner of the text box example
Contents.Translate(1.1, 1.1);

// Define constants
// Box width 3.25"
// Box height is 3.65"
// Normal font size is 9.0 points.
const Double Width = 3.15;
const Double Height = 3.65;
const Double FontSize = 9.0;

// Create text box object width 3.25"
// First line indent of 0.25"
TextBox Box = new TextBox(Width, 0.25);

// add text to the text box
Box.AddText(ArialNormal, FontSize,
    "This area is an example of displaying text that is too long to fit within a fixed width " +
    "area. The text is displayed justified to right edge. You define a text box with the required " +
    "width and first line indent. You add text to this box. The box will divide the text into " + 
    "lines. Each line is made of segments of text. For each segment, you define font, font " +
    "size, drawing style and color. After loading all the text, the program will draw the formatted text.\n");
Box.AddText(TimesNormal, FontSize + 1.0, "Example of multiple fonts: Times New Roman, ");
Box.AddText(Comic, FontSize, "Comic Sans MS, ");
Box.AddText(ArialNormal, FontSize, "Example of regular, ");
Box.AddText(ArialBold, FontSize, "bold, ");
Box.AddText(ArialItalic, FontSize, "italic, ");
Box.AddText(ArialBoldItalic, FontSize, "bold plus italic. ");
Box.AddText(ArialNormal, FontSize - 2.0, "Arial size 7, ");
Box.AddText(ArialNormal, FontSize - 1.0, "size 8, ");
Box.AddText(ArialNormal, FontSize, "size 9, ");
Box.AddText(ArialNormal, FontSize + 1.0, "size 10. ");
Box.AddText(ArialNormal, FontSize, DrawStyle.Underline, "Underline, ");
Box.AddText(ArialNormal, FontSize, DrawStyle.Strikeout, "Strikeout. ");
Box.AddText(ArialNormal, FontSize, "Subscript H");
Box.AddText(ArialNormal, FontSize, DrawStyle.Subscript, "2");
Box.AddText(ArialNormal, FontSize, "O. Superscript A");
Box.AddText(ArialNormal, FontSize, DrawStyle.Superscript, "2");
Box.AddText(ArialNormal, FontSize, "+B");
Box.AddText(ArialNormal, FontSize, DrawStyle.Superscript, "2");
Box.AddText(ArialNormal, FontSize, "=C");
Box.AddText(ArialNormal, FontSize, DrawStyle.Superscript, "2");
Box.AddText(ArialNormal, FontSize, "\n");
Box.AddText(Comic, FontSize, Color.Red, "Some color, ");
Box.AddText(Comic, FontSize, Color.Green, "green, ");
Box.AddText(Comic, FontSize, Color.Blue, "blue, ");
Box.AddText(Comic, FontSize, Color.Orange, "orange, ");
Box.AddText(Comic, FontSize, DrawStyle.Underline, Color.Purple, "and purple.\n");
Box.AddText(ArialNormal, FontSize, "Support for non-Latin letters: ");
Box.AddText(ArialNormal, FontSize, Contents.ReverseString( "עברית"));
Box.AddText(ArialNormal, FontSize, "АБВГДЕ");
Box.AddText(ArialNormal, FontSize, "αβγδεζ");

Box.AddText(ArialNormal, FontSize, "\n");

// Draw the text box
// Text left edge is at zero (note: origin was translated to 1.1") 
// The top text base line is at Height less first line ascent.
// Text drawing is limited to vertical coordinate of zero.
// First line to be drawn is line zero.
// After each line add extra 0.015".
// After each paragraph add extra 0.05"
// Stretch all lines to make smooth right edge at box width of 3.15"
// After all lines are drawn, PosY will be set to the next text line after the box's last paragraph
Double PosY = Height;
Contents.DrawText(0.0, ref PosY, 0.0, 0, 0.015, 0.05, TextBoxJustify.FitToWidth, Box);

// Create text box object width 3.25"
// No first line indent
Box = new TextBox(Width);

// Add text as before.
// No extra line spacing.
// No right edge adjustment
Box.AddText(ArialNormal, FontSize,
    "In the examples above this area the text box was set for first line indent of " +
    "0.25 inches. This paragraph has zero first line indent and no right justify.");
Contents.DrawText(0.0, ref PosY, 0.0, 0, 0.01, 0.05, TextBoxJustify.Left, Box);

// Create text box object width 2.75
// First line hanging indent of 0.5"
Box = new TextBox(Width - 0.5, -0.5);

// Add text
Box.AddText(ArialNormal, FontSize,
    "This paragraph is set to first line hanging indent of 0.5 inches. " +
    "The left margin of this paragraph is 0.5 inches.");

// Draw the text
// left edge at 0.5"
Contents.DrawText(0.5, ref PosY, 0.0, 0, 0.01, 0.05, TextBoxJustify.Left, Box);

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

3.13  绘制订单表格

  该DrawBookOrderForm


相关教程