Click here to Skip to main content
15,667,355 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
There is word file.i want to convert each page of word file into different pdf files.

What I have tried:

Convert each page of word file into pdf
Posted
Updated 28-Nov-17 0:44am
Comments
Richard MacCutchan 28-Nov-17 4:29am    
What is the question?
Member 13460675 30-Nov-17 4:57am    
suppose this code generating two pdf.i want to give one pdfs name as ABC and another pdf should have name XYZ then how to do this.
[no name] 30-Nov-17 22:57pm    
Split method always put a number (Pages.Count-1) in the name of generated file. You could try another way to split document and specify the destination file names.
//load word document
Document doc = new Document("WordDocument.docx");
//save to stream in pdf file format
var stream = new MemoryStream();
doc.SaveToStream(stream, Spire.Doc.FileFormat.PDF);
//load pdf from stream
PdfDocument pdf = new PdfDocument(stream);
int pageCount = pdf.Pages.Count;
PdfPageBase page;           
string[] destFileName = new string[] { "ABC", "DEF", "GHI"};
for (int i = 0; i < pageCount; i++)
{
    PdfDocument newPdf = new PdfDocument();
    //add a page to newPdf
    page = newPdf.Pages.Add(pdf.Pages[i].Size, new Spire.Pdf.Graphics.PdfMargins(0));
    //draw content on the page
    pdf.Pages[i].CreateTemplate().Draw(page, new System.Drawing.PointF(0, 0));
    //save file with specified name
    newPdf.SaveToFile(destFileName[i]+".pdf");
}

1 solution

Here is the solution that worked for me
This code snippet needs any explanation, because it's clear to understand.

using Word = Microsoft.Office.Interop.Word;
.
.
.
object FileName = @"c:\WordDocument.docx";
object falseValue = false; 
object missingValue = Missing.Value;
Word.Application wordApplication = new Word.Application();
Word.Document wordDocument = wordApplication.Documents.Open(ref FileName,
ref missingValue, ref missingValue, ref missingValue, ref missingValue, 
ref missingValue, ref missingValue, ref missingValue, ref missingValue, 
ref missingValue, ref missingValue, ref missingValue, ref missingValue, 
ref missingValue, ref missingValue, ref missingValue);
         
object EndOfPage = @"\page";
object wdWhat = WdGoToItem.wdGoToPage;
object wdWich = WdGoToDirection.wdGoToNext;
object count = (object)1;
int TotalPages, i;

TotalPages = wordDocument.ComputeStatistics(WdStatistic.wdStatisticPages, ref falseValue);

for (i = 1; i <= TotalPages; i++)
{
   //Select the content of actual page.
   Word.Range range  = wordDocument.Bookmarks.get_Item(ref EndOfPage).Range;
   //Export selected Range to PDF
   range.ExportAsFixedFormat(path, Word.WdExportFormat.wdExportFormatPDF);
   //Move to next Page
   range = range.GoTo(ref wdWhat, ref wdWich, ref count, ref missingValue);
   range.Select();
}
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900