горизонтально расположенные полосы: белая, синяя, красная

Как конвертировать PDF в Images на C# и VB.NET

ASP.NET - Экспорт PDF в JPEG

Это ASP.NET-приложение преобразует 1-ю страницу PDF-документа в JPEG-изображение с качеством 200 точек на дюйм. Процесс преобразования полностью выполняется в памяти с использованием массива байтов.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Result.Text = "";
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.PostedFile.FileName.Length == 0 || FileUpload1.FileBytes.Length == 0)
        {
            Result.Text = "Please select PDF file at first!";
            return;
        }
        Result.Text = "Converting ...";

        SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();
        //this property is necessary only for registered version
        //f.Serial = "XXXXXXXXXXX";

        f.OpenPdf(FileUpload1.FileBytes);
        if (f.PageCount > 0)
        {
            //set image properties
            f.ImageOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
            f.ImageOptions.Dpi = 200;

            //Let's convert 1st page from PDF document
            byte[] image = f.ToImage(1);

            //show image
            Response.Buffer = true;
            Response.Clear();
            Response.ContentType = "image/jpeg";
            Response.AddHeader("content-disposition", "attachment; filename=Page1.jpg");
            Response.BinaryWrite(image);
            Response.Flush();
            Response.End();
        }
        else
        {
            Result.Text = "Converting failed!";
        }
    }
}
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.IO

Partial Public Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        Result.Text = ""
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        If FileUpload1.PostedFile.FileName.Length = 0 OrElse FileUpload1.FileBytes.Length = 0 Then
            Result.Text = "Please select PDF file at first!"
            Return
        End If
        Dim f As New SautinSoft.PdfFocus()

        'this property is necessary only for registered version
        'f.Serial = "XXXXXXXXXXX"
        f.OpenPdf(FileUpload1.FileBytes)
        If f.PageCount > 0 Then
            'set image properties
            f.ImageOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg
            f.ImageOptions.Dpi = 200
            
            'Let's convert 1st page from PDF document
            Dim image() As Byte = f.ToImage(1)

            'show image
            Response.Buffer = True
            Response.Clear()
            Response.ContentType = "image/jpeg"
            Response.AddHeader("content-disposition", "attachment; filename=Page1.jpg")
            Response.BinaryWrite(image)
            Response.Flush()
            Response.End()
        Else
            Result.Text = "Converting failed!"
        End If
    End Sub
End Class

ASP.NET - Экспорт PDF в многостраничный TIFF

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Result.Text = "";
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.PostedFile.FileName.Length == 0 || FileUpload1.FileBytes.Length == 0)
        {
            Result.Text = "Please select PDF file at first!";
            return;
        }
        Result.Text = "Converting ...";

        SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();
        //this property is necessary only for registered version
        //f.Serial = "XXXXXXXXXXX";

        f.OpenPdf(FileUpload1.FileBytes);
        if (f.PageCount > 0)
        {
            //Set dpi for TIFF image
            f.ImageOptions.Dpi = 120;

            //Let's whole PDF into multipage-TIFF
            byte[] tiff = f.ToMultipageTiff();

            //show image
            Response.Buffer = true;
            Response.Clear();
            Response.ContentType = "image/tiff";
            Response.AddHeader("content-disposition", "attachment; filename=Result.tiff");
            Response.BinaryWrite(tiff);
            Response.Flush();
            Response.End();
        }
        else
        {
            Result.Text = "Converting failed!";
        }
    }
}
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.IO

Partial Public Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        Result.Text = ""
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        If FileUpload1.PostedFile.FileName.Length = 0 OrElse FileUpload1.FileBytes.Length = 0 Then
            Result.Text = "Please select PDF file at first!"
            Return
        End If
        Dim f As New SautinSoft.PdfFocus()
        'this property is necessary only for registered version
        'f.Serial = "XXXXXXXXXXX"

        f.OpenPdf(FileUpload1.FileBytes)
        If f.PageCount > 0 Then
            'Set dpi for TIFF image
            f.ImageOptions.Dpi = 120

            'Let's whole PDF into multipage-TIFF
            Dim tiff() As Byte = f.ToMultipageTiff()

            'show image
            Response.Buffer = True
            Response.Clear()
            Response.ContentType = "image/tiff"
            Response.AddHeader("content-disposition", "attachment; filename=Result.tiff")
            Response.BinaryWrite(tiff)
            Response.Flush()
            Response.End()
        Else
            Result.Text = "Converting failed!"
        End If
    End Sub
