Thanks for your inquiry.
Of course this is possible using Aspose.Words. Please see the code below for a demonstration on how to achieve this.
<span style=“font-size:
10.0pt;font-family:“Courier New”;color:#2B91AF;mso-no-proof:yes”>Document<span style=“font-size:10.0pt;font-family:“Courier New”;mso-no-proof:yes”> doc = new Document();<o:p></o:p>
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape =
builder.InsertImage(Image.FromFile(dataDir +
"first.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("Document
Out.pdf");
public class LargeImageSplitter
: DocumentVisitor
{
public
override VisitorAction VisitShapeStart(Shape
shape)
{
if
(shape.HasImage)
SplitImage(shape,
shape.ParentParagraph.ParentSection.PageSetup);
return
VisitorAction.Continue;
}
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);
// 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
Image
croppedImage = CropImage(bitmap, cropRect);
Shape
newShape = (Shape)shape.Clone(true);
newShape.ImageData.SetImage((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);
Image
topCroppedImage = CropImage(bitmap, cropRect);
shape.ImageData.SetImage((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);
}
}
}
If you have any other queries, please feel free to ask.