LEADTOOLS Support
Document
Document SDK Questions
Adding Images as Pages to a Document and Show it in DocumentViewer
#1
Posted
:
Tuesday, August 27, 2019 10:56:01 AM(UTC)
Groups: Registered
Posts: 2
Hello,
while currently evaluating LeadTools I'm trying to accomplish a (I though ;-) ) pretty simple task, but apparently having some issues with that:
I have a bunch of RasterImages (acquired from a scanner, but could be any image, of course) that I would like to add as pages to a LEADDocument which should then be shown in a DocumentViewer.
I've tried to create a new page with "_documentViewer.Document.Pages.CreatePage" and then setting the page image with "documentPage.SetImage", but this seems not to work.
I would appreciate any idea on this pointing me to the right direction. :-)
Best regards and thanks in advance
Andreas
#2
Posted
:
Wednesday, August 28, 2019 4:53:44 PM(UTC)
Groups: Registered, Tech Support, Administrators
Posts: 105
Was thanked: 3 time(s) in 3 post(s)
Hello,
If you are looking to add a scanned image to an existing LEADDocument in the Document Viewer, you will need to create a virtual document of the LEADDocument to set the scanned image to, then add that virtual LEADDocument to the Document Viewer UI. You need to use the Document Factory, since if you just had a regular LEADDocument in the viewer it is not seen as virtual therefor you are unable to edit it (Add Pages, Delete Pages, etc.).
For your reference I have added a code snippet below that showcases this step.
Code:// Global virtual document
LEADDocument virtualDocument;
CreateDocumentOptions cdo = new CreateDocumentOptions
{
Cache = cache,
UseCache = true
};
virtualDocument = DocumentFactory.Create(cdo);
After creating the new virtual LEADDocument you can create a new document page and add this to the virtual document. Then call the SetImage(e.Image) method inside the TwainAcquirePage Event. Should look similar to this:
private void TwainSession_AcquirePage(object sender, TwainAcquirePageEventArgs e)
{
documentViewer.BeginUpdate();
DocumentPage documentPage = virtualDocument.Pages.CreatePage(e.Image.ImageSize.ToLeadSizeD(), e.Image.XResolution);
virtualDocument.Pages.Add(documentPage);
documentPage.SetImage(e.Image.Clone());
documentViewer.EndUpdate();
}
Please let me know if you have any further questions. If you need a proof of concept application I would just need to know what type of application you are wanting to create (WPf, Winforms, etc.)
Thanks
Edited by moderator Monday, August 3, 2020 8:21:48 AM(UTC)
| Reason: Not specified
Matt Bresson
Developer Support Engineer
LEAD Technologies, Inc.
#3
Posted
:
Thursday, August 29, 2019 2:50:07 AM(UTC)
Groups: Registered
Posts: 2
Hello Matt,
first thanks a lot for your detailed reply!
So you are creating a new "LEADDocument" object and add the new page there.
I see you are calling "documentViewer.BeginUpdate()" and "documentViewer.EndUpdate()", but you never use "documentViewer.SetDocument()" to assign the new document to the document viewer:
So how does the document viewer know that the newly document should be displayed?
And another question:
If the document viewer display a document already and I just want to add an additional page from a RasterImage to this document, can I then directly use "documentViewer.Document.Pages.Add(...)" or do I always need to create a fresh new "LEADDocument" instance first?
I'm using WinForms by the way.
Best regards and again thanks for your detailed reply
Andreas
#4
Posted
:
Thursday, August 29, 2019 1:05:13 PM(UTC)
Groups: Registered, Tech Support, Administrators
Posts: 105
Was thanked: 3 time(s) in 3 post(s)
Hello Andreas,
You will not have to create a new LEADDocument every time you want to edit the document. Just create a global virtual document as shown above. You can just set the document in the viewer as shown below.
Code:// Global virtual document
LEADDocument virtualDocument;
CreateDocumentOptions cdo = new CreateDocumentOptions
{
Cache = cache,
UseCache = true
};
virtualDocument = DocumentFactory.Create(cdo);
OpenFileDialog ofd = new OpenFileDialog
{
InitialDirectory = @"C:\Users\Public\Documents\LEADTOOLS Images"
};
if (ofd.ShowDialog() == true)
{
var options = new LoadDocumentOptions
{
UseCache = true,
Cache = cache
};
LEADDocument document = DocumentFactory.LoadFromFile(ofd.FileName, options);
virtualDocument.Pages.AddRange(document.Pages);
SetDocument(virtualDocument);
}
After you set the document you can then add pages like I showed in the code snippet in the above post with creating a new DocumentPage and using documentPage.SetImage(....). So instead of directly using "documentViewer.Document.Pages.Add(...)" create a new DocumentPage and add it to the global virtual document.
Code:DocumentPage documentPage = virtualDocument.Pages.CreatePage(e.Image.ImageSize.ToLeadSizeD(), e.Image.XResolution);
virtualDocument.Pages.Add(documentPage);
Let me know if you have any further questions.
Thanks
Edited by user Friday, October 18, 2019 10:47:08 AM(UTC)
| Reason: Not specified
Matt Bresson
Developer Support Engineer
LEAD Technologies, Inc.
LEADTOOLS Support
Document
Document SDK Questions
Adding Images as Pages to a Document and Show it in DocumentViewer
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.