Add image in center of PDF page - Aspose.Pdf.VerticalAlignment.Center doesn't work

I need to convert the image to PDF, and the new image I added needs to be vertically centered within the page, but image.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Center doesn’t work anyway.

@lqin

Could you please share how you are adding the image in PDF? Are you using ImageStamp Class or adding an image in Paragraphs collection of the Page? Please share a sample code snippet with us so that we can test the scenario in our environment and address it accordingly.

Page page = doc.Pages.Add();
Aspose.Pdf.Image image = new Aspose.Pdf.Image();
image.File = pic.FilePath;
image.FixWidth = pic.FixWidth;
image.FixHeight = pic.FixHeight;

image.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
image.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Center;

page.PageInfo.Width = width;
page.PageInfo.Height = height;
page.PageInfo.Margin.Bottom = (0);
page.PageInfo.Margin.Top = (0);
page.PageInfo.Margin.Right = (0);
page.PageInfo.Margin.Left = (0);
page.Paragraphs.Add(image);

@lqin

Could you please try adding an image in a PDF Page using ImageStamp as this class also provides the alignment settings? In case the issue still occurs, please feel free to let us know.

The VerticalAlignment property of ImageMap is fine, but this class doesn’t meet my other requirements.There is a problem with the size setting of the PDF file I converted with this class.ImageMap should be used as the image watermark.

@lqin

Would you please provide your sample image and source PDF document for our reference? Also, please share your expected output PDF so that we can test the scenario in our environment and address it accordingly.

The functions I need to implement are as follows: First, I need to specify the size of the PDF document page.Second, I need to insert an image of the specified size on each page of the document, which can be centered horizontally and vertically in the page.The code is as follows:
Document doc = new Document();

            Page page = doc.Pages.Add();
            Aspose.Pdf.Image image = new Aspose.Pdf.Image();
            image.File = pic.FilePath;
		   image.FixWidth = 800;
            image.FixHeight = 600;

            image.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
            image.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Center;
            

            page.PageInfo.Width = 1024;
            page.PageInfo.Height = 768;
            page.PageInfo.Margin.Top = (0);
            page.PageInfo.Margin.Bottom = (0);
            page.PageInfo.Margin.Left = (0);
            page.PageInfo.Margin.Right = (0);
            page.Paragraphs.Add(image);

        doc.Save(this.DestDocument);

Converted PDF files, horizontal can be centered, but vertical regardless of setting any properties, the image is at the top.Image. The VerticalAlignment = Aspose. Pdf. VerticalAlignment. Center did not have any effect
1.jpg (282.6 KB)
ConvertOnePicture.pdf (966.9 KB)

@lqin

We were able to notice the issue at our end while testing the scenario with Aspose.PDF for .NET 21.3. We also tried different workarounds as follows but, none of them gave much success:

Add image using image stamp (did not work as image stamp did not honor specified height and width)

Document doc = new Document();

Page page = doc.Pages.Add();
page.PageInfo.Width = 1024;
page.PageInfo.Height = 768;
page.PageInfo.Margin.Top = (0);
page.PageInfo.Margin.Bottom = (0);
page.PageInfo.Margin.Left = (0);
page.PageInfo.Margin.Right = (0);

ImageStamp stamp = new ImageStamp(dataDir + "1.jpg");
stamp.Height = 600;
stamp.Width = 800;
stamp.HorizontalAlignment = HorizontalAlignment.Center;
stamp.VerticalAlignment = VerticalAlignment.Center;

page.AddStamp(stamp);
doc.Save(dataDir + "ConvertedPDF.pdf");

Add image using floating box (did not work as floating box did not appear at the center)

Document doc = new Document();

Page page = doc.Pages.Add();
page.PageInfo.Width = 1024;
page.PageInfo.Height = 768;
page.PageInfo.Margin.Top = (0);
page.PageInfo.Margin.Bottom = (0);
page.PageInfo.Margin.Left = (0);
page.PageInfo.Margin.Right = (0);

Aspose.Pdf.Image image = new Aspose.Pdf.Image();
image.File = dataDir + "1.jpg";
image.FixWidth = 800;
image.FixHeight = 600;

image.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
image.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Center;


FloatingBox fb = new FloatingBox(800, 600);
fb.VerticalAlignment = VerticalAlignment.Center;
fb.HorizontalAlignment = HorizontalAlignment.Center;
fb.Paragraphs.Add(image);
page.Paragraphs.Add(fb);

doc.Save(dataDir + "ConvertedPDF.pdf");

Therefore, an issue as PDFNET-49688 has been logged in our issue tracking system for the sake of correction. We will look into its details and keep you posted with the status of its rectification. Please be patient and spare us some time.

We are sorry for the inconvenience.