VintaSoft Imaging .NET SDK 15.1: Документация для .NET разработчика
В этом разделе
PDF: Работа с ресурсами изображений в PDF документе
В этом разделе
Ресурс изображения хранит изображение, которое должно быть нарисовано на PDF странице. Каждый ресурс изображения может быть использован несколько раз, т. е. его можно рисовать сразу на нескольких страницах.
Класс PdfImageResource представляет ресурс изображения, который хранится в PDF документе, и позволяет:
Все ресурсы изображений PDF документа можно получить с помощью метода PdfDocument.GetImages. Все ресурсы изображений PDF страницы могут быть получены с помощью метода PdfPage.GetImages.

Вот C#/VB.NET код, который демонстрирует, как получить информацию обо всех ресурсах изображений на PDF странице:
/// <summary>
/// Prints information about image-resources of PDF document.
/// </summary>
/// <param name="pdfFilename">The filename of PDF document.</param>
public static void GetPdfImagesInfo(string pdfFilename)
{
    // open PDF document
    using (Vintasoft.Imaging.Pdf.PdfDocument document = 
        new Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename))
    {
        // get collection of image-resources of PDF document
        Vintasoft.Imaging.Pdf.Tree.PdfImageResource[] images = document.GetImages();

        // print count of image-resource
        System.Console.WriteLine("Images count: {0}", images.Length);

        // for each image-resource
        foreach (Vintasoft.Imaging.Pdf.Tree.PdfImageResource image in images)
        {
            // print information about image-resource
            System.Console.WriteLine("\t{0,4}x{1,-4} {2,5}bpp {3}", image.Width, image.Height,
                image.BitsPerPixel, image.Compression);
        }
    }
}

/* This code example produces the following output:
Images count: 11
  189x41      24bpp Jpeg
  120x50      24bpp Jpeg
  122x56      24bpp Jpeg
  143x41      24bpp Jpeg
  216x21      24bpp Jpeg
  106x49       8bpp Zip
  209x30      24bpp Jpeg
  189x24      24bpp Jpeg
  122x42      24bpp Jpeg
    5x7       24bpp Zip
  817x292     24bpp Jpeg
*/
        ''' <summary>
        ''' Prints information about image-resources of PDF document.
        ''' </summary>
        ''' <param name="pdfFilename">The filename of PDF document.</param>
        Public Shared Sub GetPdfImagesInfo(pdfFilename As String)
            ' open PDF document
            Using document As New Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename)
                ' get collection of image-resources of PDF document
                Dim images As Vintasoft.Imaging.Pdf.Tree.PdfImageResource() = document.GetImages()

                ' print count of image-resource
                System.Console.WriteLine("Images count: {0}", images.Length)

                ' for each image-resource
                For Each image As Vintasoft.Imaging.Pdf.Tree.PdfImageResource In images
                    ' print information about image-resource
                    System.Console.WriteLine(vbTab & "{0,4}x{1,-4} {2,5}bpp {3}", image.Width, image.Height, image.BitsPerPixel, image.Compression)
                Next
            End Using
        End Sub

        ' This code example produces the following output:
'        Images count: 11
'          189x41      24bpp Jpeg
'          120x50      24bpp Jpeg
'          122x56      24bpp Jpeg
'          143x41      24bpp Jpeg
'          216x21      24bpp Jpeg
'          106x49       8bpp Zip
'          209x30      24bpp Jpeg
'          189x24      24bpp Jpeg
'          122x42      24bpp Jpeg
'            5x7       24bpp Zip
'          817x292     24bpp Jpeg
'





Чтобы добавить новый ресурс изображения в PDF документ, необходимо:
Вот C#/VB.NET код, который демонстрирует, как нарисовать новое изображение на PDF странице:
/// <summary>
/// Draws an image on PDF page.
/// </summary>
/// <param name="pdfFilename">The filename of PDF document.</param>
/// <param name="imageFilename">The filename of image.</param>
public static void DrawImageOnPdfPage(string pdfFilename, string imageFilename)
{
    // open PDF document
    using (Vintasoft.Imaging.Pdf.PdfDocument document = 
        new Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename))
    {
        // open image
        using (Vintasoft.Imaging.VintasoftImage image = 
            new Vintasoft.Imaging.VintasoftImage(imageFilename))
        {
            // get the first page
            Vintasoft.Imaging.Pdf.Tree.PdfPage page = document.Pages[0];
            // get PDF graphics of PDF page
            using (Vintasoft.Imaging.Pdf.Drawing.PdfGraphics graphics = page.GetGraphics())
            {
                // create image-resource based on image
                Vintasoft.Imaging.Pdf.Tree.PdfImageResource imageResource = 
                    new Vintasoft.Imaging.Pdf.Tree.PdfImageResource(
                        document, image, Vintasoft.Imaging.Pdf.PdfCompression.Auto);

                // draw image-resource on PDF page
                graphics.DrawImage(imageResource, new System.Drawing.RectangleF(100, 100, 200, 300));
            }
        }

        // save changes to a file
        document.SaveChanges();
    }
}

