You are viewing limited content. For full access, please sign in.

Question

Question

ImportEngine using Stream

SDK
asked on April 5, 2016 Show version history

I am trying to build a helper library that we will use to construct the ImportEngine XML files for a given PDF, but each time I try to run ImportEngine.BeginProcess(myStream) I get "Root element is missing."

Here is what I have been doing:

public void CreatePdf(string xmlTemplatePath, sting pdfName, string xmlTemplatePath){
	var xmlTemplate = XDocument.Load(xmlTemplatePath);

	var documentSettingsElement = xmlTemplate.Root.Descendants().FirstOrDefault(e => e.Name.LocalName.ToLower() == "document");
	documentSettingsElement?.SetAttributeValue("name", pdfName);

	var fileSettingsElement = xmlTemplate.Root.Descendants().FirstOrDefault(e => e.Name.LocalName.ToLower() == "fileref");
	fileSettingsElement?.SetAttributeValue("ref", pdfPath);

	var importDoc = ParseXmlTemplate(pdfName, pdfPath, xmlTemplatePath);

	var writerSettings = new XmlWriterSettings
	{
		OmitXmlDeclaration = false,
		Indent = false,
		IndentChars = "",
		ConformanceLevel = ConformanceLevel.Document,
		NewLineChars = "",
		CheckCharacters = true,
		Encoding = new UTF8Encoding(false)
	};

	var memoryStream = new MemoryStream();
	using (var writer = XmlWriter.Create(memoryStream, writerSettings))
	{
		importDoc.WriteTo(writer);
	}
	memoryStream.Position = 0;
	
	using (var session = CreateSession(_laserficheServer, _laserficheRepository, _laserficheUsername, _laserfichePassword))
	{
		var lfImportEngine = CreateImportEngine(_laserficheUploadPath, _laserficheVolume, session);

		var import = lfImportEngine.BeginProcess(memoryStream);
		while (!import.IsCompleted)
		{
			// wait for 1 second, and then refresh the status
			Thread.Sleep(1000);
			import.Refresh();
		}
		if (import.HasFailed)
		{
			session.LogOut();
			throw new Exception(import.FailureReason.Message);
		}
	}
}

Here is the XML from my template file:

<?xml version="1.0" encoding="utf-8"?>
<LF:importengine xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://laserfiche.com/namespaces/importengine ImportEngine.xsd"
                xmlns:LF="http://laserfiche.com/namespaces/importengine" version="1.0">
    
    <LF:toc on_document_conflict="unique" on_folder_conflict="unique">
      <LF:document name="DefaultDocumentName" comment="" language="EN" template="">
        <LF:electronic_document content_type="application/pdf" extension="pdf">
          <LF:fileref ref="C:\Users\visual studio 2015\Projects\LaserficheProofOfConcept\Files\LaserficheSdkImport.pdf" />
        </LF:electronic_document>
      </LF:document>
    </LF:toc>
  
</LF:importengine>

I'm hoping I'm just missing something simple, but have spent the better part of two days trying to figure out why this doesn't work. 

I have omitted a couple methods that I use to build the Session object and the ImportEngine. I have verified that if I use the String overload for BeginProcess and supply a path to the XML template file, ImportEngine performs the import without issue.

0 0

Replies

replied on December 19, 2016

Hello Greg,

     I am unsure which SDK version you are using. However I do have a suggestion. Instead of using ImportEngine you can try to use the DocumentImporter. I have a sample slice of code in C# that I use in a helper class. See below. Just Replace the FileStream contents with your XmlWriter contents and it should work just fine. Please note that your xml document will need to contain an image. If not then you will need to convert the text data to an image and then import.

using (DocumentInfo doc = new DocumentInfo(my2Sess))
{
      EntryInfo entry = Entry.TryGetEntryInfo(LaserFicheFolder, my2Sess);
      FolderInfo fi = entry as FolderInfo;
      doc.Extension = extension;
      doc.MimeType = "application/" + extension.ToLower();
      doc.Create(fi, filename, Volume, EntryNameOption.AutoRename);
       int Docid = doc.Id;
       DocumentImporter DI = new DocumentImporter();
       DI.Document = doc;
       FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
       fs.Position = 0;
       DI.ImportImages(fs);
       fs.Close();
}

 

0 1
You are not allowed to follow up in this post.

Sign in to reply to this post.