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

Question

Question

Resize document pages using SDK

asked on September 1, 2017 Show version history

I am trying to crop the pages of a document using the RA SDK.  Here is the code I have so far...

 

 const int pageWidth = 2550;
 const int pageHeight = 3300;

DocumentInfo doc = Document.GetDocumentInfo(entryId, session);

for (int pageNumber = 1; pageNumber <= doc.PageCount; pageNumber++)
{
	var repositoryPage = doc.GetPageInfo(pageNumber);
	if (repositoryPage.ImageWidth > pageWidth || repositoryPage.ImageHeight > pageHeight)
	{
		using (var reader = repositoryPage.ReadPagePart(PagePart.Image))
		{
			// Move to stream so FromStream will recognize it as a TIFF image
			using (MemoryStream mstream = new MemoryStream())
			{
				reader.CopyTo(mstream);
				// Resize Image
				Rectangle cropRect = new Rectangle(0, 0, pageWidth, pageHeight);
				Bitmap src = Image.FromStream(mstream) as Bitmap;
				Bitmap target = new Bitmap(cropRect.Width, cropRect.Height);
				Bitmap bmpImage = new Bitmap(src);
				var dest = bmpImage.Clone(cropRect, src.PixelFormat);

				// Save resized document as a TIFF
				using (MemoryStream memoryStream = new MemoryStream())
				{
					dest.Save(memoryStream, ImageFormat.Tiff);

					// Write page back to Laserfiche document
					using (Stream writer = repositoryPage.WritePagePart(PagePart.Image, (int)memoryStream.Length))
					{
						byte[] buffer = new byte[0x8000];
						int count;
						while ((count = memoryStream.Read(buffer, 0, buffer.Length)) > 0)
								writer.Write(buffer, 0, count);
					}
				}
			}
		}
	}
}

First off, this code does not work.  When I look at the page size of the document after this code runs it has not changed.   

Secondly, it seems there should be an easier way to do this.  

I am using version 10.2

Any help would be greatly appreciated.

1 0

Replies

replied on September 1, 2017 Show version history

I found out what I was doing wrong.  I had to add

memoryStream.Flush();
memoryStream.Position = 0;

after 

dest.Save(memoryStream, ImageFormat.Tiff);

and now the pages are the correct size.

 

Would still be interested in knowing if there is an easier way.

1 0
replied on September 1, 2017

You can use the DocumentServices.DocumentExporter class to export a scaled version of the image:

for (int pageNumber = 1; pageNumber <= document.PageCount; pageNumber++)
{
    DocumentExporter docExp = new DocumentExporter();
    docExp.PageFormat = DocumentPageFormat.TiffJpeg;
    docExp.ScaleFactor = 5000; // 10,000 = 100%
    using (MemoryStream ms = new System.IO.MemoryStream())
    {
        docExp.ExportPage(document, pageNumber, ms);
                            
        ms.Seek(0, System.IO.SeekOrigin.Begin);

        PageInfo page = document.GetPageInfo(pageNumber);

        // Write page back to Laserfiche document
        using (Stream writer = page.WritePagePart(PagePart.Image, (int)ms.Length))
        {
            byte[] buffer = new byte[0x8000];
            int count;
            while ((count = ms.Read(buffer, 0, buffer.Length)) > 0)
                writer.Write(buffer, 0, count);
        }
    }
}

 

1 0
replied on September 1, 2017

I am wanting to crop the image, not resize it.  Sorry for the confusion. I have corrected my description.

0 0
replied on September 1, 2017

I know Quick Fields has a built-in resize option. If you're licensed for Quick Fields Agent, that would probably be a much easier way to handle page resizing.

0 0
replied on September 1, 2017

We are licensed for it but I am not sure Quick Fields will do what I really need because I have to crop the images of thousands of documents.

 

Here is what I want to do.

 

  • Find all documents in the repository that have a width larger than 2550 OR a height larger than 3330. 
  • Resize only the size that exceeds the value (I do not wish to make the document larger if the document already fits within the page.
  • Save the documents back to the repository.

 

I want to be able to do all this with a push of a button.

I should note that we need to do this because we scanned all our documents with a Paper Size of "Maximum" (so we could get hand-written notes at the edge of the paper), but now when we use the Print function in IE 11 from version 10.2 we get blank pages between every page.  Laserfiche Support has told us this is because our page widths and page heights are too large and the solution is to resize the documents to fit 2550 x 3330.

I couldn't figure out how to do all this in Quick Fields so I started writing my own using the SDK.

 

BTW:  I discovered even the code above does not work in all cases because

var dest = bmpImage.Clone(cropRect, src.PixelFormat);

throws an OutOfMemoryException on some of our documents.

0 0
replied on September 1, 2017

In that case it doesn't sound like Quick Fields would do exactly what you want, although it does have cropping capabilities.

Just be aware that drawing activities like this eat up a lot of resources, so doing this with a large, 300+ DPI color image could be why you get memory exceptions on some.

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

Sign in to reply to this post.