Knowledgebase (2345)
Children categories
Extract Image from PDF and Save it to New PDF File in C#
2013-12-12 07:44:14 Written by AdministratorExtracting image from PDF is not a complex operation, but sometimes we need to save the image to new PDF file. So the article describes the simple c# code to extract image from PDF and save it to new PDF file through a professional PDF .NET library Spire.PDF.
First we need to complete the preparatory work before the procedure:
- Download the Spire.PDF and install it on your machine.
- Add the Spire.PDF.dll files as reference.
- Open bin folder and select the three dll files under .NET 4.0.
- Right click property and select properties in its menu.
- Set the target framework as .NET 4.
- Add Spire.PDF as namespace.
Here is the whole code:
static void Main(string[] args)
{
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"..\..\Sample.pdf");
// Get the first page of the document
PdfPageBase page = doc.Pages[0];
// Create an instance of PdfImageHelper to work with images
PdfImageHelper imageHelper = new PdfImageHelper();
// Get information about the images on the page
PdfImageInfo[] imageInfos = imageHelper.GetImagesInfo(page);
// Extract images from the page
int index = 0;
foreach (PdfImageInfo info in imageInfos)
{
// Save each image as a PNG file with a unique name
info.Image.Save(string.Format("Image-{0}.png", index));
index++;
}
PdfImage image2 = PdfImage.FromFile(@"image-0.png");
PdfDocument doc2 = new PdfDocument();
PdfPageBase page2 = doc2.Pages.Add();
float width = image2.Width * 0.75f;
float height = image2.Height * 0.75f;
float x = (page.Canvas.ClientSize.Width - width) / 2;
page.Canvas.DrawImage(image2, x, 60, width, height);
doc2.SaveToFile(@"..\..\Image.pdf");
}
Here comes to the preview of the effect picture:


We have already demonstrated how to create form field. This article mainly shows you how developers fill form field in word document in C# only with 4 simple steps by using a standalone .NET Word component Spire.Doc.
Make sure Spire.Doc for .NET has been installed correctly and then add Spire.Doc.dll as reference in the downloaded Bin folder thought the below path: "..\Spire.Doc\Bin\NET4.0\ Spire.Doc.dll". Here comes to the details of how developers Fill Form Field by using Spire.Doc:
Step 1: Open the form that needs to fill the data.
//Create word document Document document = new Document(@"..\..\..\Data\UserForm.doc");
Step 2: Load data that will fill the form.
//Fill data from XML file
using (Stream stream = File.OpenRead(@"..\..\..\Data\User.xml"))
{
XPathDocument xpathDoc = new XPathDocument(stream);
XPathNavigator user = xpathDoc.CreateNavigator().SelectSingleNode("/user");
Step 3: Use the loaded data to fill the form.
//fill data
foreach (FormField field in document.Sections[0].Body.FormFields)
{
String path = String.Format("{0}/text()", field.Name);
XPathNavigator propertyNode = user.SelectSingleNode(path);
if (propertyNode != null)
{
switch (field.Type)
{
case FieldType.FieldFormTextInput:
field.Text = propertyNode.Value;
break;
case FieldType.FieldFormDropDown:
DropDownFormField combox = field as DropDownFormField;
for(int i = 0; i < combox.DropDownItems.Count; i++)
{
if (combox.DropDownItems[i].Text == propertyNode.Value)
{
combox.DropDownSelectedIndex = i;
break;
}
if (field.Name == "country" && combox.DropDownItems[i].Text == "Others")
{
combox.DropDownSelectedIndex = i;
}
}
break;
case FieldType.FieldFormCheckBox:
if (Convert.ToBoolean(propertyNode.Value))
{
CheckBoxFormField checkBox = field as CheckBoxFormField;
checkBox.Checked = true;
}
break;
}
}
}
}
Step 4: Save the document to file in XML or Microsoft Word format.
//Save doc file
document.SaveToFile("Sample.doc",FileFormat.Doc);
Effective Screenshot:
Full Source Code for Fill FormField:
namespace FillFormField
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//open form
Document document = new Document(@"..\..\..\..\..\..\Data\UserForm.doc");
//load data
using (Stream stream = File.OpenRead(@"..\..\..\..\..\..\Data\User.xml"))
{
XPathDocument xpathDoc = new XPathDocument(stream);
XPathNavigator user = xpathDoc.CreateNavigator().SelectSingleNode("/user");
//fill data
foreach (FormField field in document.Sections[0].Body.FormFields)
{
String path = String.Format("{0}/text()", field.Name);
XPathNavigator propertyNode = user.SelectSingleNode(path);
if (propertyNode != null)
{
switch (field.Type)
{
case FieldType.FieldFormTextInput:
field.Text = propertyNode.Value;
break;
case FieldType.FieldFormDropDown:
DropDownFormField combox = field as DropDownFormField;
for(int i = 0; i < combox.DropDownItems.Count; i++)
{
if (combox.DropDownItems[i].Text == propertyNode.Value)
{
combox.DropDownSelectedIndex = i;
break;
}
if (field.Name == "country" && combox.DropDownItems[i].Text == "Others")
{
combox.DropDownSelectedIndex = i;
}
}
break;
case FieldType.FieldFormCheckBox:
if (Convert.ToBoolean(propertyNode.Value))
{
CheckBoxFormField checkBox = field as CheckBoxFormField;
checkBox.Checked = true;
}
break;
}
}
}
}
//Save doc file.
document.SaveToFile("Sample.doc",FileFormat.Doc);
//Launching the MS Word file.
WordDocViewer("Sample.doc");
}
private void WordDocViewer(string fileName)
{
try
{
System.Diagnostics.Process.Start(fileName);
}
catch { }
}
}
}