Office2PDF: How can you convert an Office document on a page-by-page basis?

Question:
I have a Microsoft Office document (.doc, .docx, .xls, .xlsx) that I would like to convert to PDF using the Apryse SDK’s Document Conversion API on a page-by-page basis. How can I do this?

Answer:
This can be achieved by using the Document Conversion API’s ConvertNextPage() functionality. Using ConvertNextPage() over another method dedicated to conversion such as Convert() will allow us to convert selectively. This is useful for handling each page selectively. By utilizing GetConversionStatus() in conjunction with this method of conversion, we can efficiently convert the entire Office document to PDF. Here is an example implementation of how that might look in your application:

   using (PDFDoc doc = new PDFDoc())
   {
      OfficeToPDFOptions options = new OfficeToPDFOptions();
      DocumentConversion conversion = pdftron.PDF.Convert.StreamingPDFConversion(
         doc, inputFile, options);

      int maxCellCount = 200000; // Set to your desired cell count for XLS or XLSX

      while (conversion.GetConversionStatus() == DocumentConversionResult.e_document_conversion_incomplete)
      {
         // If you are converting an Excel file, you may want to consider skipping certain sheets if their cell count is too large.
         // This is not necessary for non-Excel conversions (.doc, .docx).
         if (conversion.GetNextExcelSheetCellCount() > maxCellCount)
            conversion.SkipNextExcelSheet();

         // Otherwise, convert the next page as normal.
         conversion.ConvertNextPage();

         // Print info about conversion (if any).
         int numWarnings = conversion.GetNumWarnings();
         for (int i = 0; i < numWarnings; ++i)
            Console.WriteLine("Warning: " + conversion.GetWarningString(i));
         
         Console.WriteLine("Converted sheet " + conversion.GetCurrentExcelSheetName());
      }

      // Save the output.
      if (conversion.GetConversionStatus() == DocumentConversionResult.e_document_conversion_success)
      {
         doc.Save(outputFile, SDFDoc.SaveOptions.e_linearized);
      }
      else
      {
         Console.WriteLine(conversion.GetErrorString());
      }
   }
}
1 Like