Tutorial: Document Writers Tutorial

Take the following steps to start a project and to add some code that uses SVG to convert a Microsoft Word DOCX file to PDF:

  1. Start Visual Studio.
  2. Choose File->New->Project from the menu.
  3. In the New Project dialog box, choose either "Visual C# Projects" or "VB Projects" in the Projects Type List, and choose "Console Application" in the Templates List.
  4. Type the project name as "DocumentWritersTutorial" in the Project Name field, and then click OK. If desired, type a new location for your project or select a directory using the Browse button, and then click OK.
  5. Select Project->Properties from the menu, select the Build tab and change the Platform Targert from Any CPU to x86.
  6. In the Solution Explorer window, right-click on the References folder, and select "Add Reference..." from the context menu. In the "Add Reference" dialog box, select the ".NET" tab, browse to the "<LEADTOOLS_INSTALLDIR>\Bin\Dotnet\Win32" folder, and select the following DLLs:

    • Leadtools.dll
    • Leadtools.Codecs.dll
    • Leadtools.Codecs.Cmp.dll
    • Leadtools.Codecs.Fax.dll
    • Leadtools.Codecs.Tif.dll
    • Leadtools.Codecs.Dox.dll
    • Leadtools.Codecs.Jb2.dll
    • Leadtools.Codecs.Png.dll
    • Leadtools.Codecs.Wmf.dll
    • Leadtools.Svg.dll
    • Leadtools.Pdf.dll
    • Leadtools.Forms.DocumentWriters.dll

    Click the Select button and then press the OK button to add the above DLLs to the application.

  7. Add the following lines at the beginning of the file:

    C#
    VB
    using System.IO; 
    using System.Diagnostics; 
    using Leadtools; 
    using Leadtools.Codecs; 
    using Leadtools.Forms.DocumentWriters; 
                      
         
    Imports System.IO 
    Imports System.Diagnostics 
    Imports Leadtools 
    Imports Leadtools.Codecs 
    Imports Leadtools.Forms.DocumentWriters 
                      
         

  8. Add the following code snippet to the Main method:

    C#
    VB
    var inFileName = @"C:\users\Public\Documents\LEADTOOLS Images\Leadtools.docx"; 
    var format = DocumentFormat.Pdf; 
                      
    // Create the output file name 
    var dir = Path.GetDirectoryName(inFileName); 
    var outFileName = Path.Combine(dir, "DocumentWriterTutorial." + DocumentWriter.GetFormatFileExtension(format)); 
                      
    var codecs = new RasterCodecs(); 
                      
    // Set 300 as the default resolution for loading document files 
    codecs.Options.RasterizeDocument.Load.Resolution = 300; 
                      
    // Get the number of pages of the input file 
    var pageCount = codecs.GetTotalPages(inFileName); 
                      
    Console.WriteLine("Number of pages {0}", pageCount); 
                      
    // Create an instance of the LEADTOOLS DocumentWriter 
    var docWriter = new DocumentWriter(); 
    // Change this file to create a PDF file with overlay image 
    var imageOverText = false; 
                      
    // Set the options 
    if (format == DocumentFormat.Pdf) 
    { 
       // If PDF, set to PDF/A 
       var pdfOptions = docWriter.GetOptions(format) as PdfDocumentOptions; 
       pdfOptions.DocumentType = PdfDocumentType.PdfA; 
       if (imageOverText) 
          pdfOptions.ImageOverText = true; 
       docWriter.SetOptions(format, pdfOptions); 
    } 
                      
    // Begin a new PDF document 
    docWriter.BeginDocument(outFileName, format); 
                      
    // Add the pages 
    for (var pageNumber = 1; pageNumber <= pageCount; pageNumber++) 
    { 
       // Load the page as SVG 
       Console.WriteLine("Loading page {0}", pageNumber); 
       var page = new DocumentSvgPage(); 
       page.SvgDocument = codecs.LoadSvg(inFileName, pageNumber, null); 
       if (imageOverText) 
       { 
          // If we are using image/text, then load the overlay raster image 
          page.Image = codecs.Load(inFileName, pageNumber); 
       } 
                      
       // Add the page to the current document 
       Console.WriteLine("Adding page {0}", pageNumber); 
       docWriter.AddPage(page); 
                      
       if (page.SvgDocument != null) 
          page.SvgDocument.Dispose(); 
       if (page.Image != null) 
          page.Image.Dispose(); 
    } 
                      
    // Done, finish 
    Console.WriteLine("Converting to final format"); 
    docWriter.EndDocument(); 
    codecs.Dispose(); 
                      
    Console.WriteLine("Viewing the result"); 
                      
    // Open the document in the default viewer 
    Process.Start(outFileName); 
                      
         
    Dim inFileName As String = "C:\users\Public\Documents\LEADTOOLS Images\Leadtools.docx" 
    Dim format As DocumentFormat = DocumentFormat.Pdf 
                     
    ' Create the output file name 
    Dim dir As String = Path.GetDirectoryName(inFileName) 
    Dim outFileName As String = Path.Combine(dir, "DocumentWriterTutorial." + DocumentWriter.GetFormatFileExtension(format)) 
                     
    Dim codecs As New RasterCodecs 
                     
    ' Set 300 as the default resolution for loading document files 
    codecs.Options.RasterizeDocument.Load.Resolution = 300 
                     
    ' Get the number of pages of the input file 
    Dim pageCount As Integer = codecs.GetTotalPages(inFileName) 
                     
    Console.WriteLine("Number of pages {0}", pageCount) 
                     
    ' Create an instance of the LEADTOOLS DocumentWriter 
    Dim docWriter As New DocumentWriter() 
    ' Change this file to create a PDF file with overlay image 
    Dim imageOverText As Boolean = False 
                     
    ' Set the options 
    If format = DocumentFormat.Pdf Then 
       ' If PDF, set to PDF/A 
       Dim pdfOptions = DirectCast(docWriter.GetOptions(format), PdfDocumentOptions) 
       ' pdfOptions.DocumentType = PdfDocumentType.PdfA 
       If imageOverText Then pdfOptions.ImageOverText = True 
       docWriter.SetOptions(format, pdfOptions) 
    End If 
                     
    ' Begin a new PDF document 
    docWriter.BeginDocument(outFileName, format) 
                     
    ' Add the pages 
    For pageNumber As Integer = 1 To pageCount 
       ' Load the page as SVG 
       Console.WriteLine("Loading page {0}", pageNumber) 
       Dim page As New DocumentSvgPage() 
       page.SvgDocument = codecs.LoadSvg(inFileName, pageNumber, Nothing) 
       If imageOverText Then 
          ' If we are using image/text, then load the overlay raster image 
          page.Image = codecs.Load(inFileName, pageNumber) 
       End If 
                     
       ' Add the page to the current document 
       Console.WriteLine("Adding page {0}", pageNumber) 
       docWriter.AddPage(page) 
                     
       If Not IsNothing(page.SvgDocument) Then page.SvgDocument.Dispose() 
       If Not IsNothing(page.Image) Then page.Image.Dispose() 
    Next 
                     
    ' Done, finish 
    Console.WriteLine("Converting to final format") 
    docWriter.EndDocument() 
    codecs.Dispose() 
                     
    Console.WriteLine("Viewing the result") 
                     
    ' Open the document in the default viewer 
    Process.Start(outFileName) 
                      
         

  9. Build and Run the program to test it.

  10. For information on using the LEADTOOLS Document Writers, refer to Programming with the LEADTOOLS Document Writers. NOTE: If you encounter an "Invalid File Format" or "Feature Not Supported" exception, refer to Invalid File Format/Feature Not Supported.
Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.
LEADTOOLS Imaging, Medical, and Document
Click or drag to resize