Hi,
i have to get different images from database and insert into a word document,but each image has to be placed inside the word document depending on the orientation of the word s page.
i.e
for every process i have to insert images into the same page of the word document,so i need to dynamically change the orientation of that particular page depending on the image being inserted.
Please let me know how can i dynamically change orientation of one particaular page in word using C#.
Regards,
Dilip.
Loading
Jithin KkPosted Mar 11, 2015, 5:38 AM
Also please use the following code after that. To save the changed document and to kill the word application after that. otherwise memory will over flow.
document.Save();
((Microsoft.Office.Interop.Word._Document)document).Close( null, null, null);
((Microsoft.Office.Interop.Word._Application)app).Quit(null, null, null);
Kyle StoryPosted Nov 27, 2014, 5:12 AM
Hi, yes Sunny's trick is the only way because Word documents do not have page concepts but rather sections which can occupy one or more pages. Also note that page break will not suite you because that content will still be in the same section. Also section break continue will not suite you as well because the section will have a beginning on the previous section's last page and it cannot have any effect on it. So your only option is to use section break next page.
Here is a bit .NET friendlier approach that I use:
The code does not use C# calls to a word interop, but rather an API from this word library for .NET.
Sunny SharmaPosted Jul 24, 2013, 2:54 AM
I've the solution for you. It's a little tricky but I've the work around. To achieve this purpose you need to put section breaks on and after the page you want to change the orientation.
Follow the code below:
------------------------------------------
using Microsoft.Office.Interop.Word;
using System.Reflection;
static Missing mv = Missing.Value;
static void Main(string[] args) {
Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = WordApp.Documents.Add(mv, mv, mv, mv);
WordApp.Selection.InsertNewPage(); WordApp.Selection.InsertNewPage();
WordApp.Selection.InsertBreak(WdBreakType.wdSectionBreakNextPage); // here the trick lies
WordApp.Selection.PageSetup.Orientation = WdOrientation.wdOrientLandscape;
WordApp.Selection.InsertNewPage(); WordApp.Selection.InsertNewPage();
WordApp.Selection.InsertBreak(WdBreakType.wdSectionBreakNextPage);
WordApp.Selection.InsertNewPage();
WordApp.Selection.PageSetup.Orientation = WdOrientation.wdOrientPortrait;
object filename = "D:\\test.docx";
doc.SaveAs(ref filename, mv, mv, mv, mv, mv, mv, mv, mv, mv, mv, mv, mv, mv, mv, mv);
doc = null; WordApp.Quit(mv, mv, mv);
}
----------------------------------------------------------------
It's a console-sample application to demo you how to do this.
You can see I've inserted 5 pages and 2 out of them will have Landscape Orientation in the middle. So the final output file will have first two pages in Portrait, middle two in Landscape and the last two in Portrait.
Hope it save your day.
Happy Coding :)
Mark it as answer if it helps. Cheers!!
Iftikar HussainPosted Jul 24, 2013, 1:14 AM
Try like this
Regards,
Iftikar