''' <summary>
''' Draws an image on PDF page.
''' </summary>
''' <param name="pdfFilename">The filename of PDF document.</param>
''' <param name="imageFilename">The filename of image.</param>
Public Shared Sub DrawImageOnPdfPage(pdfFilename As String, imageFilename As String)
    ' open PDF document
    Using document As New Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename)
        ' open image
        Using image As New Vintasoft.Imaging.VintasoftImage(imageFilename)
            ' get the first page
            Dim page As Vintasoft.Imaging.Pdf.Tree.PdfPage = document.Pages(0)
            ' get PDF graphics of PDF page
            Using graphics As Vintasoft.Imaging.Pdf.Drawing.PdfGraphics = page.GetGraphics()
                ' create image-resource based on image
                Dim imageResource As New Vintasoft.Imaging.Pdf.Tree.PdfImageResource(document, image, Vintasoft.Imaging.Pdf.PdfCompression.Auto)

                ' draw image-resource on PDF page
                graphics.DrawImage(imageResource, New System.Drawing.RectangleF(100, 100, 200, 300))
            End Using
        End Using

        ' save changes to a file
        document.SaveChanges()
    End Using
End Sub





Чтобы изменить алгоритм сжатия ресурса изображения, необходимо выполнить следующие действия:
Вот C#/VB.NET код, который демонстрирует, как изменить алгоритм сжатия всех ресурсов черно-белых изображений в PDF документе:
/// <summary>
/// Changes compression of all black-white image-resources of PDF document.
/// </summary>
/// <param name="pdfFilename">The filename of PDF document.</param>
public static void ChangeCompressionBlackWhiteImages(string pdfFilename)
{
    // open PDF document
    using (Vintasoft.Imaging.Pdf.PdfDocument document = 
        new Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename))
    {
        // get collection of image-resources
        Vintasoft.Imaging.Pdf.Tree.PdfImageResource[] images = document.GetImages();

        // create compression settings
        Vintasoft.Imaging.Pdf.PdfCompressionSettings compressionSettings = 
            new Vintasoft.Imaging.Pdf.PdfCompressionSettings();
        
        // for each image-resource
        foreach (Vintasoft.Imaging.Pdf.Tree.PdfImageResource image in images)
        {
            //  if image is black-white
            if (image.PixelFormat == Vintasoft.Imaging.PixelFormat.BlackWhite)
                // change compression of image-resource
                image.Compress(Vintasoft.Imaging.Pdf.PdfCompression.CcittFax, compressionSettings);
        }

        // save changes to a file
        document.SaveChanges();
    }
}
''' <summary>
''' Changes compression of all black-white image-resources of PDF document.
''' </summary>
''' <param name="pdfFilename">The filename of PDF document.</param>
Public Shared Sub ChangeCompressionBlackWhiteImages(pdfFilename As String)
    ' open PDF document
    Using document As New Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename)
        ' get collection of image-resources
        Dim images As Vintasoft.Imaging.Pdf.Tree.PdfImageResource() = document.GetImages()

        ' create compression settings
        Dim compressionSettings As New Vintasoft.Imaging.Pdf.PdfCompressionSettings()

        ' for each image-resource
        For Each image As Vintasoft.Imaging.Pdf.Tree.PdfImageResource In images
            '  if image is black-white
            If image.PixelFormat = Vintasoft.Imaging.PixelFormat.BlackWhite Then
                ' change compression of image-resource
                image.Compress(Vintasoft.Imaging.Pdf.PdfCompression.CcittFax, compressionSettings)
            End If
        Next

        ' save changes to a file
        document.SaveChanges()
    End Using
End Sub