This section aims at providing developers a detail solution to set transparency for image in PDF file with C#, VB.NET via this PDF api Spire.PDF for .NET.
Spire.PDF for .NET enables you to set transparency for PDF image directly by utilizing one core method: Spire.Pdf.PdfPageBase.Canvas.SetTransparency(float alphaPen, float alphaBrush, PdfBlendMode blendMode); There are three parameters passed in this method. The first parameter is the alpha value for pen operations; while the second parameter is the alpha value for brush operations; and the last parameter is the blend mode. Now, let us see how to set PDF transparency step by step.
Set Transparency Images in PDF File
Step1: Prepare an image file
In my solution, I need create a new PDF file and insert an existing image file to PDF. Finally set transparency for this PDF image. So I prepare an image as below:
Step2: Download and Install Spire.PDF for .NET
Spire.PDF for .NET is a PDF component that enables developers to generate, read, edit and handle PDF files without Adobe Acrobat. Here you can download Spire.PDF for .NET and install it on system.
Step3: Start a new project and Add references
We can create a project either in Console Application or in Windows Forms Application, either in C# or in VB.NET. Here I use C# Console Application. Since we will use Spire.PDF for .NET, we need add Spire.Pdf.dll as reference. The default path is “..\Spire.PDF\Bin\NET4.0\ Spire.Pdf.dll”
Step 4: Set PDF transparency for PDF image
In this step, first, I initialize a new instance of the class Spire.Pdf.PdfDocument and add a section in the newly created PDF. Then, load the image I have already prepared to the PDF and set the PDF image size. Finally set transparency for this PDF image. When I set transparency, I add a title for the transparency and set position and format for it. After I save the image in PDF by calling this method: PdfPageBase.Canvas.DrawImage(PdfImage image, float x, float y, float width, float height).Then, by using this method: PdfPageBase.Canvas.SetTransparency(float alphaPen, float alphaBrush, PdfBlendMode blendMode); I successfully set transparency for this PDF image. Here we can see the whole code:
using Spire.Pdf; using Spire.Pdf.Graphics; using System; using System.Drawing; namespace SetTransparencyOfImage { class Program { static void Main(string[] args) { //Initiate a new instance of PdfDocument PdfDocument doc = new PdfDocument(); //Add one section to the PDF document PdfSection section = doc.Sections.Add(); //Open Image File PdfImage image = PdfImage.FromFile(@"..\016.png"); //Set the Image size in PDF file float imageWidth = image.PhysicalDimension.Width / 2; float imageHeight = image.PhysicalDimension.Height / 2; //Set PDF granphic transparency foreach (PdfBlendMode mode in Enum.GetValues(typeof(PdfBlendMode))) { PdfPageBase page = section.Pages.Add(); float pageWidth = page.Canvas.ClientSize.Width; float y = 1; //Set transparency image title, title position and format y = y + 5; PdfBrush brush = new PdfSolidBrush(Color.Firebrick); PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold)); PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Center); String text = String.Format("Transparency Blend Mode: {0}", mode); page.Canvas.DrawString(text, font, brush, pageWidth / 2, y, format); SizeF size = font.MeasureString(text, format); y = y + size.Height + 6; //write and save the image loaded into PDF page.Canvas.DrawImage(image, 0, y, imageWidth, imageHeight); page.Canvas.Save(); //set left and top distance between graphic images float d = (page.Canvas.ClientSize.Width - imageWidth) / 5; float x = d; y = y + d / 2; for (int i = 0; i < 5; i++) { float alpha = 1.0f / 6 * (5 - i); //set transparency to be alpha page.Canvas.SetTransparency(alpha, alpha, mode); //draw transparency images for the original PDF image page.Canvas.DrawImage(image, x, y, imageWidth, imageHeight); x = x + d; y = y + d / 2; } page.Canvas.Restore(); } //Save pdf file. doc.SaveToFile("Transparency.pdf"); doc.Close(); //Launching the Pdf file. System.Diagnostics.Process.Start("Transparency.pdf"); } } }
Imports Spire.Pdf Imports Spire.Pdf.Graphics Imports System.Drawing Namespace SetTransparencyOfImage Class Program Private Shared Sub Main(args As String()) 'Initiate a new instance of PdfDocument Dim doc As New PdfDocument() 'Add one section to the PDF document Dim section As PdfSection = doc.Sections.Add() 'Open Image File Dim image As PdfImage = PdfImage.FromFile("..\016.png") 'Set the Image size in PDF file Dim imageWidth As Single = image.PhysicalDimension.Width / 2 Dim imageHeight As Single = image.PhysicalDimension.Height / 2 'Set PDF granphic transparency For Each mode As PdfBlendMode In [Enum].GetValues(GetType(PdfBlendMode)) Dim page As PdfPageBase = section.Pages.Add() Dim pageWidth As Single = page.Canvas.ClientSize.Width Dim y As Single = 1 'Set transparency image title, title position and format y = y + 5 Dim brush As PdfBrush = New PdfSolidBrush(Color.Firebrick) Dim font As New PdfTrueTypeFont(New Font("Arial", 16F, FontStyle.Bold)) Dim format As New PdfStringFormat(PdfTextAlignment.Center) Dim text As [String] = [String].Format("Transparency Blend Mode: {0}", mode) page.Canvas.DrawString(text, font, brush, pageWidth / 2, y, format) Dim size As SizeF = font.MeasureString(text, format) y = y + size.Height + 6 'write and save the image loaded into PDF page.Canvas.DrawImage(image, 0, y, imageWidth, imageHeight) page.Canvas.Save() 'set left and top distance between graphic images Dim d As Single = (page.Canvas.ClientSize.Width - imageWidth) / 5 Dim x As Single = d y = y + d / 2 For i As Integer = 0 To 4 Dim alpha As Single = 1F / 6 * (5 - i) 'set transparency to be alpha page.Canvas.SetTransparency(alpha, alpha, mode) 'draw transparency images for the original PDF image page.Canvas.DrawImage(image, x, y, imageWidth, imageHeight) x = x + d y = y + d / 2 Next page.Canvas.Restore() Next 'Save pdf file. doc.SaveToFile("Transparency.pdf") doc.Close() 'Launching the Pdf file. System.Diagnostics.Process.Start("Transparency.pdf") End Sub End Class End Namespace
Result Task
After performing above code, we can see the result PDF document as below:
I have set transparency for PDF image by Spire.PDF for .NET. I sincerely wish it can help you. We e-iceblue team appreciate any kind of queries, comments and advice at E-iceblue Forum. Our professionals are ready to reply you as quick as possible.
Spire.PDF for .NET is a PDF library that meets customers need with fast speed and high efficiency.