Convert diapason of PDF pages to Word file in C# and .NET


In today's world, working with various documents is a daily task. This becomes especially relevant when extracting or converting a specific page range from a multi-page PDF file to an editable Word format (.docx). SautinSoft's library from PDF Focus .NET offers an excellent solution for this task, significantly simplifying the task and improving the quality of the results.

Converting a range of PDF pages to Word is the process of converting selected pages from a PDF document into a format compatible with word processors, allowing you to edit content, transfer tables, graphics, and formatting that are no longer possible in the original PDF. This approach is suitable for a variety of scenarios: report preparation, archiving, and highlighting important sections of a document.

The code that implements converting a range of PDF pages to Word allows you to:

  • Automate document preparation.
  • Speed up the processing of groups of files.
  • Integrate into business processes.
  • Ensure accurate conversion of only the required pages.
  • Avoid manual processing errors.
  • What interesting aspects are related to the use case?

    1. Page range selection: You can specify specific pages or ranges, such as 1-3, 5, 7-10.
    2. Large file processing: Efficiently handle large PDF documents without sacrificing performance.
    3. Conversion quality settings: Parameters that affect conversion accuracy.
    4. Error and exception handling: To ensure application stability.
    5. Process automation: Integration into scripts, APIs, or backend services.

    This simple Console App shows how to convert specified page diapason (2 to 4) from PDF into RTF document.

    Complete code

    using System;
    using System.IO;
    
    namespace Sample
    {
        class Sample
        {
            static void Main(string[] args)
            {
                // Before starting, we recommend to get a free key:
                // https://sautinsoft.com/start-for-free/
                
                // Apply the key here:
                // SautinSoft.PdfFocus.SetLicense("...");
    			
                string inpFile = Path.GetFullPath(@"..\..\..\Potato Beetle.pdf");
                string outFile = "Result.rtf";
    			
                SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();
    
                f.OpenPdf(inpFile);
    
                if (f.PageCount > 0)
                {
                    // You may set an output format to docx or rtf.
                    f.WordOptions.Format = SautinSoft.PdfFocus.CWordOptions.eWordDocument.Rtf;
    
                    // Specify to convert these pages: 2 - 4  and 6.
    
                    // Way 1:
                    f.RenderPagesString = "2-4, 6";
    
                    // Way 2 (do the same as Way 1):
                    f.RenderPages = new int[][] {new int[] {2, 4},
                                                 new int[] {6, 6}};
    
                    int result = f.ToWord(outFile);
    
                    // Open the result.
                    if (result == 0)
                    {
                        System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true });
                    }
                }
            }
        }
    }
    

    Download

    Imports System.IO
    Imports System.Drawing.Imaging
    Imports System.Collections.Generic
    Imports SautinSoft
    
    Module Sample
    
        Sub Main()
            ' Before starting, we recommend to get a free key:
            ' https://sautinsoft.com/start-for-free/
    
            ' Apply the key here
            ' SautinSoft.PdfFocus.SetLicense("...");
    
    		Dim inpFile As String = Path.GetFullPath("..\..\..\Potato Beetle.pdf")
    		Dim outFile As String = "Result.rtf"
    		
    		Dim f As New SautinSoft.PdfFocus()
    		f.OpenPdf(inpFile)
    
    		If f.PageCount > 0 Then
    			' You may set an output format to docx or rtf.
    			f.WordOptions.Format = SautinSoft.PdfFocus.CWordOptions.eWordDocument.Rtf
    
    			' Specify to convert these pages: 2 - 4  and 6.
    
    			' Way 1:
    			f.RenderPagesString = "2-4, 6"
    
    			' Way 2 (do the same as Way 1):
    			f.RenderPages = New Integer()() {
    					New Integer() {2, 4},
    					New Integer() {6, 6}
    				}
    
    			Dim result As Integer = f.ToWord(outFile)
    
    			' Open the result.
    			If result = 0 Then
    				System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outFile) With {.UseShellExecute = True})
    			End If
    		End If
    	End Sub
    End Module
    

    Download


    If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:


    Captcha

    Questions and suggestions from you are always welcome!

    We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.