End Class

ASP.NET - Просмотр всех PDF-страниц

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Collections;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Result.Text = "";
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.PostedFile.FileName.Length == 0 || FileUpload1.FileBytes.Length == 0)
        {
            Result.Text = "Please select PDF file at first!";
            return;
        }

        SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();
        //this property is necessary only for registered version
        //f.Serial = "XXXXXXXXXXX";

        f.OpenPdf(FileUpload1.FileBytes);
        if (f.PageCount > 0)
        {
            //set image properties
            f.ImageOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Png;
            f.ImageOptions.Dpi = 72;

            //Let's convert whole PDF document
            ArrayList pages = f.ToImage();

            //Show images
            if (pages.Count > 0)
            {
                int width = 3;
                int imgWidth = 300;

                HtmlTable table = new HtmlTable();
                table.Border = 1;
                table.CellPadding = 3;
                table.CellSpacing = 3;

                HtmlTableRow row;
                HtmlTableCell cell;
                HtmlImage img;

                string imagePath = Server.MapPath("~");
                string imageName = "Page";

                row = new HtmlTableRow();
                int count = 0;

                foreach (byte[] page in pages)
                {
                    count++;
                    string src = imageName + count.ToString() + ".png";
                    File.WriteAllBytes(Path.Combine(imagePath, src), page);
                    cell = new HtmlTableCell();
                    cell.Style.Add("vertical-align","top");
                    img = new HtmlImage();
                    img.Src = src;
                    img.Width = imgWidth;
                    cell.InnerHtml = "<div align=\"center\">Page" + count.ToString() + "</div>";
                    cell.Controls.Add(img);
                    row.Cells.Add(cell);
                    if (count % width == 0)
                    {
                        table.Rows.Add(row);
                        row = new HtmlTableRow();
                    }
                }
                table.Rows.Add(row);
                this.Controls.Add(table);
            }
        }
        else
        {
            Result.Text = "Converting failed!";
        }
    }
}
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.IO

Partial Public Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        Result.Text = ""
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        If FileUpload1.PostedFile.FileName.Length = 0 OrElse FileUpload1.FileBytes.Length = 0 Then
            Result.Text = "Please select PDF file at first!"
            Return
        End If
        Dim f As New SautinSoft.PdfFocus()
        'this property is necessary only for registered version
        'f.Serial = "XXXXXXXXXXX"

        f.OpenPdf(FileUpload1.FileBytes)
        If f.PageCount > 0 Then
            'set image properties
            f.ImageOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Png
            f.ImageOptions.Dpi = 72

            'Let's convert whole PDF document
            Dim pages As ArrayList = f.ToImage()

            'Show images
            If pages.Count > 0 Then
                Dim width As Integer = 3
                Dim imgWidth As Integer = 300

                Dim table As New HtmlTable()
                table.Border = 1
                table.CellPadding = 3
                table.CellSpacing = 3

                Dim row As HtmlTableRow
                Dim cell As HtmlTableCell
                Dim img As HtmlImage
                Dim imagePath As String = Server.MapPath("~")
                Dim imageName As String = "Page"

                row = New HtmlTableRow()
                Dim count As Integer = 0
                For Each page As Byte() In pages
                    count += 1
                    Dim src As String = imageName & count.ToString() & ".png"
                    File.WriteAllBytes(Path.Combine(imagePath, src), page)

                    cell = New HtmlTableCell()
                    cell.Style.Add("vertical-align", "top")
                    img = New HtmlImage()
                    img.Src = src
                    img.Width = imgWidth
                    cell.InnerHtml = "<div align=""center"">Page" & count.ToString() & "</div>"
                    cell.Controls.Add(img)
                    row.Cells.Add(cell)
                    If count Mod width = 0 Then
                        table.Rows.Add(row)
                        row = New HtmlTableRow()
                    End If
                Next page
                table.Rows.Add(row)
                Me.Controls.Add(table)
            End If
            Else
                Result.Text = "Converting failed!"
        End If
    End Sub
End Class

