Calibration large image during conversion image to PDF using Aspose.Pdf

Hello,

I try to find the way to calibration image during conversion JPG to PDF using Aspose.pdf for .NET

MY CODE:

var doc = new Aspose.Pdf.Document();
Page page = doc.Pages.Add();
Stream imageStream;
using (imageStream){
       Aspose.Pdf.Image image = new Aspose.Pdf.Image();
       operationName = "Setting image stream";
       image.ImageStream = imageStream;

       operationName = "Set page properties";
       page.PageInfo.Width = PageSize.A4.Width;
       page.PageInfo.Height = PageSize.A4.Height;
       page.Paragraphs.Add(image);

}

Please, could you provide some examples to calibration image in a way to looks nice if:
Image width is greatter than page size but height is smaller then page size ( now image expand height to fill page)
And another scenario when image height is bigger than page but width is smaller.

@Kodofix

Thanks for contacting support.

You may calibrate image during JPG to PDF conversion through different ways. For instance, you may either adjust height/width of the page equal to height/width of the image, or adjust height/width of the image according to desired page height/width.

Please check following sample code snippet(s), where I have generated PDF from JPG through both ways, mentioned above.

Adjust Page Height/Width

var pdf = new Aspose.Pdf.Document();
var pdfImageSection = pdf.Pages.Add();
FileStream stream = new FileStream("inFile.jpg", FileMode.Open);
var image = new Aspose.Pdf.Image { ImageStream = stream };
var bmp = new System.Drawing.Bitmap(stream);
pdfImageSection.PageInfo.Width = bmp.Width;
pdfImageSection.PageInfo.Height = bmp.Height;
pdfImageSection.Paragraphs.Add(image);
// remove extra white space in four corners by setting page margin information
pdfImageSection.PageInfo.Margin.Left = pdfImageSection.PageInfo.Margin.Right = pdfImageSection.PageInfo.Margin.Top = pdfImageSection.PageInfo.Margin.Bottom = 0f;
pdf.Save(dataDir + "outFile.pdf");

Scale Input Image

var pdf = new Aspose.Pdf.Document();
var pdfImageSection = pdf.Pages.Add();
FileStream stream = new FileStream("inFile.jpg", FileMode.Open);
System.Drawing.Image img = new Bitmap(stream);
img = ScaleImage(img, (int)pdfImageSection.PageInfo.Width, (int)pdfImageSection.PageInfo.Height);
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
ms.Seek(0, SeekOrigin.Begin);
var image = new Aspose.Pdf.Image { ImageStream = ms };
pdfImageSection.Paragraphs.Add(image);
pdfImageSection.PageInfo.Margin.Left = pdfImageSection.PageInfo.Margin.Right = pdfImageSection.PageInfo.Margin.Top = pdfImageSection.PageInfo.Margin.Bottom = 0;
pdf.Save(dataDir + "outFile.pdf");

Definition of ScaleImage(…) method

public static System.Drawing.Image ScaleImage(System.Drawing.Image image, int maxWidth, int maxHeight)
{
 var ratioX = (double)maxWidth / image.Width;
 var ratioY = (double)maxHeight / image.Height;
 var ratio = Math.Min(ratioX, ratioY);
 var newWidth = (int)(image.Width * ratio);
 var newHeight = (int)(image.Height * ratio);
 var newImage = new Bitmap(newWidth, newHeight);
 using (var graphics = Graphics.FromImage(newImage))
    graphics.DrawImage(image, 0, 0, newWidth, newHeight);
 return newImage;
}

In case of any further assistance, please feel free to let us know.

1 Like