We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Images are not centered when exporting to PDF but fine when exporting to Word

When using Aspose Word and dynamically creating a PDF document inserted images are not centered but when the document is created in Word format the images are centered. See Word and PDF attachments to compare. The images in question are under Cross-Functional Flow Diagram and is repeat a number of times in the document.


Update: The Word export images line up when opening in Word 2003 but don’t line up when opening in word 2007.

The code below is used to resize and center each image.


if (verticalOrientation == “Horizontal”)
top = 160;
else
top = 220;

if (verticalOrientation == “Vertical”)
{
ps.HeaderDistance = 40;

Double contentHeight = ps.PageHeight - ps.TopMargin - ps.BottomMargin;

create_CFFD mydiagram = new create_CFFD();
System.Drawing.Bitmap cFFDimage = mydiagram.generateCFFD(processName, Int32.Parse(exportData[“ID”].ToString()), exportData[“Name”].ToString(), null);

MemoryStream ms = new MemoryStream(ImageToByte2(cFFDimage));
Image image = Image.FromStream(ms);
double percentW, adjustedWidth, adjustedHeight;
if (image.Width > contentHeight)
{
percentW = (double)((contentWidth) / image.Width);
adjustedWidth = (image.Width * percentW);
adjustedHeight = image.Height * percentW;
}
else
{
percentW = (double)(contentWidth / image.Width);
adjustedWidth = image.Width * percentW;
adjustedHeight = image.Height * percentW;
}
Aspose.Words.Drawing.Shape shape = builder.InsertImage(cFFDimage, RelativeHorizontalPosition.InsideMargin, -40, RelativeVerticalPosition.TextFrameDefault, top, adjustedWidth, adjustedHeight, WrapType.None);
shape.Rotation = 90;
shape.DistanceTop = top;
mydiagram.Dispose();
}
else
{
contentWidth = ps.PageWidth - ps.LeftMargin - ps.RightMargin;

create_CFFD mydiagram = new create_CFFD();
System.Drawing.Bitmap cFFDimage = mydiagram.generateCFFD(processName, Int32.Parse(exportData[“ID”].ToString()), exportData[“Name”].ToString(), null);

MemoryStream ms = new MemoryStream(ImageToByte2(cFFDimage));
Image image = Image.FromStream(ms);
double percentW, adjustedWidth, adjustedHeight;
if (image.Width > contentWidth)
{
percentW = (double)(contentWidth / image.Width);
adjustedWidth = (image.Width * percentW);
adjustedHeight = image.Height * percentW;
}
else
{
percentW = (double)(contentWidth / image.Width);
adjustedWidth = image.Width * percentW;
adjustedHeight = image.Height * percentW;
}
Aspose.Words.Drawing.Shape shape = builder.InsertImage(cFFDimage, RelativeHorizontalPosition.LeftMargin, 0, RelativeVerticalPosition.TextFrameDefault, top, adjustedWidth, adjustedHeight, WrapType.None);
shape.DistanceTop = top;
mydiagram.Dispose();
}
}
Hi Paul,

Thanks for your inquiry. In case you are using an older version of Aspose.Words, I would suggest you please upgrade to the latest version (v13.3.0) from here and let us know how it goes on your side. If the problem still remains, please attach your input Word document for testing. I will investigate the issue on my side and provide you more information.

Moreover, I suggest you please use the Shape's properties used in following code snippet to achieve your requiremetns. Please read following documentation links for your kind refernece. Hope this helps you.
http://www.aspose.com/docs/display/wordsnet/RelativeVerticalPosition+Enumeration
http://www.aspose.com/docs/display/wordsnet/RelativeHorizontalPosition+Enumeration

http://www.aspose.com/docs/display/wordsnet/VerticalAlignment+Enumeration
http://www.aspose.com/docs/display/wordsnet/HorizontalAlignment+Enumeration


Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

byte[] image = File.ReadAllBytes(MyDir + "Aspose.jpg");

Shape shape = builder.InsertImage(image);

shape.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center;

shape.VerticalAlignment = VerticalAlignment.Center;

shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Margin;

shape.RelativeVerticalPosition = RelativeVerticalPosition.Margin;

shape.WrapType = WrapType.Square;

shape.Rotation = 90;

doc.Save(MyDir + "out.doc");

doc.Save(MyDir + "out.pdf");


Utilized the recommended code. Now Works fine when exporting to Word 2003 and Word 2007 but export to Pdf still not working. See attached documents.


