VintaSoft Imaging .NET SDK 14.0: Документация для .NET разработчика
Vintasoft.Imaging.Pdf.Drawing Namespace / PdfGraphics Class / SetGraphicsStateParameters(PdfGraphicsStateParameters) Method
Синтаксис Example Требования Смотрите также
В этом разделе
    SetGraphicsStateParameters(PdfGraphicsStateParameters) Метод (PdfGraphics)
    В этом разделе
    Задает параметры состояния графики.
    Синтаксис
    'Declaration
    
    Public Sub SetGraphicsStateParameters( _
    ByVal gsParams
    Объект, в котором хранятся параметры состояния графики.
    As Vintasoft.Imaging.Pdf.Tree.PdfGraphicsStateParameters _
    )

    Parameters

    gsParams
    Объект, в котором хранятся параметры состояния графики.
    Пример

    Вот пример, показывающий, как добавить водяной знак в PDF документ:

    
    ''' <summary>
    ''' Adds the watermark image to the PDF document.
    ''' </summary>
    ''' <param name="inPdfFilename">The filename of input PDF document.</param>
    ''' <param name="outPdfFilename">The filename of output PDF document.</param>
    ''' <param name="watermarkImageFilename">The filename of watermark image.</param>
    Public Shared Sub AddWatermarkToPdfDocument(inPdfFilename As String, outPdfFilename As String, watermarkImageFilename As String)
        AddWatermarkToPdfDocument(inPdfFilename, outPdfFilename, watermarkImageFilename, 0.25F, New Vintasoft.Imaging.PaddingF(0.1F), False)
    End Sub
    
    ''' <summary>
    ''' Adds the watermark image to the PDF document.
    ''' </summary>
    ''' <param name="inPdfFilename">The filename of input PDF document.</param>
    ''' <param name="outPdfFilename">The filename of output PDF document.</param>
    ''' <param name="watermarkImageFilename">The filename of watermark image.</param>
    ''' <param name="watermarkImageAlpha">The watermark image alpha.</param>
    ''' <param name="watermarkImagePadding">The watermark image padding.</param>
    ''' <param name="ignorePageRotation">Indicates that value of Vintasoft.Imaging.Pdf.Tree.PdfPage.Rotate property should be ignored.</param>
    Public Shared Sub AddWatermarkToPdfDocument(inPdfFilename As String, outPdfFilename As String, watermarkImageFilename As String, watermarkImageAlpha As Single, watermarkImagePadding As Vintasoft.Imaging.PaddingF, ignorePageRotation As Boolean)
        ' check watermark padding
        If watermarkImagePadding.Horizontal < 0 OrElse watermarkImagePadding.Vertical < 0 Then
            Throw New System.ArgumentOutOfRangeException("watermarkImagePadding")
        End If
    
        ' open PDF document
        Using document As New Vintasoft.Imaging.Pdf.PdfDocument(inPdfFilename)
            ' create "watermark" resource
            Dim watermakResource As Vintasoft.Imaging.Pdf.Tree.PdfResource
            Using watermarkImage As New Vintasoft.Imaging.VintasoftImage(watermarkImageFilename)
                ' if watermarkImage is PDF page
                If watermarkImage.SourceInfo.DecoderName = "Pdf" Then
                    ' create form resource based on PDF page
                    Dim page As Vintasoft.Imaging.Pdf.Tree.PdfPage = Vintasoft.Imaging.Pdf.PdfDocumentController.GetPageAssociatedWithImage(watermarkImage)
                    watermakResource = New Vintasoft.Imaging.Pdf.Tree.PdfFormXObjectResource(document, page)
                Else
                    ' create watermark image resource with JPEG compression
                    Dim watermarkResourceCompression As Vintasoft.Imaging.Pdf.PdfCompression = Vintasoft.Imaging.Pdf.PdfCompression.Jpeg
                    watermakResource = New Vintasoft.Imaging.Pdf.Tree.PdfImageResource(document, watermarkImage, watermarkResourceCompression)
                End If
            End Using
    
            ' for each page in PDF document
            For Each page As Vintasoft.Imaging.Pdf.Tree.PdfPage In document.Pages
                ' add watermark on PDF page
                AddWatermarkToPdfPage(page, watermakResource, watermarkImagePadding, watermarkImageAlpha, ignorePageRotation)
            Next
    
            ' pack PDF document to an output file
            document.Pack(outPdfFilename)
        End Using
    End Sub
    
    ''' <summary>
    ''' Adds the watermark image to the PDF page.
    ''' </summary>
    ''' <param name="page">The PDF page.</param>
    ''' <param name="watermarkResource">The PDF resource that contains watermark.</param>
    ''' <param name="watermarkImagePadding">The watermark image padding.</param>
    ''' <param name="watermarkImageAlpha">The watermark image alpha.</param>
    ''' <param name="ignorePageRotation">Indicates that value of Vintasoft.Imaging.Pdf.Tree.PdfPage.Rotate property should be ignored.</param>
    Public Shared Sub AddWatermarkToPdfPage(page As Vintasoft.Imaging.Pdf.Tree.PdfPage, watermarkResource As Vintasoft.Imaging.Pdf.Tree.PdfResource, watermarkImagePadding As Vintasoft.Imaging.PaddingF, watermarkImageAlpha As Single, ignorePageRotation As Boolean)
        Dim watermarkImageResource As Vintasoft.Imaging.Pdf.Tree.PdfImageResource = TryCast(watermarkResource, Vintasoft.Imaging.Pdf.Tree.PdfImageResource)
        Dim watermarkFormResource As Vintasoft.Imaging.Pdf.Tree.PdfFormXObjectResource = TryCast(watermarkResource, Vintasoft.Imaging.Pdf.Tree.PdfFormXObjectResource)
    
        ' gets the size of watermark resource
        Dim watermarkResourceSize As System.Drawing.SizeF
        If watermarkImageResource IsNot Nothing Then
            watermarkResourceSize = New System.Drawing.SizeF(watermarkImageResource.Width, watermarkImageResource.Height)
        Else
            watermarkResourceSize = New System.Drawing.SizeF(watermarkFormResource.BoundingBox.Width, watermarkFormResource.BoundingBox.Height)
        End If
    
        ' create PDF graphics from PDF page and specify that graphics must clear and add new content
        Using graphics As Vintasoft.Imaging.Pdf.Drawing.PdfGraphics = Vintasoft.Imaging.Pdf.Drawing.PdfGraphics.FromPage(page)
            ' get mediabox of PDF page
            Dim pageMediaBox As System.Drawing.RectangleF = page.MediaBox
    
            ' if watermark image must be rotated
            If Not ignorePageRotation AndAlso page.Rotate <> 0 Then
                ' rotate padding
                watermarkImagePadding.Rotate(page.Rotate)
            End If
    
            ' calculate watermark rectangle
            Dim watermarkRect As New System.Drawing.RectangleF(pageMediaBox.X + watermarkImagePadding.Left * pageMediaBox.Width, pageMediaBox.Y + watermarkImagePadding.Bottom * pageMediaBox.Height, pageMediaBox.Width - watermarkImagePadding.Horizontal * pageMediaBox.Width, pageMediaBox.Height - watermarkImagePadding.Vertical * pageMediaBox.Height)
            Dim scale As Single = System.Math.Min(watermarkRect.Width / watermarkResourceSize.Width, watermarkRect.Height / watermarkResourceSize.Height)
            Dim watermarkImageRect As New System.Drawing.RectangleF(watermarkRect.X + (watermarkRect.Width - watermarkResourceSize.Width * scale) / 2, watermarkRect.Y + (watermarkRect.Height - watermarkResourceSize.Height * scale) / 2, watermarkResourceSize.Width * scale, watermarkResourceSize.Height * scale)
    
            ' create graphics state parameters
            Dim gsParams As New Vintasoft.Imaging.Pdf.Tree.PdfGraphicsStateParameters(page.Document)
            ' set blending mode to "Multiply"
            gsParams.BlendMode = Vintasoft.Imaging.Pdf.Tree.GraphicsStateBlendMode.Multiply
            ' set alpha transparency
            gsParams.AlphaForNonStrokingOperations = watermarkImageAlpha
    
            ' save graphics state
            graphics.SaveGraphicsState()
    
            ' set graphics state parameters
            graphics.SetGraphicsStateParameters(gsParams)
    
            ' if watermark image must be rotated
            If Not ignorePageRotation AndAlso page.Rotate <> 0 Then
                ' apply rotation to the PdfGraphics
                Dim matrix As New Vintasoft.Imaging.AffineMatrix()
                matrix.RotateAt(page.Rotate, watermarkImageRect.X + watermarkImageRect.Width / 2, watermarkImageRect.Y + watermarkImageRect.Height / 2)
                graphics.MultiplyTransform(matrix)
            End If
    
            ' if watermark is an image
            If watermarkImageResource IsNot Nothing Then
                ' draw watermark image
                graphics.DrawImage(watermarkImageResource, watermarkImageRect)
            Else
                ' draw watermark form
                graphics.DrawForm(watermarkFormResource, watermarkImageRect)
            End If
    
            ' restore graphics state
            graphics.RestoreGraphicsState()
        End Using
    End Sub
    
    
    
    /// <summary>
    /// Adds the watermark image to the PDF document.
    /// </summary>
    /// <param name="inPdfFilename">The filename of input PDF document.</param>
    /// <param name="outPdfFilename">The filename of output PDF document.</param>
    /// <param name="watermarkImageFilename">The filename of watermark image.</param>
    public static void AddWatermarkToPdfDocument(string inPdfFilename, string outPdfFilename, string watermarkImageFilename)
    {
        AddWatermarkToPdfDocument(inPdfFilename, outPdfFilename, watermarkImageFilename, 0.25f, new Vintasoft.Imaging.PaddingF(0.1f), false);
    }
    
    /// <summary>
    /// Adds the watermark image to the PDF document.
    /// </summary>
    /// <param name="inPdfFilename">The filename of input PDF document.</param>
    /// <param name="outPdfFilename">The filename of output PDF document.</param>
    /// <param name="watermarkImageFilename">The filename of watermark image.</param>
    /// <param name="watermarkImageAlpha">The watermark image alpha.</param>
    /// <param name="watermarkImagePadding">The watermark image padding.</param>
    /// <param name="ignorePageRotation">Indicates that value of Vintasoft.Imaging.Pdf.Tree.PdfPage.Rotate property should be ignored.</param>
    public static void AddWatermarkToPdfDocument(
        string inPdfFilename,
        string outPdfFilename,
        string watermarkImageFilename, 
        float watermarkImageAlpha,
        Vintasoft.Imaging.PaddingF watermarkImagePadding,
        bool ignorePageRotation)
    {
        // check watermark padding
        if (watermarkImagePadding.Horizontal < 0 || watermarkImagePadding.Vertical < 0)
            throw new System.ArgumentOutOfRangeException("watermarkImagePadding");
    
        // open PDF document
        using (Vintasoft.Imaging.Pdf.PdfDocument document = new Vintasoft.Imaging.Pdf.PdfDocument(inPdfFilename))
        {
            // create "watermark" resource
            Vintasoft.Imaging.Pdf.Tree.PdfResource watermakResource;
            using (Vintasoft.Imaging.VintasoftImage watermarkImage = new Vintasoft.Imaging.VintasoftImage(watermarkImageFilename))
            {
                // if watermarkImage is PDF page
                if (watermarkImage.SourceInfo.DecoderName == "Pdf")
                {
                    // create form resource based on PDF page
                    Vintasoft.Imaging.Pdf.Tree.PdfPage page = Vintasoft.Imaging.Pdf.PdfDocumentController.GetPageAssociatedWithImage(watermarkImage);
                    watermakResource = new Vintasoft.Imaging.Pdf.Tree.PdfFormXObjectResource(document, page);
                }
                else
                {
                    // create watermark image resource with JPEG compression
                    Vintasoft.Imaging.Pdf.PdfCompression watermarkResourceCompression = Vintasoft.Imaging.Pdf.PdfCompression.Jpeg;
                    watermakResource = new Vintasoft.Imaging.Pdf.Tree.PdfImageResource(document, watermarkImage, watermarkResourceCompression);
                }
            }
    
            // for each page in PDF document
            foreach (Vintasoft.Imaging.Pdf.Tree.PdfPage page in document.Pages)
            {
                // add watermark on PDF page
                AddWatermarkToPdfPage(page, watermakResource, watermarkImagePadding, watermarkImageAlpha, ignorePageRotation);
            }
    
            // pack PDF document to an output file
            document.Pack(outPdfFilename);
        }
    }
    
    /// <summary>
    /// Adds the watermark image to the PDF page.
    /// </summary>
    /// <param name="page">The PDF page.</param>
    /// <param name="watermarkResource">The PDF resource that contains watermark.</param>
    /// <param name="watermarkImagePadding">The watermark image padding.</param>
    /// <param name="watermarkImageAlpha">The watermark image alpha.</param>
    /// <param name="ignorePageRotation">Indicates that value of Vintasoft.Imaging.Pdf.Tree.PdfPage.Rotate property should be ignored.</param>
    public static void AddWatermarkToPdfPage(
        Vintasoft.Imaging.Pdf.Tree.PdfPage page,
        Vintasoft.Imaging.Pdf.Tree.PdfResource watermarkResource,
        Vintasoft.Imaging.PaddingF watermarkImagePadding,
        float watermarkImageAlpha,
        bool ignorePageRotation)
    {
        Vintasoft.Imaging.Pdf.Tree.PdfImageResource watermarkImageResource = watermarkResource as Vintasoft.Imaging.Pdf.Tree.PdfImageResource;
        Vintasoft.Imaging.Pdf.Tree.PdfFormXObjectResource watermarkFormResource = watermarkResource as Vintasoft.Imaging.Pdf.Tree.PdfFormXObjectResource;
    
        // gets the size of watermark resource
        System.Drawing.SizeF watermarkResourceSize;
        if (watermarkImageResource != null)
            watermarkResourceSize = new System.Drawing.SizeF(watermarkImageResource.Width, watermarkImageResource.Height);
        else
            watermarkResourceSize = new System.Drawing.SizeF(watermarkFormResource.BoundingBox.Width, watermarkFormResource.BoundingBox.Height);
    
        // create PDF graphics from PDF page and specify that graphics must clear and add new content
        using (Vintasoft.Imaging.Pdf.Drawing.PdfGraphics graphics = Vintasoft.Imaging.Pdf.Drawing.PdfGraphics.FromPage(page))
        {
            // get mediabox of PDF page
            System.Drawing.RectangleF pageMediaBox = page.MediaBox;
    
            // if watermark image must be rotated
            if (!ignorePageRotation && page.Rotate != 0)
            {
                // rotate padding
                watermarkImagePadding.Rotate(page.Rotate);
            }
    
            // calculate watermark rectangle
            System.Drawing.RectangleF watermarkRect = new System.Drawing.RectangleF(
                pageMediaBox.X + watermarkImagePadding.Left * pageMediaBox.Width,
                pageMediaBox.Y + watermarkImagePadding.Bottom * pageMediaBox.Height,
                pageMediaBox.Width - watermarkImagePadding.Horizontal * pageMediaBox.Width,
                pageMediaBox.Height - watermarkImagePadding.Vertical * pageMediaBox.Height);
            float scale = System.Math.Min(watermarkRect.Width / watermarkResourceSize.Width, watermarkRect.Height / watermarkResourceSize.Height);
            System.Drawing.RectangleF watermarkImageRect = new System.Drawing.RectangleF(
                watermarkRect.X + (watermarkRect.Width - watermarkResourceSize.Width * scale) / 2,
                watermarkRect.Y + (watermarkRect.Height - watermarkResourceSize.Height * scale) / 2,
                watermarkResourceSize.Width * scale,
                watermarkResourceSize.Height * scale);
    
            // create graphics state parameters
            Vintasoft.Imaging.Pdf.Tree.PdfGraphicsStateParameters gsParams = new Vintasoft.Imaging.Pdf.Tree.PdfGraphicsStateParameters(page.Document);
            // set blending mode to "Multiply"
            gsParams.BlendMode = Vintasoft.Imaging.Pdf.Tree.GraphicsStateBlendMode.Multiply;
            // set alpha transparency
            gsParams.AlphaForNonStrokingOperations = watermarkImageAlpha;
    
            // save graphics state
            graphics.SaveGraphicsState();
    
            // set graphics state parameters
            graphics.SetGraphicsStateParameters(gsParams);
    
            // if watermark image must be rotated
            if (!ignorePageRotation && page.Rotate != 0)
            {
                // apply rotation to the PdfGraphics
                Vintasoft.Imaging.AffineMatrix matrix = new Vintasoft.Imaging.AffineMatrix();
                matrix.RotateAt(
                    page.Rotate,
                    watermarkImageRect.X + watermarkImageRect.Width / 2,
                    watermarkImageRect.Y + watermarkImageRect.Height / 2);
                graphics.MultiplyTransform(matrix);
            }
    
            // if watermark is an image
            if (watermarkImageResource != null)
                // draw watermark image
                graphics.DrawImage(watermarkImageResource, watermarkImageRect);
            else
                // draw watermark form
                graphics.DrawForm(watermarkFormResource, watermarkImageRect);
    
            // restore graphics state
            graphics.RestoreGraphicsState();
        }
    }     
    
    

    Требования

    Целевые платформы: .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5

    Смотрите также