←Select platform

BeginDocument(Stream,DocumentFormat) Method

Summary

Creates a new document stream and prepares it for the new pages to be added.

Syntax
C#
C++/CLI
Python
public void BeginDocument( 
   Stream stream, 
   DocumentFormat format 
) 
public:  
   void BeginDocument( 
      Stream^ stream, 
      DocumentFormat^ format 
   ) 
def BeginDocument(self,stream,format): 

Parameters

stream

The stream that will contain the new document file.

format

The format of the new document.

Remarks

Note that the DocumentWriter object does not own stream and it should be kept alive between calls to BeginDocument and EndDocument. The DocumentWriter object can only be disposed by the user after EndDocument is finished.

Use the DocumentWriter class to create multipage and searchable documents from one or more SVG, EMF, or raster image-based pages, as follows:

  1. DocumentWriter.BeginDocument
  2. DocumentWriter.AddPage or DocumentWriter.InsertPage>
  3. DocumentWriter.EndDocument
  4. DocumentWriter.Convert

Refer to LEADTOOLS Document Writers for more information.

Various popular formats are supported, including PDF, DOC/DOCX, XPS, HTML, RTF, and Text. For a list of all the document formats supported, refer to the DocumentFormat enumeration.

For more information on creating documents from EMF objects, refer to DocumentWriterEmfPage.

For more information on creating mixed documents from SVG, EMF, and empty pages; refer to DocumentWriterRasterPage.

Example

This example shows how to create a document stream from SVG-based pages using the basic functionality of the DocumentWriter class.

C#
Java
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Document.Writer; 
using Leadtools.Ocr; 
 
 
public void DocumentWriterStreamExample() 
{ 
   var inputFileName = Path.Combine(LEAD_VARS.ImagesDir, "Leadtools.docx"); 
   var outputFileName = Path.Combine(LEAD_VARS.ImagesDir, "Example.pdf"); 
 
   // Setup a new RasterCodecs object 
   var codecs = new RasterCodecs(); 
   codecs.Options.RasterizeDocument.Load.Resolution = 300; 
 
   // Get the number of pages in the input document 
   var pageCount = codecs.GetTotalPages(inputFileName); 
 
   // Create a new instance of the LEADTOOLS Document Writer 
   var docWriter = new DocumentWriter(); 
 
   // Change the PDF options 
   var pdfOptions = docWriter.GetOptions(DocumentFormat.Pdf) as PdfDocumentOptions; 
   pdfOptions.DocumentType = PdfDocumentType.PdfA; 
   docWriter.SetOptions(DocumentFormat.Pdf, pdfOptions); 
 
   // Create a new PDF document stream 
   using (Stream stream = new MemoryStream()) 
   { 
      docWriter.BeginDocument(stream, DocumentFormat.Pdf); 
 
      // Loop through all the pages 
      for (var pageNumber = 1; pageNumber <= pageCount; pageNumber++) 
      { 
         // Get the page as SVG 
         Debug.WriteLine("Loading page {0}", pageNumber); 
         var page = new DocumentWriterSvgPage(); 
         page.SvgDocument = codecs.LoadSvg(inputFileName, pageNumber, null); 
 
         // Add the page 
         Debug.WriteLine("Adding page {0}", pageNumber); 
         docWriter.AddPage(page); 
 
         page.SvgDocument.Dispose(); 
      } 
 
      // Finally finish writing the PDF file on disk 
      docWriter.EndDocument(); 
      codecs.Dispose(); 
 
      // Save the stream to the output file 
      stream.Seek(0, SeekOrigin.Begin); 
      using (var fileStream = File.Create(outputFileName)) 
         stream.CopyTo(fileStream); 
 
      Debug.WriteLine("Creating new PDF document: {0}", outputFileName); 
   } 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
} 
 
import static org.junit.Assert.assertTrue; 
 
import java.io.ByteArrayInputStream; 
import java.io.ByteArrayOutputStream; 
import java.io.File; 
import java.io.IOException; 
 
import org.junit.*; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 
 
import leadtools.*; 
import leadtools.codecs.*; 
import leadtools.document.writer.*; 
import leadtools.ocr.*; 
 
 
public void documentWriterStreamExample() throws IOException { 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
   String inputFileName = combine(LEAD_VARS_IMAGES_DIR, "LEADTOOLSEditor.docx"); 
   String outputFileName = combine(LEAD_VARS_IMAGES_DIR, "ExternalAnnotationsDoc.pdf"); 
 
   // Setup a new RasterCodecs object 
   RasterCodecs codecs = new RasterCodecs(); 
   codecs.canLoadSvg(inputFileName); 
   codecs.getOptions().getRasterizeDocument().getLoad().setResolution(300); 
 
   // Get the number of pages in the input document 
   int pageCount = codecs.getTotalPages(inputFileName); 
 
   // Create a new instance of the LEADTOOLS Document Writer 
   DocumentWriter docWriter = new DocumentWriter(); 
 
   // Change the PDF options 
   PdfDocumentOptions pdfOptions = (PdfDocumentOptions) docWriter.getOptions(DocumentFormat.PDF); 
   pdfOptions.setDocumentType(PdfDocumentType.PDFA); 
   docWriter.setOptions(DocumentFormat.PDF, pdfOptions); 
 
   // Create a new PDF document stream 
   ILeadStream leadStream = LeadStreamFactory.create(outputFileName); 
   docWriter.beginDocument(leadStream, DocumentFormat.PDF); 
 
   // Loop through all the pages 
   for (int pageNumber = 1; pageNumber <= pageCount; pageNumber++) { 
      // Get the page as SVG 
      System.out.printf("Loading page %s\n", pageNumber); 
      DocumentWriterSvgPage page = new DocumentWriterSvgPage(); 
      if (!codecs.canLoadSvg(inputFileName)) 
         return; 
      ISvgDocument loadedSvg = codecs.loadSvg(inputFileName, pageNumber, null); 
      page.setSvgDocument(loadedSvg); 
 
      // Add the page 
      System.out.printf("Adding page %s\n", pageNumber); 
      docWriter.addPage(page); 
 
      page.getSvgDocument().dispose(); 
   } 
 
   // Finally finish writing the PDF file on disk 
   docWriter.endDocument(); 
   codecs.dispose(); 
 
   // Save the stream to the output file 
   leadStream.seek(LeadSeekOrigin.BEGIN, 0); 
   System.out.printf("Creating new PDF document: %s\n", outputFileName); 
   LeadStreamFactory.create(outputFileName); 
       
   assertTrue((new File(outputFileName)).exists()); 
   System.out.println("Document successfully created"); 
} 
Requirements

Target Platforms

Help Version 23.0.2024.2.29
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Document.Writer Assembly

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.