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

Question

Question

Importing electronic document from Stream using RA and C#

asked on April 10, 2019

Hello, I'm learning Repository Access and am working on code to query the API for Eversign and download a resulting PDF into a Laserfiche Electronic File. This means that unlike examples I've seen for creating electronic files, I'll get the document from an HTTPWebResponse instead of from a file on the file system.

This code creates a PDF electronic file in Laserfiche, but the length of the PDF in Laserfiche is always zero. I'm stumped and would welcome being directed to documentation or examples that would help me crack this.

Here's the relevant section of code:

FolderInfo myFolder = Folder.GetFolderInfo("\\My Folder", session);
docInfo.Create(myFolder, "Created By Document Builder", EntryNameOption.AutoRename);

Stream responseStream = response.GetResponseStream();

using (MemoryStream buffer = new MemoryStream())
{
	// copy from http response to a MemoryStream in order to get the length prior to calling WriteEDoc()
	responseStream.CopyTo(buffer);

	using (Stream eDocStream = docInfo.WriteEdoc(response.ContentType, buffer.Length))
	{
		docInfo.Extension = "pdf";
		buffer.CopyTo(eDocStream); // The length of eDocStream is 88534 after running this statement
		docInfo.Save();
		docInfo.Unlock();
	}
}

// log out of the repository
session.LogOut();

Thank you!

0 0

Answer

SELECTED ANSWER
replied on April 10, 2019

This is the code I'm using to write PDFs to documents (my stream variable is ms instead of buffer).

// Write PDF to document
using(Stream eDocStream = doc.WriteEdoc("application/pdf",ms.ToArray().LongLength)){
    eDocStream.Write(ms.ToArray(),0,ms.ToArray().Length);
}
1 0

Replies

replied on April 11, 2019

Try moving the Save/Unlock calls outside of the using block, so that the edoc write stream is flushed before unlocking the document.

2 0
replied on April 11, 2019

Thank you Jason and Robert. I've implemented your suggestions and it works.

I anticipate that your replies will help someone else learning to do the same thing.

0 0
replied on April 11, 2019 Show version history

Another tip from my experience, put part of your code into a try-catch-finally or just a try-finally and put the docInfo.Unlock() call in the Finally.

The purpose of that is to ensure that if something goes wrong, the document doesn't get stuck in a locked state because it will always try to unlock even if an exception occurs.

Don't put the save in there, just the unlock, and possible the session logout.

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

Sign in to reply to this post.