Forgive me to ask a question at the beginning of my post. What will you do when you need to insert an image that is in another PDF document and you neither can find this image on internet nor paste it directly to your own PDF? The answer must be you have to extract the image first, then, insert the image you want to your own PDF file.
This article is particularly designed to solve this problem. A method will be shown to extract images & text from PDF document and insert the extracted image to a newly built PDF file for WPF with C#, VB.NET. Please look at the below procedure.
Step1. Create a new project.
2. Create a new project in WPF Application.
3. Add a button in MainWindow and set the button Content property to be “Run”.
4. Add Spire.Pdf.Wpf.dll and System.Drawing as references.
This article is particularly designed to solve this problem. A method will be shown to extract images & text from PDF document and insert the extracted image to a newly built PDF file for WPF with C#, VB.NET. Please look at the below procedure.
Step1. Create a new project.
- Freely Download Spire.PDF for WPF.
2. Create a new project in WPF Application.
3. Add a button in MainWindow and set the button Content property to be “Run”.
4. Add Spire.Pdf.Wpf.dll and System.Drawing as references.
Step2. Extract images and text from PDF document.
//Create a pdf document.
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"D:\e-iceblue\Spire.PDF\Demos\Data\Sample2.pdf");
VB:
'Create a pdf document.
Dim doc As New PdfDocument()
doc.LoadFromFile("D:\e-iceblue\Spire.PDF\Demos\Data\Sample2.pdf")
2. Extract images and text from PDF document
- Load a PDF file from system.
//Create a pdf document.
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"D:\e-iceblue\Spire.PDF\Demos\Data\Sample2.pdf");
VB:
'Create a pdf document.
Dim doc As New PdfDocument()
doc.LoadFromFile("D:\e-iceblue\Spire.PDF\Demos\Data\Sample2.pdf")
2. Extract images and text from PDF document
C#:
StringBuilder buffer = new StringBuilder();
IList<System.Drawing.Image> images = new List<System.Drawing.Image>();
foreach (PdfPageBase page in doc.Pages)
{
buffer.Append(page.ExtractText());
foreach (System.Drawing.Image image in page.ExtractImages())
{
images.Add(image);
}
}
doc.Close();
VB:
Dim buffer As New StringBuilder()
Dim images As IList(Of System.Drawing.Image) = New List(Of System.Drawing.Image)()
For Each page As PdfPageBase In doc.Pages
buffer.Append(page.ExtractText())
For Each image As System.Drawing.Image In page.ExtractImages()
images.Add(image)
Next
Next
doc.Close()
3. Save the extracted images and text.
StringBuilder buffer = new StringBuilder();
IList<System.Drawing.Image> images = new List<System.Drawing.Image>();
foreach (PdfPageBase page in doc.Pages)
{
buffer.Append(page.ExtractText());
foreach (System.Drawing.Image image in page.ExtractImages())
{
images.Add(image);
}
}
doc.Close();
VB:
Dim buffer As New StringBuilder()
Dim images As IList(Of System.Drawing.Image) = New List(Of System.Drawing.Image)()
For Each page As PdfPageBase In doc.Pages
buffer.Append(page.ExtractText())
For Each image As System.Drawing.Image In page.ExtractImages()
images.Add(image)
Next
Next
doc.Close()
3. Save the extracted images and text.
C#:
//save text
String fileName = "TextInPdf.txt";
File.WriteAllText(fileName, buffer.ToString());
//save image
int index = 0;
foreach (System.Drawing.Image image in images)
{
String imageFileName
= String.Format("Image-{0}.png", index++);
image.Save(imageFileName, ImageFormat.Png);
}
VB:
'save text
Dim fileName As [String] = "TextInPdf.txt"
File.WriteAllText(fileName, buffer.ToString())
'save image
Dim index As Integer = 0
For Each image As System.Drawing.Image In images
Dim imageFileName As [String] = [String].Format("Image-{0}.png", System.Math.Max(System.Threading.Interlocked.Increment(index),index - 1))
image.Save(imageFileName, ImageFormat.Png)
Next
//save text
String fileName = "TextInPdf.txt";
File.WriteAllText(fileName, buffer.ToString());
//save image
int index = 0;
foreach (System.Drawing.Image image in images)
{
String imageFileName
= String.Format("Image-{0}.png", index++);
image.Save(imageFileName, ImageFormat.Png);
}
VB:
'save text
Dim fileName As [String] = "TextInPdf.txt"
File.WriteAllText(fileName, buffer.ToString())
'save image
Dim index As Integer = 0
For Each image As System.Drawing.Image In images
Dim imageFileName As [String] = [String].Format("Image-{0}.png", System.Math.Max(System.Threading.Interlocked.Increment(index),index - 1))
image.Save(imageFileName, ImageFormat.Png)
Next
Step3. Insert the extracted image to a newly built PDF file.
PdfDocument newDoc = new PdfDocument();
PdfPageBase newPage = newDoc.Pages.Add();
VB:
Dim newDoc As New PdfDocument()
Dim newPage As PdfPageBase = newDoc.Pages.Add()
- Create a new PDF document and add a page in it.
PdfDocument newDoc = new PdfDocument();
PdfPageBase newPage = newDoc.Pages.Add();
VB:
Dim newDoc As New PdfDocument()
Dim newPage As PdfPageBase = newDoc.Pages.Add()
2. Draw the PDF text. And insert the extracted the third image in the newly built PDF document.
C#:
newPage.Canvas.DrawString("Extract PDF images & text and insert an extracted image to a newly built PDF",
new PdfFont(PdfFontFamily.Helvetica, 14.5f),
new PdfSolidBrush(new PdfRGBColor(0,100,200)),
10, 40);
PdfImage img = PdfImage.FromImage(images[2]);
float width = img.Width * 0.75f;
float height = img.Height * 0.75f;
float x = (newPage.Canvas.ClientSize.Width - width) / 2;
newPage.Canvas.DrawImage(img, x, 100, width, height);
VB:
newPage.Canvas.DrawString("Extract PDF images & text and insert an extracted image to a newly built PDF",
New PdfFont(PdfFontFamily.Helvetica, 14.5F),
New PdfSolidBrush(New PdfRGBColor(0, 100, 200)), 10, 40)
Dim img As PdfImage = PdfImage.FromImage(images(2))
Dim width As Single = img.Width * 0.75F
Dim height As Single = img.Height * 0.75F
Dim x As Single = (newPage.Canvas.ClientSize.Width - width) / 2
newPage.Canvas.DrawImage(img, x, 100, width, height)
3. Save and launch the PDF file.
C#:
newDoc.SaveToFile("Image.pdf");
newDoc.Close();
System.Diagnostics.Process.Start("Image.pdf");
VB:
newDoc.SaveToFile("Image.pdf")
newDoc.Close()
System.Diagnostics.Process.Start("Image.pdf")
Preview
C#:
newPage.Canvas.DrawString("Extract PDF images & text and insert an extracted image to a newly built PDF",
new PdfFont(PdfFontFamily.Helvetica, 14.5f),
new PdfSolidBrush(new PdfRGBColor(0,100,200)),
10, 40);
PdfImage img = PdfImage.FromImage(images[2]);
float width = img.Width * 0.75f;
float height = img.Height * 0.75f;
float x = (newPage.Canvas.ClientSize.Width - width) / 2;
newPage.Canvas.DrawImage(img, x, 100, width, height);
VB:
newPage.Canvas.DrawString("Extract PDF images & text and insert an extracted image to a newly built PDF",
New PdfFont(PdfFontFamily.Helvetica, 14.5F),
New PdfSolidBrush(New PdfRGBColor(0, 100, 200)), 10, 40)
Dim img As PdfImage = PdfImage.FromImage(images(2))
Dim width As Single = img.Width * 0.75F
Dim height As Single = img.Height * 0.75F
Dim x As Single = (newPage.Canvas.ClientSize.Width - width) / 2
newPage.Canvas.DrawImage(img, x, 100, width, height)
3. Save and launch the PDF file.
C#:
newDoc.SaveToFile("Image.pdf");
newDoc.Close();
System.Diagnostics.Process.Start("Image.pdf");
VB:
newDoc.SaveToFile("Image.pdf")
newDoc.Close()
System.Diagnostics.Process.Start("Image.pdf")
Preview
Spire.PDF for WPF allows its users not only to extract images and text from PDF document, but also can save the images to the most popular formats such as .PNG, JPG, BMP, GIF and so on.
More about WPF Family
WPF family is made up of Spire.PDF for WPF, Spire.Doc for WPF, Spire.XLS for WPF andSpire.PDFViewer for WPF, which are all packed in Spire.Office for WPF. You can use this powerful pack to create, write and modify MS Word/XLS, PDF documents at your own will.
More about WPF Family
WPF family is made up of Spire.PDF for WPF, Spire.Doc for WPF, Spire.XLS for WPF andSpire.PDFViewer for WPF, which are all packed in Spire.Office for WPF. You can use this powerful pack to create, write and modify MS Word/XLS, PDF documents at your own will.