private void DrawLineFunc(PdfPageBase page) { state = page.Canvas.Save(); //Draw Rectagle PdfPen pen = PdfPens.DeepSkyBlue; PdfBrush brush = new PdfSolidBrush(Color.Red);//画刷,颜色为red PdfPath path = new PdfPath(); float offset = 50; PointF[] points = new PointF[] { new PointF(0, 0), new PointF(0, 80), new PointF(80, 80), new PointF(80, 0) }; for (int i = 0; i < points.Length; i++) { points[i].X += offset; points[i].Y += offset; } path.AddLine(points[0], points[1]); path.AddLine(points[1], points[2]); path.AddLine(points[2], points[3]); path.AddLine(points[3], points[0]); page.Canvas.DrawPath(pen, path); page.Canvas.TranslateTransform(280, 0); path.FillMode = PdfFillMode.Winding; page.Canvas.DrawPath(pen, brush, path); //drawing line PdfPath Linepath = new PdfPath(); Linepath.AddLine(new PointF(70, 270), new PointF(96, 192)); page.Canvas.DrawPath(pen, Linepath); //restor graphics page.Canvas.Restore(state); }
protected virtual void BeforeProcessing() { // Check PDF path if (!PdfPath.isFilePathOK(".pdf")) { throw new System.IO.FileNotFoundException(String.Concat("File not found: ", PdfPath)); } // Open Logger try { myLogger = new Logger(String.Concat(System.IO.Path.GetDirectoryName(PdfPath), "\\ErrorLog.txt")); } catch { throw; } // Start Timer timer = new Stopwatch(); timer.Start(); // Start counter at 0 counter = 0; if (Bw == null) { throw new ArgumentNullException("Background Worker is null"); } }
private void DrawStrightLine(PdfPageBase page) { PointF[] points = new PointF[4]; //clear the watermark int downRate = 1; int leftRate = 2; points[0] = new PointF(-1 - leftRate, -1 + downRate); points[1] = new PointF(-1 - leftRate, 1 + downRate); points[2] = new PointF(1 - leftRate, 1 + downRate); points[3] = new PointF(1 - leftRate, -1 + downRate); PdfPath path = new PdfPath(); path.AddLine(points[0], points[1]); path.AddLine(points[1], points[2]); path.AddLine(points[2], points[3]); path.AddLine(points[3], points[0]); //save graphics state //PdfGraphicsState state = page.Canvas.Save(); PdfPen pen = new PdfPen(Color.DeepSkyBlue, 0.02f); page.Canvas.ScaleTransform(105f, 105f); page.Canvas.TranslateTransform(5f, 1.2f); page.Canvas.DrawPath(pen, path); //drawing a stright line PdfPath path2 = new PdfPath(); path2.AddLine(new PointF(-4.5f, 0), new PointF(-3f, 3.53f)); PdfPen pen2 = new PdfPen(Color.Red, 0.046f); page.Canvas.DrawPath(pen2, path2); }
private string GetCode(PdfPath path, decimal pageHeight, bool subPaths) { if (path == null) { return(string.Empty); } pathCount++; string hocr = string.Empty; if (subPaths) { foreach (var subPath in path.Commands) { var subBbox = subPath.GetBoundingRectangle(); if (subBbox != null) { hocr += _indent + _indent + @"<span class='ocr_linedrawing' id='drawing_" + pageCount + "_" + pathCount + "' title='" + GetCode(subBbox.Value, pageHeight) + "' />\n"; } } } var bbox = GetBoundingRectangle(path.Commands); if (bbox.HasValue) { hocr += _indent + _indent + @"<span class='ocr_linedrawing' id='drawing_" + pageCount + "_0' title='" + GetCode(bbox.Value, pageHeight) + "' />"; } return(hocr); }
private static void setBrushAndPen(PdfCanvas target, PdfPath path) { if (path.PaintMode == PdfDrawMode.Fill || path.PaintMode == PdfDrawMode.FillAndStroke) { target.Brush.Opacity = path.Brush.Opacity; PdfColor color = path.Brush.Color; if (color != null) { target.Brush.Color = path.Brush.Color; } } if (path.PaintMode == PdfDrawMode.Stroke || path.PaintMode == PdfDrawMode.FillAndStroke) { target.Pen.Opacity = path.Pen.Opacity; target.Pen.Width = path.Pen.Width; target.Pen.DashPattern = path.Pen.DashPattern; target.Pen.EndCap = path.Pen.EndCap; target.Pen.LineJoin = path.Pen.LineJoin; target.Pen.MiterLimit = path.Pen.MiterLimit; PdfColor color = path.Pen.Color; if (color != null) { target.Pen.Color = path.Pen.Color; } } }
private void DrawStrightLine(PdfPageBase page) { PointF[] points = new PointF[4]; points[0] = new PointF(-1, -1); points[1] = new PointF(-1, 1); points[2] = new PointF(1, 1); points[3] = new PointF(1, -1); PdfPath path = new PdfPath(); path.AddLine(points[0], points[1]); path.AddLine(points[1], points[2]); path.AddLine(points[2], points[3]); path.AddLine(points[3], points[0]); //save graphics state PdfGraphicsState state = page.Canvas.Save(); PdfPen pen = new PdfPen(Color.DeepSkyBlue, 0.02f); // PdfBrush brush1 = new PdfSolidBrush(Color.CadetBlue); page.Canvas.ScaleTransform(80f, 80f); page.Canvas.TranslateTransform(5f, 1.2f); page.Canvas.DrawPath(pen, path); PdfPath path2 = new PdfPath(); path2.AddLine(new PointF(-3, 0), new PointF(-3, 3)); page.Canvas.DrawPath(pen, path2); }
public bool TryGenerate(string name, out PdfPath path) { path = default(PdfPath); lock (locker) { if (glyphs.TryGetValue(name, out path)) { return(true); } if (!CharStrings.TryGetValue(name, out var sequence)) { return(false); } try { path = Run(sequence); glyphs[name] = path; } catch { return(false); } } return(true); }
/// <summary> /// Extracts text fragments from the 2nd page and highlights the glyphs in the fragment. /// </summary> /// <param name="document"></param> private static void ExtractTextAndHighlightGlyphs(PdfFixedDocument document) { PdfRgbColor penColor = new PdfRgbColor(); PdfPen pen = new PdfPen(penColor, 0.5); Random rnd = new Random(); byte[] rgb = new byte[3]; PdfContentExtractor ce = new PdfContentExtractor(document.Pages[1]); PdfTextFragmentCollection tfc = ce.ExtractTextFragments(); PdfTextFragment tf = tfc[1]; for (int i = 0; i < tf.Glyphs.Count; i++) { rnd.NextBytes(rgb); penColor.R = rgb[0]; penColor.G = rgb[1]; penColor.B = rgb[2]; PdfPath boundingPath = new PdfPath(); boundingPath.StartSubpath(tf.Glyphs[i].GlyphCorners[0].X, tf.Glyphs[i].GlyphCorners[0].Y); boundingPath.AddLineTo(tf.Glyphs[i].GlyphCorners[1].X, tf.Glyphs[i].GlyphCorners[1].Y); boundingPath.AddLineTo(tf.Glyphs[i].GlyphCorners[2].X, tf.Glyphs[i].GlyphCorners[2].Y); boundingPath.AddLineTo(tf.Glyphs[i].GlyphCorners[3].X, tf.Glyphs[i].GlyphCorners[3].Y); boundingPath.CloseSubpath(); document.Pages[1].Graphics.DrawPath(pen, boundingPath); } }
/// <summary> /// Extracts text fragments from the 3rd page and highlights the glyphs in the fragment. /// </summary> /// <param name="document"></param> private static void ExtractImagesAndHighlight(PdfFixedDocument document) { PdfPen pen = new PdfPen(new PdfRgbColor(255, 0, 192), 0.5); PdfBrush brush = new PdfBrush(new PdfRgbColor(0, 0, 0)); PdfStandardFont helvetica = new PdfStandardFont(PdfStandardFontFace.Helvetica, 8); PdfStringAppearanceOptions sao = new PdfStringAppearanceOptions(); sao.Brush = brush; sao.Font = helvetica; PdfStringLayoutOptions slo = new PdfStringLayoutOptions(); slo.Width = 1000; PdfContentExtractor ce = new PdfContentExtractor(document.Pages[2]); PdfVisualImageCollection eic = ce.ExtractImages(false); for (int i = 0; i < eic.Count; i++) { string imageProperties = string.Format("Image ID: {0}\nPixel width: {1} pixels\nPixel height: {2} pixels\n" + "Display width: {3} points\nDisplay height: {4} points\nHorizonal Resolution: {5} dpi\nVertical Resolution: {6} dpi", eic[i].ImageID, eic[i].Width, eic[i].Height, eic[i].DisplayWidth, eic[i].DisplayHeight, eic[i].DpiX, eic[i].DpiY); PdfPath boundingPath = new PdfPath(); boundingPath.StartSubpath(eic[i].ImageCorners[0].X, eic[i].ImageCorners[0].Y); boundingPath.AddLineTo(eic[i].ImageCorners[1].X, eic[i].ImageCorners[1].Y); boundingPath.AddLineTo(eic[i].ImageCorners[2].X, eic[i].ImageCorners[2].Y); boundingPath.AddLineTo(eic[i].ImageCorners[3].X, eic[i].ImageCorners[3].Y); boundingPath.CloseSubpath(); document.Pages[2].Graphics.DrawPath(pen, boundingPath); slo.X = eic[i].ImageCorners[3].X + 1; slo.Y = eic[i].ImageCorners[3].Y + 1; document.Pages[2].Graphics.DrawString(imageProperties, sao, slo); } }
private static void appendPath(PdfCanvas target, PdfPath path) { foreach (PdfSubpath subpath in path.Subpaths) { foreach (PdfPathSegment segment in subpath.Segments) { switch (segment.Type) { case PdfPathSegmentType.Point: target.CurrentPosition = ((PdfPointSegment)segment).Value; break; case PdfPathSegmentType.Line: PdfLineSegment line = (PdfLineSegment)segment; target.CurrentPosition = line.Start; target.AppendLineTo(line.End); break; case PdfPathSegmentType.Bezier: PdfBezierSegment bezier = (PdfBezierSegment)segment; target.CurrentPosition = bezier.Start; target.AppendCurveTo(bezier.FirstControl, bezier.SecondControl, bezier.End); break; case PdfPathSegmentType.Rectangle: target.AppendRectangle(((PdfRectangleSegment)segment).Bounds); break; case PdfPathSegmentType.CloseSubpath: target.ClosePath(); break; } } } }
private void DrawPath(PdfPageBase page) { PointF[] points = new PointF[5]; for (int i = 0; i < points.Length; i++) { float x = (float)Math.Cos(i * 2 * Math.PI / 5); float y = (float)Math.Sin(i * 2 * Math.PI / 5); points[i] = new PointF(x, y); } PdfPath path = new PdfPath(); path.AddLine(points[2], points[0]); path.AddLine(points[0], points[3]); path.AddLine(points[3], points[1]); path.AddLine(points[1], points[4]); path.AddLine(points[4], points[2]); //save graphics state PdfGraphicsState state = page.Canvas.Save(); PdfPen pen = new PdfPen(Color.DeepSkyBlue, 0.02f); PdfBrush brush1 = new PdfSolidBrush(Color.CadetBlue); page.Canvas.ScaleTransform(50f, 50f); page.Canvas.TranslateTransform(5f, 1.2f); page.Canvas.DrawPath(pen, path); page.Canvas.TranslateTransform(2f, 0f); path.FillMode = PdfFillMode.Alternate; page.Canvas.DrawPath(pen, brush1, path); page.Canvas.TranslateTransform(2f, 0f); path.FillMode = PdfFillMode.Winding; page.Canvas.DrawPath(pen, brush1, path); PdfLinearGradientBrush brush2 = new PdfLinearGradientBrush(new PointF(-2, 0), new PointF(2, 0), Color.Red, Color.Blue); page.Canvas.TranslateTransform(-4f, 2f); path.FillMode = PdfFillMode.Alternate; page.Canvas.DrawPath(pen, brush2, path); PdfRadialGradientBrush brush3 = new PdfRadialGradientBrush(new PointF(0f, 0f), 0f, new PointF(0f, 0f), 1f, Color.Red, Color.Blue); page.Canvas.TranslateTransform(2f, 0f); path.FillMode = PdfFillMode.Winding; page.Canvas.DrawPath(pen, brush3, path); PdfTilingBrush brush4 = new PdfTilingBrush(new RectangleF(0, 0, 4f, 4f)); brush4.Graphics.DrawRectangle(brush2, 0, 0, 4f, 4f); page.Canvas.TranslateTransform(2f, 0f); path.FillMode = PdfFillMode.Winding; page.Canvas.DrawPath(pen, brush4, path); //restor graphics page.Canvas.Restore(state); }
private static void DrawBezierCurves(PdfPage page, PdfFont titleFont, PdfFont sectionFont) { PdfBrush brush = new PdfBrush(); PdfPen blackPen = new PdfPen(PdfRgbColor.Black, 1); PdfPen redPen = new PdfPen(PdfRgbColor.Red, 1); PdfBrush blueBrush = new PdfBrush(PdfRgbColor.DarkBlue); PdfRgbColor randomPenColor = new PdfRgbColor(); PdfPen randomPen = new PdfPen(randomPenColor, 1); page.Graphics.DrawString("Bezier curves", titleFont, brush, 20, 50); page.Graphics.DrawLine(blackPen, 20, 210, 600, 210); page.Graphics.DrawLine(blackPen, 306, 70, 306, 350); page.Graphics.DrawRectangle(blueBrush, 39, 339, 2, 2); page.Graphics.DrawRectangle(blueBrush, 279, 79, 2, 2); page.Graphics.DrawRectangle(blueBrush, 499, 299, 2, 2); page.Graphics.DrawRectangle(blueBrush, 589, 69, 2, 2); page.Graphics.DrawBezier(redPen, 40, 340, 280, 80, 500, 300, 590, 70); page.Graphics.DrawString("Random bezier curves clipped to view", sectionFont, brush, 20, 385); PdfPath rectPath = new PdfPath(); rectPath.AddRectangle(20, 400, 570, 300); page.Graphics.SaveGraphicsState(); page.Graphics.SetClip(rectPath); Random rnd = new Random(); for (int i = 0; i < 100; i++) { randomPenColor.R = (byte)rnd.Next(256); randomPenColor.G = (byte)rnd.Next(256); randomPenColor.B = (byte)rnd.Next(256); double x1 = rnd.NextDouble() * page.Width; double y1 = 380 + rnd.NextDouble() * 350; double x2 = rnd.NextDouble() * page.Width; double y2 = 380 + rnd.NextDouble() * 350; double x3 = rnd.NextDouble() * page.Width; double y3 = 380 + rnd.NextDouble() * 350; double x4 = rnd.NextDouble() * page.Width; double y4 = 380 + rnd.NextDouble() * 350; page.Graphics.DrawBezier(randomPen, x1, y1, x2, y2, x3, y3, x4, y4); } page.Graphics.RestoreGraphicsState(); blackPen.DashPattern = null; page.Graphics.DrawPath(blackPen, rectPath); page.Graphics.CompressAndClose(); }
private void button1_Click(object sender, EventArgs e) { //Load a pdf document string input = "..\\..\\..\\..\\..\\..\\Data\\AddTextStamp.pdf"; //Open a pdf document PdfDocument document = new PdfDocument(); document.LoadFromFile(input); //Get the first page PdfPageBase page = document.Pages[0]; //Create a pdf template PdfTemplate template = new PdfTemplate(125, 55); PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Elephant", 10f, FontStyle.Italic), true); PdfSolidBrush brush = new PdfSolidBrush(Color.DarkRed); PdfPen pen = new PdfPen(brush); RectangleF rectangle = new RectangleF(new PointF(5, 5), template.Size); int CornerRadius = 20; PdfPath path = new PdfPath(); path.AddArc(template.GetBounds().X, template.GetBounds().Y, CornerRadius, CornerRadius, 180, 90); path.AddArc(template.GetBounds().X + template.Width - CornerRadius, template.GetBounds().Y, CornerRadius, CornerRadius, 270, 90); path.AddArc(template.GetBounds().X + template.Width - CornerRadius, template.GetBounds().Y + template.Height - CornerRadius, CornerRadius, CornerRadius, 0, 90); path.AddArc(template.GetBounds().X, template.GetBounds().Y + template.Height - CornerRadius, CornerRadius, CornerRadius, 90, 90); path.AddLine(template.GetBounds().X, template.GetBounds().Y + template.Height - CornerRadius, template.GetBounds().X, template.GetBounds().Y + CornerRadius / 2); template.Graphics.DrawPath(pen, path); //Draw stamp text String s1 = "REVISED\n"; String s2 = "by E-iceblue at " + DateTime.Now.ToString("MM dd, yyyy"); template.Graphics.DrawString(s1, font1, brush, new PointF(5, 10)); PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Lucida Sans Unicode", 9f, FontStyle.Bold), true); template.Graphics.DrawString(s2, font2, brush, new PointF(2, 30)); //Create a rubber stamp PdfRubberStampAnnotation stamp = new PdfRubberStampAnnotation(rectangle); PdfAppearance apprearance = new PdfAppearance(stamp); apprearance.Normal = template; stamp.Appearance = apprearance; //Draw stamp into page page.AnnotationsWidget.Add(stamp); string output = "AddTextStamp.pdf"; //Save pdf document document.SaveToFile(output); //Launch the file PDFDocumentViewer(output); }
private void DrawStrightLine(PdfPageBase page) { PointF[] points = new PointF[4]; int downRate = 350; int leftRate = 150; int baseRate = 94; points[0] = new PointF(-baseRate + leftRate, -baseRate + downRate); points[1] = new PointF(-baseRate + leftRate, baseRate + downRate); points[2] = new PointF(baseRate + leftRate, baseRate + downRate); points[3] = new PointF(baseRate + leftRate, -baseRate + downRate); PdfPath path = new PdfPath(); path.AddLine(points[0], points[1]); path.AddLine(points[1], points[2]); path.AddLine(points[2], points[3]); path.AddLine(points[3], points[0]); //save graphics state //PdfGraphicsState state = page.Canvas.Save(); PdfPen pen = new PdfPen(Color.DeepSkyBlue, 2f); // page.Canvas.ScaleTransform(95f, 95f); //skewTransform б±ä»» page.Canvas.SkewTransform(10, 10); //page.Canvas.RotateTransform(-15.5f); //translation operator page.Canvas.TranslateTransform(0, -90);//shift shapes // page.Canvas.TranslateTransform(5f, 1.2f); page.Canvas.DrawPath(pen, path); //drawing a stright line PdfPath path2 = new PdfPath(); path2.AddLine(new PointF(0, 450f), new PointF(258f, 450f)); PdfPen pen2 = new PdfPen(Color.Blue, 3f); page.Canvas.DrawPath(pen2, path2); }
private void button1_Click(object sender, EventArgs e) { //pdf file string input = "..\\..\\..\\..\\..\\..\\..\\Data\\Sample5.pdf"; //open a pdf document PdfDocument document = new PdfDocument(input); //get the first page PdfPageBase page = document.Pages[0]; //create a pdf template PdfTemplate template = new PdfTemplate(200, 50); PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Elephant", 16f, FontStyle.Italic), true); PdfSolidBrush brush = new PdfSolidBrush(Color.DarkRed); PdfPen pen = new PdfPen(brush); RectangleF rectangle = new RectangleF(new PointF(0, 0), template.Size); int CornerRadius = 20; PdfPath path = new PdfPath(); path.AddArc(template.GetBounds().X, template.GetBounds().Y, CornerRadius, CornerRadius, 180, 90); path.AddArc(template.GetBounds().X + template.Width - CornerRadius, template.GetBounds().Y, CornerRadius, CornerRadius, 270, 90); path.AddArc(template.GetBounds().X + template.Width - CornerRadius, template.GetBounds().Y + template.Height - CornerRadius, CornerRadius, CornerRadius, 0, 90); path.AddArc(template.GetBounds().X, template.GetBounds().Y + template.Height - CornerRadius, CornerRadius, CornerRadius, 90, 90); path.AddLine(template.GetBounds().X, template.GetBounds().Y + template.Height - CornerRadius, template.GetBounds().X, template.GetBounds().Y + CornerRadius / 2); template.Graphics.DrawPath(pen, path); //draw stamp text String s1 = "REVISED\n"; String s2 = "By Jack at " + DateTime.Now.ToString("HH:mm, MM dd, yyyy"); template.Graphics.DrawString(s1, font1, brush, new PointF(5, 5)); PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Gadugi", 12f, FontStyle.Bold), true); template.Graphics.DrawString(s2, font2, brush, new PointF(2, 28)); //create a rubber stamp PdfRubberStampAnnotation stamp = new PdfRubberStampAnnotation(rectangle); PdfAppearance apprearance = new PdfAppearance(stamp); apprearance.Normal = template; stamp.Appearance = apprearance; //draw stamp into page page.AnnotationsWidget.Add(stamp); string output = "AddTextStamp.pdf"; //save pdf document document.SaveToFile(output); //Launching the Pdf file PDFDocumentViewer(output); }
public void IsCounterClockwise(double[][] source, bool expected) { PdfPath pdfPath = new PdfPath(); foreach (var point in source) { pdfPath.LineTo(point[0], point[1]); } pdfPath.LineTo(source[0][0], source[0][1]); // close path Assert.Equal(expected, pdfPath.IsCounterClockwise); }
public static void Main() { // NOTE: // When used in trial mode, the library imposes some restrictions. // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx // for more information. const string PathToFile = "ExtractPaths.pdf"; using (var pdf = new PdfDocument(@"..\Sample Data\gmail-cheat-sheet.pdf")) { using (PdfDocument copy = pdf.CopyPages(0, 1)) { PdfPage sourcePage = copy.Pages[0]; PdfPage copyPage = copy.AddPage(); copyPage.UserUnit = sourcePage.UserUnit; copyPage.Rotation = sourcePage.Rotation; copyPage.MediaBox = sourcePage.MediaBox; if (sourcePage.CropBox != sourcePage.MediaBox) { copyPage.CropBox = sourcePage.CropBox; } PdfCanvas target = copyPage.Canvas; foreach (PdfPageObject obj in sourcePage.GetObjects()) { if (obj.Type != PdfPageObjectType.Path) { continue; } target.SaveState(); { PdfPath path = (PdfPath)obj; target.Transform(path.TransformationMatrix); setClipRegion(target, path.ClipRegion); setBrushAndPen(target, path); appendPath(target, path); drawPath(target, path); } target.RestoreState(); } copy.RemovePage(0); copy.Save(PathToFile); } } Console.WriteLine($"The output is located in {Environment.CurrentDirectory}"); }
protected override void Execute(CodeActivityContext context) { var BOME = new BOM_Extractor { FilePath = PdfPath.Get(context) }; BOME.ExtractRawData(); BOME.ParseBomDataTable(); BomDataTable.Set(context, BOME.BomDataTable); DrawingElements.Set(context, BOME.DrawingElements); }
private void DrawRectangle(PdfPageBase page) { //notice the watermarker //drawing a stright line PdfPath path2 = new PdfPath(); path2.AddLine(new PointF(1f, 310), new PointF(1f, 50f)); PdfPen pen2 = new PdfPen(Color.Black, 2f); page.Canvas.RotateTransform(-45f); // page.Canvas.ScaleTransform(85f,85f); page.Canvas.DrawPath(pen2, path2); }
private static void toGdiPath(PdfPath path, GraphicsPath gdiPath) { foreach (PdfSubpath subpath in path.Subpaths) { gdiPath.StartFigure(); foreach (PdfPathSegment segment in subpath.Segments) { switch (segment.Type) { case PdfPathSegmentType.Point: // A singlepoint open subpath produces no output. break; case PdfPathSegmentType.Line: PdfLineSegment line = (PdfLineSegment)segment; gdiPath.AddLine(line.Start.ToPointF(), line.End.ToPointF()); break; case PdfPathSegmentType.Bezier: PdfBezierSegment bezier = (PdfBezierSegment)segment; gdiPath.AddBezier(bezier.Start.ToPointF(), bezier.FirstControl.ToPointF(), bezier.SecondControl.ToPointF(), bezier.End.ToPointF() ); break; case PdfPathSegmentType.Rectangle: RectangleF rect = ((PdfRectangleSegment)segment).Bounds.ToRectangleF(); // GDI+ does not render rectangles with negative or very small width and height. Render such // rectangles by lines, but respect direction. Otherwise non-zero winding rule for // path filling will not work. gdiPath.AddLines(new PointF[] { rect.Location, new PointF(rect.X + rect.Width, rect.Y), new PointF(rect.X + rect.Width, rect.Y + rect.Height), new PointF(rect.X, rect.Y + rect.Height), rect.Location }); break; case PdfPathSegmentType.CloseSubpath: gdiPath.CloseFigure(); break; } } } }
private string GetCode(PdfPath path, decimal pageHeight) { if (path == null) { return(string.Empty); } var bbox = GetBoundingRectangle(path.Commands); if (bbox != null) { return(_indent + _indent + @"<span class='ocr_linedrawing' id='drawing_" + pageCount + "_0' title='" + GetCode((PdfRectangle)bbox, pageHeight) + "'/ >"); } return(string.Empty); }
private PageXmlDocument.PageXmlLineDrawingRegion ToPageXmlLineDrawingRegion(PdfPath pdfPath, double pageWidth, double pageHeight) { var bbox = pdfPath.GetBoundingRectangle(); if (bbox.HasValue) { regionCount++; return(new PageXmlDocument.PageXmlLineDrawingRegion() { Coords = ToCoords(bbox.Value, pageWidth, pageHeight), Id = "r" + regionCount }); } return(null); }
private static void HighlightSearchResults(PdfPage page, PdfTextSearchResultCollection searchResults, PdfColor color) { PdfPen pen = new PdfPen(color, 0.5); for (int i = 0; i < searchResults.Count; i++) { PdfTextFragmentCollection tfc = searchResults[i].TextFragments; for (int j = 0; j < tfc.Count; j++) { PdfPath path = new PdfPath(); path.StartSubpath(tfc[j].FragmentCorners[0].X, tfc[j].FragmentCorners[0].Y); path.AddPolygon(tfc[j].FragmentCorners); page.Graphics.DrawPath(pen, path); } } }
private static void drawPath(Graphics gr, PdfPath path, float userUnit) { if (!path.PaintMode.HasValue) { return; } saveStateAndDraw(gr, path.ClipRegion, userUnit, () => { concatMatrix(gr, path.TransformationMatrix); using (var gdiPath = new GraphicsPath()) { toGdiPath(path, gdiPath); fillStrokePath(gr, path, gdiPath); } }); }
/// <summary> /// Get the hOCR string for the path. /// <para>http://kba.cloud/hocr-spec/1.2/#elementdef-ocr_linedrawing</para> /// </summary> /// <param name="path"></param> /// <param name="pageHeight"></param> /// <param name="subPaths"></param> /// <param name="level">The indent level.</param> private string GetCode(PdfPath path, double pageHeight, bool subPaths, int level) { if (path == null) { return(string.Empty); } string hocr = string.Empty; if (subPaths) { var bbox = path.GetBoundingRectangle(); if (bbox.HasValue) { areaCount++; hocr += GetIndent(level) + @"<div class='ocr_carea' id='block_" + pageCount + "_" + areaCount + "' title='" + GetCode(bbox.Value, pageHeight) + "'>\n"; foreach (var subPath in path.Commands) { var subBbox = subPath.GetBoundingRectangle(); if (subBbox.HasValue) { pathCount++; hocr += GetIndent(level + 1) + @"<span class='ocr_linedrawing' id='drawing_" + pageCount + "_" + pathCount + "' title='" + GetCode(subBbox.Value, pageHeight) + "' />\n"; } } hocr += GetIndent(level) + @"</div>"; } } else { var bbox = path.GetBoundingRectangle(); if (bbox.HasValue) { pathCount++; hocr += GetIndent(level) + @"<span class='ocr_linedrawing' id='drawing_" + pageCount + "_" + pathCount + "' title='" + GetCode(bbox.Value, pageHeight) + "' />"; } } return(hocr); }
public static PdfRectangle BoundingBox(this PdfPath pdfPath) { IReadOnlyList <IPathCommand> commands = pdfPath.Commands; var minX = decimal.MaxValue; var maxX = decimal.MinValue; var minY = decimal.MaxValue; var maxY = decimal.MinValue; foreach (var command in commands) { var rect = command.GetBoundingRectangle(); if (rect == null) { continue; } if (rect.Value.Left < minX) { minX = rect.Value.Left; } if (rect.Value.Right > maxX) { maxX = rect.Value.Right; } if (rect.Value.Bottom < minY) { minY = rect.Value.Bottom; } if (rect.Value.Top > maxY) { maxY = rect.Value.Top; } } return(new PdfRectangle(minX, minY, maxX, maxY)); }
private static void fillStrokePath(Graphics gr, PdfPath path, GraphicsPath gdiPath) { PdfDrawMode paintMode = path.PaintMode.Value; if (paintMode == PdfDrawMode.Fill || paintMode == PdfDrawMode.FillAndStroke) { using (var brush = toGdiBrush(path.Brush)) { gdiPath.FillMode = (FillMode)path.FillMode.Value; gr.FillPath(brush, gdiPath); } } if (paintMode == PdfDrawMode.Stroke || paintMode == PdfDrawMode.FillAndStroke) { using (var pen = toGdiPen(path.Pen)) { gr.DrawPath(pen, gdiPath); } } }
private static void drawPath(PdfCanvas target, PdfPath path) { switch (path.PaintMode) { case PdfDrawMode.Fill: target.FillPath(path.FillMode.Value); break; case PdfDrawMode.FillAndStroke: target.FillAndStrokePath(path.FillMode.Value); break; case PdfDrawMode.Stroke: target.StrokePath(); break; default: target.ResetPath(); break; } }
private AltoDocument.AltoGraphicalElement ToAltoGraphicalElement(PdfPath pdfPath, decimal height) { graphicalElementCount++; var rectangle = pdfPath.GetBoundingRectangle(); if (rectangle.HasValue) { return(new AltoDocument.AltoGraphicalElement { VerticalPosition = (float)Math.Round((height - rectangle.Value.Top) * scale), HorizontalPosition = (float)Math.Round(rectangle.Value.Left * scale), Height = (float)Math.Round(rectangle.Value.Height * scale), Width = (float)Math.Round(rectangle.Value.Width * scale), Rotation = 0, StyleRefs = null, TagRefs = null, Title = null, Type = null, Id = "P" + pageCount + "_GE" + graphicalElementCount.ToString("#00000") }); } return(null); }
/// <summary> /// Create a new <see cref="Type2Glyph"/>. /// </summary> public Type2Glyph(PdfPath path, decimal?width) { Path = path ?? throw new ArgumentNullException(nameof(path)); Width = width; }
private static void DrawTextWrap(PdfPage page, PdfStandardFont titleFont) { PdfBrush brush = new PdfBrush(); PdfPen redPen = new PdfPen(PdfRgbColor.Red, 0.5); PdfStandardFont helvetica = new PdfStandardFont(PdfStandardFontFace.Helvetica, 12); page.Graphics.DrawString("Text wrapping", titleFont, brush, 20, 50); page.Graphics.DrawLine(redPen, 20, 70, 20, 150); page.Graphics.DrawLine(redPen, 300, 70, 300, 150); page.Graphics.DrawLine(redPen, 20, 70, 300, 70); PdfStringAppearanceOptions sao = new PdfStringAppearanceOptions(); sao.Brush = brush; sao.Font = helvetica; // Height is not set, text has no vertical limit. PdfStringLayoutOptions slo = new PdfStringLayoutOptions(); slo.HorizontalAlign = PdfStringHorizontalAlign.Justified; slo.VerticalAlign = PdfStringVerticalAlign.Top; slo.X = 20; slo.Y = 70; slo.Width = 280; string text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " + "Sed vel euismod risus. Fusce viverra, nisi auctor ullamcorper porttitor, " + "ipsum lacus lobortis metus, sit amet dictum lacus velit nec diam. " + "Morbi arcu diam, euismod a auctor nec, aliquam in lectus." + "Ut ultricies iaculis augue sit amet adipiscing. Aenean blandit tortor a nisi " + "dignissim fermentum id adipiscing mauris. Aenean libero turpis, varius nec ultricies " + "faucibus, pretium quis lectus. Morbi mollis lorem vel erat condimentum mattis mollis " + "nulla sollicitudin. Nunc ut massa id felis laoreet feugiat eget at eros."; page.Graphics.DrawString(text, sao, slo); page.Graphics.DrawLine(redPen, 310, 70, 310, 147); page.Graphics.DrawLine(redPen, 590, 70, 590, 147); page.Graphics.DrawLine(redPen, 310, 70, 590, 70); page.Graphics.DrawLine(redPen, 310, 147, 590, 147); // Height is set, text is limited on vertical. slo.X = 310; slo.Y = 70; slo.Width = 280; slo.Height = 77; page.Graphics.DrawString(text, sao, slo); PdfPath clipPath = new PdfPath(); clipPath.AddRectangle(310, 160, 280, 77); page.Graphics.DrawPath(redPen, clipPath); page.Graphics.SaveGraphicsState(); page.Graphics.SetClip(clipPath); // Height is not set but text is cliped on vertical. slo.X = 310; slo.Y = 160; slo.Width = 280; slo.Height = 0; page.Graphics.DrawString(text, sao, slo); page.Graphics.RestoreGraphicsState(); page.Graphics.DrawLine(redPen, 10, 400, 300, 400); page.Graphics.DrawLine(redPen, 20, 300, 20, 500); // Wrapped text is always rotated around top left corner, no matter the text alignment page.Graphics.DrawRectangle(redPen, 20, 400, 280, 80, 30); slo.X = 20; slo.Y = 400; slo.Width = 280; slo.Height = 80; slo.Rotation = 30; page.Graphics.DrawString(text, sao, slo); page.Graphics.DrawLine(redPen, 310, 600, 590, 600); page.Graphics.DrawLine(redPen, 450, 450, 450, 750); // Rotation around the center of the box requires some affine transformations. page.Graphics.SaveGraphicsState(); page.Graphics.TranslateTransform(450, 600); page.Graphics.RotateTransform(30); page.Graphics.DrawRectangle(redPen, -140, -40, 280, 80); slo.X = -140; slo.Y = -40; slo.Width = 280; slo.Height = 80; slo.Rotation = 0; page.Graphics.DrawString(text, sao, slo); page.Graphics.RestoreGraphicsState(); }
/// <summary> /// Main method for running the sample. /// </summary> public static SampleOutputInfo[] Run(Stream input) { PdfBrush brush = new PdfBrush(); PdfPen redPen = new PdfPen(PdfRgbColor.Red, 1); PdfStandardFont helvetica = new PdfStandardFont(PdfStandardFontFace.Helvetica, 10); PdfFixedDocument document = new PdfFixedDocument(input); PdfContentExtractor ce = new PdfContentExtractor(document.Pages[0]); PdfVisualObjectCollection voc = ce.ExtractVisualObjects(false); PdfPath contour = null; for (int i = 0; i < voc.Count; i++) { switch (voc[i].Type) { case PdfVisualObjectType.Image: PdfImageVisualObject ivo = voc[i] as PdfImageVisualObject; contour = new PdfPath(); contour.StartSubpath(ivo.Image.ImageCorners[0].X - 5, ivo.Image.ImageCorners[0].Y + 5); contour.AddLineTo(ivo.Image.ImageCorners[1].X + 5, ivo.Image.ImageCorners[1].Y + 5); contour.AddLineTo(ivo.Image.ImageCorners[2].X + 5, ivo.Image.ImageCorners[2].Y - 5); contour.AddLineTo(ivo.Image.ImageCorners[3].X - 5, ivo.Image.ImageCorners[3].Y - 5); contour.CloseSubpath(); document.Pages[0].Graphics.DrawPath(redPen, contour); document.Pages[0].Graphics.DrawString("Image", helvetica, brush, ivo.Image.ImageCorners[0].X - 5, ivo.Image.ImageCorners[0].Y + 5); break; case PdfVisualObjectType.Text: PdfTextVisualObject tvo = voc[i] as PdfTextVisualObject; contour = new PdfPath(); contour.StartSubpath(tvo.TextFragment.FragmentCorners[0].X - 5, tvo.TextFragment.FragmentCorners[0].Y + 5); contour.AddLineTo(tvo.TextFragment.FragmentCorners[1].X + 5, tvo.TextFragment.FragmentCorners[1].Y + 5); contour.AddLineTo(tvo.TextFragment.FragmentCorners[2].X + 5, tvo.TextFragment.FragmentCorners[2].Y - 5); contour.AddLineTo(tvo.TextFragment.FragmentCorners[3].X - 5, tvo.TextFragment.FragmentCorners[3].Y - 5); contour.CloseSubpath(); document.Pages[0].Graphics.DrawPath(redPen, contour); document.Pages[0].Graphics.DrawString("Text", helvetica, brush, tvo.TextFragment.FragmentCorners[0].X - 5, tvo.TextFragment.FragmentCorners[0].Y + 5); break; case PdfVisualObjectType.Path: PdfPathVisualObject pvo = voc[i] as PdfPathVisualObject; // Examine all the path points and determine the minimum rectangle that bounds the path. double minX = 999999, minY = 999999, maxX = -999999, maxY = -999999; for (int j = 0; j < pvo.PathItems.Count; j++) { PdfPathItem pi = pvo.PathItems[j]; if (pi.Points != null) { for (int k = 0; k < pi.Points.Length; k++) { if (minX >= pi.Points[k].X) { minX = pi.Points[k].X; } if (minY >= pi.Points[k].Y) { minY = pi.Points[k].Y; } if (maxX <= pi.Points[k].X) { maxX = pi.Points[k].X; } if (maxY <= pi.Points[k].Y) { maxY = pi.Points[k].Y; } } } } contour = new PdfPath(); contour.StartSubpath(minX - 5, minY - 5); contour.AddLineTo(maxX + 5, minY - 5); contour.AddLineTo(maxX + 5, maxY + 5); contour.AddLineTo(minX - 5, maxY + 5); contour.CloseSubpath(); document.Pages[0].Graphics.DrawPath(redPen, contour); document.Pages[0].Graphics.DrawString("Path", helvetica, brush, minX - 5, maxY + 5); // Skip the rest of path objects, they are the evaluation message i = voc.Count; break; } } SampleOutputInfo[] output = new SampleOutputInfo[] { new SampleOutputInfo(document, "xfinium.pdf.sample.pageobjects.pdf") }; return output; }
private static void DrawArcsAndPies(PdfPage page, PdfFont titleFont, PdfFont sectionFont) { PdfBrush brush = new PdfBrush(); PdfPen blackPen = new PdfPen(PdfRgbColor.Black, 1); PdfPen redPen = new PdfPen(PdfRgbColor.Red, 1); PdfRgbColor randomPenColor = new PdfRgbColor(); PdfPen randomPen = new PdfPen(randomPenColor, 1); PdfRgbColor randomBrushColor = new PdfRgbColor(); PdfBrush randomBrush = new PdfBrush(randomBrushColor); page.Graphics.DrawString("Arcs", titleFont, brush, 20, 50); page.Graphics.DrawString("Pies", titleFont, brush, 310, 50); page.Graphics.DrawLine(blackPen, 20, 210, 300, 210); page.Graphics.DrawLine(blackPen, 160, 70, 160, 350); page.Graphics.DrawLine(blackPen, 310, 210, 590, 210); page.Graphics.DrawLine(blackPen, 450, 70, 450, 350); blackPen.DashPattern = new double[] { 2, 2 }; page.Graphics.DrawLine(blackPen, 20, 70, 300, 350); page.Graphics.DrawLine(blackPen, 20, 350, 300, 70); page.Graphics.DrawLine(blackPen, 310, 70, 590, 350); page.Graphics.DrawLine(blackPen, 310, 350, 590, 70); page.Graphics.DrawArc(redPen, 30, 80, 260, 260, 0, 135); page.Graphics.DrawPie(redPen, 320, 80, 260, 260, 45, 270); page.Graphics.DrawString("Random arcs and pies clipped to view", sectionFont, brush, 20, 385); PdfPath rectPath = new PdfPath(); rectPath.AddRectangle(20, 400, 570, 300); page.Graphics.SaveGraphicsState(); page.Graphics.SetClip(rectPath); Random rnd = new Random(); for (int i = 0; i < 100; i++) { randomPenColor.R = (byte)rnd.Next(256); randomPenColor.G = (byte)rnd.Next(256); randomPenColor.B = (byte)rnd.Next(256); randomBrushColor.R = (byte)rnd.Next(256); randomBrushColor.G = (byte)rnd.Next(256); randomBrushColor.B = (byte)rnd.Next(256); int mode = rnd.Next(4); double left = rnd.NextDouble() * page.Width; double top = 380 + rnd.NextDouble() * 350; double width = rnd.NextDouble() * page.Width; double height = rnd.NextDouble() * 250; double startAngle = rnd.Next(360); double sweepAngle = rnd.Next(360); switch (mode) { case 0: // Stroke arc outline page.Graphics.DrawArc(randomPen, left, top, width, height, startAngle, sweepAngle); break; case 1: // Stroke pie outline page.Graphics.DrawPie(randomPen, left, top, width, height, startAngle, sweepAngle); break; case 2: // Fill pie interior page.Graphics.DrawPie(randomBrush, left, top, width, height, startAngle, sweepAngle); break; case 3: // Stroke and fill pie page.Graphics.DrawPie(randomPen, randomBrush, left, top, width, height, startAngle, sweepAngle); break; } } page.Graphics.RestoreGraphicsState(); blackPen.DashPattern = null; page.Graphics.DrawPath(blackPen, rectPath); page.Graphics.CompressAndClose(); }
private static void DrawLines(PdfPage page, PdfFont titleFont, PdfFont sectionFont) { PdfBrush brush = new PdfBrush(); PdfPen blackPen = new PdfPen(PdfRgbColor.Black, 1); PdfPen bluePen = new PdfPen(PdfRgbColor.LightBlue, 16); page.Graphics.DrawString("Lines", titleFont, brush, 20, 50); page.Graphics.DrawString("Line styles:", sectionFont, brush, 20, 70); page.Graphics.DrawString("Solid", sectionFont, brush, 20, 90); page.Graphics.DrawLine(blackPen, 100, 95, 400, 95); page.Graphics.DrawString("Dashed", sectionFont, brush, 20, 110); blackPen.DashPattern = new double[] { 3, 3 }; page.Graphics.DrawLine(blackPen, 100, 115, 400, 115); page.Graphics.DrawString("Line cap styles:", sectionFont, brush, 20, 150); page.Graphics.DrawString("Flat", sectionFont, brush, 20, 175); page.Graphics.DrawLine(bluePen, 100, 180, 400, 180); blackPen.DashPattern = null; page.Graphics.DrawLine(blackPen, 100, 180, 400, 180); page.Graphics.DrawString("Square", sectionFont, brush, 20, 195); bluePen.LineCap = PdfLineCap.Square; page.Graphics.DrawLine(bluePen, 100, 200, 400, 200); blackPen.DashPattern = null; page.Graphics.DrawLine(blackPen, 100, 200, 400, 200); page.Graphics.DrawString("Round", sectionFont, brush, 20, 215); bluePen.LineCap = PdfLineCap.Round; page.Graphics.DrawLine(bluePen, 100, 220, 400, 220); blackPen.DashPattern = null; page.Graphics.DrawLine(blackPen, 100, 220, 400, 220); page.Graphics.DrawString("Line join styles:", sectionFont, brush, 20, 250); page.Graphics.DrawString("Miter", sectionFont, brush, 20, 280); PdfPath miterPath = new PdfPath(); miterPath.StartSubpath(150, 320); miterPath.AddLineTo(250, 260); miterPath.AddLineTo(350, 320); bluePen.LineCap = PdfLineCap.Flat; bluePen.LineJoin = PdfLineJoin.Miter; page.Graphics.DrawPath(bluePen, miterPath); page.Graphics.DrawString("Bevel", sectionFont, brush, 20, 360); PdfPath bevelPath = new PdfPath(); bevelPath.StartSubpath(150, 400); bevelPath.AddLineTo(250, 340); bevelPath.AddLineTo(350, 400); bluePen.LineCap = PdfLineCap.Flat; bluePen.LineJoin = PdfLineJoin.Bevel; page.Graphics.DrawPath(bluePen, bevelPath); page.Graphics.DrawString("Round", sectionFont, brush, 20, 440); PdfPath roundPath = new PdfPath(); roundPath.StartSubpath(150, 480); roundPath.AddLineTo(250, 420); roundPath.AddLineTo(350, 480); bluePen.LineCap = PdfLineCap.Flat; bluePen.LineJoin = PdfLineJoin.Round; page.Graphics.DrawPath(bluePen, roundPath); page.Graphics.DrawString("Random lines clipped to rectangle", sectionFont, brush, 20, 520); PdfPath clipPath = new PdfPath(); clipPath.AddRectangle(20, 550, 570, 230); page.Graphics.SaveGraphicsState(); page.Graphics.SetClip(clipPath); PdfRgbColor randomColor = new PdfRgbColor(); PdfPen randomPen = new PdfPen(randomColor, 1); Random rnd = new Random(); for (int i = 0; i < 100; i++) { randomColor.R = (byte)rnd.Next(256); randomColor.G = (byte)rnd.Next(256); randomColor.B = (byte)rnd.Next(256); page.Graphics.DrawLine(randomPen, rnd.NextDouble() * page.Width, 550 + rnd.NextDouble() * 250, rnd.NextDouble() * page.Width, 550 + rnd.NextDouble() * 250); } page.Graphics.RestoreGraphicsState(); page.Graphics.DrawPath(blackPen, clipPath); page.Graphics.CompressAndClose(); }
private static void DrawPatterns(PdfPage page, PdfFont titleFont, PdfFont sectionFont) { PdfBrush brush = new PdfBrush(); PdfPen blackPen = new PdfPen(PdfRgbColor.Black, 1); PdfPen darkRedPen = new PdfPen(new PdfRgbColor(0xFF, 0x40, 0x40), 0.8); PdfPen darkOrangePen = new PdfPen(new PdfRgbColor(0xA6, 0x4B, 0x00), 0.8); PdfPen darkCyanPen = new PdfPen(new PdfRgbColor(0x00, 0x63, 0x63), 0.8); PdfPen darkGreenPen = new PdfPen(new PdfRgbColor(0x00, 0x85, 0x00), 0.8); PdfBrush lightRedBrush = new PdfBrush(new PdfRgbColor(0xFF, 0x73, 0x73)); PdfBrush lightOrangeBrush = new PdfBrush(new PdfRgbColor(0xFF, 0x96, 0x40)); PdfBrush lightCyanBrush = new PdfBrush(new PdfRgbColor(0x33, 0xCC, 0xCC)); PdfBrush lightGreenBrush = new PdfBrush(new PdfRgbColor(0x67, 0xE6, 0x67)); page.Graphics.DrawString("Patterns", titleFont, brush, 20, 50); page.Graphics.DrawString("Colored patterns", sectionFont, brush, 25, 70); // Create the pattern visual appearance. PdfColoredTilingPattern ctp = new PdfColoredTilingPattern(20, 20); // Red circle ctp.Graphics.DrawEllipse(darkRedPen, lightRedBrush, 1, 1, 8, 8); // Cyan square ctp.Graphics.DrawRectangle(darkCyanPen, lightCyanBrush, 11, 1, 8, 8); // Green diamond PdfPath diamondPath = new PdfPath(); diamondPath.StartSubpath(1, 15); diamondPath.AddPolyLineTo(new PdfPoint[] { new PdfPoint(5, 11), new PdfPoint(9, 15), new PdfPoint(5, 19) }); diamondPath.CloseSubpath(); ctp.Graphics.DrawPath(darkGreenPen, lightGreenBrush, diamondPath); // Orange triangle PdfPath trianglePath = new PdfPath(); trianglePath.StartSubpath(11, 19); trianglePath.AddPolyLineTo(new PdfPoint[] { new PdfPoint(15, 11), new PdfPoint(19, 19) }); trianglePath.CloseSubpath(); ctp.Graphics.DrawPath(darkOrangePen, lightOrangeBrush, trianglePath); // Create a pattern colorspace from the pattern object. PdfPatternColorSpace coloredPatternColorSpace = new PdfPatternColorSpace(ctp); // Create a color based on the pattern colorspace. PdfPatternColor coloredPatternColor = new PdfPatternColor(coloredPatternColorSpace); // The pen and brush use the pattern color like any other color. PdfPatternBrush patternBrush = new PdfPatternBrush(coloredPatternColor); PdfPatternPen patternPen = new PdfPatternPen(coloredPatternColor, 40); page.Graphics.DrawEllipse(patternBrush, 25, 90, 250, 200); page.Graphics.DrawRoundRectangle(patternPen, 310, 110, 250, 160, 100, 100); page.Graphics.DrawString("Uncolored patterns", sectionFont, brush, 25, 300); // Create the pattern visual appearance. PdfUncoloredTilingPattern uctp = new PdfUncoloredTilingPattern(20, 20); // A pen without color is used to create the pattern content. PdfPen noColorPen = new PdfPen(null, 0.8); // Circle uctp.Graphics.DrawEllipse(noColorPen, 1, 1, 8, 8); // Square uctp.Graphics.DrawRectangle(noColorPen, 11, 1, 8, 8); // Diamond diamondPath = new PdfPath(); diamondPath.StartSubpath(1, 15); diamondPath.AddPolyLineTo(new PdfPoint[] { new PdfPoint(5, 11), new PdfPoint(9, 15), new PdfPoint(5, 19) }); diamondPath.CloseSubpath(); uctp.Graphics.DrawPath(noColorPen, diamondPath); // Triangle trianglePath = new PdfPath(); trianglePath.StartSubpath(11, 19); trianglePath.AddPolyLineTo(new PdfPoint[] { new PdfPoint(15, 11), new PdfPoint(19, 19) }); trianglePath.CloseSubpath(); uctp.Graphics.DrawPath(noColorPen, trianglePath); // Create a pattern colorspace from the pattern object. PdfPatternColorSpace uncoloredPatternColorSpace = new PdfPatternColorSpace(uctp); // Create a color based on the pattern colorspace. PdfPatternColor uncoloredPatternColor = new PdfPatternColor(uncoloredPatternColorSpace); // The pen and brush use the pattern color like any other color. patternBrush = new PdfPatternBrush(uncoloredPatternColor); // Before using the uncolored pattern set the color that will be used to paint the pattern. patternBrush.UncoloredPatternPaintColor = new PdfRgbColor(0xFF, 0x40, 0x40); page.Graphics.DrawEllipse(patternBrush, 25, 320, 125, 200); patternBrush.UncoloredPatternPaintColor = new PdfRgbColor(0xA6, 0x4B, 0x00); page.Graphics.DrawEllipse(patternBrush, 175, 320, 125, 200); patternBrush.UncoloredPatternPaintColor = new PdfRgbColor(0x00, 0x63, 0x63); page.Graphics.DrawEllipse(patternBrush, 325, 320, 125, 200); patternBrush.UncoloredPatternPaintColor = new PdfRgbColor(0x00, 0x85, 0x00); page.Graphics.DrawEllipse(patternBrush, 475, 320, 125, 200); page.Graphics.DrawString("Shading patterns", sectionFont, brush, 25, 550); // Create the pattern visual appearance. PdfAxialShading horizontalShading = new PdfAxialShading(); horizontalShading.StartColor = new PdfRgbColor(255, 0, 0); horizontalShading.EndColor = new PdfRgbColor(0, 0, 255); horizontalShading.StartPoint = new PdfPoint(25, 600); horizontalShading.EndPoint = new PdfPoint(575, 600); PdfShadingPattern sp = new PdfShadingPattern(horizontalShading); // Create a pattern colorspace from the pattern object. PdfPatternColorSpace shadingPatternColorSpace = new PdfPatternColorSpace(sp); // Create a color based on the pattern colorspace. PdfPatternColor shadingPatternColor = new PdfPatternColor(shadingPatternColorSpace); // The pen and brush use the pattern color like any other color. patternPen = new PdfPatternPen(shadingPatternColor, 40); page.Graphics.DrawEllipse(patternPen, 50, 600, 500, 150); page.Graphics.CompressAndClose(); }
private static void DrawRoundRectangles(PdfPage page, PdfFont titleFont, PdfFont sectionFont) { PdfBrush brush = new PdfBrush(); PdfPen blackPen = new PdfPen(PdfRgbColor.Black, 1); PdfPen redPen = new PdfPen(PdfRgbColor.Red, 1); PdfRgbColor randomPenColor = new PdfRgbColor(); PdfPen randomPen = new PdfPen(randomPenColor, 1); PdfRgbColor randomBrushColor = new PdfRgbColor(); PdfBrush randomBrush = new PdfBrush(randomBrushColor); page.Graphics.DrawString("Round rectangles", titleFont, brush, 20, 50); page.Graphics.DrawLine(blackPen, 20, 150, 300, 150); page.Graphics.DrawLine(blackPen, 80, 70, 80, 350); page.Graphics.DrawRoundRectangle(redPen, 80, 150, 180, 100, 20, 20); page.Graphics.DrawLine(blackPen, 320, 150, 600, 150); page.Graphics.DrawLine(blackPen, 380, 70, 380, 350); page.Graphics.DrawRoundRectangle(redPen, 380, 150, 180, 100, 20, 20, 30); page.Graphics.DrawString("Random round rectangles clipped to view", sectionFont, brush, 20, 385); PdfPath roundRectPath = new PdfPath(); roundRectPath.AddRoundRectangle(20, 400, 570, 300, 20, 20); page.Graphics.SaveGraphicsState(); page.Graphics.SetClip(roundRectPath); Random rnd = new Random(); for (int i = 0; i < 100; i++) { randomPenColor.R = (byte)rnd.Next(256); randomPenColor.G = (byte)rnd.Next(256); randomPenColor.B = (byte)rnd.Next(256); randomBrushColor.R = (byte)rnd.Next(256); randomBrushColor.G = (byte)rnd.Next(256); randomBrushColor.B = (byte)rnd.Next(256); int mode = rnd.Next(3); double left = rnd.NextDouble() * page.Width; double top = 380 + rnd.NextDouble() * 350; double width = rnd.NextDouble() * page.Width; double height = rnd.NextDouble() * 250; double orientation = rnd.Next(360); switch (mode) { case 0: // Stroke rectangle outline page.Graphics.DrawRoundRectangle(randomPen, left, top, width, height, width * 0.1, height * 0.1, orientation); break; case 1: // Fill rectangle interior page.Graphics.DrawRoundRectangle(randomBrush, left, top, width, height, width * 0.1, height * 0.1, orientation); break; case 2: // Stroke and fill rectangle page.Graphics.DrawRoundRectangle(randomPen, randomBrush, left, top, width, height, width * 0.1, height * 0.1, orientation); break; } } page.Graphics.RestoreGraphicsState(); page.Graphics.DrawPath(blackPen, roundRectPath); page.Graphics.CompressAndClose(); }
private static void DrawShadings(PdfPage page, PdfFont titleFont, PdfFont sectionFont) { PdfBrush brush = new PdfBrush(); PdfPen blackPen = new PdfPen(PdfRgbColor.Black, 1); PdfRgbColor randomPenColor = new PdfRgbColor(); PdfPen randomPen = new PdfPen(randomPenColor, 1); PdfRgbColor randomBrushColor = new PdfRgbColor(); PdfBrush randomBrush = new PdfBrush(randomBrushColor); page.Graphics.DrawString("Shadings", titleFont, brush, 20, 50); page.Graphics.DrawString("Horizontal", sectionFont, brush, 25, 70); PdfAxialShading horizontalShading = new PdfAxialShading(); horizontalShading.StartColor = new PdfRgbColor(255, 0, 0); horizontalShading.EndColor = new PdfRgbColor(0, 0, 255); horizontalShading.StartPoint = new PdfPoint(25, 90); horizontalShading.EndPoint = new PdfPoint(175, 90); // Clip the shading to desired area. PdfPath hsArea = new PdfPath(); hsArea.AddRectangle(25, 90, 150, 150); page.Graphics.SaveGraphicsState(); page.Graphics.SetClip(hsArea); page.Graphics.DrawShading(horizontalShading); page.Graphics.RestoreGraphicsState(); page.Graphics.DrawString("Vertical", sectionFont, brush, 225, 70); PdfAxialShading verticalShading = new PdfAxialShading(); verticalShading.StartColor = new PdfRgbColor(255, 0, 0); verticalShading.EndColor = new PdfRgbColor(0, 0, 255); verticalShading.StartPoint = new PdfPoint(225, 90); verticalShading.EndPoint = new PdfPoint(225, 240); // Clip the shading to desired area. PdfPath vsArea = new PdfPath(); vsArea.AddRectangle(225, 90, 150, 150); page.Graphics.SaveGraphicsState(); page.Graphics.SetClip(vsArea); page.Graphics.DrawShading(verticalShading); page.Graphics.RestoreGraphicsState(); page.Graphics.DrawString("Diagonal", sectionFont, brush, 425, 70); PdfAxialShading diagonalShading = new PdfAxialShading(); diagonalShading.StartColor = new PdfRgbColor(255, 0, 0); diagonalShading.EndColor = new PdfRgbColor(0, 0, 255); diagonalShading.StartPoint = new PdfPoint(425, 90); diagonalShading.EndPoint = new PdfPoint(575, 240); // Clip the shading to desired area. PdfPath dsArea = new PdfPath(); dsArea.AddRectangle(425, 90, 150, 150); page.Graphics.SaveGraphicsState(); page.Graphics.SetClip(dsArea); page.Graphics.DrawShading(diagonalShading); page.Graphics.RestoreGraphicsState(); page.Graphics.DrawString("Extended shading", sectionFont, brush, 25, 260); PdfAxialShading extendedShading = new PdfAxialShading(); extendedShading.StartColor = new PdfRgbColor(255, 0, 0); extendedShading.EndColor = new PdfRgbColor(0, 0, 255); extendedShading.StartPoint = new PdfPoint(225, 280); extendedShading.EndPoint = new PdfPoint(375, 280); extendedShading.ExtendStart = true; extendedShading.ExtendEnd = true; // Clip the shading to desired area. PdfPath esArea = new PdfPath(); esArea.AddRectangle(25, 280, 550, 30); page.Graphics.SaveGraphicsState(); page.Graphics.SetClip(esArea); page.Graphics.DrawShading(extendedShading); page.Graphics.RestoreGraphicsState(); page.Graphics.DrawPath(blackPen, esArea); page.Graphics.DrawString("Limited shading", sectionFont, brush, 25, 330); PdfAxialShading limitedShading = new PdfAxialShading(); limitedShading.StartColor = new PdfRgbColor(255, 0, 0); limitedShading.EndColor = new PdfRgbColor(0, 0, 255); limitedShading.StartPoint = new PdfPoint(225, 350); limitedShading.EndPoint = new PdfPoint(375, 350); limitedShading.ExtendStart = false; limitedShading.ExtendEnd = false; // Clip the shading to desired area. PdfPath lsArea = new PdfPath(); lsArea.AddRectangle(25, 350, 550, 30); page.Graphics.SaveGraphicsState(); page.Graphics.SetClip(lsArea); page.Graphics.DrawShading(limitedShading); page.Graphics.RestoreGraphicsState(); page.Graphics.DrawPath(blackPen, lsArea); page.Graphics.DrawString("Multi-stop shading", sectionFont, brush, 25, 400); // Multi-stop shadings use a stitching function to combine the functions that define each gradient part. // Function for red to blue shading. PdfExponentialFunction redToBlueFunc = new PdfExponentialFunction(); // Linear function redToBlueFunc.Exponent = 1; redToBlueFunc.Domain = new double[] { 0, 1 }; // Red color for start redToBlueFunc.C0 = new double[] { 1, 0, 0 }; // Blue color for start redToBlueFunc.C1 = new double[] { 0, 0, 1 }; // Function for blue to green shading. PdfExponentialFunction blueToGreenFunc = new PdfExponentialFunction(); // Linear function blueToGreenFunc.Exponent = 1; blueToGreenFunc.Domain = new double[] { 0, 1 }; // Blue color for start blueToGreenFunc.C0 = new double[] { 0, 0, 1 }; // Green color for start blueToGreenFunc.C1 = new double[] { 0, 1, 0 }; //Stitching function for the shading. PdfStitchingFunction shadingFunction = new PdfStitchingFunction(); shadingFunction.Functions.Add(redToBlueFunc); shadingFunction.Functions.Add(blueToGreenFunc); shadingFunction.Domain = new double[] { 0, 1 }; shadingFunction.Encode = new double[] { 0, 1, 0, 1 }; // Entire shading goes from 0 to 1 (100%). // We set the first shading (red->blue) to cover 30% (0 - 0.3) and // the second shading to cover 70% (0.3 - 1). shadingFunction.Bounds = new double[] { 0.3 }; // The multistop shading PdfAxialShading multiStopShading = new PdfAxialShading(); multiStopShading.StartPoint = new PdfPoint(25, 420); multiStopShading.EndPoint = new PdfPoint(575, 420); // The colorspace must match the colors specified in C0 & C1 multiStopShading.ColorSpace = new PdfRgbColorSpace(); multiStopShading.Function = shadingFunction; // Clip the shading to desired area. PdfPath mssArea = new PdfPath(); mssArea.AddRectangle(25, 420, 550, 30); page.Graphics.SaveGraphicsState(); page.Graphics.SetClip(mssArea); page.Graphics.DrawShading(multiStopShading); page.Graphics.RestoreGraphicsState(); page.Graphics.DrawPath(blackPen, lsArea); page.Graphics.DrawString("Radial shading", sectionFont, brush, 25, 470); PdfRadialShading rs1 = new PdfRadialShading(); rs1.StartColor = new PdfRgbColor(0, 255, 0); rs1.EndColor = new PdfRgbColor(255, 0, 255); rs1.StartCircleCenter = new PdfPoint(50, 500); rs1.StartCircleRadius = 10; rs1.EndCircleCenter = new PdfPoint(500, 570); rs1.EndCircleRadius = 100; page.Graphics.DrawShading(rs1); PdfRadialShading rs2 = new PdfRadialShading(); rs2.StartColor = new PdfRgbColor(0, 255, 0); rs2.EndColor = new PdfRgbColor(255, 0, 255); rs2.StartCircleCenter = new PdfPoint(80, 600); rs2.StartCircleRadius = 10; rs2.EndCircleCenter = new PdfPoint(110, 690); rs2.EndCircleRadius = 100; page.Graphics.DrawShading(rs2); page.Graphics.CompressAndClose(); }