Update: The issue only occurs when the image is rotated 90% and exported to Pdf.


if (verticalOrientation == “Vertical”)
{
ps.HeaderDistance = 40;

Double contentHeight = ps.PageHeight - ps.TopMargin - ps.BottomMargin;

create_CFFD mydiagram = new create_CFFD();
System.Drawing.Bitmap cFFDimage = mydiagram.generateCFFD(processName, Int32.Parse(exportData[“ID”].ToString()), exportData[“Name”].ToString(), null);

MemoryStream ms = new MemoryStream(ImageToByte2(cFFDimage));
Image image = Image.FromStream(ms);
double percentW, adjustedWidth, adjustedHeight;
if (image.Width > contentHeight)
{
percentW = (double)((contentWidth) / image.Width);
adjustedWidth = (image.Width * percentW);
adjustedHeight = image.Height * percentW;
}
else
{
percentW = (double)(contentWidth / image.Width);
adjustedWidth = image.Width * percentW;
adjustedHeight = image.Height * percentW;
}
Aspose.Words.Drawing.Shape shape = builder.InsertImage(cFFDimage);
shape.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center;
shape.VerticalAlignment = VerticalAlignment.Center;

shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Margin;
shape.RelativeVerticalPosition = RelativeVerticalPosition.Margin;

shape.WrapType = WrapType.Square;
shape.Rotation = 90;
mydiagram.Dispose();
}
else
{
contentWidth = ps.PageWidth - ps.LeftMargin - ps.RightMargin;

create_CFFD mydiagram = new create_CFFD();
System.Drawing.Bitmap cFFDimage = mydiagram.generateCFFD(processName, Int32.Parse(exportData[“ID”].ToString()), exportData[“Name”].ToString(), null);

MemoryStream ms = new MemoryStream(ImageToByte2(cFFDimage));
Image image = Image.FromStream(ms);
double percentW, adjustedWidth, adjustedHeight;
if (image.Width > contentWidth)
{
percentW = (double)(contentWidth / image.Width);
adjustedWidth = (image.Width * percentW);
adjustedHeight = image.Height * percentW;
}
else
{
percentW = (double)(contentWidth / image.Width);
adjustedWidth = image.Width * percentW;
adjustedHeight = image.Height * percentW;
}
Aspose.Words.Drawing.Shape shape = builder.InsertImage(cFFDimage);
shape.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center;
shape.VerticalAlignment = VerticalAlignment.Center;

shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Margin;
shape.RelativeVerticalPosition = RelativeVerticalPosition.Margin;

shape.WrapType = WrapType.Square;
mydiagram.Dispose();
}
}
Hi Paul,

Thanks for your inquiry. Perhaps, you are using an older version of Aspose.Words; as with Aspose.Words v13.3.0, I am unable to reproduce this problem on my side. I would suggest you please upgrade to the latest version of Aspose.Words i.e. v13.3.0 and let us know how it goes on your side. I hope, this will help.

If the problem still remains, please share following details for investigation purposes.

What environment are you running on?
  • OS (Windows Version or Linux Version)
  • Architecture (32 / 64 bit)
  • .NET Framework version
  • Please supply us with the input document that is causing the issue
  • I tested the shared code and have not found the issue, please supply us with the complete code that is causing the issue
  • Please execute the following code snippet at your end and share the output PDF file

As soon as you get these pieces of information to us we'll start our investigation into your issue.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

byte[] image = File.ReadAllBytes(MyDir + "image.jpg");

Shape shape = builder.InsertImage(image);

shape.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center;

shape.VerticalAlignment = VerticalAlignment.Center;

shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Margin;

shape.RelativeVerticalPosition = RelativeVerticalPosition.Margin;

shape.WrapType = WrapType.Square;

shape.Rotation = 90;

doc.Save(MyDir + "out.pdf");



OS is Windows 7 and Windows 2008 Server both x64.


.net Framework 3.5

There is no input document. The document is dynamically created.

The issue is just image alignment when the shape rotation is set to 90 and the output is Pdf. There is no issue when Rotation is set to 90 when output is Word.
Horizontal output works in both word and Pdf.

See attached generated documents.

Here is the code I am using:

