Splitting a large image which contains text into multiple images for printing

protected void Button1_Click(object sender, EventArgs e)

{

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);


string dataDir = @"D:\Aspose\RDP\NEWFiles\";


Shape shape = builder.InsertImage(System.Drawing.Image.FromFile(dataDir + "GridImage.jpg"),

RelativeHorizontalPosition.Page, // Insert the image floating at the top left of the page.

0,

RelativeVerticalPosition.Page,

0,

doc.FirstSection.PageSetup.PageWidth, // Make the image fit the page width.

-1, // The height doesn't matter as we will split the image into separate parts.

WrapType.None);



doc.Accept(new LargeImageSplitter());


doc.Save("C:\\Documentout.doc",SaveFormat.Doc);

}


public class LargeImageSplitter : DocumentVisitor

{

public override VisitorAction VisitShapeStart(Shape shape)

{

if (shape.HasImage)

SplitImage(shape, shape.ParentParagraph.ParentSection.PageSetup);


return VisitorAction.Continue;

}


private static System.Drawing.Image CropImage(System.Drawing.Image img, Rectangle cropArea)

{

Bitmap bmpImage = new Bitmap(img);

Bitmap bmpCrop = bmpImage.Clone(cropArea, bmpImage.PixelFormat);

return (System.Drawing.Image)(bmpCrop);

}

private void SplitImage(Shape shape, PageSetup pageSetup)

{

// Store the original height of shape.

double origHeight = shape.ImageData.ImageSize.HeightPoints;

double currentHeight = origHeight;


// Get the avaliable height on the page.

//double pageHeight = pageSetup.PageHeight - (pageSetup.TopMargin + pageSetup.BottomMargin);

double pageHeight = pageSetup.PageHeight;


// If the height of this shape is bigger than the page then split it.

if (currentHeight > pageHeight)

{

// At what ratio of the shape does it cut off at the end of the page. e.g 0.5 if half of the image is on the first page, the other half in the second.

double ratio = pageHeight / currentHeight;


// Create a new bitmap from the image data.

Bitmap bitmap = new Bitmap(shape.ImageData.ToImage());


// Find the pixel height point where the image is to be cut. Ratio needs to be used in this case as the shape height is in points while

// the bitmap is in pixels.

int heightToCrop = (int)(bitmap.Height * ratio);


// Define which section of the original image should be used for the new shape on the next page. This is all

// of the image which is cut off at the end of the page.

Rectangle cropRect = new Rectangle(0, heightToCrop, bitmap.Width, bitmap.Height - heightToCrop);


// Crop the image at this point, create a clone of the original shape and insert it

System.Drawing.Image croppedImage = CropImage(bitmap, cropRect);

Shape newShape = (Shape)shape.Clone(true);

newShape.ImageData.SetImage((System.Drawing.Image)croppedImage);


// Repeat the process but this time for the original image on the preceeding page. Crop this to the point where the page ends.

bitmap = new Bitmap(shape.ImageData.ToImage());


heightToCrop = (int)(bitmap.Height * ratio);

cropRect = new Rectangle(0, 0, bitmap.Width, heightToCrop);


System.Drawing.Image topCroppedImage = CropImage(bitmap, cropRect);

shape.ImageData.SetImage((System.Drawing.Image)topCroppedImage);


// The height of the original shape should now be the page height.

shape.Height = pageHeight;


// The shape on the next page should be the difference of the original height and what the original shape is now.

newShape.Height = origHeight - shape.Height;


// Create a new paragraph aftr the parent paragraph of the original shape and insert the new shape into it.

// This new image should automatically be pushed to the next page.

Paragraph parentPara = shape.ParentParagraph;

Paragraph newPara = new Paragraph(shape.Document);

newPara.AppendChild(newShape);

parentPara.ParentNode.InsertAfter(newPara, parentPara);



// If the original image is not inline then we need to separate the images with a section break.

if (!shape.IsInline)

parentPara.AppendChild(new Run(shape.Document, ControlChar.SectionBreak));


// Visit the new shape.

VisitShapeStart(newShape);

}

}

}

Hi Aspose Team,

My requirement is to split an input image which contain text into multiple pages,which are later used for printing purpose.

I am using the above code to split the input image "GridImage.jpg".

With the above code I am able to successfully split the image into mulptiple pages.But some text is got cut in between and split into two pages which makes it unreadable.

I want to know is it possible to use Aspose.OCR..to detect whether there is any text present at the line where we are going to cut the image so that we can avoid the text at that particaular line being cut into two pages.

For example:
-------------
If you see in the attached output file "Documentout.doc", the field "Desired base pay rate" is split into two pages.


Please find the attached files:

Iput image: GridImage.jpg
OutpUt File: Documentout.doc

Please let me know if you need any more information.

Thanks,
Siddi.

Hi ,


Currently I am unable to attach the files.
I am getting the following warning.

“Security Warnings
  • License files are not allowed here. Please check How to send a license? for more information.
  • HTML file is not allowed here. Please zip it at first.
You do not have permission to upload or link to files. Please contact your system administrator.”

Please let me knkow if the aspose staff want to see the files I am referring in the above description so that I can mail those files as mentioned in the URL
http://www.aspose.com/corporate/purchase/faqs/send-license-to-aspose-staff.aspx
THanks,
Siddi.

Hi ,


Is there any update on this.

Thanks,
Siddi.

Hi Siddi,

Multiple products were involved in your query so it was moved to Aspose.Total forum. Please follow the same thread in Aspose.Total forum http://www.aspose.com/community/forums/364796/how-to-split-a-single-large-image-into-multiple-image-files/showthread.aspx#364796. Sorry for the inconvenience.

Best Regards,