Skip to content

Commit 4176cab

Browse files
committed
261980-ug :Explained about set and restore clipping mechanism in PDF document
1 parent d9d7b88 commit 4176cab

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

Document-Processing/PDF/PDF-Library/NET/Working-with-Images.md

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,136 @@ doc.Close(True)
618618

619619
You can download a complete working sample from [GitHub](https://github.com/SyncfusionExamples/PDF-Examples/tree/master/Images/Paginate-an-image-in-PDF-document).
620620

621+
## Clipping and Graphics State
622+
623+
This example demonstrates how to draw an image in a PDF document and apply a clipping region using the [SetClip](https://help.syncfusion.com/cr/document-processing/Syncfusion.Pdf.Graphics.PdfGraphics.html#methods) method. Clipping restricts drawing to a defined area, allowing partial rendering of content. The code also uses [Save](https://help.syncfusion.com/cr/document-processing/Syncfusion.Pdf.Graphics.PdfGraphics.html#methods) and [Restore](https://help.syncfusion.com/cr/document-processing/Syncfusion.Pdf.Graphics.PdfGraphics.html#methods) methods of [PdfGraphics](https://help.syncfusion.com/cr/document-processing/Syncfusion.Pdf.Graphics.PdfGraphics.html) to manage the graphics state, enabling temporary clipping and restoring the full drawing area afterward.
624+
625+
{% tabs %}
626+
627+
{% highlight c# tabtitle="C# [Cross-platform]" %}
628+
629+
using Syncfusion.Drawing;
630+
using Syncfusion.Pdf;
631+
using Syncfusion.Pdf.Graphics;
632+
633+
// Create a new PDF document
634+
using (PdfDocument document = new PdfDocument())
635+
{
636+
// Add a page to the document
637+
PdfPage page = document.Pages.Add();
638+
// Get the graphics object for the page
639+
PdfGraphics graphics = page.Graphics;
640+
// Open the image file as a stream
641+
using FileStream imageStream = new FileStream(Path.GetFullPath("Input.png"), FileMode.Open, FileAccess.Read);
642+
// Load the image from the stream
643+
PdfBitmap image = new PdfBitmap(imageStream);
644+
645+
// Save the current graphics state (to restore later)
646+
PdfGraphicsState state = graphics.Save();
647+
648+
// Define a rectangular clipping region
649+
RectangleF clipRect = new RectangleF(50, 50, 200, 100);
650+
graphics.SetClip(clipRect);
651+
652+
// Draw the image — only the part within the clipping region will be visible
653+
graphics.DrawImage(image, new RectangleF(40, 60, 150, 80));
654+
655+
// Restore the graphics state to remove the clipping region
656+
graphics.Restore(state);
657+
// Draw the image again — this time the full image will be visible
658+
graphics.DrawImage(image, new RectangleF(60, 160, 150, 80));
659+
660+
// Save the PDF document
661+
document.Save("Output.pdf");
662+
}
663+
664+
{% endhighlight %}
665+
666+
{% highlight c# tabtitle="C# [Windows-specific]" %}
667+
668+
using System.Drawing;
669+
using Syncfusion.Pdf;
670+
using Syncfusion.Pdf.Graphics;
671+
672+
// Create a new PDF document
673+
using (PdfDocument document = new PdfDocument())
674+
{
675+
// Add a page to the document
676+
PdfPage page = document.Pages.Add();
677+
// Get the graphics object for the page
678+
PdfGraphics graphics = page.Graphics;
679+
// Open the image file as a stream
680+
using FileStream imageStream = new FileStream(Path.GetFullPath("Input.png"), FileMode.Open, FileAccess.Read);
681+
// Load the image from the stream
682+
PdfBitmap image = new PdfBitmap(imageStream);
683+
684+
// Save the current graphics state (to restore later)
685+
PdfGraphicsState state = graphics.Save();
686+
687+
// Define a rectangular clipping region
688+
RectangleF clipRect = new RectangleF(50, 50, 200, 100);
689+
graphics.SetClip(clipRect);
690+
691+
// Draw the image — only the part within the clipping region will be visible
692+
graphics.DrawImage(image, new RectangleF(40, 60, 150, 80));
693+
694+
// Restore the graphics state to remove the clipping region
695+
graphics.Restore(state);
696+
// Draw the image again — this time the full image will be visible
697+
graphics.DrawImage(image, new RectangleF(60, 160, 150, 80));
698+
699+
// Save the PDF document
700+
document.Save("Output.pdf");
701+
}
702+
703+
{% endhighlight %}
704+
705+
{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %}
706+
707+
Imports System.Drawing
708+
Imports Syncfusion.Pdf
709+
Imports Syncfusion.Pdf.Graphics
710+
711+
' Create a new PDF document
712+
Using document As New PdfDocument()
713+
' Add a page to the document
714+
Dim page As PdfPage = document.Pages.Add()
715+
716+
' Get the graphics object for the page
717+
Dim graphics As PdfGraphics = page.Graphics
718+
719+
' Open the image file as a stream
720+
Using imageStream As New FileStream(Path.GetFullPath("Input.png"), FileMode.Open, FileAccess.Read)
721+
' Load the image from the stream
722+
Dim image As New PdfBitmap(imageStream)
723+
724+
' Save the current graphics state (to restore later)
725+
Dim state As PdfGraphicsState = graphics.Save()
726+
727+
' Define a rectangular clipping region
728+
Dim clipRect As New RectangleF(50, 50, 200, 100)
729+
graphics.SetClip(clipRect)
730+
731+
' Draw the image — only the part within the clipping region will be visible
732+
graphics.DrawImage(image, New RectangleF(40, 60, 150, 80))
733+
734+
' Restore the graphics state to remove the clipping region
735+
graphics.Restore(state)
736+
737+
' Draw the image again — this time the full image will be visible
738+
graphics.DrawImage(image, New RectangleF(60, 160, 150, 80))
739+
End Using
740+
741+
' Save the PDF document
742+
document.Save("Output.pdf")
743+
End Using
744+
745+
{% endhighlight %}
746+
747+
{% endtabs %}
748+
749+
You can download a complete working sample from GitHub.
750+
621751
## Applying transparency and rotation to the image
622752

623753
You can add transparency and rotation to the image using [SetTransparency](https://help.syncfusion.com/cr/document-processing/Syncfusion.Pdf.Graphics.PdfGraphics.html#Syncfusion_Pdf_Graphics_PdfGraphics_SetTransparency_System_Single_) and [RotateTransform](https://help.syncfusion.com/cr/document-processing/Syncfusion.Pdf.Graphics.PdfGraphics.html#Syncfusion_Pdf_Graphics_PdfGraphics_RotateTransform_System_Single_) methods of [PdfGraphics](https://help.syncfusion.com/cr/document-processing/Syncfusion.Pdf.Graphics.PdfGraphics.html) respectively. This is explained in the below code snippet.

0 commit comments

Comments
 (0)