You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Document-Processing/PDF/PDF-Library/NET/Working-with-Images.md
+130Lines changed: 130 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -618,6 +618,136 @@ doc.Close(True)
618
618
619
619
You can download a complete working sample from [GitHub](https://github.com/SyncfusionExamples/PDF-Examples/tree/master/Images/Paginate-an-image-in-PDF-document).
620
620
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));
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
+
621
751
## Applying transparency and rotation to the image
622
752
623
753
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