Skip to content

Commit 52aa93f

Browse files
authored
Merge pull request #213 from SyncfusionExamples/261980
261980: Added code example for set and restore clipping mechanism in PDF document
2 parents 6542a4f + df020ad commit 52aa93f

File tree

7 files changed

+100
-20
lines changed

7 files changed

+100
-20
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.14.36616.10 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Clipping-and-graphics-state", "Clipping-and-graphics-state\Clipping-and-graphics-state.csproj", "{D3AD004B-8B2A-4230-9AA7-E0EC9CFC6C9C}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{D3AD004B-8B2A-4230-9AA7-E0EC9CFC6C9C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{D3AD004B-8B2A-4230-9AA7-E0EC9CFC6C9C}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{D3AD004B-8B2A-4230-9AA7-E0EC9CFC6C9C}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{D3AD004B-8B2A-4230-9AA7-E0EC9CFC6C9C}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {820F8B8B-A228-4044-AE9F-406099112325}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Clipping_and_graphics_state</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.Pdf.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
</Project>
20.9 KB
Loading

Images/Clipping-and-graphics-state/.NET/Clipping-and-graphics-state/Output/gitkeep.txt

Whitespace-only changes.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Syncfusion.Drawing;
2+
using Syncfusion.Pdf;
3+
using Syncfusion.Pdf.Graphics;
4+
5+
// Create a new PDF document
6+
using (PdfDocument document = new PdfDocument())
7+
{
8+
// Add a page to the document
9+
PdfPage page = document.Pages.Add();
10+
// Get the graphics object for the page
11+
PdfGraphics graphics = page.Graphics;
12+
// Open the image file as a stream
13+
using FileStream imageStream = new FileStream(Path.GetFullPath(@"Data/Input.png"), FileMode.Open, FileAccess.Read);
14+
// Load the image from the stream
15+
PdfBitmap image = new PdfBitmap(imageStream);
16+
// Save the current graphics state (to restore later)
17+
PdfGraphicsState state = graphics.Save();
18+
// Define a rectangular clipping region
19+
RectangleF clipRect = new RectangleF(50, 50, 200, 100);
20+
graphics.SetClip(clipRect);
21+
// Draw the image — only the part within the clipping region will be visible
22+
graphics.DrawImage(image, new RectangleF(40, 60, 150, 80));
23+
// Restore the graphics state to remove the clipping region
24+
graphics.Restore(state);
25+
// Draw the image again — this time the full image will be visible
26+
graphics.DrawImage(image, new RectangleF(60, 160, 150, 80));
27+
// Save the PDF document
28+
document.Save(Path.GetFullPath(@"Output/Output.pdf"));
29+
}

Portfolio/Create-a-portfolio-and-attach-variety-of-documents/.NET/Create-a-portfolio-and-attach-variety-of-documents/Program.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,24 @@
66
{
77
//Create a new portfolio.
88
document.PortfolioInformation = new PdfPortfolioInformation();
9-
109
//Set the view mode of the portfolio.
1110
document.PortfolioInformation.ViewMode = PdfPortfolioViewMode.Tile;
12-
1311
//Get stream from the attachment PDF file.
1412
FileStream pdfStream = new FileStream(Path.GetFullPath(@"Data/CorporateBrochure.pdf"), FileMode.Open, FileAccess.Read);
15-
16-
//Create the attachment.
17-
PdfAttachment pdfFile = new PdfAttachment("CorporateBrochure.pdf", pdfStream);
18-
19-
//Set the file name.
13+
// Set the name of the attachment file
2014
pdfFile.FileName = "CorporateBrochure.pdf";
21-
22-
//Set the startup document to view.
15+
// Provide a description for the attachment
16+
pdfFile.Description = "This is a PDF document";
17+
// Set the creation date of the attachment
18+
pdfFile.CreationDate = DateTime.Now;
19+
// Specify the MIME type of the attachment (important for identifying file type)
20+
pdfFile.MimeType = "application/pdf";
21+
// Optionally, set the modification date if needed
22+
pdfFile.ModificationDate = DateTime.Now;
23+
// Optionally, set the relationship (e.g., "Data", "Source", etc.)
24+
pdfFile.Relationship = PdfAttachmentRelationship.Unspecified;
25+
// Set this attachment as the startup document in the PDF portfolio
2326
document.PortfolioInformation.StartupDocument = pdfFile;
24-
2527
//Add the attachment to the document.
2628
document.Attachments.Add(pdfFile);
2729
//Save the PDF document

Portfolio/Extracting-the-files-from-PDF-portfolio/.NET/Extracting-the-files-from-PDF-portfolio/Program.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,27 @@
33

44
//Load the PDF document.
55
PdfLoadedDocument document = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf"));
6-
7-
string outputFileName = "";
8-
9-
//Iterate the attachments.
6+
// Iterate through all attachments in the PDF document
107
foreach (PdfAttachment attachment in document.Attachments)
118
{
12-
//Extract the attachment and save to the disk.
13-
FileStream s = new FileStream(attachment.FileName, FileMode.Create);
14-
s.Write(attachment.Data, 0, attachment.Data.Length);
15-
s.Dispose();
16-
17-
outputFileName = attachment.FileName;
9+
// Create a file stream to save the attachment to disk using its original file name
10+
using (FileStream s = new FileStream(attachment.FileName, FileMode.Create))
11+
{
12+
// Write the attachment data to the file
13+
s.Write(attachment.Data, 0, attachment.Data.Length);
14+
}
15+
// Retrieve the MIME type of the attachment (e.g., application/pdf, image/png)
16+
string mimeType = attachment.MimeType;
17+
Console.WriteLine($"Saved: {attachment.FileName}, MIME Type: {mimeType}");
18+
// Optional: Access additional metadata if needed
19+
DateTime creationDate = attachment.CreationDate;
20+
DateTime modificationDate = attachment.ModificationDate;
21+
string description = attachment.Description;
22+
PdfAttachmentRelationship relationship = attachment.Relationship;
23+
// Log or use the metadata as needed
24+
Console.WriteLine($"Description: {description}");
25+
Console.WriteLine($"Created on: {creationDate}, Modified on: {modificationDate}");
26+
Console.WriteLine($"Relationship: {relationship}");
1827
}
1928
//Save the PDF document
2029
document.Save(Path.GetFullPath(@"Output/Output.pdf"));

0 commit comments

Comments
 (0)