Free Support Forum - aspose.com

Output PDF is not displaying the image properly

Hi Aspose Team,


I am using the follwoing code to split a large image into multiple images:
as mentioned in the post:

http://www.aspose.com/community/forums/thread/361763/how-to-split-a-single-large-image-into-multiple-image-files.aspx

============================================================================================================================


I will be calling the below method ConvertImageToPDFBySplittingImage() as

string TempDirectoryPath = @"C:\WINDOWS\Temp\ImageFiles";
ConvertImageToPDFBySplittingImage(TempDirectoryPath, TempDirectoryPath + "\\ImagePDF.png", TempDirectoryPath + "\\ImagePDF.PDF");
====================================================================================================================

//Input parameters:
//TempDirectoryPath-->Temporay directory
//inputFileName------>input image file to be splitted into multiple pages
//outputFileName-->out put pdf file...
public string ConvertImageToPDFBySplittingImage(string TempDirectoryPath, string inputFileName, string outputFileName)
{

string base64String = string.Empty;
try
{
string imageDirectory = TempDirectoryPath + "Images\\";
Aspose.Words.License license = new Aspose.Words.License();
//license.SetLicense(@"C:\jetstream\500\Resources\Aspose.Words.lic");
if (File.Exists(@"C:\jetstream\500\Resources\Aspose.Total.lic"))
license.SetLicense(@"C:\jetstream\500\Resources\Aspose.Total.lic");
else
license.SetLicense(@"C:\jetstream\500\Resources\Aspose.Words.lic");

Aspose.Pdf.License license2 = new Aspose.Pdf.License();
//license2.SetLicense(@"C:\Jetstream\500\Resources\Aspose.Pdf.Kit.lic");
if (File.Exists(@"C:\jetstream\500\Resources\Aspose.Total.lic"))
license2.SetLicense(@"C:\jetstream\500\Resources\Aspose.Total.lic");
else
license2.SetLicense(@"C:\jetstream\500\Resources\Aspose.Pdf.Kit.lic");

Aspose.Words.Document doc = new Aspose.Words.Document();
DocumentBuilder builder = new DocumentBuilder(doc);


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

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.
-1, // 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", Aspose.Words.SaveFormat.Doc);

//Aspose.Words.Document doc2 = new Aspose.Words.Document(TempDirectoryPath + "ImageDOC.doc");

for (int pageCounter = 0, stop = doc.PageCount; pageCounter < stop; pageCounter++)
{
Aspose.Words.Saving.ImageSaveOptions options = new Aspose.Words.Saving.ImageSaveOptions(Aspose.Words.SaveFormat.Png);
options.PageIndex = pageCounter;
options.PrettyFormat = true;
// images are of the format \<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);
}


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;

// Create a BitMap object in order to get the information of image file
//Bitmap myimage = new Bitmap(fileEntries[counter]);

// check if the width of the image file is greater than Page width or not
//if (myimage.Width > section1.PageInfo.PageWidth)
// // if the Image width is greater than page width, then set the page orientation to Landscape
// section1.IsLandscape = true;
//else
// // if the Image width is less than page width, then set the page orientation to Portrait
section1.IsLandscape = false;
// add the image to paragraphs collection of the PDF document
//section1.IsLandscape = false;
section1.Paragraphs.Add(image1);
}
//pdf.Title = "This is a test title !!";
System.Diagnostics.Debugger.Break();
pdf.Save(outputFileName);
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(outputFileName);

DocumentInfo docInfo = new DocumentInfo(pdfDocument);
docInfo.Title = "Setting PDF Document Information";
pdfDocument.Save(outputFileName);


//pdf.Save(outputFileName);
base64String= ConvertTOBase64(outputFileName, TempDirectoryPath);
}
catch (Exception ex)
{
throw ex;
}
return base64String;
}

==============================================================================================================

private 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;
}
}
}
=======================================================================================================

The problem started occurring once after using the latest dlls and lic files..

Recently we have modified the code in the function "ConvertImageToPDFBySplittingImage" to use the Aspose.Total.lic:

It used to work previously..But it is not working now..Please see the attached files with splitting and with out splitting the image.


public string ConvertImageToPDFBySplittingImage(string TempDirectoryPath, string inputFileName, string outputFileName)

from

Earlier with the following licenses it worked fine..

Aspose.Words.License license = new Aspose.Words.License();
license.SetLicense(@"C:\jetstream\500\Resources\Aspose.Words.lic");

Aspose.Pdf.License license2 = new Aspose.Pdf.License();
license2.SetLicense(@"C:\jetstream\500\Resources\Aspose.Pdf.Kit.lic");

But after changing the license to

Aspose.Words.License license = new Aspose.Words.License();
//license.SetLicense(@"C:\jetstream\500\Resources\Aspose.Words.lic");
if (File.Exists(@"C:\jetstream\500\Resources\Aspose.Total.lic"))
license.SetLicense(@"C:\jetstream\500\Resources\Aspose.Total.lic");
else
license.SetLicense(@"C:\jetstream\500\Resources\Aspose.Words.lic");

Aspose.Pdf.License license2 = new Aspose.Pdf.License();
//license2.SetLicense(@"C:\Jetstream\500\Resources\Aspose.Pdf.Kit.lic");
if (File.Exists(@"C:\jetstream\500\Resources\Aspose.Total.lic"))
license2.SetLicense(@"C:\jetstream\500\Resources\Aspose.Total.lic");
else
license2.SetLicense(@"C:\jetstream\500\Resources\Aspose.Pdf.Kit.lic");

The output Pdf is not displaying correctly.



=======================================================

1)Further I identified that this issue is occurring with the Aspose.Words dll Version: 11.6.0.0.

2)With Aspose.Word Version:10.7.0.0 ..It is working fine..

Please review this and let me know if you need more information.

Thanks,
Siddi

Thanks,
Siddi.

Hi Siddi,


Thanks for your inquiry. Yes, you’re right; the PNG images produced by the latest version of Aspose.Words are blurred. I have tested the scenario and have managed to reproduce the same problem on my side. For the sake of correction, I have logged this problem as WORDSNET-6866 in our issue tracking system. We will further look into the details of this problem and will keep you updated on the status of correction. We apologize for your inconvenience.

Best Regards,

Hi Siddi,


Thanks for your patience.

You’re facing this behaviour because older versions of Aspose.Words e.g. 10.7.0 didn’t scale down the image with large dimensions so that it fits inside a single page in Word document. However, the latest versions of Aspose.Words scale the image down so it fits inside a single page. In order to resolve this problem, I would recommend the following small change in your old code so that it works with the newer versions of Aspose.Words as desired.

Aspose.Words.Drawing.Shape
shape = builder.InsertImage(System.Drawing.
Image.FromFile(@“C:\Files\ImagePDF.png”),

RelativeHorizontalPosition.Page,

0,

RelativeVerticalPosition.Page,

0,

doc.FirstSection.PageSetup.PageWidth,

doc.FirstSection.PageSetup.PageHeight,

WrapType.None);


I hope, this will help.

Best Regards,

Hi Siddi,


It is to update you that our development team has finished working on your issue (WORDSNET-6866) and has come to a conclusion that your issue and the behaviour you’re observing is actually not a bug in Aspose.Words. So, we’ve closed this issue as ‘Not a Bug’. If we can help you with anything else, please feel free to ask.

Best Regards,

The issues you have found earlier (filed as ) have been fixed in this update. This message was posted using BugNotificationTool from Downloads module by MuzammilKhan