How can I convert images to pdf in A4

Hi,

SDK: Pdf for C++
I’m trying to convert images to pdf.
But the size of these images are different.
How can I make them all to A4 pdf?

Thanks.

For example:
Image like:图片.jpg (94.1 KB)
Output pdf page like: 2.jpg (109.0 KB)

@kngstr

Can you please also share sample code snippet that you are using to convert an image to PDF? We will test the scenario in our environment and address it accordingly.

@asad.ali Here is my test code

   auto doc = System::MakeObject<Aspose::Pdf::Document>();
   auto page = doc->get_Pages()->Add();

   System::String fileName = System::String(u"F:\\xxxx.jpg");

   auto fileStream = System::MakeObject<System::IO::FileStream>(fileName, System::IO::FileMode::Open, System::IO::FileAccess::Read);
   auto bmp = System::MakeObject<System::Drawing::Bitmap>(fileStream);
   auto r = System::MakeObject<Aspose::Pdf::Rectangle>(0.0, 0.0, (double)bmp->get_Width(), (double)bmp->get_Height());
   page->set_CropBox(r);

   // 设置页面大小
   auto pageInfo = page->get_PageInfo();
   pageInfo->get_Margin()->set_Bottom(0);
   pageInfo->get_Margin()->set_Top(0);
   pageInfo->get_Margin()->set_Left(0);
   pageInfo->get_Margin()->set_Right(0);
   page->SetPageSize(r->get_Width(), r->get_Height());

   page->AddImage(fileStream, r);
   fileStream->Close();
   doc->Save(u"imageToPdf.pdf");

@kngstr

You are getting size different than A4 because you are setting the page size according to image dimensions:

page->set_CropBox(r);
page->SetPageSize(r->get_Width(), r->get_Height());

Please do not set the page size and remove the above line(s). In case you still face any issues, please let us know.

@asad.ali

Thanks.
So how can I set page size to A4 or anything else?

@kngstr

In order to keep all images to A4 size, please do not set the Page Size in the PDF according to image dimensions - as shared earlier. However, if you want to change the page size differently, you can do it using page->SetPageSize(r->get_Width(), r->get_Height()); line as you were doing before. Please share some sample source images along with the expected output PDF and currently generated output so that we can further check it and share our feedback with you accordingly.

@asad.ali

Thanks.

What I expected like this:图片.jpg (165.8 KB)
Here is my test images:宠物.7z (1.0 MB)

@kngstr

As per our understandings of the scenario, you want to generate A4 size pages in PDF and want the image to be centered and scaled inside the page boundaries. Please let us know if we misunderstood your requirements?

@asad.ali

Yes.
But page size will be customed, A4,A3,100x100 and so on.

@kngstr

There are two ways to achieve what you require:

1- You can change the rectangle value and specify it to the size in which you want to add the image and at which location.

2- The image will get stretched or distorted due to size change while adding into the PDF. So, you can scale image according to your desired page size by using the below sample method:

void ImagetoPDF()
{
    auto doc = System::MakeObject<Aspose::Pdf::Document>();
    auto page = doc->get_Pages()->Add();

    System::String fileName = System::String(u"20fd22c79fb34939ab8e85a3222e6a30.jpg");

    auto fileStream = System::MakeObject<System::IO::FileStream>(fileName, System::IO::FileMode::Open, System::IO::FileAccess::Read);
    System::SharedPtr<System::Drawing::Image> bmp = System::MakeObject<System::Drawing::Bitmap>(fileStream);
    auto newsizeImage = ScaleImage(bmp, 100, 100);
    // You can change the rectangle values in order to change the image position over page
    auto r = System::MakeObject<Aspose::Pdf::Rectangle>(0.0, 0.0, (double)newsizeImage->get_Width(), (double)newsizeImage->get_Height());
    newsizeImage->Save(u"resizedimage.jpg");
    //page->set_CropBox(r);

    auto pageInfo = page->get_PageInfo();
    pageInfo->get_Margin()->set_Bottom(0);
    pageInfo->get_Margin()->set_Top(0);
    pageInfo->get_Margin()->set_Left(0);
    pageInfo->get_Margin()->set_Right(0);
    page->SetPageSize(100, 100);

    page->AddImage(u"resizedimage.jpg", r);
    fileStream->Close();
    doc->Save(u"imageToPdf.pdf");
}

System::SharedPtr<System::Drawing::Image> ScaleImage(System::SharedPtr<System::Drawing::Image> image, int maxWidth, int maxHeight)
{
    auto ratioX = (double)maxWidth / image->get_Width();
    auto ratioY = (double)maxHeight / image->get_Height();
    auto ratio = fmin(ratioX, ratioY);
    auto newWidth = (int)(image->get_Width() * ratio);
    auto newHeight = (int)(image->get_Height() * ratio);
    auto newImage = System::MakeObject<System::Drawing::Bitmap>(newWidth, newHeight);
    auto graphics = System::Drawing::Graphics::FromImage(newImage);
    graphics->DrawImage(image, 0, 0, newWidth, newHeight);
    return newImage;
}

@asad.ali

Thanks

1 Like