Конвертирование обычной PDF-страницы в объект System.Drawing.Image

using System;
using System.IO;
namespace Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            //Convert custom PDF page to Image object
            SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();
            //this property is necessary only for registered version
            //f.Serial = "XXXXXXXXXXX";

            string pdfPath = @"..\..\simple text.pdf";
            string imagePath = Path.ChangeExtension(pdfPath, ".png");

            f.OpenPdf(pdfPath);
            if (f.PageCount > 0)
            {
                //Let's convert 1st page into System.Drawing.Image object, 120 dpi
                f.ImageOptions.Dpi = 120;
                System.Drawing.Image img = f.ToDrawingImage(1);

                //Save to file
                if (img != null)
                {
                    img.Save(imagePath);
                    System.Diagnostics.Process.Start(imagePath);
                }
            }
        }
    }
}
Imports System.IO
Imports System.Drawing.Imaging
Imports System.Collections.Generic
Imports SautinSoft

Module Sample
    Sub Main()
        'Convert custom PDF page to Image object
        Dim f As New SautinSoft.PdfFocus()
        'this property is necessary only for registered version
        'f.Serial = "XXXXXXXXXXX"

        Dim pdfPath As String = "..\simple text.pdf"
        Dim imagePath As String = Path.ChangeExtension(pdfPath, ".png")

        f.OpenPdf(pdfPath)
        If f.PageCount > 0 Then
            'Let's convert 1st page into System.Drawing.Image object, 120 dpi
            f.ImageOptions.Dpi = 120
            Dim img As System.Drawing.Image = f.ToDrawingImage(1)

            'Save to file
            If img IsNot Nothing Then
                img.Save(imagePath)
                System.Diagnostics.Process.Start(imagePath)
            End If
        End If
    End Sub
End Module

Конвертирование 1-ой страницы PDF-файла в PNG

using System;
using System.IO;
namespace Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            // Convert PDF 1st page to PNG file.
            SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();
            // this property is necessary only for registered version.
            //f.Serial = "XXXXXXXXXXX";

            string pdfPath = @"..\..\simple text.pdf";
            string imagePath = Path.ChangeExtension(pdfPath, ".png");

            f.OpenPdf(pdfPath);
            if (f.PageCount > 0)
            {
                //save 1st page to png file, 120 dpi
                f.ImageOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Png;
                f.ImageOptions.Dpi = 120;
                if (f.ToImage(imagePath, 1) == 0)
                {
                    // 0 - converting successfully
                    // 2 - can't create output file, check the output path
                    // 3 - converting failed
                    System.Diagnostics.Process.Start(imagePath);
                }
            }
        }
    }
}
Imports System.IO
Imports System.Drawing.Imaging
Imports System.Collections.Generic
Imports SautinSoft

Module Sample
    Sub Main()
        'Convert PDF 1st page to PNG file
        Dim f As New SautinSoft.PdfFocus()
        'this property is necessary only for registered version
        'f.Serial = "XXXXXXXXXXX"

        Dim pdfPath As String = "..\simple text.pdf"
        Dim imagePath As String = Path.ChangeExtension(pdfPath, ".png")

        f.OpenPdf(pdfPath)
        If f.PageCount > 0 Then
            'save 1st page to png file, 120 dpi
            f.ImageOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Png
            f.ImageOptions.Dpi = 120
            If f.ToImage(imagePath, 1) = 0 Then
                ' 0 - converting successfully
                ' 2 - can't create output file, check the output path
                ' 3 - converting failed
                System.Diagnostics.Process.Start(imagePath)
            End If
        End If
    End Sub
End Module

Конвертирование PDF-файла в черно-белый многостраничный TIFF

using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            // Convert PDF file to BlackAndWhite Multipage-TIFF.
            SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();

            // This property is necessary only for registered version.
            //f.Serial = "XXXXXXXXXXX";

            string pdfPath = @"..\..\simple text.pdf";
            string tiffPath = Path.ChangeExtension(pdfPath, ".tiff");

            f.OpenPdf(pdfPath);
            if (f.PageCount > 0)
            {
                f.ImageOptions.Dpi = 120;
                f.ImageOptions.ColorDepth = SautinSoft.PdfFocus.CImageOptions.eColorDepth.BlackWhite1bpp;

                // EncoderValue.CompressionCCITT4 - also makes image black&white 1 bit
                if (f.ToMultipageTiff(tiffPath, EncoderValue.CompressionCCITT4) == 0)
                {
                    System.Diagnostics.Process.Start(tiffPath);
                }
            }
        }
    }
}
Imports System.IO
Imports System.Drawing.Imaging
Imports System.Collections.Generic
Imports SautinSoft

