In this article, I'll show you how to programmatically create a grid in a PDF document and insert a picture into a specific grid.
There are some similar workarounds on the web, where I chose a free version of the PDF component. After you install the control, create a new project, add the DLL file under the installation directory as a reference to the project, and the namespace as follows:
Using spire.pdf;using spire.pdf.graphics;using Spire.Pdf.Grid;
The next step is the detailed steps and code snippets:
Step 1: first create a PDF document and add a new page.
Pdfdocument doc = new pdfdocument (); Pdfpagebase page = doc. Pages.Add ();
Step 2 : creates a grid of two columns in a row.
Pdfgrid Grid = new Pdfgrid (); Pdfgridrow row = grid. Rows.Add (); grid. Columns.Add (2);
Step 3 : Sets the spacing between the cell border and the filled content.
Grid. style.cellpadding = new Pdfpaddings (1, 1, 1, 1);
Step 4 : sets the column width.
Float width = page. Canvas.ClientSize.Width-(grid. Columns.count + 1); grid. Columns[0]. width = width * 0.1f;grid. COLUMNS[1]. width = width * 0.1f;
Step 5 : loads the picture.
Pdfgridcelltextandstylelist lst = new Pdfgridcelltextandstylelist (); Pdfgridcelltextandstyle Textandstyle = new Pdfgridcelltextandstyle (); Textandstyle.image=pdfimage.fromfile (@ "C: \ Users\administrator\pictures\448a5ba8f8851709a1f53e.jpg ");
Step 6 : Sets the size of the picture and inserts it into the first cell.
Textandstyle.imagesize = new SizeF (a); lst. List.add (Textandstyle); row. Cells[0]. Value = LST;
Step 7: draw the pdf grid at a specific location on the page.
Pdflayoutresult result = grid. Draw (page, new PointF (10, 30));
Step 8 : Save and run the PDF file.
Doc. SaveToFile (OutputFile, fileformat.pdf); System.Diagnostics.Process.Start (OutputFile);
:
This spire. The PDF component is based on the. NET Office software Library, and there are other rich features. So for the office development needs of friends, interested in the words can be in the official website reference online tutorials.
All code:
Using system;using system.drawing;using system.windows.forms;using spire.pdf;using spire.pdf.graphics;using Spire.pdf.grid;namespace insert_an_image_to_pdf_grid_cell{public partial class Form1:form {public Form1 () {InitializeComponent (); private void Button1_Click (object sender, EventArgs e) {string outputFile = "Output.pdf"; Create a new PDF document Pdfdocument doc = new pdfdocument (); Add pages Pdfpagebase page = doc. Pages.Add (); Create a PDF grid Pdfgrid Grid = new Pdfgrid (); Sets the spacing between the cell border and the fill content grid. style.cellpadding = new Pdfpaddings (1, 1, 1, 1); Add row Pdfgridrow row = grid. Rows.Add (); Add a column grid. Columns.Add (2); Float width = page. Canvas.ClientSize.Width-(grid. Columns.count + 1); Sets the column width grid. Columns[0]. width = width * 0.1f; Grid. COLUMNS[1]. width = width * 0.1f; Load picture pdfgridcelltextandstylelist lst = new Pdfgridcelltextandstylelist (); Pdfgridcelltextandstyle Textandstyle = new Pdfgridcelltextandstyle (); Textandstyle.image=pdfimage.fromfile (@ "C:\Users\Administrator\Pictures\448a5ba8f8851709a1f53e.jpg"); Set Picture size textandstyle.imagesize = new SizeF (50, 50); Lst. List.add (Textandstyle); In the first cell, add a picture row. Cells[0]. Value = LST; Draws a PDF grid at a page-specific location pdflayoutresult result = Grid. Draw (page, new PointF (10, 30)); Save and run the PDF file doc. SaveToFile (OutputFile, fileformat.pdf); System.Diagnostics.Process.Start (OutputFile); } }}
How to create a PDF grid and insert pictures in C #