Images scaling to fit page in available space or move it next page

Hi,

We are using Aspose.pdf.4.1 for dot net currently to generate PDFs, we faced issues when PDFs generated with Images.
We upgraded to Aspose.pdf.4.5. issues with images are fixed but, earlier pdfs which were displaying images properly are now not displaying images properly.

We are using table to display the paragraphs in the PDFS and these paragraphs might contain an images as well.
Image will be coming dynamically from the server, and these image sizes vary for each request. Hence the images are not displayed completely. We can view just first half of the image.

We tried below things to fix this issue. but still we have not got the images right for the all the PDFs generated.
1. setting the paragraph with images as first paragraph so that the image is displayed in next page.
2. setting the position as paragraph relative to the paragraph which has image.
3. setting iskeptWithNext=true
4. Giving the fixwidth and fixheight properties under the imageInfo object.(this is not a preferred solution even if this could be the solution.)

We can reduce the image using the ImageScale property, but we want to calculate it using some formula so that smaller images should not turn into even smaller images.

Please advise on the solution for this.

Hello Praveen,

Can you please share the code snippet/sample project that can help us to reproduce this problem at our end. More along, please share some sample PDF outputs from v4.1.0 and v4.5.0 so that we can notice the problem clearly. You may also share some of the images that you are using in order for us to replicate the problem properly. We apologize for your inconvenience.

praveen_nh:
Giving the fixwidth and fixheight properties under the imageInfo object.(this is not a preferred solution even if this could be the solution.)

As per your requirement of placing image object in table cell, may be you can set the image Height and Width based over Table cell width and height. Please take a look over the following code snippet, may be it can be of any help in order to get your current issue resolved.