Module Sample
    Sub Main()
        ' Convert PDF file to BlackAndWhite Multipage-TIFF.
        Dim f As New SautinSoft.PdfFocus()

        ' This property is necessary only for registered version.
        'f.Serial = "XXXXXXXXXXX"

        Dim pdfPath As String = "..\simple text.pdf"
        Dim tiffPath As String = Path.ChangeExtension(pdfPath, ".png")

        f.OpenPdf(pdfPath)
        If f.PageCount > 0 Then
            f.ImageOptions.Dpi = 120
            f.ImageOptions.ColorDepth = SautinSoft.PdfFocus.CImageOptions.eColorDepth.BlackWhite1bpp
            ' EncoderValue.CompressionCCITT4 - also makes image black&white 1 bit
            If f.ToMultipageTiff(tiffPath, EncoderValue.CompressionCCITT4) = 0 Then
                System.Diagnostics.Process.Start(tiffPath)
            End If
        End If
    End Sub
End Module

Конвертирование PDF-файла в многостраничный TIFF

using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            //Convert PDF file to Multipage TIFF file
            SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();
            //this property is necessary only for registered version
            //f.Serial = "XXXXXXXXXXX";

            string pdfPath = @"..\..\simple text.pdf";
            string tiffPath = Path.ChangeExtension(pdfPath, ".tiff");

            f.OpenPdf(pdfPath);
            if (f.PageCount > 0)
            {
                f.ImageOptions.Dpi = 120;
                if (f.ToMultipageTiff(tiffPath) == 0)
                {
                    System.Diagnostics.Process.Start(tiffPath);
                }
            }
        }
    }
}
Imports System.IO
Imports System.Drawing.Imaging
Imports System.Collections.Generic
Imports SautinSoft

Module Sample
    Sub Main()
        'Convert PDF file to Multipage TIFF file
        Dim f As New SautinSoft.PdfFocus()
        'this property is necessary only for registered version
        'f.Serial = "XXXXXXXXXXX"

        Dim pdfPath As String = "..\simple text.pdf"
        Dim tiffPath As String = Path.ChangeExtension(pdfPath, ".tiff")

        f.OpenPdf(pdfPath)
        If f.PageCount > 0 Then
            f.ImageOptions.Dpi = 120
            If f.ToMultipageTiff(tiffPath) = 0 Then
                System.Diagnostics.Process.Start(tiffPath)
            End If
        End If
    End Sub
End Module

Конвертирование PDF-файлов в TIFF с разрешением 300 точек на дюйм

using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            //Convert PDF files to 300-dpi TIFF files
            SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();
            //this property is necessary only for registered version
            //f.Serial = "XXXXXXXXXXX";

            string[] pdfFiles = Directory.GetFiles(@"..\..\", "*.pdf");
            string folderWithTiffs = @"..\..\";

            foreach (string pdffile in pdfFiles)
            {
                f.OpenPdf(pdffile);
                if (f.PageCount > 0)
                {
                    //Set image format: TIFF, 300 dpi
                    f.ImageOptions.Dpi = 300;
                    f.ImageOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Tiff;

                    //Save all pages to tiff files with 300 dpi
                    f.ToImage(folderWithTiffs, Path.GetFileNameWithoutExtension(pdffile));
                }
                f.ClosePdf();
            }
            //Show folder with tiffs
            System.Diagnostics.Process.Start(folderWithTiffs);
        }
    }
}
Imports System.IO
Imports System.Drawing.Imaging
Imports System.Collections.Generic
Imports SautinSoft

