Skip to content

Commit 2044b53

Browse files
Convert-5-pages-in-Word-to-PDF
1 parent 1d2015b commit 2044b53

File tree

1 file changed

+28
-31
lines changed
  • Word-to-PDF-Conversion/Convert-5-pages-in-Word-to-PDF/.NET-Framework/Convert-5-pages-in-Word-to-PDF

1 file changed

+28
-31
lines changed

Word-to-PDF-Conversion/Convert-5-pages-in-Word-to-PDF/.NET-Framework/Convert-5-pages-in-Word-to-PDF/Program.cs

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,45 +20,42 @@ static void Main(string[] args)
2020
// Create a converter to convert the Word document to PDF
2121
using (DocToPDFConverter render = new DocToPDFConverter())
2222
{
23-
// Convert the Word document to a PDF document
24-
using (PdfDocument pdfDocument = render.ConvertToPDF(document))
23+
// Convert the Word document to a PDF document and save it to a MemoryStream
24+
using (MemoryStream pdfStream = new MemoryStream())
2525
{
26-
// Create a file stream to save the converted PDF
27-
using (FileStream docStream1 = new FileStream(Path.GetFullPath(@"../../Data/Output.pdf"), FileMode.Create, FileAccess.Write))
26+
using (PdfDocument pdfDocument = render.ConvertToPDF(document))
2827
{
29-
// Save the PDF document to the output file stream
30-
pdfDocument.Save(docStream1);
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+
}
3153
}
3254
}
3355
}
3456
}
3557
}
3658

37-
// Get stream from the newly created PDF document
38-
using (FileStream inputFileStream = new FileStream(Path.GetFullPath(@"../../Data/Output.pdf"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
39-
{
40-
// Load the PDF document
41-
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputFileStream))
42-
{
43-
// Get the total number of pages in the PDF document
44-
int totalPages = loadedDocument.Pages.Count;
45-
if (totalPages > 5)
46-
{
47-
// Remove all pages after the 5th page
48-
for (int i = totalPages - 1; i >= 5; i--)
49-
{
50-
loadedDocument.Pages.RemoveAt(i);
51-
}
52-
}
53-
54-
// Create a file stream to save the modified PDF
55-
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../Data/First-5-pages-Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
56-
{
57-
// Save the modified PDF document to the output file stream
58-
loadedDocument.Save(outputFileStream);
59-
}
60-
}
61-
}
6259
}
6360
}
6461
}

0 commit comments

Comments
 (0)