How do you set the size of an inline image added to Word docx document using RadDocument?
We need to add the image to a table and have the image scaled at 100%. It scales the image at 156% when exporting to Word docx format
Images added outside of a table are sized at 100% and not scaled up.
When we use the following code to add an image outside of a table,.
//Setup RadDocument
RadDocument document = new RadDocument() { LayoutMode = DocumentLayoutMode.Paged };
document.SectionDefaultPageSize = PaperTypeConverter.ToSize(PaperTypes.Letter);
document.SectionDefaultPageOrientation = PageOrientation.Portrait;
const double pageMarginSizeInches = 0.5;
int marginSizeDip = (int) Unit.InchToDip(pageMarginSizeInches);
document.SectionDefaultPageMargin = new Padding(marginSizeDip);
//Add text and other document content
AddImageTable(document);
//add more text and document content//output to .docx file
//write to .docx file
using (Stream outStream = new FileStream("out.doc, FileMode.Create))
{
DocxFormatProvider provider = new DocxFormatProvider();
provider.Export(document, outStream);
}
//end of program
void AddImageTable(RadDocument document)
{
Section section = new Section();
System.Drawing.Bitmap testImage = ( a 500 pixel by 500 pixel bitmap image taken of an application window)
Table table = new Table();
table.StyleName = RadDocumentDefaultStyles.DefaultTableGridStyleName;
row = new TableRow();
TableCell cell = new TableCell() { PreferredWidth = new TableWidthUnit(TableWidthUnitType.Percent, 20.0f) };
Paragraph par = new Paragraph();
par.Inlines.Add(new Span("Image test"))
cell.Blocks.Add(par);
row.Cells.Add(cell);
cell = new TableCell() { PreferredWidth = new TableWidthUnit(TableWidthUnitType.Percent, 80.0f) };
cell.Blocks.Add(AddImage(null, testImage, 0.0f));
row.Cells.Add(cell);
table.Rows.Add(row);
section.Blocks.Add(table);
document.Sections.Add(section);
}
static Paragraph AddImage(string imagePath, System.Drawing.Bitmap bitmapImage)
{ //add image from the assembly resource or from a Bitmap
try
{
ImageInline imageTmp;
Stream inStream;
if (String.IsNullOrWhiteSpace(imagePath) == false)
{
inStream = Application.GetResourceStream(new Uri(imagePath, UriKind.RelativeOrAbsolute)).Stream;
imageTmp = new ImageInline(inStream);
}
else
{
inStream = new MemoryStream();
bitmapImage.Save(inStream, ImageFormat.Png);
imageTmp = new ImageInline(inStream, new Size(bitmapImage.Width, bitmapImage.Height), "png");
}
Paragraph par = new Paragraph();
par.Inlines.Add(imageTmp);
inStream = null;
return (par);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("AddInlinePicture() - exception - " + ex.ToString() + "\n\n");
throw;
}
}
We need to add the image to a table and have the image scaled at 100%. It scales the image at 156% when exporting to Word docx format
Images added outside of a table are sized at 100% and not scaled up.
When we use the following code to add an image outside of a table,.
//Setup RadDocument
RadDocument document = new RadDocument() { LayoutMode = DocumentLayoutMode.Paged };
document.SectionDefaultPageSize = PaperTypeConverter.ToSize(PaperTypes.Letter);
document.SectionDefaultPageOrientation = PageOrientation.Portrait;
const double pageMarginSizeInches = 0.5;
int marginSizeDip = (int) Unit.InchToDip(pageMarginSizeInches);
document.SectionDefaultPageMargin = new Padding(marginSizeDip);
//Add text and other document content
AddImageTable(document);
//add more text and document content//output to .docx file
//write to .docx file
using (Stream outStream = new FileStream("out.doc, FileMode.Create))
{
DocxFormatProvider provider = new DocxFormatProvider();
provider.Export(document, outStream);
}
//end of program
void AddImageTable(RadDocument document)
{
Section section = new Section();
System.Drawing.Bitmap testImage = ( a 500 pixel by 500 pixel bitmap image taken of an application window)
Table table = new Table();
table.StyleName = RadDocumentDefaultStyles.DefaultTableGridStyleName;
row = new TableRow();
TableCell cell = new TableCell() { PreferredWidth = new TableWidthUnit(TableWidthUnitType.Percent, 20.0f) };
Paragraph par = new Paragraph();
par.Inlines.Add(new Span("Image test"))
cell.Blocks.Add(par);
row.Cells.Add(cell);
cell = new TableCell() { PreferredWidth = new TableWidthUnit(TableWidthUnitType.Percent, 80.0f) };
cell.Blocks.Add(AddImage(null, testImage, 0.0f));
row.Cells.Add(cell);
table.Rows.Add(row);
section.Blocks.Add(table);
document.Sections.Add(section);
}
static Paragraph AddImage(string imagePath, System.Drawing.Bitmap bitmapImage)
{ //add image from the assembly resource or from a Bitmap
try
{
ImageInline imageTmp;
Stream inStream;
if (String.IsNullOrWhiteSpace(imagePath) == false)
{
inStream = Application.GetResourceStream(new Uri(imagePath, UriKind.RelativeOrAbsolute)).Stream;
imageTmp = new ImageInline(inStream);
}
else
{
inStream = new MemoryStream();
bitmapImage.Save(inStream, ImageFormat.Png);
imageTmp = new ImageInline(inStream, new Size(bitmapImage.Width, bitmapImage.Height), "png");
}
Paragraph par = new Paragraph();
par.Inlines.Add(imageTmp);
inStream = null;
return (par);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("AddInlinePicture() - exception - " + ex.ToString() + "\n\n");
throw;
}
}