DsPdf allows you to draw an image on a page using DrawImage method of the GcGraphics class. You can load an image from a file or using a stream. The library supports various image formats, such as BMP, GIF (single frame only), JPEG, SVG, and PNG. Additionally, on Windows, TIFF, JpegXR, and ICO formats are also supported.
While rendering a single image object repeatedly in a GcPdfDocument instance, DsPdf automatically stores the image in a dictionary and reuses the same object throughout the document. This lets you create PDF files of optimum size at a faster pace as it saves time and uses cache to load the same image multiple times. This feature stands very beneficial when rendering document that contains company logo in its repeating page header.
In addition, DsPdf library provides ImageAlign class to let you align images in different ways using properties such as AlignHorz, AlignVert, BestFit, TileHorz, etc. The library also allows you to control the image quality such as compressing color values, setting JPEG image quality, etc. through ImageOptions property available in the GcPdfDocument class.
To add an image in a PDF document, load the image in your application using Image.FromFile method. This method will store the image in an object of Image class. Once, the image is added, you can use the DrawImage method provided by the GcGraphics class to render the image.
C# |
Copy Code
|
---|---|
public void CreatePDF(Stream stream) { GcPdfDocument doc = new GcPdfDocument(); var page = doc.NewPage(); var g = page.Graphics; // Add image to the application var image = GrapeCity.Documents.Drawing.Image.FromFile (Path.Combine("Resources", "Images", "clouds.jpg")); // Use DrawImage to render the image g.DrawImage(image, new RectangleF(30.6F, 30.7F, 40.8F, 100.9F), null, ImageAlign.CenterImage); // Save the PDF file doc.Save(stream); } |
To add an image in a PDF document using stream, you need to store image in a stream using Image.FromStream method. Once the image is stored, it can be added to the application. Then, you can use the DrawImage method provided by the GcGraphics class to render the image.
C# |
Copy Code
|
---|---|
public void CreatePDF(Stream stream) { GcPdfDocument doc = new GcPdfDocument(); var page = doc.NewPage(); var g = page.Graphics; string fileName = @"C:\Users\Admin\Desktop\clouds.png"; FileStream fs = new FileStream(fileName, System.IO.FileMode.Open); // Add image to the application var image = GrapeCity.Documents.Drawing.Image.FromStream(fs); // Use DrawImage to render the image g.DrawImage(image, new RectangleF(30.6F, 30.7F, 40.8F, 100.9F), null, ImageAlign.CenterImage); // Save the PDF file doc.Save(stream); } |
DsPdf provides RemoveImages method in GcPdfDocument class to remove all or some of the images from the PDF document. GetImages method and its overloads in GcPdfDocument class help to retrieve images from a PDF document. This includes images embedded within content streams such as Pages and FormXObjects, as well as images directly referenced by WidgetAnnotation objects through WidgetAnnotation.ButtonAppearance.Image, WidgetAnnotation.ButtonAppearance.RolloverAppearance.Image, and WidgetAnnotation.ButtonAppearance.DownAppearance.Image properties. GetImages method can retrieve images referenced by WidgetAnnotation objects using PdfImageInfo.WidgetReferences property.
PdfImageLocation class provides Annotation and FormXObject properties that indicate the positions of images referenced in an Annotation or FormXObject. These properties are non-null if the annotation appearance stream incorporates the image. Additionally, by default, the GetImages method also retrieves images that are unused within the document. Unused images are those that appear in resource dictionaries but are not used in content streams or mentioned in any of the properties.
Example 1:
Refer to the example code to remove all images from the PDF document:
C# |
Copy Code
|
---|---|
// Initialize GcPdfDocument. GcPdfDocument doc = new GcPdfDocument(); // Load PDF file with images. var stream = new FileStream("ImageTestCommon_E-0.pdf", FileMode.Open); doc.Load(stream); // Get all the images. var images = doc.GetImages(); // Remove all images. doc.RemoveImages(images); // Save the PDF document. doc.Save("RemovedAllImages.pdf"); |
Example 2:
Refer to the following example code to remove the first image from the PDF document:
C# |
Copy Code
|
---|---|
// Initialize GcPdfDocument. GcPdfDocument doc = new GcPdfDocument(); // Load PDF file with images. var stream = new FileStream("ImageTestCommon_E-0.pdf", FileMode.Open); doc.Load(stream); // Get all the images. var images = doc.GetImages(); // Remove first image. doc.RemoveImages(new PdfImageInfo[] { images[0] }); // Save the PDF document. doc.Save("RemovedFirstImage.pdf"); |
Example 3:
Refer to the following example code to remove the first image from all locations except for the first index (0):
C# |
Copy Code
|
---|---|
// Initialize GcPdfDocument. GcPdfDocument doc = new GcPdfDocument(); // Load PDF file with images. var stream = new FileStream("ImageTestCommon_E-0.pdf", FileMode.Open); doc.Load(stream); // Get all the images. var images = doc.GetImages(); // Fetch information about first image. PdfImageInfo pii = images[0]; // Select image range. pii.Locations.RemoveRange(1, 2); // Remove from all locations except for first. doc.RemoveImages(new PdfImageInfo[] { pii }); // Save the PDF document. doc.Save("RemoveExceptFirstIndexImage.pdf"); |
Example 4:
Refer to the following example code to remove the first image completely from all locations and widget references:
C# |
Copy Code
|
---|---|
// Initialize GcPdfDocument. GcPdfDocument doc = new GcPdfDocument(); // Load PDF file with images. var stream = new FileStream("ImageTestCommon_E-0.pdf", FileMode.Open); doc.Load(stream); // Get all the images. var images = doc.GetImages(); // Remove first image completely. doc.RemoveImages(new PdfImageInfo[] { new PdfImageInfo(images[0].Image) }); // Save the PDF document. doc.Save("RemoveFirstImageCompletely.pdf"); |
To render an image with a specified transparency, you can add an image to a PDF document using the DrawImage method that takes opacity as one of the parameters.
C# |
Copy Code
|
---|---|
//Create a basic pdf GcPdfDocument doc = new GcPdfDocument(); GcPdfGraphics g = doc.NewPage().Graphics; g.DrawString("A sample document showing an image with controlled opacity.", new TextFormat() { Font = StandardFonts.Times, FontSize = 12 }, new PointF(72, 72)); //Add an image by controlling its opacity var image = Image.FromFile(Path.Combine("Resources", "sea.jpg"), RawImageFormat.Jpeg, 800, 532); ImageAlign ia = new ImageAlign(ImageAlignHorz.Center, ImageAlignVert.Center, true, true, true, false, false); g.DrawImage(image, new RectangleF(100, 100, 180, 100), null, ImageAlign.ScaleImage,0.3F); //Save the final pdf doc.Save("AddImage_Opacity.pdf"); Console.WriteLine("Press any key to exit"); Console.ReadKey(); |
To extract an image from a PDF document, use GetImages method:
C# |
Copy Code
|
---|---|
using (FileStream fs = new FileStream(Path.Combine("Resources", "Wetlands.pdf"), FileMode.Open, FileAccess.Read)) { GcPdfDocument docSrc = new GcPdfDocument(); // Load an existing PDF with some images docSrc.Load(fs); //Extract information about images from the loaded PDF var imageInfos = docSrc.GetImages(); GcPdfDocument doc = new GcPdfDocument(); var textPt = new PointF(72, 72); foreach (var imageInfo in imageInfos) { // The same image may appear on multiple locations, // imageInfo includes page indices and locations on pages; var g = doc.NewPage().Graphics; g.DrawImage(imageInfo.Image, new RectangleF(10,0,400,400), null, ImageAlign.ScaleImage); } doc.Save("ExtractImage.pdf"); } Console.WriteLine("Press any key to exit"); Console.ReadKey(); |
For more information about implementation of images using DsPdf, see DsPdf sample browser.
To create an SVG image using code:
C# |
Copy Code
|
---|---|
public static GcSvgDocument DrawCarrot() { // Create a new SVG document var doc = new GcSvgDocument(); var svg = doc.RootSvg; svg.ViewBox = new SvgViewBox(0, 0, 313.666f, 164.519f); //Create an instance of SvgPathBuilder class. var pb = new SvgPathBuilder(); //Define the path pb.AddMoveTo(false, 29.649f, 9.683f); pb.AddCurveTo(true, -2.389f, -0.468f, -4.797f, 2.57f, -6.137f, 5.697f); pb.AddCurveTo(true, 2.075f, -2.255f, 3.596f, -1.051f, 4.91, 5f, -0.675f); pb.AddCurveTo(true, -2.122f, 2.795f, -4f, 5.877f, -7.746f, 5.568f); pb.AddCurveTo(true, 2.384f, -6.014f, 2.963f, -12.977f, 0.394f, -17.78f); pb.AddCurveTo(true, -1.296f, 2.591f, -1.854f, 6.054f, -5.204f, 7.395f); pb.AddCurveTo(true, 3.575f, 2.455f, 0.986f, 7.637f, 1.208f, 11.437f); pb.AddCurveTo(false, 11.967f, 21.17f, 6.428f, 16.391f, 9.058f, 10.67f); pb.AddCurveTo(true, -3.922f, 8.312f, -2.715f, 19.745f, 4.363f, 22.224f); pb.AddCurveTo(true, -3.86f, 4.265f, -2.204f, 10.343f, 0.209f, 13.781f); pb.AddCurveTo(true, -0.96f, 1.808f, -1.83f, 2.546f, -3.774f, 3.195f); pb.AddCurveTo(true, 3.376f, 1.628f, 6.612f, 4.866f, 11.326f, 3.366f); pb.AddCurveTo(true, -1.005f, 2.345f, -12.389f, 9.499f, -15.16f, 10.35f); pb.AddCurveTo(true, 3.216f, 0.267f, 14.492f, -2.308f, 16.903f, -5.349f); pb.AddCurveTo(true, -1.583f, 2.84f, 1.431f, 2.28f, 2.86f, 4.56f); pb.AddCurveTo(true, 1.877f, -3.088f, 3.978f, -2.374f, 5.677f, -3.311f); pb.AddCurveTo(true, -0.406f, 4.826f, -2.12f, 9.27f, -5.447f, 13.582f); pb.AddCurveTo(true, 2.834f, -4.894f, 6.922f, -5.367f, 10.474f, -5.879f); pb.AddCurveTo(true, -0.893f, 4.245f, -3.146f, 8.646f, -7.077f, 10.479f); pb.AddCurveTo(true, 5.359f, 0.445f, 11.123f, -3.934f, 13.509f, -9.944f); pb.AddCurveTo(true, 12.688f, 3.209f, 28.763f, -1.932f, 39.894f, 7.084f); pb.AddCurveTo(true, 1.024f, 0.625f, 1.761f, -4.98f, 1.023f, -5.852f); pb.AddCurveTo(false, 72.823f, 55.357f, 69.273f, 68.83f, 52.651f, 54.498f); pb.AddCurveTo(true, -0.492f, -0.584f, 1.563f, -5.81f, 1f, -8.825f); pb.AddCurveTo(true, -1.048f, -3.596f, -3.799f, -6.249f, -7.594f, -6.027f); pb.AddCurveTo(true, -2.191f, 0.361f, -5.448f, 0.631f, -7.84f, 0.159f); pb.AddCurveTo(true, 2.923f, -5.961f, 9.848f, -4.849f, 12.28f, -11.396f); pb.AddCurveTo(true, -4.759f, 2.039f, -7.864f, -2.808f, -12.329f, -1.018f); pb.AddCurveTo(true, 1.63f, -3.377f, 4.557f, -2.863f, 6.786f, -3.755f); pb.AddCurveTo(true, -3.817f, -2.746f, -9.295f, -5.091f, -14.56f, -0.129f); pb.AddCurveTo(false, 33.228f, 18.615f, 32.064f, 13.119f, 29.649f, 9.683f); //Add elements into Children collection of SVG svg.Children.Add(new SvgPathElement() { FillRule = SvgFillRule.EvenOdd, Fill = new SvgPaint(Color.FromArgb(0x43, 0x95, 0x39)), PathData = pb.ToPathData(), }); pb.Reset(); pb.AddMoveTo(false, 29.649f, 9.683f); pb.AddCurveTo(true, -2.389f, -0.468f, -4.797f, 2.57f, -6.137f, 5.697f); pb.AddCurveTo(true, 2.075f, -2.255f, 3.596f, -1.051f, 4.915f, -0.675f); pb.AddCurveTo(true, -2.122f, 2.795f, -4f, 5.877f, -7.746f, 5.568f); pb.AddCurveTo(true, 2.384f, -6.014f, 2.963f, -12.977f, 0.394f, -17.78f); pb.AddCurveTo(true, -1.296f, 2.591f, -1.854f, 6.054f, -5.204f, 7.395f); pb.AddCurveTo(true, 3.575f, 2.455f, 0.986f, 7.637f, 1.208f, 11.437f); pb.AddCurveTo(false, 11.967f, 21.17f, 6.428f, 16.391f, 9.058f, 10.67f); pb.AddCurveTo(true, -3.922f, 8.312f, -2.715f, 19.745f, 4.363f, 22.224f); pb.AddCurveTo(true, -3.86f, 4.265f, -2.204f, 10.343f, 0.209f, 13.781f); pb.AddCurveTo(true, -0.96f, 1.808f, -1.83f, 2.546f, -3.774f, 3.195f); pb.AddCurveTo(true, 3.376f, 1.628f, 6.612f, 4.866f, 11.326f, 3.366f); pb.AddCurveTo(true, -1.005f, 2.345f, -12.389f, 9.499f, -15.16f, 10.35f); pb.AddCurveTo(true, 3.216f, 0.267f, 14.492f, -2.308f, 16.903f, -5.349f); pb.AddCurveTo(true, -1.583f, 2.84f, 1.431f, 2.28f, 2.86f, 4.56f); pb.AddCurveTo(true, 1.877f, -3.088f, 3.978f, -2.374f, 5.677f, -3.311f); pb.AddCurveTo(true, -0.406f, 4.826f, -2.12f, 9.27f, -5.447f, 13.582f); pb.AddCurveTo(true, 2.834f, -4.894f, 6.922f, -5.367f, 10.474f, -5.879f); pb.AddCurveTo(true, -0.893f, 4.245f, -3.146f, 8.646f, -7.077f, 10.479f); pb.AddCurveTo(true, 5.359f, 0.445f, 11.123f, -3.934f, 13.509f, -9.944f); pb.AddCurveTo(true, 12.688f, 3.209f, 28.763f, -1.932f, 39.894f, 7.084f); pb.AddCurveTo(true, 1.024f, 0.625f, 1.761f, -4.98f, 1.023f, -5.852f); pb.AddCurveTo(false, 72.823f, 55.357f, 69.273f, 68.83f, 52.651f, 54.498f); pb.AddCurveTo(true, -0.492f, -0.584f, 1.563f, -5.81f, 1f, -8.825f); pb.AddCurveTo(true, -1.048f, -3.596f, -3.799f, -6.249f, -7.594f, -6.027f); pb.AddCurveTo(true, -2.191f, 0.361f, -5.448f, 0.631f, -7.84f, 0.159f); pb.AddCurveTo(true, 2.923f, -5.961f, 9.848f, -4.849f, 12.28f, -11.396f); pb.AddCurveTo(true, -4.759f, 2.039f, -7.864f, -2.808f, -12.329f, -1.018f); pb.AddCurveTo(true, 1.63f, -3.377f, 4.557f, -2.863f, 6.786f, -3.755f); pb.AddCurveTo(true, -3.817f, -2.746f, -9.295f, -5.091f, -14.56f, -0.129f); pb.AddCurveTo(false, 33.228f, 18.615f, 32.064f, 13.119f, 29.649f, 9.683f); pb.AddClosePath(); //Add elements into Children collection of SVG svg.Children.Add(new SvgPathElement() { Fill = SvgPaint.None, Stroke = new SvgPaint(Color.Black), StrokeWidth = new SvgLength(2.292f), StrokeMiterLimit = 14.3f, PathData = pb.ToPathData(), }); pb.Reset(); pb.AddMoveTo(false, 85.989f, 101.047f); pb.AddCurveTo(true, 0f, 0f, 3.202f, 3.67f, 8.536f, 4.673f); pb.AddCurveTo(true, 7.828f, 1.472f, 17.269f, 0.936f, 17.269f, 0.936f); pb.AddCurveTo(true, 0f, 0f, 2.546f, 5.166f, 10.787f, 7.338f); pb.AddCurveTo(true, 8.248f, 2.168f, 17.802f, 0.484f, 17.802f, 0.484f); pb.AddCurveTo(true, 0f, 0f, 8.781f, 1.722f, 19.654f, 8.074f); pb.AddCurveTo(true, 10.871f, 6.353f, 20.142f, 2.163f, 20.142f, 2.163f); pb.AddCurveTo(true, 0f, 0f, 1.722f, 3.118f, 14.11f, 9.102f); pb.AddCurveTo(true, 12.39f, 5.982f, 14.152f, 2.658f, 28.387f, 4.339f); pb.AddCurveTo(true, 14.232f, 1.672f, 19.36f, 5.568f, 30.108f, 7.449f); pb.AddCurveTo(true, 10.747f, 1.886f, 25.801f, 5.607f, 25.801f, 5.607f); pb.AddCurveTo(true, 0f, 0f, 4.925f, 0.409f, 12.313f, 6.967f); pb.AddCurveTo(true, 7.381f, 6.564f, 18.453f, 4.506f, 18.453f, 4.506f); pb.AddCurveTo(true, 0f, 0f, -10.869f, -6.352f, -15.467f, -10.702f); pb.AddCurveTo(true, -4.594f, -4.342f, -16.901f, -11.309f, -24.984f, -15.448f); pb.AddCurveTo(true, -8.079f, -4.14f, -18.215f, -7.46f, -30.233f, -11.924f); pb.AddCurveTo(true, -12.018f, -4.468f, -6.934f, -6.029f, -23.632f, -13.855f); pb.AddCurveTo(true, -16.695f, -7.822f, -13.662f, -8.565f, -28.347f, -10.776f); pb.AddCurveTo(true, -14.686f, -2.208f, -6.444f, -11.933f, -23.917f, -16.356f); pb.AddCurveTo(true, -17.479f, -4.423f, -11.037f, -4.382f, -26.016f, -9.093f); pb.AddCurveTo(true, -14.97f, -4.715f, -10.638f, -10.104f, -26.665f, -13.116f); pb.AddCurveTo(true, -14.149f, -2.66f, -21.318f, 0.468f, -27.722f, 11.581f); pb.AddCurveTo(false, 73.104f, 89.075f, 85.989f, 101.047f, 85.989f, 101.047f); // Add elements into Children collection of SVG svg.Children.Add(new SvgPathElement() { FillRule = SvgFillRule.EvenOdd, Fill = new SvgPaint(Color.FromArgb(0xFF, 0xC2, 0x22)), PathData = pb.ToPathData(), }); pb.Reset(); pb.AddMoveTo(false, 221.771f, 126.738f); pb.AddCurveTo(true, 0f, 0f, 1.874f, -4.211f, 4.215f, -6.087f); pb.AddCurveTo(true, 2.347f, -1.868f, 2.812f, -2.339f, 2.812f, -2.339f); pb.AddMoveTo(false, 147.11f, 105.122f); pb.AddCurveTo(true, 0f, 0f, 0.882f, -11.047f, 6.765f, -15.793f); pb.AddCurveTo(true, 5.879f, -4.745f, 10.882f, -5.568f, 10.882f, -5.568f); pb.AddMoveTo(false, 125.391f, 86.008f); pb.AddCurveTo(true, 0f, 0f, 2.797f, -6.289f, 6.291f, -9.081f); pb.AddCurveTo(true, 3.495f, -2.791f, 4.194f, -3.49f, 4.194f, -3.49f); pb.AddMoveTo(false, 181.153f, 124.8f); pb.AddCurveTo(true, 0f, 0f, -1.206f, -4.014f, -0.709f, -6.671f); pb.AddCurveTo(true, 0.493f, -2.66f, 0.539f, -3.256f, 0.539f, -3.256f); pb.AddMoveTo(false, 111.704f, 107.641f); pb.AddCurveTo(true, 0f, 0f, -1.935f, -6.604f, -1.076f, -10.991f); pb.AddCurveTo(true, 0.862f, -4.389f, 0.942f, -5.376f, 0.942f, -5.376f); pb.AddMoveTo(false, 85.989f, 101.047f); pb.AddCurveTo(true, 0f, 0f, 3.202f, 3.67f, 8.536f, 4.673f); pb.AddCurveTo(true, 7.828f, 1.472f, 17.269f, 0.936f, 17.269f, 0.936f); pb.AddCurveTo(true, 0f, 0f, 2.546f, 5.166f, 10.787f, 7.338f); pb.AddCurveTo(true, 8.248f, 2.168f, 17.802f, 0.484f, 17.802f, 0.484f); pb.AddCurveTo(true, 0f, 0f, 8.781f, 1.722f, 19.654f, 8.074f); pb.AddCurveTo(true, 10.871f, 6.353f, 20.142f, 2.163f, 20.142f, 2.163f); pb.AddCurveTo(true, 0f, 0f, 1.722f, 3.118f, 14.11f, 9.102f); pb.AddCurveTo(true, 12.39f, 5.982f, 14.152f, 2.658f, 28.387f, 4.339f); pb.AddCurveTo(true, 14.232f, 1.672f, 19.36f, 5.568f, 30.108f, 7.449f); pb.AddCurveTo(true, 10.747f, 1.886f, 25.801f, 5.607f, 25.801f, 5.607f); pb.AddCurveTo(true, 0f, 0f, 4.925f, 0.409f, 12.313f, 6.967f); pb.AddCurveTo(true, 7.381f, 6.564f, 18.453f, 4.506f, 18.453f, 4.506f); pb.AddCurveTo(true, 0f, 0f, -10.869f, -6.352f, -15.467f, -10.702f); pb.AddCurveTo(true, -4.594f, -4.342f, -16.901f, -11.309f, -24.984f, -15.448f); pb.AddCurveTo(true, -8.079f, -4.14f, -18.215f, -7.46f, -30.233f, -11.924f); pb.AddCurveTo(true, -12.018f, -4.468f, -6.934f, -6.029f, -23.632f, -13.855f); pb.AddCurveTo(true, -16.695f, -7.822f, -13.662f, -8.565f, -28.347f, -10.776f); pb.AddCurveTo(true, -14.686f, -2.208f, -6.444f, -11.933f, -23.917f, -16.356f); pb.AddCurveTo(true, -17.479f, -4.423f, -11.037f, -4.382f, -26.016f, -9.093f); pb.AddCurveTo(true, -14.97f, -4.715f, -10.638f, -10.104f, -26.665f, -13.116f); pb.AddCurveTo(true, -14.149f, -2.66f, -21.318f, 0.468f, -27.722f, 11.581f); pb.AddCurveTo(false, 73.104f, 89.075f, 85.989f, 101.047f, 85.989f, 101.047f); pb.AddClosePath(); //Add elements into Children collection of SVG svg.Children.Add(new SvgPathElement() { Fill = SvgPaint.None, Stroke = new SvgPaint(Color.Black), StrokeWidth = new SvgLength(3.056f), StrokeMiterLimit = 11.5f, PathData = pb.ToPathData(), }); //Save the document as svg doc.Save("demo.svg"); return doc; } |
The GrapeCity.Documents.Svg namespace provides GcSvgDocument class which can be used to render SVG files on PDF pages.
To render an SVG image file to a PDF document:
C# |
Copy Code
|
---|---|
var doc = new GcPdfDocument(); var g = doc.NewPage().Graphics; var prevT = g.Transform; g.Transform = Matrix3x2.CreateScale(factor); using var svg = GcSvgDocument.FromFile("Rectangle.svg"); g.DrawSvg(svg, new PointF(72 / factor, 72 / factor)); g.Transform = prevT; doc.Save("SVGImage.pdf"); |
For more information about rendering SVG images to PDF files using DsPdf, see DsPdf sample browser.
You can also render an SVG image to a PNG file, create, load, inspect, modify, and save the internal structure of an SVG image. For more information, refer Work with SVG Files topic in DsImaging docs.