Hello Team,
I have converted test.docx to test_00000001.jpg with ASPOSE.Words.
Please find error point with attached file(test_00000001 - ErrorPoint.jpg).
-
direction
-
narrow
This is my code.
public static List ConvertWordToImage(string docName, MemoryStream docStream, Dictionary config)
{
try
{
var pageInfoList = new List();
// Get config setting
string docRootDir = config["DocumentsFilePath"];
int dpi = int.Parse(config["ImageDPI"]);
int thumbMaxH = int.Parse(config["ThumbnailMaxHeight"]);
int thumbMaxW = int.Parse(config["ThumbnailMaxWidth"]);
int maxPageNum = int.Parse(config["DocumentMaxPageNum"]);
float readScale = float.Parse(config["ImageReadScale"]);
int readDpi = Convert.ToInt32(dpi * readScale);
// Create images directory
string pageImgDir = docRootDir;
if (!Directory.Exists(pageImgDir))
Directory.CreateDirectory(pageImgDir);
// Get the encoder of JPEG
ImageCodecInfo jpgEncoder = GetCodecInfo();
//EncoderParameters encParams = GetEncoderParams();
EncoderParameters encParams = GetEncoderParams(int.Parse(config["ImageJpegQuality"]));
// Instantiate the License class
Aspose.Words.License license = new Aspose.Words.License();
license.SetLicense("Aspose.Total.lic");
// Convert .doc and .docx file to PDF.
Document document = new Document(docStream);
for (int i = 0; (i < document.PageCount) && (i < maxPageNum); i++)
{
var pageInfo = document.GetPageInfo(i);
Size pixelSize = pageInfo.GetSizeInPixels(1.0f, readDpi);
// Export image file
string imgFile = string.Format("{0}_{1:D8}.jpg",
Path.GetFileNameWithoutExtension(docName), i + 1);
string imgPath = Path.Combine(pageImgDir, imgFile);
using (Bitmap bitmap = new Bitmap(pixelSize.Width, pixelSize.Height))
{
bitmap.SetResolution(dpi, dpi);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.Clear(Color.White);
document.RenderToScale(i, graphics, 0, 0, readScale);
bitmap.Save(imgPath, jpgEncoder, encParams);
}
}
// Add PageInfo List
pageInfoList.Add(imgPath);
}
// return results with flag of max page limit over
return pageInfoList;
}
finally
{
}
}
protected static ImageCodecInfo GetCodecInfo()
{
// Get the encoder of JPEG
ImageCodecInfo jpgEncoder = null;
foreach (ImageCodecInfo ici in ImageCodecInfo.GetImageEncoders())
{
if (ici.FormatID == ImageFormat.Jpeg.Guid)
{
jpgEncoder = ici;
break;
}
}
return jpgEncoder;
}
protected static EncoderParameters GetEncoderParams(int quality)
{
// Create the parameter for the encoder
EncoderParameter encParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
// Store the parameter in the parameter array
EncoderParameters encParams = new EncoderParameters(1);
encParams.Param[0] = encParam;
return encParams;
}
Thank you,
Atsuo Nakatani