Skip to content

Commit 746d91b

Browse files
Merge pull request #210 from SyncfusionExamples/Convert-5-pages-in-Word-to-PDF
Convert-5-pages-in-Word-to-PDF
2 parents 6c68473 + be0043f commit 746d91b

File tree

7 files changed

+230
-0
lines changed

7 files changed

+230
-0
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.9.34622.214
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Convert-5-pages-in-Word-to-PDF", "Convert-5-pages-in-Word-to-PDF\Convert-5-pages-in-Word-to-PDF.csproj", "{EC0E3D2F-E1CF-4A77-9448-5C871139C469}"
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+
{EC0E3D2F-E1CF-4A77-9448-5C871139C469}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{EC0E3D2F-E1CF-4A77-9448-5C871139C469}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{EC0E3D2F-E1CF-4A77-9448-5C871139C469}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{EC0E3D2F-E1CF-4A77-9448-5C871139C469}.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 = {E4E72D03-F028-4E31-A543-F521F8FC2D91}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
5+
</startup>
6+
<runtime>
7+
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
8+
<dependentAssembly>
9+
<assemblyIdentity name="Syncfusion.DocIO.Base" publicKeyToken="3d67ed1f87d44c89" culture="neutral"/>
10+
<bindingRedirect oldVersion="0.0.0.0-26.2462.9.0" newVersion="26.2462.9.0"/>
11+
</dependentAssembly>
12+
<dependentAssembly>
13+
<assemblyIdentity name="Syncfusion.Pdf.Base" publicKeyToken="3d67ed1f87d44c89" culture="neutral"/>
14+
<bindingRedirect oldVersion="0.0.0.0-26.2462.9.0" newVersion="26.2462.9.0"/>
15+
</dependentAssembly>
16+
<dependentAssembly>
17+
<assemblyIdentity name="Syncfusion.Compression.Base" publicKeyToken="3d67ed1f87d44c89" culture="neutral"/>
18+
<bindingRedirect oldVersion="0.0.0.0-26.2462.9.0" newVersion="26.2462.9.0"/>
19+
</dependentAssembly>
20+
<dependentAssembly>
21+
<assemblyIdentity name="Syncfusion.OfficeChart.Base" publicKeyToken="3d67ed1f87d44c89" culture="neutral"/>
22+
<bindingRedirect oldVersion="0.0.0.0-26.2462.9.0" newVersion="26.2462.9.0"/>
23+
</dependentAssembly>
24+
</assemblyBinding>
25+
</runtime>
26+
</configuration>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{EC0E3D2F-E1CF-4A77-9448-5C871139C469}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>Convert_5_pages_in_Word_to_PDF</RootNamespace>
10+
<AssemblyName>Convert-5-pages-in-Word-to-PDF</AssemblyName>
11+
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
<Deterministic>true</Deterministic>
15+
<TargetFrameworkProfile />
16+
</PropertyGroup>
17+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
18+
<PlatformTarget>AnyCPU</PlatformTarget>
19+
<DebugSymbols>true</DebugSymbols>
20+
<DebugType>full</DebugType>
21+
<Optimize>false</Optimize>
22+
<OutputPath>bin\Debug\</OutputPath>
23+
<DefineConstants>DEBUG;TRACE</DefineConstants>
24+
<ErrorReport>prompt</ErrorReport>
25+
<WarningLevel>4</WarningLevel>
26+
</PropertyGroup>
27+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
28+
<PlatformTarget>AnyCPU</PlatformTarget>
29+
<DebugType>pdbonly</DebugType>
30+
<Optimize>true</Optimize>
31+
<OutputPath>bin\Release\</OutputPath>
32+
<DefineConstants>TRACE</DefineConstants>
33+
<ErrorReport>prompt</ErrorReport>
34+
<WarningLevel>4</WarningLevel>
35+
</PropertyGroup>
36+
<ItemGroup>
37+
<Reference Include="Syncfusion.Compression.Base, Version=26.2462.9.0, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
38+
<HintPath>..\packages\Syncfusion.Compression.Base.26.2.9\lib\net462\Syncfusion.Compression.Base.dll</HintPath>
39+
</Reference>
40+
<Reference Include="Syncfusion.DocIO.Base, Version=26.2462.9.0, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
41+
<HintPath>..\packages\Syncfusion.DocIO.WinForms.26.2.9\lib\net462\Syncfusion.DocIO.Base.dll</HintPath>
42+
</Reference>
43+
<Reference Include="Syncfusion.DocToPdfConverter.Base, Version=26.1462.42.0, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
44+
<HintPath>..\packages\Syncfusion.DocToPDFConverter.WinForms.26.1.42\lib\net462\Syncfusion.DocToPdfConverter.Base.dll</HintPath>
45+
</Reference>
46+
<Reference Include="Syncfusion.Licensing, Version=26.2462.9.0, Culture=neutral, PublicKeyToken=632609b4d040f6b4, processorArchitecture=MSIL">
47+
<HintPath>..\packages\Syncfusion.Licensing.26.2.9\lib\net462\Syncfusion.Licensing.dll</HintPath>
48+
</Reference>
49+
<Reference Include="Syncfusion.OfficeChart.Base, Version=26.2462.9.0, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
50+
<HintPath>..\packages\Syncfusion.OfficeChart.Base.26.2.9\lib\net462\Syncfusion.OfficeChart.Base.dll</HintPath>
51+
</Reference>
52+
<Reference Include="Syncfusion.Pdf.Base, Version=26.2462.9.0, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
53+
<HintPath>..\packages\Syncfusion.Pdf.WinForms.26.2.9\lib\net462\Syncfusion.Pdf.Base.dll</HintPath>
54+
</Reference>
55+
<Reference Include="System" />
56+
<Reference Include="System.Core" />
57+
<Reference Include="System.Xml.Linq" />
58+
<Reference Include="System.Data.DataSetExtensions" />
59+
<Reference Include="Microsoft.CSharp" />
60+
<Reference Include="System.Data" />
61+
<Reference Include="System.Net.Http" />
62+
<Reference Include="System.Xml" />
63+
</ItemGroup>
64+
<ItemGroup>
65+
<Compile Include="Program.cs" />
66+
<Compile Include="Properties\AssemblyInfo.cs" />
67+
</ItemGroup>
68+
<ItemGroup>
69+
<None Include="App.config" />
70+
<None Include="packages.config" />
71+
</ItemGroup>
72+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
73+
</Project>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using Syncfusion.DocIO.DLS;
2+
using Syncfusion.DocIO;
3+
using System.IO;
4+
using Syncfusion.DocToPDFConverter;
5+
using Syncfusion.Pdf;
6+
using Syncfusion.Pdf.Parsing;
7+
8+
namespace Convert_5_pages_in_Word_to_PDF
9+
{
10+
internal class Program
11+
{
12+
static void Main(string[] args)
13+
{
14+
// Open the input Word document
15+
using (FileStream docStream = new FileStream(Path.GetFullPath(@"../../Data/Input.docx"), FileMode.Open, FileAccess.Read))
16+
{
17+
// Load the Word document
18+
using (WordDocument document = new WordDocument(docStream, FormatType.Docx))
19+
{
20+
// Create a converter to convert the Word document to PDF
21+
using (DocToPDFConverter render = new DocToPDFConverter())
22+
{
23+
// Convert the Word document to a PDF document and save it to a MemoryStream
24+
using (MemoryStream pdfStream = new MemoryStream())
25+
{
26+
using (PdfDocument pdfDocument = render.ConvertToPDF(document))
27+
{
28+
// Save the PDF document to the MemoryStream
29+
pdfDocument.Save(pdfStream);
30+
pdfStream.Position = 0; // Reset the stream position to the beginning
31+
32+
// Load the PDF document from the MemoryStream
33+
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(pdfStream))
34+
{
35+
// Get the total number of pages in the PDF document
36+
int totalPages = loadedDocument.Pages.Count;
37+
if (totalPages > 5)
38+
{
39+
// Remove all pages after the 5th page
40+
for (int i = totalPages - 1; i >= 5; i--)
41+
{
42+
loadedDocument.Pages.RemoveAt(i);
43+
}
44+
}
45+
46+
// Create a file stream to save the modified PDF
47+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../Data/First-5-pages-Output.pdf"), FileMode.Create, FileAccess.Write))
48+
{
49+
// Save the modified PDF document to the output file stream
50+
loadedDocument.Save(outputFileStream);
51+
}
52+
}
53+
}
54+
}
55+
}
56+
}
57+
}
58+
59+
}
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("Convert-5-pages-in-Word-to-PDF")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("Convert-5-pages-in-Word-to-PDF")]
13+
[assembly: AssemblyCopyright("Copyright © 2024")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("ec0e3d2f-e1cf-4a77-9448-5c871139c469")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="Syncfusion.Compression.Base" version="26.2.9" targetFramework="net48" />
4+
<package id="Syncfusion.DocIO.WinForms" version="26.2.9" targetFramework="net48" />
5+
<package id="Syncfusion.DocToPDFConverter.WinForms" version="26.1.42" targetFramework="net48" />
6+
<package id="Syncfusion.Licensing" version="26.2.9" targetFramework="net48" />
7+
<package id="Syncfusion.OfficeChart.Base" version="26.2.9" targetFramework="net48" />
8+
<package id="Syncfusion.Pdf.WinForms" version="26.2.9" targetFramework="net48" />
9+
</packages>

0 commit comments

Comments
 (0)