Torna al Thread
using PdfSharp;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using PdfSharp.Drawing;
private void esportaimmagine()
{
const string filename = @"C:\log\miofile.pdf";
PdfDocument document = PdfReader.Open(filename);
int imageCount = 0;
foreach (PdfPage page in document.Pages)
{
PdfDictionary resources = page.Elements.GetDictionary("/Resources");
if (resources != null)
{
PdfDictionary xObjects = resources.Elements.GetDictionary("/XObject");
if (xObjects != null)
{
ICollection<PdfItem> items = xObjects.Elements.Values;
foreach (PdfItem item in items)
{
PdfSharp.Pdf.Advanced.PdfReference reference = (PdfSharp.Pdf.Advanced.PdfReference)item;
if (reference != null)
{
PdfDictionary xObject = reference.Value as PdfDictionary;
if (xObject != null && xObject.Elements.GetString("/Subtype") == "/Image")
{
ExportJpegImage(xObject, ref imageCount);
}
}
}
}
}
}
System.Windows.Forms. MessageBox.Show(imageCount + " images exported.", "Export Images");
}
void ExportJpegImage(PdfDictionary image, ref int count)
{
byte[] stream = image.Stream.Value;
FileStream fs = new FileStream(String.Format(@"c:\log\Image{0}.jpeg", count++), FileMode.Create, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(stream);
bw.Close();
}