This is a migrated thread and some comments may be shown as answers.

RadDocument - add ImageInline bitmap at 100% scaling in a table cell

3 Answers 260 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
C
Top achievements
Rank 1
C asked on 17 Jul 2014, 09:54 PM
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;
 }
}


3 Answers, 1 is accepted

Sort by
0
C
Top achievements
Rank 1
answered on 17 Jul 2014, 10:21 PM
The System.Drawing.Bitmap has
 height 500,
 width 500,
 horizontal resolution 149.987
 vertical resolution 149.987
 pixel format Format32bppArgb
 physical dimension width 500, height 500


0
Accepted
Petya
Telerik team
answered on 22 Jul 2014, 08:48 AM
Hello,

Please find attached a sample application that uses the code you sent us with some minor changes to make it run. As far as I tested this, both when showing the document in RadRichTextBox and exporting it the image is with its original dimensions. Please let me know if I am missing something in your scenario.

Looking forward to your reply.

Regards,
Petya
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
C
Top achievements
Rank 1
answered on 01 Oct 2014, 10:25 PM
We ended up going with fixed width tables and table cells since the percentage scaling did not work out.

Tags
RichTextBox
Asked by
C
Top achievements
Rank 1
Answers by
C
Top achievements
Rank 1
Petya
Telerik team
Share this question
or