//////////////////////////////////////////////////////////////////// // Draw image and clip it //////////////////////////////////////////////////////////////////// private static void DrawTitle() { // define local image resources as 200ppi and 100% quality PdfImageControl ImageControl = new PdfImageControl(); ImageControl.Resolution = 200; ImageControl.ImageQuality = 100; // Get image from embedded local resource Image BocTitle = Properties.Resources.BocTitleBitmap; PdfImage titleImage = new PdfImage(document, BocTitle, ImageControl); // save graphics state Contents.SaveGraphicsState(); // set coordinate Contents.Translate(1.3, 26.8); // set image size PdfRectangle size = titleImage.ImageSizePosition(18.39, 1.85, ContentAlignment.MiddleCenter); // clipping path Contents.DrawRectangle(size.Left, size.Bottom, size.Width, size.Height, PaintOp.ClipPathEor); // draw image Contents.DrawImage(titleImage, size.Left, size.Bottom, size.Width, size.Height); // restore graphics state Contents.RestoreGraphicsState(); return; }
private void DrawLogImage() { // define local image resources // resolution 96 pixels per inch, image quality 100% PdfImageControl ImageControl = new PdfImageControl(); ImageControl.Resolution = 96.0; ImageControl.ImageQuality = 100; CreateLogoFile(); PdfImage Image1 = new PdfImage(Document, "temps/" + pk.Logo.Filename, ImageControl); // save graphics state Contents.SaveGraphicsState(); // translate coordinate origin to the center of the picture Contents.Translate(0, 10); // adjust image size an preserve aspect ratio PdfRectangle NewSize = Image1.ImageSizePosition(3, 1.1, ContentAlignment.MiddleCenter); // clipping path Contents.DrawOval(NewSize.Left, NewSize.Bottom, NewSize.Width, NewSize.Height, PaintOp.Fill); // draw image Contents.DrawImage(Image1, NewSize.Left, NewSize.Bottom, NewSize.Width, NewSize.Height); // restore graphics state Contents.RestoreGraphicsState(); return; }
//////////////////////////////////////////////////////////////////// // Draw image of a flower and clip it //////////////////////////////////////////////////////////////////// private void DrawFlower ( PdfDocument Document, PdfContents Contents ) { // define local image resources PdfImage Image1 = new PdfImage(Document, "flower.jpg"); // image size will be limited to 1.4" by 1.4" SizeD ImageSize = Image1.ImageSize(1.4, 1.4); // save graphics state Contents.SaveGraphicsState(); // translate coordinate origin to the center of the picture Contents.Translate(3.36, 5.7); // clipping path Contents.DrawOval(-ImageSize.Width / 2, -ImageSize.Height / 2, ImageSize.Width, ImageSize.Height, PaintOp.ClipPathEor); // draw image Contents.DrawImage(Image1, -ImageSize.Width / 2, -ImageSize.Height / 2, ImageSize.Width, ImageSize.Height); // restore graphics state Contents.RestoreGraphicsState(); return; }
//////////////////////////////////////////////////////////////////// // 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; }
// Draw example of a text box private static void DrawTextBox(PdfDocument document, PdfPage page) { PdfContents zContents = new PdfContents(page); PdfFont zArialNormal = new PdfFont(document, "Arial", FontStyle.Regular, true); zArialNormal.CharSubstitution(945, 950, 161); // Save graphics state: zContents.SaveGraphicsState(); // translate origin to PosX=1.1" and PosY=1.1" this is the bottom left corner of the text box example zContents.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(zArialNormal, 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(zArialNormal, FontSize - 2.0, "Arial size 7, "); Box.AddText(zArialNormal, FontSize - 1.0, "size 8, "); Box.AddText(zArialNormal, FontSize, "size 9, "); Box.AddText(zArialNormal, FontSize + 1.0, "size 10. "); Box.AddText(zArialNormal, FontSize, DrawStyle.Underline, "Underline, "); Box.AddText(zArialNormal, FontSize, DrawStyle.Strikeout, "Strikeout. "); Box.AddText(zArialNormal, FontSize, "Subscript H"); Box.AddText(zArialNormal, FontSize, DrawStyle.Subscript, "2"); Box.AddText(zArialNormal, FontSize, "O. Superscript A"); Box.AddText(zArialNormal, FontSize, DrawStyle.Superscript, "2"); Box.AddText(zArialNormal, FontSize, "+B"); Box.AddText(zArialNormal, FontSize, DrawStyle.Superscript, "2"); Box.AddText(zArialNormal, FontSize, "=C"); Box.AddText(zArialNormal, FontSize, DrawStyle.Superscript, "2"); Box.AddText(zArialNormal, FontSize, "\n"); Box.AddText(zArialNormal, 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; zContents.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(zArialNormal, 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."); zContents.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(zArialNormal, 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" zContents.DrawText(0.5, ref PosY, 0.0, 0, 0.01, 0.05, TextBoxJustify.Left, Box); // restore graphics state zContents.RestoreGraphicsState(); }
//////////////////////////////////////////////////////////////////// // Draw example of order form //////////////////////////////////////////////////////////////////// private void DrawBookOrderForm ( PdfContents Contents ) { // Order form simulation // Define constants to make the code readable // Define constants const Double Width = 3.05; const Double Height = 3.65; const Double Margin = 0.04; const Double FontSize = 9.0; Double LineSpacing = ArialNormal.LineSpacing(FontSize); Double Descent = ArialNormal.Descent(FontSize); Double ColWidth1 = ArialNormal.TextWidth(FontSize, "9999.99") + 2 * Margin; Double ColWidth2 = ArialNormal.TextWidth(FontSize, "Qty") + 2 * Margin; Double Col4LinePosX = Width - ColWidth1; Double Col3LinePosX = Col4LinePosX - ColWidth2; Double Col2LinePosX = Col3LinePosX - ColWidth1; Double Col1TextPosX = Margin; Double Col2TextPosX = Col3LinePosX - Margin; Double Col3TextPosX = Col4LinePosX - Margin; Double Col4TextPosX = Width - Margin; // save graphics state Contents.SaveGraphicsState(); // form line width 0.01" Contents.SetLineWidth(0.01); // Initial vertical position for contents Double PosY1 = Height - LineSpacing - 2 * Margin; // bottom of the contents area of the form Double PosY2 = 2 * Margin + 3 * LineSpacing; // shift origin, bottom left of the form to X=4.35" and Y=1.1" Contents.Translate(4.35, 1.1); // draw outline rectangle Contents.DrawRectangle(0.0, 0.0, Width, Height, PaintOp.CloseStroke); // draw two horizontal lines. under table heading and above total Contents.DrawLine(0, PosY1, Width, PosY1); Contents.DrawLine(0, PosY2, Width, PosY2); // draw three vertical lines separating the columns Contents.DrawLine(Col2LinePosX, Height, Col2LinePosX, PosY2); Contents.DrawLine(Col3LinePosX, Height, Col3LinePosX, PosY2); Contents.DrawLine(Col4LinePosX, Height, Col4LinePosX, 0); // draw table heading Double PosY = PosY1 + Margin + Descent; Contents.DrawText(ArialNormal, FontSize, Col1TextPosX, PosY, "Description"); Contents.DrawText(ArialNormal, FontSize, Col2TextPosX, PosY, TextJustify.Right, "Price"); Contents.DrawText(ArialNormal, FontSize, Col3TextPosX, PosY, TextJustify.Right, "Qty"); Contents.DrawText(ArialNormal, FontSize, Col4TextPosX, PosY, TextJustify.Right, "Total"); // reset order total Double Total = 0; // define text box for book title and author TextBox Box = new TextBox(Col2LinePosX - 2 * Margin); // initial vertical position PosY = PosY1 - Margin; // loop for all items in the order // Order class is a atabase simulation for this example foreach (Order Book in Order.OrderList) { // clear the text box Box.Clear(); // add book title and authors to the box Box.AddText(ArialNormal, FontSize, Book.Title); Box.AddText(ArialNormal, FontSize, ". By: "); Box.AddText(ArialNormal, FontSize, Book.Authors); // draw the title and authors. // on exit, PosY will be for next line Contents.DrawText(Col1TextPosX, ref PosY, PosY2, 0, Box); // move PosY up to allow drawing cost on the same line as the last text line of the box PosY += Descent; // draw price quantity and item's total Contents.DrawText(ArialNormal, FontSize, Col2TextPosX, PosY, TextJustify.Right, Book.Price.ToString("#.00")); Contents.DrawText(ArialNormal, FontSize, Col3TextPosX, PosY, TextJustify.Right, Book.Qty.ToString()); Contents.DrawText(ArialNormal, FontSize, Col4TextPosX, PosY, TextJustify.Right, Book.Total.ToString("#.00")); // update PosY for next item PosY -= Descent + 0.5 * LineSpacing; // accumulate total Total += Book.Total; } // draw total before tax PosY = PosY2 - Margin - ArialNormal.Ascent(FontSize); Contents.DrawText(ArialNormal, FontSize, Col3TextPosX, PosY, TextJustify.Right, "Total before tax"); Contents.DrawText(ArialNormal, FontSize, Col4TextPosX, PosY, TextJustify.Right, Total.ToString("#.00")); // draw tax (Ontario Canada HST) PosY -= LineSpacing; Contents.DrawText(ArialNormal, FontSize, Col3TextPosX, PosY, TextJustify.Right, "Tax (13%)"); Double Tax = Math.Round(0.13 * Total, 2, MidpointRounding.AwayFromZero); Contents.DrawText(ArialNormal, FontSize, Col4TextPosX, PosY, TextJustify.Right, Tax.ToString("#.00")); // draw final total PosY -= LineSpacing; Contents.DrawText(ArialNormal, FontSize, Col3TextPosX, PosY, TextJustify.Right, "Total payable"); Total += Tax; Contents.DrawText(ArialNormal, FontSize, Col4TextPosX, PosY, TextJustify.Right, Total.ToString("#.00")); // restore graphics state Contents.RestoreGraphicsState(); return; }
//////////////////////////////////////////////////////////////////// // Draw example of a text box //////////////////////////////////////////////////////////////////// private void DrawTextBox ( PdfContents Contents ) { // 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); // Contents.TranslateScaleRotate(7.4, 1.1, 1.0, Math.PI / 2); // 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, "Lets add 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"); // 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, true, 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, false, 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, false, Box); // restore graphics state Contents.RestoreGraphicsState(); return; }