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

Free Support Forum - aspose.com

Centering an image on pdf

Hey there again,


I’ve converted an aspose.slides presentation to images using GetThumbnail, then i add these images to an Aspose.PDF object.

The image is rendered, but it does not fit to page but rather is placed and the edge is a bit cut.

Is there a way to centralize the image or fit it to the page?

this is the code I’m using :

//Loop over the worksheets collection
for (int index = 0; index < wrkExcel.Worksheets.Count; index++)
{
MemoryStream ms = new MemoryStream();
Worksheet worksheet = wrkExcel.Worksheets[index];

//Define ImageOrPrintOptions
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
//Specify the image format
imgOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Bmp;
//Only one page for the whole sheet would be rendered
imgOptions.OnePagePerSheet = false;
imgOptions.IsImageFitToPage = false;
//Render the sheet with respect to specified image/print options
SheetRender sr = new SheetRender(worksheet, imgOptions);

for (int j = 0; j < sr.PageCount; j++)
{
//sr.ToImage(0, ms);
//Render the image for the sheet
Bitmap bitmap = sr.ToImage(j);
bitmap.Save(ms, ImageFormat.Bmp);
int newindex = dstWrk.Worksheets.Add();
dstWrk.Worksheets[newindex].Pictures.Add(0, 0, ms);
}
}

How may I go about this ?

Thanks,
Garkler

Hello Garkler,

Thanks for using our products.

In order to center align the image while placing it inside PDF document, please try using Alignment property of ImageInfo class. Please take a look over following code snippet in which I have specified the alignment information for image as Center aligned.

[C#]

//Instantiate a Pdf object by calling its empty constructor
Pdf pdf1 = new Pdf();
//Create a section in the Pdf object
Aspose.Pdf.Section sec1 = pdf1.Sections.Add();

//Create an image object in the section
Aspose.Pdf.Image image1 = new Aspose.Pdf.Image(sec1);
// specify the Horizontal alignment information for image
image1.ImageInfo.Alignment = AlignmentType.Center;
//Add image object into the Paragraphs collection of the section
sec1.Paragraphs.Add(image1);
//Set the path of image file
image1.ImageInfo.File = "d:/pdftest/Photo.JPG";
//Set the type of image using ImageFileType enumeration
image1.ImageInfo.ImageFileType = ImageFileType.Jpeg;
// specify the image heigh information eqaul to Section height minus Top and Bottom margin of page
image1.ImageInfo.FixHeight = sec1.PageInfo.PageHeight - sec1.PageInfo.Margin.Top - sec1.PageInfo.Margin.Bottom;
// specify the image Width information eqaul to Section Width minus Left and Right margin of page
image1.ImageInfo.FixWidth= sec1.PageInfo.PageWidth- sec1.PageInfo.Margin.Left- sec1.PageInfo.Margin.Right;

//Save the Pdf
pdf1.Save("d:/pdftest/ImageConversionTest.pdf");

In case it does not resolve your problem, please share the source image so that we can test the scenario at our end. We are sorry for your inconvenience.

How could I do the same thing using the new DOM approach which is now the preferred method?

Hi Kerby,

Thanks for contacting support.

Please try using the following code snippet based on new Document Object Model (DOM) of Aspose.Pdf namespace.

[C#]

//Instantiate a Pdf object by calling its empty constructor

Aspose.Pdf.Document pdf1 = new Aspose.Pdf.Document();

//Create a section in the Pdf object

Aspose.Pdf.Page sec1 = pdf1.Pages.Add();

//Create an image object in the section

Aspose.Pdf.Image image1 = new Aspose.Pdf.Image();

// specify the Horizontal alignment information for image

image1.HorizontalAlignment= Aspose.Pdf.HorizontalAlignment.Center;

//Add image object into the Paragraphs collection of the section

sec1.Paragraphs.Add(image1);

//Set the path of image file

image1.File = "c:/pdftest/Illustrator+Screen+Capture.jpg";

// specify the image heigh information eqaul to Section height minus Top and Bottom margin of page

image1.FixHeight = sec1.PageInfo.Height - sec1.PageInfo.Margin.Top - sec1.PageInfo.Margin.Bottom;

// specify the image Width information eqaul to Section Width minus Left and Right margin of page

image1.FixWidth = sec1.PageInfo.Width - sec1.PageInfo.Margin.Left - sec1.PageInfo.Margin.Right;

//Save the Pdf

pdf1.Save("C:/pdftest/ImageConversionTest.pdf");