Module Sample
    Sub Main()
        'Convert PDF files to 300-dpi TIFF files
        Dim f As New SautinSoft.PdfFocus()
        'this property is necessary only for registered version
        'f.Serial = "XXXXXXXXXXX"

        Dim pdfFiles() As String = Directory.GetFiles("..\", "*.pdf")
        Dim folderWithTiffs As String = "..\"

        For Each pdffile As String In pdfFiles
        f.OpenPdf(pdffile)
        If f.PageCount > 0 Then
            'Set image format: TIFF, 300 dpi
            f.ImageOptions.Dpi = 300
            f.ImageOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Tiff

            'Save all pages to tiff files with 300 dpi
            f.ToImage(folderWithTiffs, Path.GetFileNameWithoutExtension(pdffile))
        End If
        f.ClosePdf()
        Next pdffile

        'Show folder with tiffs
        System.Diagnostics.Process.Start(folderWithTiffs)
    End Sub
End Module

Конвертирование PDF в изображения с заданной высотой и шириной

using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            //Convert PDF into specified Image height & width
            SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();
            //this property is necessary only for registered version
            //f.Serial = "XXXXXXXXXXX";

            // Set initial values
            string pdfPath = @"..\..\simple text.pdf";
            string imageFolder = Path.GetDirectoryName(pdfPath);
            int width = 1600; // Width in Px
            int height = 1900; // Height in Px

            //Set image options
            f.ImageOptions.ImageFormat = ImageFormat.Png;
            f.ImageOptions.Resize(new Size { Width = width, Height = height }, false);
            f.OpenPdf(pdfPath);
            if (f.PageCount > 0)
            {
                // Convert all pages to PNG images
                f.ToImage(imageFolder, "Page");

                //Show image
                System.Diagnostics.Process.Start(imageFolder);
            }
        }
    }
}
Imports System.IO
Imports System.Drawing.Imaging
Imports System.Collections.Generic
Imports System.Drawing
Imports SautinSoft

Module Sample
    Sub Main()
        'Convert PDF into specified Image height & width
        Dim f As New SautinSoft.PdfFocus()
        'this property is necessary only for registered version
        'f.Serial = "XXXXXXXXXXX"

        ' Set initial values
        Dim pdfPath As String = "..\simple text.pdf"
        Dim imageFolder As String = Path.GetDirectoryName(pdfPath)
        Dim width As Integer = 1600 ' Width in Px
        Dim height As Integer = 1900 ' Height in Px

        'Set image options
        f.ImageOptions.ImageFormat = ImageFormat.Png
        f.ImageOptions.Resize(New Size With {.Width = width, .Height = height}, False)

        f.OpenPdf(pdfPath)
        If f.PageCount > 0 Then
            ' Convert all pages to PNG images
            f.ToImage(imageFolder, "Page")

            'Show image
            System.Diagnostics.Process.Start(imageFolder)
        End If
    End Sub
End Module

Конвертирование PDF в 1-битный черно-белый PNG

using System;
using System.IO;
namespace Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            //How to convert PDF to 1-bit black and white PNG
            SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();
            //this property is necessary only for registered version
            //f.Serial = "XXXXXXXXXXX";

            string pdfPath = @"..\..\simple text.pdf";
            string imagePath = Path.ChangeExtension(pdfPath, ".png");

            f.OpenPdf(pdfPath);
            if (f.PageCount > 0)
            {
                //save 1st page to png file, 120 dpi
                f.ImageOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Png;
                f.ImageOptions.Dpi = 120;

                //Make "Black and White 1-bit indexed" image
                f.ImageOptions.ColorDepth = SautinSoft.PdfFocus.CImageOptions.eColorDepth.BlackWhite1bpp;
                if (f.ToImage(imagePath, 1) == 0)
                {
                    // 0 - converting successfully
                    // 2 - can't create output file, check the output path
                    // 3 - converting failed
                    System.Diagnostics.Process.Start(imagePath);
                }
            }
        }
    }
}
Imports System.IO
Imports System.Drawing.Imaging
Imports System.Collections.Generic
Imports SautinSoft

Module Sample
    Sub Main()
        'How to convert PDF to 1-bit black and white PNG
        Dim f As New SautinSoft.PdfFocus()
        'this property is necessary only for registered version
        'f.Serial = "XXXXXXXXXXX"

        Dim pdfPath As String = "..\simple text.pdf"
        Dim imagePath As String = Path.ChangeExtension(pdfPath, ".png")

        f.OpenPdf(pdfPath)
        If f.PageCount > 0 Then
            'save 1st page to png file, 120 dpi
            f.ImageOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Png
            f.ImageOptions.Dpi = 120

            'Make "Black and White 1-bit indexed" image
            f.ImageOptions.ColorDepth = SautinSoft.PdfFocus.CImageOptions.eColorDepth.BlackWhite1bpp
            If f.ToImage(imagePath, 1) = 0 Then
                ' 0 - converting successfully
                ' 2 - can't create output file, check the output path
                ' 3 - converting failed
                System.Diagnostics.Process.Start(imagePath)
            End If
        End If
    End Sub
