|
//Get page size
SizeF pageSize = page.Size; |
|
//Create a PdfGrid
PdfGrid pdfGrid = new PdfGrid();
//Draw grid to the page of PDF document
PdfGridLayoutResult result = pdfGrid.Draw(page, new PointF(10, 10));
RectangleF tableSize = result.Bounds; |
|
If the image I'm inserting is large (2000px width 1000px height) would this scale to fit the cell?
Do I need to scale the image down? Ideally, I could get the page-grid-width OR the cell-width compared to the actual image width, then scale the image proportionally by that factor.
PdfBitmap does not seem to offer that kind of functionality however.
Any suggestions on doing that?
I could scale the image using SkiaSharp functionlity then insert the scaled image into the grid. Not sure if that'll work...
|
While inserting the image to PdfGridCell, there is no need to scale the image to cell size. Because the image will fit to the cell size while adding it as background. Please refer the below code snippet,
Or else you can insert the image to grid cell by using BeginCellLayout event handler. Please refer the below code snippet for more details,
KB: https://www.syncfusion.com/kb/9746/how-to-add-two-images-in-one-pdf-table-cell-using-c-and-vb-net
| ||
|
As-is, the image doesn't even print on the pdf, no error either. |
Please provide us the below details to replicate to check this issue in our end and provide the precise solution on this.
|
|
pdfGrid.BeginCellLayout += PdfGrid_BeginCellLayout;
private static voidPdfGrid_BeginCellLayout(object sender, PdfGridBeginCellLayoutEventArgs args)
{
//Draw image in first row of cell 0
if (args.RowIndex == 1 && args.CellIndex == 0)
{
//Load the image
PdfBitmap image = new PdfBitmap(imageStream);
//Draw the image
args.Graphics.DrawImage(image1, newRectangleF(args.Bounds.X, args.Bounds.Y,image.Width, args.Bounds.Height));
}
} |
|
private static void ReduceImageSize(System.Drawing.Image image, string destinationPath,int percentage)
{
if (percentage < 0 || percentage > 100)
throw new ArgumentOutOfRangeException("quality must be between 0 and 100.");
// Encoder parameter for image quality
EncoderParameter qualityParam = newEncoderParameter(System.Drawing.Imaging.Encoder.Quality, percentage);
// JPEG image codec
ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");
EncoderParameters encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = qualityParam;
System.Drawing.Image img = Resize(image, 300, 300);
img.Save(destinationPath, jpegCodec, encoderParams);
}
public static System.Drawing.Image Resize(System.Drawing.Image image, int width, intheight)
{
var res = new System.Drawing.Bitmap(width, height);
using (var graphic = System.Drawing.Graphics.FromImage(res))
{
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphic.SmoothingMode = SmoothingMode.HighQuality;
graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphic.CompositingQuality = CompositingQuality.HighQuality;
graphic.DrawImage(image, 0, 0, width, height);
}
return res;
}
/// <summary>
/// Returns the image codec with the given mime type
/// </summary>
private static ImageCodecInfo GetEncoderInfo(string mimeType)
{
// Get image codecs for all image formats
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
// Find the correct image codec
for (int i = 0; i < codecs.Length; i++)
if (codecs[i].MimeType == mimeType)
return codecs[i];
return null;
} |
|
- Does the doc.PageSettings.Width property include margins? is that actual page-width? |
Yes, document.PageSettings.Width includes the margin. If you want to set the page width without margin, please use the below code snippet,
| |
|
- Are ALL size properties in the Syncfusion.PDF assembly given in points? (not pixels) |
The unit of measurement used in Syncfusion PDF is in Points. | |
|
- What is the resolution of the PDFs generated? 75dpi? higher? That will affect point or pixel dimensions...
|
PDF is not dependent on resolution and we draw every element in the sale of 96 DPI. | |
|
- what about inserting an image into a cell but NOT as background?
|
As we said earlier, inserting images in a grid cell is of below two types.
| |
|
When inserting images as background, they scale to the width of a col or a col-span already.
ALL I WANT out of this is to maintain image aspect ratio... like DO NOT stretch or shrink the image HEIGHT to fill the row or row-span. Maintain image proportions!
|
While inserting an image to grid cell using background or BeginCellLayout event handler, the image should be shrink or stretch if the image size is greater than cell width and height. If you want to add an image without stretch or shrink, you can set the height of the grid cell using below code snippet,
|
- Does the doc.PageSettings.Width property include margins? is that actual page-width? | Yes, document.PageSettings.Width includes the margin. If you want to set the page width without margin, please use the below code snippet, In that case, what is the default margin value?
| |
- Are ALL size properties in the Syncfusion.PDF assembly given in points? (not pixels) | The unit of measurement used in Syncfusion PDF is in Points. | |
- What is the resolution of the PDFs generated? 75dpi? higher? That will affect point or pixel dimensions... | PDF is not dependent on resolution and we draw every element in the sale of 96 DPI. Very good to know | |
- what about inserting an image into a cell but NOT as background? | As we said earlier, inserting images in a grid cell is of below two types.
The problem with the sample you posted on that is that not every row[0] cell[0] will have an image. It is difficult to insert into the right cell when the first cell in each row does not always require an image. See my sample pdf I attached to last post. | |
When inserting images as background, they scale to the width of a col or a col-span already. ALL I WANT out of this is to maintain image aspect ratio... like DO NOT stretch or shrink the image HEIGHT to fill the row or row-span. Maintain image proportions! | While inserting an image to grid cell using background or BeginCellLayout event handler, the image should be shrink or stretch if the image size is greater than cell width and height. If you want to add an image without stretch or shrink, you can set the height of the grid cell using below code snippet,
If I do this and the image is 1200x wide and 900px high, the image scales down width-wise to fit the cell BUT the height is then distorted. |
|
In that case, what is the default margin value?
|
The default margin value of the PDF document is 40. | ||
|
The problem with the sample you posted on that is that not every row[0] cell[0] will have an image. It is difficult to insert into the right cell when the first cell in each row does not always require an image. See the sample pdf I attached to last post. |
Using BeginCellLayout event handler, you can insert an image in any row (any cell) or particular row (particular) by checking the condition of the respective row is equal toargs.RowIndex or args.CellIndex.
i.e.,
Note: We have one property to set the image position in the cell by using ImagePosition. Please refer the below code snippet for more details,
This Enum contains Center, Fit, Stretch, and tile values. If this is suitable for your requirement, please make use of it.
| ||
|
If I do this and the image is 1200x wide and 900px high, the image scales down width-wise to fit the cell BUT the height is then distorted. |
As we said earlier, to insert image in PdfGrid cell by using the below methods,
By using the above image should be shrink or stretch if the image size is greater than the cell size. Other than this we do not contain any method to do this. So we could not able to proceed further in this.
|