Click here to Skip to main content
15,885,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My software copies graphs into a Word document, and the document is based on various templates that users have made, therefore the usable page height varies, depending on header/footer and margins. I'm trying to optimize the graph height and there may be 2-3 graphs per page, depending on the report type.

What I have tried:

I've tried using the Range.PageSetup.PageHeight property and subtracting the Range.PageSetup.TopMargin, Range.PageSetup.BottomMargin, Range.PageSetup.HeaderDistance, and Range.PageSetup.FooterDistance, but the calculation seems to be missing something, i.e. the returned graph height is a little too big or small to fit the number of graphs per pg. I realize it depends when this calculation is done in the code, since the formatting may be adjusted, so the calculation is done just prior to coping the graph.
Posted
Updated 30-Jul-20 20:48pm
Comments
Maciej Los 30-Jul-20 4:10am    
What method do you use to copy a graph?
Darryl Bryk 30-Jul-20 20:41pm    
Code to copy a graph:
using (MemoryStream mem = new MemoryStream()) {
cht.SaveImage(mem, ChartImageFormat.Bmp);
using (Bitmap bmp = new Bitmap(mem)) {
Clipboard.SetImage(bmp);
if (Clipboard.ContainsImage())
par.Range.Paste();
else throw new Exception("Clipboard.ContainsImage() failed");
}
}
[no name] 30-Jul-20 17:10pm    
The "template" should be set up so that the images are scaled properly. Or use image "placeholders" to get properties.
Darryl Bryk 30-Jul-20 20:42pm    
I'd rather not rely on users that make templates determining the placement of images.

1 solution

Darryl Bryk wrote:
Code to copy a graph:
C#
using (MemoryStream mem = new MemoryStream()) {
cht.SaveImage(mem, ChartImageFormat.Bmp);
using (Bitmap bmp = new Bitmap(mem)) {
Clipboard.SetImage(bmp);
if (Clipboard.ContainsImage())
par.Range.Paste();
else throw new Exception("Clipboard.ContainsImage() failed");
}
}


I was almost sure that you're pasting the image instead of graph...

One way to fit image is to add an image into a cell of table. I'd suggest to use borderless table and change its default AutoFitBehaviour to wdAutoFitWindow. You can also try to use wdAutoFitFixed. Please see: Table.AutoFitBehavior method (Word) | Microsoft Docs[^]

Another solution is to use TextFrame object (Word) | Microsoft Docs[^].
 
Share this answer
 
Comments
Darryl Bryk 2-Aug-20 16:28pm    
Yes, Word will let the image be pasted into the page with auto re-sizing, but this doesn't allow me to make it have 2-3 images per pg. To do that I need to fit placeholders, like one above suggested, or re-size them by calculation, which is what I'm trying to do.

It seems to me that Microsoft would know how to break this info. out since it auto-resizes, so it knows how much space is left on the pg. I just need to know how to access the same info.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900