Hi,
My requirement is to split an input image which is very large into multiple small image files,
which are later inserted into a word document and then converted to pdf file.
I am using the following code which was mentioned as shown in the url:
https://forum.aspose.com/t/31110
The code I am using is as follows:
protected void Button1_Click(object sender, EventArgs e)
{
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
string TempDirectoryPath = @"D:\Aspose\RDP\NEWFiles";
string inputFileName = TempDirectoryPath + "\ImagePDF.png";
Aspose.Words.Drawing.Shape shape = builder.InsertImage(System.Drawing.Image.FromFile(inputFileName),
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.
doc.FirstSection.PageSetup.PageHeight, // The height doesnÆt matter as we will split the image into separate parts.
WrapType.None);
GC.Collect();
GC.WaitForPendingFinalizers();
doc.Accept(new LargeImageSplitter());
doc.Save(TempDirectoryPath + "ImageDOC.doc", SaveFormat.Doc);
doc.Accept(new LargeImageSplitter());
doc.Save(TempDirectoryPath + "ImageDOC.doc", SaveFormat.Doc); //Step 1: Split the single larege image file into multiple images and save into a word document
// Step 2: generate the individual image files from the above word document
for (int pageCounter = 0, stop = doc.PageCount; pageCounter < stop; pageCounter++)
{
Aspose.Words.Saving.ImageSaveOptions options = new Aspose.Words.Saving.ImageSaveOptions(SaveFormat.Png);
options.PageIndex = pageCounter;
options.PrettyFormat = true;
// images are of the format <file><0-padded-page-index>.png, e.g. (somepath\myfile02.png)
doc.Save(string.Format("{0}{1}{2}{3:d2}.png", imageDirectory, "", "Image", pageCounter + 1), options);
}
// Step 3: save the above image files in a PDF file
Aspose.Pdf.Generator.Pdf pdf = new Aspose.Pdf.Generator.Pdf();
string[] fileEntries = Directory.GetFiles(imageDirectory, "*.png");
int length = fileEntries.GetLength(0);
int counter;
for (counter = 0; counter < length; counter++)
{
// Create a section object
Aspose.Pdf.Generator.Section section1 = pdf.Sections.Add();
// creat an image object
section1.PageInfo.PageWidth = Aspose.Pdf.Generator.PageSize.A4Width;
section1.PageInfo.PageHeight = Aspose.Pdf.Generator.PageSize.A4Height;
section1.PageInfo.Margin.Top = 50;
section1.PageInfo.Margin.Left = 20;
section1.PageInfo.Margin.Bottom = 20;
section1.PageInfo.Margin.Right = 35;
Aspose.Pdf.Generator.Image image1 = new Aspose.Pdf.Generator.Image(section1);
image1.ImageInfo.File = fileEntries[counter];
image1.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Png;
image1.ImageInfo.Alignment = Aspose.Pdf.Generator.AlignmentType.Center;
image1.ImageInfo.FixHeight = section1.PageInfo.PageHeight - section1.PageInfo.Margin.Top - section1.PageInfo.Margin.Bottom;
// specify the image Width information eqaul to Section Width minus Left and Right margin of page
image1.ImageInfo.FixWidth = section1.PageInfo.PageWidth - section1.PageInfo.Margin.Left - section1.PageInfo.Margin.Right;
section1.IsLandscape = false;
section1.Paragraphs.Add(image1);
}
pdf.Save(outputFileName);
}
The image splitting logic is as follows:
public class LargeImageSplitter : DocumentVisitor
{
public override VisitorAction VisitShapeStart(Aspose.Words.Drawing.Shape shape)
{
if (shape.HasImage)
SplitImage(shape, shape.ParentParagraph.ParentSection.PageSetup);
return VisitorAction.Continue;
}
private static System.Drawing.Image CropImage(System.Drawing.Image img, System.Drawing.Rectangle cropArea)
{
try
{
Bitmap bmpImage = new Bitmap(img);
Bitmap bmpCrop = bmpImage.Clone(cropArea, bmpImage.PixelFormat);
return (System.Drawing.Image)(bmpCrop);
}
catch (Exception ex)
{
throw ex;
}
}
private void SplitImage(Aspose.Words.Drawing.Shape shape, Aspose.Words.PageSetup pageSetup)
{
try
{
// 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.
System.Drawing.Rectangle cropRect = new System.Drawing.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);
Aspose.Words.Drawing.Shape newShape = (Aspose.Words.Drawing.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 System.Drawing.Rectangle(0, 0, bitmap.Width, heightToCrop);
GC.Collect();
GC.WaitForPendingFinalizers();
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.
Aspose.Words.Paragraph parentPara = shape.ParentParagraph;
Aspose.Words.Paragraph newPara = new Aspose.Words.Paragraph(shape.Document);
newPara.AppendChild(newShape);
GC.Collect();
GC.WaitForPendingFinalizers();
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);
}
}
catch (Exception ex)
{
throw ex;
}
}
}
Now the problem is that the text in the generated pdf looks blurry.
Please see the attached files:
What I observed is that in the above code in the step 1 while splitting the single large image file and inserting into the workd document it self has the text as blurry.
(Step 1: Split the single large image file into multiple images and save into a word document)
Please see the attached word document “ImageDOC.doc” .
Can you please look into this issue and let me know how to resolve this issue.
Please let me know if you need more information.
Thanks,
Siddi.