End Module

Конвертирование PDF в JPG с высоким качеством

using System;
using System.IO;
namespace Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            // Convert PDF to JPG with high Quality
            SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();

            // This property is necessary only for registered version
            // f.Serial = "XXXXXXXXXXX";
            string pdfFile = @"..\..\simple text.pdf";
            string jpegDir = Path.GetDirectoryName(pdfFile);

            f.OpenPdf(pdfFile);
            if (f.PageCount > 0)
            {
                // Set image properties: Jpeg, 200 dpi
                f.ImageOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
                f.ImageOptions.Dpi = 200;

                // Set 95 as JPEG quality
                f.ImageOptions.JpegQuality = 95;

                //Save all PDF pages to image folder, each file will have name Page 1.jpg, Page 2.jpg, Page N.jpg
                for (int page = 1; page <= f.PageCount; page++)
                {
                    string jpegFile = Path.Combine(jpegDir, String.Format("Page {0}.jpg", page));

                    // 0 - converted successfully
                    // 2 - can't create output file, check the output path
                    // 3 - conversion failed
                    int result = f.ToImage(jpegFile, page);

                    // Show only 1st page
                    if (page == 1 && result == 0)
                        System.Diagnostics.Process.Start(jpegFile);
                }
            }
        }
    }
}
Imports System.IO
Imports System.Drawing.Imaging
Imports System.Collections.Generic
Imports SautinSoft

Module Sample
    Sub Main()
        ' Convert PDF to JPG with high Quality
        Dim f As New SautinSoft.PdfFocus()

        ' This property is necessary only for registered version
        ' f.Serial = "XXXXXXXXXXX"
        Dim pdfFile As String = "..\simple text.pdf"
        Dim jpegDir As String = Path.GetDirectoryName(pdfFile)

        f.OpenPdf(pdfFile)
        If f.PageCount > 0 Then
            ' Set image properties: Jpeg, 200 dpi
            f.ImageOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg
            f.ImageOptions.Dpi = 200

            ' Set 95 as JPEG quality
            f.ImageOptions.JpegQuality = 95

            'Save all PDF pages to image folder, each file will have name Page 1.jpg, Page 2.jpg, Page N.jpg
            For page As Integer = 1 To f.PageCount
                Dim jpegFile As String = Path.Combine(jpegDir, String.Format("Page {0}.jpg", page))
                ' 0 - converted successfully
                ' 2 - can't create output file, check the output path
                ' 3 - conversion failed
                Dim result As Integer = f.ToImage(jpegFile, page)

                ' Show only 1st page
                If page = 1 AndAlso result = 0 Then
                    System.Diagnostics.Process.Start(jpegFile)
                End If
            Next page
        End If
    End Sub
End Module

Конвертирование PDF в PNG в многопоточном режиме

using System;
using System.IO;
using System.Collections.Generic;
using System.Threading;
using SautinSoft;