[C#]

Pdf pdf1 = new Pdf();
Aspose.Pdf.Section SampleSection = new Aspose.Pdf.Section();
SampleSection = pdf1.Sections.Add();

Table SampleTable = new Table();\
SampleTable.ColumnWidths = "100 100";
Aspose.Pdf.Row SampleRow= SampleTable.Rows.Add();
SampleRow.FixedRowHeight = 100;

Aspose.Pdf.Image myimage = new Aspose.Pdf.Image();
myimage.ImageInfo.File = "d:/pdftest/DRAFT+20100525_new.001.jpeg";
myimage.ImageInfo.ImageFileType = ImageFileType.Jpeg;

Aspose.Pdf.Cell ImageCell= SampleRow.Cells.Add();
ImageCell.Paragraphs.Add(myimage);
// Set the Image Widht equal to cell width
myimage.ImageInfo.FixWidth = ImageCell.FitWidth;
// Set the Image Height equal to Row Height
myimage.ImageInfo.FixHeight = SampleRow.FixedRowHeight;

SampleSection.Paragraphs.Add(SampleTable);
pdf1.Save(@"D:\pdftest\ColorMixText.pdf");

Thanks for the reply. We are using same code that you have shared.
In our case, we are adding text and image paragraphs to the same cell.
Please refer the sample code below(Modified the code you have shared.)

Solution that you have shared will shrink the image to fit in into the cell, this way image looks very small, instead, we want to move the image to the next page. Please let us know if there is way to do that.
Also the paragraph after image is overlapping with copyright.

Code:

Pdf pdf1 = new Pdf();
Aspose.Pdf.Section SampleSection = new Aspose.Pdf.Section();
SampleSection = pdf1.Sections.Add();
Text text = new Text();
Segment seg = new Segment();
StringBuilder sb = new StringBuilder();
sb.Append(“Evénement”);
sb.Append(“Une catastrophe équivalente à 40 fois l’« Erika »La quantité…”);
sb.Append(“versement correspond à un fonds de pension d’environ 11 millions de livres. Ces paiements, non sans”);
sb.Append(“rappeler ceux de Fred Goodwin, l’ancien patron de Royal Bank of Scotland, se justifient contractuellement”);
sb.Append(“dans la mesure où Tony Hayward n’est pas mis à la porte, mais qu’il s’agit d’un départ à l’amiable.”);
sb.Append(“Néanmoins, ils devraient provoquer la colère des pêcheurs et petites entreprises touchées par la marée”);
sb.Append(“noire.”);
sb.Append(“Le président de BP sur un siège éjectable ?”);
sb.Append(“Mais à quoi joue donc Carl-Henric Svanberg ? Le président non exécutif de BP n’a pas cru bon mardi de”);
sb.Append(“passer plus de quinze minutes lors de la très attendue conférence avec les analystes. Après quatre”);
sb.Append(“questions, il a présenté ses excuses, citant un emploi du temps très chargé. L’incident aurait pu passer”);
sb.Append(“inaperçu si l’homme n’avait pas déjà été très critiqué pour avoir été particulièrement absent pendant la”);
sb.Append(“marée noire, n’apparaissant finalement en juin qu’au moment de la rencontre avec Barack Obama. Si bien”);
sb.Append(“que Carl-Henric Svanberg, qui n’est président de BP que depuis le 1er janvier, pourrait être le prochain sur”);
sb.Append(“la liste à prendre la porte, après Tony Hayward. « La réaction générale des investisseurs et de l’industrie est”);
sb.Append("[…] qu’il sera probablement remplacé dès que possible », estiment les analystes de CM-CIC.");
seg.Content = sb.ToString();
text.Segments.Add(seg);

Aspose.Pdf.Table SampleTable = new Aspose.Pdf.Table();
SampleTable.ColumnWidths = “100 100”;
Aspose.Pdf.Row SampleRow= SampleTable.Rows.Add();
SampleRow.FixedRowHeight = 100;
Aspose.Pdf.Cell samplecell = SampleRow.Cells.Add();
samplecell.ColumnsSpan = 3;
samplecell.Paragraphs.Add(text);
Aspose.Pdf.Image myimage = new Aspose.Pdf.Image();
myimage.ImageInfo.File = “d:/pdftest/test1.JPG”;
myimage.ImageInfo.ImageFileType = ImageFileType.Jpeg;

samplecell.Paragraphs.Add(myimage);

[//Aspose.Pdf.Cell](https://aspose.pdf.cell/) ImageCell= SampleRow.Cells.Add();
[//ImageCell.Paragraphs.Add](https://imagecell.paragraphs.add/)(myimage);
// Set the Image Widht equal to cell width
[//myimage.ImageInfo.FixWidth](https://myimage.imageinfo.fixwidth/) = samplecell.FitWidth;
// Set the Image Height equal to Row Height
myimage.ImageInfo.FixHeight = SampleRow.FixedRowHeight;
Cell cell2 = SampleRow.Cells.Add();
SampleSection.Paragraphs.Add(SampleTable);
pdf1.Save(@“D:\pdftest\ColorMixText.pdf”);

Hi Nayyer,

PFA the sample PDF generated from the application using 4.6.0 version of Aspose dll. As explained in the other post, we use a single cell and add all the texts’ and images’ to the same cell (as different paragraphs).

In this scenario, is it possible to move the image to the next page?

Thanks.

Hello Praveen,

As far as I have noticed, you are using same table cell to place the text contents and image file. In order to resolve this problem, please create a separate row with a table cell and place the image inside it. In case the image is small it will be placed in the same page as the text contents and in case the image file is large, it will automatically be moved to next page. This way the image will not be truncated. Please take a look over the following code snippet that you can add before saving the PDF document.

[C#]

Aspose.Pdf.Image myimage = new Aspose.Pdf.Image();
myimage.ImageInfo.File = "d:/pdftest/microsoft-silverlight.png";
myimage.ImageInfo.ImageFileType = ImageFileType.Png;

Aspose.Pdf.Row ImageRow = SampleTable.Rows.Add();
Aspose.Pdf.Cell ImageCell = ImageRow.Cells.Add();
ImageCell.Paragraphs.Add(myimage);

Please take a look over the attached resultant PDF documents that I have generated using the updated code. In case it does not resolve your problem or you have any further query, please feel free to contact. We apologize for your inconvenience.

Hi Nayyer,

Thanks for the response. Adding image in a new Row did fix the issue.

But please let us know, if it was possible to fix it in the other scenario (adding text contents and image to the same cell content).

Hello Praveen,

Thanks for your feedback.

I need to check with our development team to see if we can resolve this problem or not. Please spare us little time and we will get back to you soon.

Your patience and comprehension is greatly appreciated in this regard. We apologize for your inconvenience.

Hi Nayyer,

A gentle reminder. We are waiting for your response.

Thanks.

Hello Praveen,

Thanks for your patience.

I am pleased to inform you that the issue reported earlier has been resolved in Aspose.Pdf for .NET 4.7.0 (which can be downloaded from this link). In this release version, a new capability to adjust the image dimensions has been added. In case the image is large and it cannot be placed over single page, its size is automatically adjusted to be accommodated over single page. Please try using it and in case you still face any problem or you have any further query, please feel free to contact. We apologize for the delay and inconvenience.