Draw PDF Image in C#, VB.NET
PDF image is an important part of PDF document, especially in some material instructions, a small image can show useful information that even thousands of words cannot explain. While PDF images are also used to make the document more attractive. This section will show you a solution to draw PDF image via a .NET PDF component in C#, VB.NET.
Spire.PDF for .NET, a .NET PDF library for handling PDF file, enables you to draw image in PDF with C#, VB.NET. Using this PDF component, you can not only set your image size in the PDF file but also place the image at a specific position. In my solution, I set the transparency to avoid covering PDF text by PDF image. You can view the target PDF file as below picture:
Here you can download Spire.PDF for .NET, after installing it on system and adding Spire.Pdf dll, you can start your PDF task by following steps.
Detail Code:
There are mainly three simple steps in my solution. The first step requires you to load an image, which can be achieved by this method: System.Drawing.Image FromFile(string filename). The second step needs you to set image size to match the PDF appropriately through setting image width and height via System.Drawing.Size(int width, int height). The last step is designed to set the PDF image position and draw image in PDF. You can call DrawImage(PdfImage image, PointF point) method in which there are with two parameters passed. The second parameter is mainly responsible for setting image to a specific position. If necessary, you also can set transparency by SetTransparency(float alpha). Please see the whole code here.
using System.Drawing; using Spire.Pdf; using Spire.Pdf.Graphics; namespace PDFimage { class Program { static void Main(string[] args) { //Create a pdf document. PdfDocument doc = new PdfDocument(); doc.LoadFromFile(@"..\Sample.pdf"); Image image = Image.FromFile(@"..\images.jpg"); //adjust image size int width=image.Width; int height=image.Height; float schale=1.5f; Size size=new Size ((int)(width*schale),(int)(height*schale)); Bitmap schaleImage = new Bitmap(image, size); //insert image into the first PDF page at specific position PdfImage pdfImage = PdfImage.FromImage(schaleImage); PdfPageBase page0 = doc.Pages[0]; PointF position = new PointF((page0.Canvas.ClientSize.Width - schaleImage.Width) / 2, 210); page0.Canvas.SetTransparency(0.5f); page0.Canvas.DrawImage(pdfImage, position); page0.Canvas.SetTransparency(1.0f); doc.SaveToFile(@"pdfimage.pdf"); System.Diagnostics.Process.Start(@"pdfimage.pdf"); } } }
Imports System.Drawing Imports Spire.Pdf Imports Spire.Pdf.Graphics Namespace PDFimage Class Program Private Shared Sub Main(ByVal args() As String) 'Create a pdf document. Dim doc As PdfDocument = New PdfDocument doc.LoadFromFile("..\Sample.pdf") Dim image As Image = Image.FromFile("..\images.jpg") 'adjust image size Dim width As Integer = image.Width Dim height As Integer = image.Height Dim schale As Single = 1.5! Dim size As Size = New Size(CType((width * schale),Integer), CType((height * schale),Integer)) Dim schaleImage As Bitmap = New Bitmap(image, size) 'insert image into the first PDF page at specific position Dim pdfImage As PdfImage = PdfImage.FromImage(schaleImage) Dim page0 As PdfPageBase = doc.Pages(0) Dim position As PointF = New PointF(((page0.Canvas.ClientSize.Width - schaleImage.Width) _ / 2), 210) page0.Canvas.SetTransparency(0.5!) page0.Canvas.DrawImage(pdfImage, position) page0.Canvas.SetTransparency(1!) doc.SaveToFile("pdfimage.pdf") System.Diagnostics.Process.Start("pdfimage.pdf") End Sub End Class End Namespace
Spire.PDF for .NET allows developers to perform a wide range of tasks directly without Adobe Acrobat such as create, edit, read and manage PDF documents in C#,VB.NET.
Set Excel Background Color in C#, VB.NET
Excel background color is very useful for developers to analyze data information. Setting background color of rows or columns in a similar category makes the data obviously shown. In this section, one line of code will help you easily set your excel background color in a fast way by a .NET Excel component.
Spire.XLS for .NET is an Excel document generating, reading, writing and manipulating component for .NET. It enables you to set excel background color by the class Spire.Xls.Worksheet. Range[].Style.Color. While before setting the background color, you need to use Workbook.LoadFromFile(string fileName, bool preserveMode) method to load your Excel file from system and save the excel file by Workbook.SaveToFile(string fileName) after.
Please Download Spire.XLS for .NET and preview the target excel as picture below:
using System.Drawing; using Spire.Xls; namespace background_color { class Program { static void Main(string[] args) { Workbook workbook = new Workbook(); workbook.LoadFromFile(@"..\Backgroundcolor.xls", ExcelVersion.Version97to2003); Worksheet worksheet = workbook.Worksheets[0]; //set the backgroundcolor of Range["A1:D1"] worksheet.Range["A1:D1"].Style.Color = Color.LightSeaGreen; //set the backgroundcolor of Range["A2:D6"] worksheet.Range["A2:D6"].Style.Color = Color.SpringGreen; //set the backgroundcolor of Range["A7:D11"] worksheet.Range["A7:D11"].Style.Color = Color.DeepSkyBlue; //set the backgroundcolor of Range["A12:D16"] worksheet.Range["A12:D16"].Style.Color = Color.Yellow; //save and launch the project workbook.SaveToFile("test.xls", ExcelVersion.Version97to2003); System.Diagnostics.Process.Start(workbook.FileName); } } }
Imports System.Drawing Imports Spire.Xls Namespace background_color Class Program Private Shared Sub Main(ByVal args() As String) Dim workbook As Workbook = New Workbook workbook.LoadFromFile("..\Backgroundcolor.xls", ExcelVersion.Version97to2003) Dim worksheet As Worksheet = workbook.Worksheets(0) 'set the backgroundcolor of Range["A1:D1"] worksheet.Range("A1:D1").Style.Color = Color.LightSeaGreen 'set the backgroundcolor of Range["A2:D6"] worksheet.Range("A2:D6").Style.Color = Color.SpringGreen 'set the backgroundcolor of Range["A7:D11"] worksheet.Range("A7:D11").Style.Color = Color.DeepSkyBlue 'set the backgroundcolor of Range["A12:D16"] worksheet.Range("A12:D16").Style.Color = Color.Yellow 'save and launch the project workbook.SaveToFile("test.xls", ExcelVersion.Version97to2003) System.Diagnostics.Process.Start(workbook.FileName) End Sub End Class End Namespace
Spire.XLS allows user to operate Excel document directly such as save to stream, save as web response, copy, lock/unlock worksheet, set up workbook properties, etc. As a professional .NET/Silverlight Excel component, it owns the ability of inserting content into Excel document, formatting cells and converting Excel documents to popular office file formats. Spire.XLS for .NET supports Excel 97-2003, Excel 2007 and Excel 2010.
Merge PDF Documents in Silverlight
The sample demonstrates how to Merge PDF documents in Silverlight via Spire.XLS.
(No Screenshots)
Create Excel Group in Silverlight
The sample demonstrates how to create Excel group in Silverlight via Spire.XLS.
Word Header in Silverlight
The sample demonstrates how to add Word header in Silverlight via Spire.Doc.
Set Graphic Overlay in PDF File
This section is designed to provide developers a solution to set graphic overlay in PDF file with C#, VB.NET via a .NET PDF library Spire.PDF for .NET.
Spire.PDF for .NET enables you to set graphic overlay for PDF pages in an easy way. In order to see content in both PDF files, we can set the transparency for the overlay. Now, let us see the code detail step by step.
First, we need to load two PDF documents from system which are used for creating overlay PDF document. Then, we can create the page template as one page of the first PDF file. In my project, I set the first page in the first PDF document as the template. Finally draw this template page into every page of the second PDF file by this method: PdfPageBase.Canvas.DrawTemplate(PdfTemplate template, PointF location) and set the transparency for the page: PdfPageBase.Canvas.SetTransparency(float alphaPen, float alphaBrush, PdfBlendMode blendMode).
Here you can know more about Spire.PDF for .NET. Spire.PDF for .NET is a PDF component that is applied to manipulate PDF files in .NET applications. You can download Spire.PDF for .NET. After installing it on system, you can start your project by C# or VB.NET in either Console Application or Windows Forms Application. Since we will use Spire.PDF, you need add Spire.Pdf.dll in the Bin folder, the default path is: "..\Spire.Pdf\Bin\NET4.0\Spire.Pdf.dll". Below is the whole code of the task:
using System.Drawing; using Spire.Pdf; using Spire.Pdf.Graphics; namespace pdf_graphic_overlay { class Program { static void Main(string[] args) { //load two documents PdfDocument doc1 = new PdfDocument(); doc1.LoadFromFile(@"..\ Sample1.pdf"); PdfDocument doc2 = new PdfDocument(); doc2.LoadFromFile(@"..\Sample3.pdf"); //Create page template PdfTemplate template = doc1.Pages[0].CreateTemplate(); //set PDF overlay effect and set transparency mode foreach (PdfPageBase page in doc2.Pages) { //set transparency page.Canvas.SetTransparency(0.25f, 0.25f, PdfBlendMode.Overlay); //add the first page of doc1 to every page of doc2 page.Canvas.DrawTemplate(template, PointF.Empty); } //Save pdf file. doc2.SaveToFile("Overlay.pdf"); doc1.Close(); doc2.Close(); //Launching the Pdf file. System.Diagnostics.Process.Start("Overlay.pdf"); } } }
Imports System.Drawing Imports Spire.Pdf Imports Spire.Pdf.Graphics Namespace pdf_graphic_overlay Class Program Private Shared Sub Main(args As String()) 'load two documents Dim doc1 As New PdfDocument() doc1.LoadFromFile("..\Sample1.pdf") Dim doc2 As New PdfDocument() doc2.LoadFromFile("..\Sample3.pdf") 'Create page template Dim template As PdfTemplate = doc1.Pages(0).CreateTemplate() 'set PDF overlay effect and set transparency mode For Each page As PdfPageBase In doc2.Pages 'set transparency page.Canvas.SetTransparency(0.25F, 0.25F, PdfBlendMode.Overlay) 'add the first page of doc1 to every page of doc2 page.Canvas.DrawTemplate(template, PointF.Empty) Next 'Save pdf file. doc2.SaveToFile("Overlay.pdf") doc1.Close() doc2.Close() 'Launching the Pdf file. System.Diagnostics.Process.Start("Overlay.pdf") End Sub End Class End Namespace
After executing above code, we can get the output file as below:
I have introduced one solution to set graphic overlay for PDF document, I wish it can give you some insights. If you have problem, feedback and suggestions, please do not hesitate to put them on E-iceblue Forum, we will give prompt reply.
Spire.PDF for .NET is a PDF application which is designed to perform processing tasks on managing PDF files. It is completely written in C# but also support VB.NET.
Set Transparency for PDF Image in C#, VB.NET
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.
Excel Subtotal in C#, VB.NET
The sample demonstrates how to set Excel subtotal formula via Spire.XLS.
Replace Data in Excel in C#, VB.NET
The sample demonstrates how to replace data in Excel workbook via Spire.XLS.
Copy Cell Data in Excel in C#, VB.NET
The sample demonstrates how to copy cell data in Excel workbook via Spire.XLS.