namespace Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            ConvertPdfToPngInThread();
        }
        public class TArgument
        {
            public string PdfFile { get; set; }
            public int PageNumber { get; set; }
        }
        public static void ConvertPdfToPngInThread()
        {
            string pdfs = @"..\..\";
            string[] files = Directory.GetFiles(pdfs, "*.pdf");
            List<Thread> threads = new List<Thread>();
            for (int i = 0; i < files.Length; i++)
            {
                TArgument targ = new TArgument()
                {
                    PdfFile = files[i],
                    PageNumber = 1
                };
                var t = new Thread((a) => ConvertToPng(a));
                t.Start(targ);
                threads.Add(t);
            }
            foreach (var thread in threads)
                thread.Join();
            Console.WriteLine("Done.");
            Console.ReadLine();
        }

        public static void ConvertToPng(object targ)
        {
            TArgument targum = (TArgument)targ;
            string pdfFile = targum.PdfFile;
            int page = targum.PageNumber;

            string pngFile = Path.ChangeExtension(pdfFile, ".png");
            SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();

            f.ImageOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Png;
            f.ImageOptions.Dpi = 300;
            f.OpenPdf(pdfFile);
            bool done = false;
            if (f.PageCount > 0)
            {
                if (page >= f.PageCount)
                    page = 1;
                if (f.ToImage(pngFile, page) == 0)
                    done = true;
                f.ClosePdf();
            }
            if (done)
                Console.WriteLine("{0}\t - Done!", Path.GetFileName(pdfFile));
            else
                Console.WriteLine("{0}\t - Error!", Path.GetFileName(pdfFile));
        }
    }
}
Imports System.IO
Imports System.Drawing.Imaging
Imports System.Collections.Generic
Imports System.Threading
Imports SautinSoft

Module Sample
    Sub Main()
        ConvertPdfToPngInThread()
    End Sub

    Public Class TArgument
        Public Property PdfFile() As String
        Public Property PageNumber() As Integer
    End Class

    Public Sub ConvertPdfToPngInThread()
        Dim pdfs As String = "..\"
        Dim files() As String = Directory.GetFiles(pdfs, "*.pdf")
        Dim threads As New List(Of Thread)()
        For i As Integer = 0 To files.Length - 1
            Dim targ As New TArgument() With {
                .PdfFile = files(i),
                .PageNumber = 1}
            Dim t = New Thread(Sub(a) ConvertToPng(a))
            t.Start(targ)
            threads.Add(t)
        Next i
        For Each t As Thread In threads
            t.Join()
        Next t
        Console.WriteLine("Done.")
        Console.ReadLine()
    End Sub

    Public Sub ConvertToPng(ByVal targ As Object)
        Dim targum As TArgument = DirectCast(targ, TArgument)
        Dim pdfFile As String = targum.PdfFile
        Dim page As Integer = targum.PageNumber
        Dim pngFile As String = Path.ChangeExtension(pdfFile, ".png")
        Dim f As New SautinSoft.PdfFocus()

        f.ImageOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Png
        f.ImageOptions.Dpi = 300
        f.OpenPdf(pdfFile)

        Dim done As Boolean = False
        If f.PageCount > 0 Then
            If page >= f.PageCount Then
                page = 1
            End If
            If f.ToImage(pngFile, page) = 0 Then
                done = True
            End If
            f.ClosePdf()
        End If
        If done Then
            Console.WriteLine("{0}" & ControlChars.Tab & " - Done!", Path.GetFileName(pdfFile))
        Else
            Console.WriteLine("{0}" & ControlChars.Tab & " - Error!", Path.GetFileName(pdfFile))
        End If
    End Sub
End Module

Установите глубину цвета - 24-битные оттенки серого

using System;
using System.IO;
using System.Collections;
namespace Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            string pdfPath = @"..\..\simple text.pdf";
            string imagePath = Path.ChangeExtension(pdfPath, ".png");

            SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();
            //this property is necessary only for registered version
            //f.Serial = "XXXXXXXXXXX";

            f.OpenPdf(pdfPath);
            if (f.PageCount > 0)
            {
                //Set color depth: Grayscale 24 bit
                f.ImageOptions.ColorDepth = SautinSoft.PdfFocus.CImageOptions.eColorDepth.Grayscale24bpp;

                //Convert 1st page from PDF to image file
                if (f.ToImage(imagePath, 1) == 0)
                {
                    // 0 - converting successfully
                    // 2 - can't create output file, check the output path
                    // 3 - converting failed
                    System.Diagnostics.Process.Start(imagePath);
                }
            }
        }
    }
}
Imports System.IO
Imports System.Drawing.Imaging
Imports System.Collections.Generic
Imports SautinSoft

