コード例 #1
0
        public void InsertImageFromByteArrayCustomSize()
        {
            //ExStart
            //ExFor:DocumentBuilder.InsertImage(Byte[], Double, Double)
            //ExSummary:Shows how to import an image into a document from a byte array, with a custom size.
            Document        doc     = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);

#if NETSTANDARD2_0 || __MOBILE__
            using (SkiaSharp.SKBitmap bitmap = SkiaSharp.SKBitmap.Decode(ImageDir + "Aspose.Words.gif"))
            {
                using (SkiaSharp.SKFileWStream fs =
                           new SkiaSharp.SKFileWStream(MyDir + "Artifacts/InsertImageFromByteArrayCustomSize.png"))
                {
                    bitmap.PeekPixels().Encode(fs, SKEncodedImageFormat.Png, 100);
                }

                builder.InsertImage(bitmap, ConvertUtil.PixelToPoint(250), ConvertUtil.PixelToPoint(144));
                builder.Document.Save(MyDir + "Artifacts/Image.CreateFromByteArrayCustomSize.doc");
            }
#else
// Prepare a byte array of an image.
            using (Image image = Image.FromFile(ImageDir + "Aspose.Words.gif"))
            {
                using (MemoryStream imageBytes = new MemoryStream())
                {
                    image.Save(imageBytes, ImageFormat.Png);

                    builder.InsertImage(imageBytes, ConvertUtil.PixelToPoint(250), ConvertUtil.PixelToPoint(144));
                    builder.Document.Save(MyDir + @"\Artifacts\Image.CreateFromByteArrayCustomSize.doc");
                }
            }
#endif
            //ExEnd
        }
コード例 #2
0
        public static void CreatePdfSample(SKCanvas canvas, int width, int height)
        {
            canvas.Clear(SKColors.White);

            using (var paint = new SKPaint()) {
                paint.TextSize    = 24.0f;
                paint.IsAntialias = true;
                paint.Color       = new SKColor(0x9C, 0xAF, 0xB7);
                paint.StrokeWidth = 3;
                paint.TextAlign   = SKTextAlign.Center;

                canvas.DrawText("tap to open", width / 2f, 100f, paint);
            }

            using (var stream = new SKFileWStream(Path.Combine(WorkingDirectory, "document.pdf")))
                using (var document = SKDocument.CreatePdf(stream))
                    using (var pdfCanvas = document.BeginPage(width, height))
                        using (var paint = new SKPaint()) {
                            paint.TextSize    = 64.0f;
                            paint.IsAntialias = true;
                            paint.Color       = new SKColor(0x9C, 0xAF, 0xB7);
                            paint.IsStroke    = true;
                            paint.StrokeWidth = 3;
                            paint.TextAlign   = SKTextAlign.Center;

                            pdfCanvas.DrawText("...PDF...", width / 2f, 100f, paint);
                            document.EndPage();
                            document.Close();
                        }
        }
コード例 #3
0
        public void InsertImageFromByteArray()
        {
            //ExStart
            //ExFor:DocumentBuilder.InsertImage(Byte[])
            //ExSummary:Shows how to import an image into a document from a byte array.
            Document        doc     = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);

#if NETSTANDARD2_0 || __MOBILE__
            using (SkiaSharp.SKBitmap bitmap = SkiaSharp.SKBitmap.Decode(ImageDir + "Aspose.Words.gif"))
            {
                using (SkiaSharp.SKFileWStream fs = new SkiaSharp.SKFileWStream(MyDir + "Artifacts/InsertImageFromByteArray.png"))
                {
                    bitmap.Encode(fs, SKEncodedImageFormat.Png, 100);
                }

                builder.InsertImage(bitmap);
                builder.Document.Save(MyDir + "Artifacts/Image.CreateFromByteArrayDefault.docx");
            }
#else
            // Prepare a byte array of an image.
            Image image = Image.FromFile(ImageDir + "Aspose.Words.gif");

            using (MemoryStream imageBytes = new MemoryStream())
            {
                image.Save(imageBytes, ImageFormat.Png);

                builder.InsertImage(imageBytes.ToArray());
                builder.Document.Save(MyDir + @"\Artifacts\Image.CreateFromByteArrayDefault.doc");
            }
#endif
            //ExEnd
        }
コード例 #4
0
        public void InsertImageFromByteArrayRelativePosition()
        {
            //ExStart
            //ExFor:DocumentBuilder.InsertImage(Byte[], RelativeHorizontalPosition, Double, RelativeVerticalPosition, Double, Double, Double, WrapType)
            //ExSummary:Shows how to import an image into a document from a byte array, also using relative positions.
            Document        doc     = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);

