I use the library spire.pdf to convert a pdf file into pictures, do it through the code available in the documentation. Generates a PDF file to image because he wants them to print, and PDF file itself can not print (I mean the printer)
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Spire.Pdf.PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("sample.pdf");
BitmapSource source;
Bitmap bmp;
for (int i = 1; i < pdf.Pages.Count+1; i++)
{
source = pdf.SaveAsImage(i);
bmp = SourceToBitmap(source);
bmp.Save(string.Format("result-{0}.png", i), ImageFormat.Png);
}
}
private Bitmap SourceToBitmap(BitmapSource source)
{
Bitmap bmp;
using (MemoryStream ms = new MemoryStream())
{
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(source));
encoder.Save(ms);
bmp = new Bitmap(ms);
}
return bmp;
}
}
My includes:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Spire.Pdf;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Media.Imaging;
using System.IO;
but I get an error from source = pdf.SaveAsImage(i);
Error 1 Cannot implicitly convert type 'System.Drawing.Image' to 'System.Windows.Media.Imaging.BitmapSource' c:\users\łukasz\documents\visual studio 2013\Projects\WpfApplication5\WpfApplication5\MainWindow.xaml.cs 39 26 WpfApplication5
Apparently PdfDocument.SaveAsImage
returns a System.Drawing.Image
, so you don't need to do any WPF to WinForms bitmap conversion.
This should be sufficient:
var source = pdf.SaveAsImage(i);
source.Save(string.Format("result-{0}.png", i), ImageFormat.Png);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.