Skip to content

Commit 61be671

Browse files
Merge pull request #218 from SyncfusionExamples/Task-935636-FlattenField
Task-935636-How to preserve form fields after creating template from the PDF document
2 parents 01d4edb + 4bf1994 commit 61be671

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Solution>
2+
<Project Path="Preserve-Formfields- in-the-Template-created-from-existing-PDF/Preserve-Formfields- in-the-Template-created-from-existing-PDF.csproj" />
3+
</Solution>
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>Preserve_Formfields__in_the_Template_created_from_existing_PDF</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>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Syncfusion.Drawing;
2+
using Syncfusion.Pdf;
3+
using Syncfusion.Pdf.Graphics;
4+
using Syncfusion.Pdf.Parsing;
5+
using System.IO;
6+
7+
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf"));
8+
//Flatten all form fields to make them part of the page graphics.
9+
PdfLoadedForm loadedForm = loadedDocument.Form;
10+
loadedForm.FlattenFields();
11+
12+
//Create a template from the first page.
13+
PdfLoadedPage loadedPage = loadedDocument.Pages[0] as PdfLoadedPage;
14+
PdfTemplate template = loadedPage.CreateTemplate();
15+
16+
//Create the destination PDF (no page margins so the template fills it edge-to-edge).
17+
PdfDocument newDocument = new PdfDocument();
18+
newDocument.PageSettings.Margins.All = 0;
19+
PdfPage newPage = newDocument.Pages.Add();
20+
21+
//Draw the template so it fills the entire new page.
22+
newPage.Graphics.DrawPdfTemplate(
23+
template,
24+
PointF.Empty,
25+
new SizeF(newPage.Size.Width, newPage.Size.Height));
26+
27+
//Save the result.
28+
newDocument.Save(Path.GetFullPath(@"Output/Output.pdf"));
29+
30+
//Close documents.
31+
loadedDocument.Close(true);
32+
newDocument.Close(true);

0 commit comments

Comments
 (0)