#if NETSTANDARD2_0 || __MOBILE__
            using (SkiaSharp.SKBitmap bitmap = SkiaSharp.SKBitmap.Decode(ImageDir + "Aspose.Words.gif"))
            {
                using (SkiaSharp.SKFileWStream fs = new SkiaSharp.SKFileWStream(MyDir + "Artifacts/InsertImageFromByteArrayCustomSize.png"))
                {
                    bitmap.Encode(fs, SKEncodedImageFormat.Png, 100);
                }

                builder.InsertImage(bitmap, RelativeHorizontalPosition.Margin, 100, RelativeVerticalPosition.Margin, 100, 200, 100, WrapType.Square);
                builder.Document.Save(MyDir + "Artifacts/Image.CreateFromByteArrayCustomSize.doc");
            }
#else
            // Prepare a byte array of an image.
            Image image = Image.FromFile(ImageDir + "Aspose.Words.gif");

            using (MemoryStream imageBytes = new MemoryStream())
            {
                image.Save(imageBytes, ImageFormat.Png);

                builder.InsertImage(imageBytes, RelativeHorizontalPosition.Margin, 100, RelativeVerticalPosition.Margin, 100, 200, 100, WrapType.Square);
                builder.Document.Save(MyDir + @"\Artifacts\Image.CreateFromByteArrayRelativePosition.doc");
            }
#endif
            //ExEnd
        }
コード例 #5
0
ファイル: SKDocument.cs プロジェクト: mdiolola/SkiaSharp
        public static SKDocument CreateXps(string path, float dpi = DefaultRasterDpi)
        {
            var stream = new SKFileWStream(path);
            var doc    = CreateXps(stream, dpi);

            stream.RevokeOwnership();
            return(doc);
        }
コード例 #6
0
        public static SKDocument CreatePdf(string path)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            var stream = SKFileWStream.OpenStream(path);

            return(Owned(CreatePdf(stream), stream));
        }
コード例 #7
0
ファイル: SKStream.cs プロジェクト: zbyszekpy/SkiaSharp
        public static SKWStream OpenStream(string path)
        {
            var stream = new SKFileWStream(path);

            if (!stream.IsValid)
            {
                stream.Dispose();
                stream = null;
            }
            return(stream);
        }
コード例 #8
0
ファイル: PdfExporter.cs プロジェクト: Core2D/Core2D
 /// <inheritdoc/>
 void IProjectExporter.Save(string path, XContainer container)
 {
     using (var stream = new SKFileWStream(path))
     {
         using (var pdf = SKDocument.CreatePdf(stream, _targetDpi))
         {
             Add(pdf, container);
             pdf.Close();
         }
     }
 }
コード例 #9
0
        public static SKDocument CreatePdf(string path, float dpi = DefaultRasterDpi)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            var stream = SKFileWStream.OpenStream(path);
            var doc    = CreatePdf(stream, dpi);

            doc.SetDisposeChild(stream);
            return(doc);
        }
コード例 #10
0
ファイル: PdfExporter.cs プロジェクト: Core2D/Core2D
        /// <inheritdoc/>
        void IProjectExporter.Save(string path, XDocument document)
        {
            using (var stream = new SKFileWStream(path))
            {
                using (var pdf = SKDocument.CreatePdf(stream, _targetDpi))
                {
                    foreach (var container in document.Pages)
                    {
                        Add(pdf, container);
                    }

                    pdf.Close();
                    _renderer.ClearCache(isZooming: false);
                }
            }
        }
コード例 #11
0
ファイル: SkiaSharp.Demos.cs プロジェクト: Core2D/SkiaSharp
		public static void CreatePdfSample (SKCanvas canvas, int width, int height)
		{
			canvas.Clear(SKColors.White);

			using (var paint = new SKPaint ()) {
				paint.TextSize = 24.0f;
				paint.IsAntialias = true;
				paint.Color = new SKColor (0x9C, 0xAF, 0xB7);
				paint.StrokeWidth = 3;
				paint.TextAlign = SKTextAlign.Center;

				canvas.DrawText ("tap to open", width / 2f, 100f, paint);
			}

			using (var stream = new SKFileWStream (Path.Combine (WorkingDirectory, "document.pdf")))
			using (var document = SKDocument.CreatePdf (stream))
			using (var pdfCanvas = document.BeginPage (width, height))
			using (var paint = new SKPaint ()) {
				paint.TextSize = 64.0f;
				paint.IsAntialias = true;
				paint.Color = new SKColor (0x9C, 0xAF, 0xB7);
				paint.IsStroke = true;
				paint.StrokeWidth = 3;
				paint.TextAlign = SKTextAlign.Center;

				pdfCanvas.DrawText ("...PDF...", width / 2f, 100f, paint);
				document.EndPage ();
				document.Close ();
			}
		}