I’m using Aspose.Pdf for .Net and I’m having an issue with getting the image to be sized correctly for the pdf. I want to take a jpg and convert it to an exact same sized pdf file.
Hi Connor,
Thanks for contacting support. Please try using following code snippet to accomplish your requirement. In case you encounter any issue, please share your sample image files.
[C#]
// Create Document instance
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
// Add page to pages collection of document instance
doc.Pages.Add();
// Create Bitmap image so we can to get image information
Bitmap bitmap = new Bitmap("c:/pdftest/-logo.png");
Aspose.Pdf.Image image = new Aspose.Pdf.Image();
image.File = "c:/pdftest/logo.png";
// Add image to paragraphs collection of first page
doc.Pages[1].Paragraphs.Add(image);
// Set height of first page equal to image height
doc.Pages[1].PageInfo.Height = bitmap.Height;
// Set width of first page equal to image width
doc.Pages[1].PageInfo.Width = bitmap.Width;
// Trim margin around all corners of page
doc.Pages[1].PageInfo.Margin.Left = 1;
doc.Pages[1].PageInfo.Margin.Top = 1;
doc.Pages[1].PageInfo.Margin.Bottom = 1;
doc.Pages[1].PageInfo.Margin.Right = 1;
// Save the PDF document
doc.Save("c:/pdftest/Image_to_PDF.pdf");
Thank you this seemed to fix the issue. Also I’m curious if there is a way to rotate the pdf image to portrait? Wondering specifically about Aspose.PDF.
Hi Connor,
In order to accomplish this requirement, you need to rotate a PDF page containing an image. Please take a look at the following code snippet. Note that I have saved the file to a MemoryStream
instance and then created another Document
object because when placing an image inside PDF that occupies the complete page area, the page rotation is not producing correct results.
[C#]
// Create Document instance
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
// add page to pages collection of document instance
doc.Pages.Add();
// Create Image object
Aspose.Pdf.Image image = new Aspose.Pdf.Image();
// create Bitmap image so we can get image information
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap("c:/pdftest/PDF_A_1b_Compliance-Test.PNG");
image.File = "c:/pdftest/PDF_A_1b_Compliance-Test.PNG";
// add image to paragraphs collection of the first page
doc.Pages[1].Paragraphs.Add(image);
// set height of the first page equal to image height
doc.Pages[1].PageInfo.Height = bitmap.Height;
// set width of the first page equal to image width
doc.Pages[1].PageInfo.Width = bitmap.Width;
// trim margin around all corners of page
doc.Pages[1].PageInfo.Margin.Left = 1;
doc.Pages[1].PageInfo.Margin.Top = 1;
doc.Pages[1].PageInfo.Margin.Bottom = 1;
doc.Pages[1].PageInfo.Margin.Right = 1;
// Create a MemoryStream to save the PDF document
System.IO.MemoryStream ms = new System.IO.MemoryStream();
// save the PDF document
doc.Save(ms);
// Create a new Document object from the MemoryStream
Aspose.Pdf.Document temp_doc = new Aspose.Pdf.Document(ms);
// Rotate the first page by 90 degrees
temp_doc.Pages[1].Rotate = Aspose.Pdf.Helpers.Rotation.on90;
// Save the rotated document
temp_doc.Save("c:/pdftest/Image_to_PDF.pdf");
// Close the MemoryStream
ms.Close();
Is there a way to check to see which orientation it is in?
Hi Connor,connork:
Is there a way to check to see which orientation it is in?We would only like to rotate if it is in landscape and also want to make sure we don’t flip the image upside down.