try
{
if (includeCFFD)
{
// List CFFD on new page in Landscape
Section sectionCFFD = new Section(doc);
doc.Sections.Add(sectionCFFD);

Body bodyCFFD = new Body(doc);
sectionCFFD.AppendChild(bodyCFFD);

headingPara = new Paragraph(doc);
bodyCFFD.AppendChild(headingPara);

SetSectionHeader(ref headingPara, 3);
builder.MoveTo(headingPara);
builder.Write(cFFDTitle);

Paragraph cFFDPara = new Paragraph(doc);
bodyCFFD.AppendChild(cFFDPara);
builder.MoveTo(cFFDPara);

SetUpPage(ref generalOptionsDataSet);
SetNormalTheme(ref descriptionPara);

ps = builder.PageSetup;
builder.MoveTo(cFFDPara);

int top = 0;

if (verticalOrientation == “Horizontal”)
top = 160;
else
top = 220;

if (verticalOrientation == “Vertical”)
{
ps.HeaderDistance = 40;

Double contentHeight = ps.PageHeight - ps.TopMargin - ps.BottomMargin;

create_CFFD mydiagram = new create_CFFD();
System.Drawing.Bitmap cFFDimage = mydiagram.generateCFFD(processName, Int32.Parse(exportData[“ID”].ToString()), exportData[“Name”].ToString(), null);

MemoryStream ms = new MemoryStream(ImageToByte2(cFFDimage));
Image image = Image.FromStream(ms);
double percentW, adjustedWidth, adjustedHeight;
if (image.Width > contentHeight)
{
percentW = (double)((contentWidth) / image.Width);
adjustedWidth = (image.Width * percentW);
adjustedHeight = image.Height * percentW;
}
else
{
percentW = (double)(contentWidth / image.Width);
adjustedWidth = image.Width * percentW;
adjustedHeight = image.Height * percentW;
}
Aspose.Words.Drawing.Shape shape = builder.InsertImage(cFFDimage);
shape.Rotation = 90;
shape.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center;
shape.VerticalAlignment = VerticalAlignment.Center;
shape.Height = adjustedHeight;
shape.Width = adjustedWidth;
shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Margin;
shape.RelativeVerticalPosition = RelativeVerticalPosition.Margin;
shape.WrapType = WrapType.Square;
mydiagram.Dispose();
}
else
{
contentWidth = ps.PageWidth - ps.LeftMargin - ps.RightMargin;

create_CFFD mydiagram = new create_CFFD();
System.Drawing.Bitmap cFFDimage = mydiagram.generateCFFD(processName, Int32.Parse(exportData[“ID”].ToString()), exportData[“Name”].ToString(), null);

MemoryStream ms = new MemoryStream(ImageToByte2(cFFDimage));
Image image = Image.FromStream(ms);
double percentW, adjustedWidth, adjustedHeight;
if (image.Width > contentWidth)
{
percentW = (double)(contentWidth / image.Width);
adjustedWidth = (image.Width * percentW);
adjustedHeight = image.Height * percentW;
}
else
{
percentW = (double)(contentWidth / image.Width);
adjustedWidth = image.Width * percentW;
adjustedHeight = image.Height * percentW;
}
Aspose.Words.Drawing.Shape shape = builder.InsertImage(cFFDimage);
shape.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center;
shape.VerticalAlignment = VerticalAlignment.Center;
shape.Height = adjustedHeight;
shape.Width = adjustedWidth;
shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Margin;
shape.RelativeVerticalPosition = RelativeVerticalPosition.Margin;

shape.WrapType = WrapType.Square;
mydiagram.Dispose();
}
}
}
catch (Exception eCFFD)
{
}

switch (docSaveFormat)
{
case “Doc”:
{
doc.Save(@serverAttachmentPath + docFileName, SaveFormat.Doc);
break;
}
case “Pdf”:
{
doc.Save(@serverAttachmentPath + docFileName, SaveFormat.Pdf);
break;
}
case “Html”:
{
HtmlSaveOptions htmlOptions = new HtmlSaveOptions();
htmlOptions.ExportXhtmlTransitional = true;
htmlOptions.ExportHeadersFootersMode = ExportHeadersFootersMode.None;
htmlOptions.ScaleImageToShapeSize = true;
doc.Save(@serverAttachmentPath + docFileName, htmlOptions);
break;
}
}
Hi Paul,

Could you please share which version of Aspose.Words you are using? Please try the shared code in my previous post and share the output Pdf file.

Many thanks,

Aspose word version 2013.2.28.0


See attached.
Hi Paul,

Thanks for sharing the detail. I have managed to reproduce the same issue at my side. I have logged this issue as WORDSNET-8067 in our issue tracking system. I have linked this forum thread to the same issue and you will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

The issues you have found earlier (filed as WORDSNET-8067) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.