Module Sample
    Sub Main()
        Dim pdfPath As String = "..\simple text.pdf"
        Dim imagePath As String = Path.ChangeExtension(pdfPath, ".png")

        Dim f As New SautinSoft.PdfFocus()
        'this property is necessary only for registered version
        'f.Serial = "XXXXXXXXXXX"

        f.OpenPdf(pdfPath)
        If f.PageCount > 0 Then
            'Set color depth: Grayscale 24 bit
            f.ImageOptions.ColorDepth = SautinSoft.PdfFocus.CImageOptions.eColorDepth.Grayscale24bpp

            'Convert 1st page from PDF to image file
            If f.ToImage(imagePath, 1) = 0 Then
                ' 0 - converting successfully
                ' 2 - can't create output file, check the output path
                ' 3 - converting failed
                System.Diagnostics.Process.Start(imagePath)
            End If
        End If
    End Sub
End Module

Установите нужное количество точек на дюйм

using System;
using System.IO;
using System.Collections;
namespace Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            string pdfPath = @"..\..\simple text.pdf";
            string imagePath = Path.ChangeExtension(pdfPath, ".png");

            SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();
            //this property is necessary only for registered version
            //f.Serial = "XXXXXXXXXXX";

            f.OpenPdf(pdfPath);
            if (f.PageCount > 0)
            {
                //In most cases we recommend to set 120 dpi to decrease the image size and converting speed
                //Now set 300 dpi - very high quality
                f.ImageOptions.Dpi = 300;

                //Convert 1st page from PDF to image file
                if (f.ToImage(imagePath, 1) == 0)
                {
                    // 0 - converting successfully
                    // 2 - can't create output file, check the output path
                    // 3 - converting failed
                    System.Diagnostics.Process.Start(imagePath);
                }
            }
        }
    }
}
Imports System.IO
Imports System.Drawing.Imaging
Imports System.Collections.Generic
Imports SautinSoft

Module Sample
    Sub Main()
        Dim pdfPath As String = "..\simple text.pdf"
        Dim imagePath As String = Path.ChangeExtension(pdfPath, ".png")

        Dim f As New SautinSoft.PdfFocus()
        'this property is necessary only for registered version
        'f.Serial = "XXXXXXXXXXX"

        f.OpenPdf(pdfPath)
        If f.PageCount > 0 Then
            'In most cases we recommend to set 120 dpi to decrease the image size and converting speed
            'Now set 300 dpi - very high quality
            f.ImageOptions.Dpi = 300

            'Convert 1st page from PDF to image file
            If f.ToImage(imagePath, 1) = 0 Then
                ' 0 - converting successfully
                ' 2 - can't create output file, check the output path
                ' 3 - converting failed
                System.Diagnostics.Process.Start(imagePath)
            End If
        End If
    End Sub
End Module

Установите формат изображения

using System;
using System.IO;
using System.Collections;
namespace Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            string pdfPath = @"..\..\simple text.pdf";
            string imagePath = Path.ChangeExtension(pdfPath, ".gif");

            SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();
            //this property is necessary only for registered version
            //f.Serial = "XXXXXXXXXXX";

            f.OpenPdf(pdfPath);
            if (f.PageCount > 0)
            {
                //Set "GIF" format for image
                f.ImageOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Gif;

                //Convert 1st page from PDF to image file
                if (f.ToImage(imagePath, 1) == 0)
                {
                    // 0 - converting successfully
                    // 2 - can't create output file, check the output path
                    // 3 - converting failed
                    System.Diagnostics.Process.Start(imagePath);
                }
            }
        }
    }
}
Imports System.IO
Imports System.Drawing.Imaging
Imports System.Collections.Generic
Imports SautinSoft

Module Sample
    Sub Main()
        Dim pdfPath As String = "..\simple text.pdf"
        Dim imagePath As String = Path.ChangeExtension(pdfPath, ".gif")

        Dim f As New SautinSoft.PdfFocus()
        'this property is necessary only for registered version
        'f.Serial = "XXXXXXXXXXX"

        f.OpenPdf(pdfPath)
        If f.PageCount > 0 Then
            'Set "GIF" format for image
            f.ImageOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Gif

            'Convert 1st page from PDF to image file
            If f.ToImage(imagePath, 1) = 0 Then
                ' 0 - converting successfully
                ' 2 - can't create output file, check the output path
                ' 3 - converting failed
                System.Diagnostics.Process.Start(imagePath)
            End If
        End If
    End Sub
End Module

Другие примеры кода SautinSoft.PdfFocus

PDF в Word PDF в HTML ✦ PDF в Img Image из PDF PDF в Excel PDF в XML PDF в Text PDF во Всё
 ВВЕРХ