Get page by name or by number interop Word - c#

I'm trying to access a page in Word by it's name or number. I thought I was going in the right direction but there doesn't seem to be a page.name or page.number property. The issue is in my if statement where I'm trying to say if there is a page named Page 4 Content then select it.
var wordApplication = (word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
string path = CommonMethods.getFile(fileName);
myDoc = wordApplication.Documents.Open(path);
wordApplication.Visible = true;
wordApplication.WindowState = word.WdWindowState.wdWindowStateMaximize;
word.Pages pages = myDoc.ActiveWindow.ActivePane.Pages;
foreach (word.Page p in pages )
{
if (p.)
{
}
}

As you already mentioned there is no number or name property on the page object.
In order to get the page number you have to access the Information property of a Range or Selection object on that page.
In addition to that I recommend to study the article Selecting or referring to a page in the Word object model by Shauna Kelly. In her article she explains in detail why it is often not a good idea to rely on the page object for automated document processing. The reason for that is that Word uses a flow layout instead of a fixed layout. In order to determine the current page rendering Word has to talk to the current printer driver. This means that your page breaks may vary depending on your printer.

I ended up doing the following and it works like a charm.
object What = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage;
object Which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToAbsolute;
object Miss = System.Reflection.Missing.Value;
word.Pages pages = doc.ActiveWindow.ActivePane.Pages;
for (int i = 0; i < pages.Count; i++)
{
if (i == pageNumber)
{
doc.Application.Selection.GoTo(ref What, ref Which, pageNumber, ref Miss);
}
}
}

Related

How to create a tag on a specific line/column/length in Visual Studio editor?

I would like to create tags in the Visual Studio editor to insert all sorts of glyphs, adornments, text hightlightings, etc., based on line/column/length locations in the code.
I have been carefully reading the documentation walkthrough pages (https://learn.microsoft.com/en-us/visualstudio/extensibility/walkthrough-creating-a-margin-glyph?view=vs-2017 and related pages). Although a bit complex and hard to understand, it seems like the API is very much oriented on giving the means to analyse the code: it is able to give your code split into spans, with classifications, etc.
However, I have the "opposite" need: I already have the analysis done by my external analysis engine. And I already have a set of results to be displayed in the editor with line/column/length for each one. Like:
function "foo", located at line 345, column 1, length 3, and other fields containing information to be displayed,
variable "my_var", located at line 349, column 13, length 6, and other fields containing information to be displayed,
Is it possible to create tags in the Visual Studio editor directly based on their line/column/length location? Any hint, any pointer to more detailed documentation or tutorial would be greatly appreciated.
Lance's link was quite helpful to understand another way to create tags different from the MS documentation example.
Indeed, I don't analyse the text contained into the spans, the analysis is already done outside. I get some list of "defects" locations.
I get them into a defectsLocation dictionary (defectsLocation[filename][line] = location data (...)
Here is was I did:
internal class MyDefectTagger : ITagger<MyDefectTag>
{
private IClassifier m_classifier;
private ITextBuffer m_buffer;
internal MyDefectTagger(IClassifier classifier, ITextBuffer buffer)
{
m_classifier = classifier;
m_buffer = buffer;
}
IEnumerable<ITagSpan<MyDefectTag>>
ITagger<MyDefectTag>.GetTags(NormalizedSnapshotSpanCollection spans)
{
if (MyModel.Instance == null || MyModel.Instance.defectsLocation == null)
{
yield return null;
}
var filename = GetFileName(m_buffer);
if (!MyModel.Instance.defectsLocation.ContainsKey(filename))
{
yield return null;
}
foreach (SnapshotSpan span in spans)
{
ITextSnapshot textSnapshot = span.Snapshot;
foreach (ITextSnapshotLine textSnapshotLine in textSnapshot.Lines)
{
var line = textSnapshotLine.LineNumber + 1; // Lines start at 1 in VS Editor
if (MyModel.Instance.defectsLocation[filename].ContainsKey(line) &&
!MyModel.Instance.defectsLocation[filename][line].rendered)
{
var rendered = MyModel.Instance.defectsLocation[filename][line].rendered;
yield return new TagSpan<MyDefectTag>(
new SnapshotSpan(textSnapshotLine.Start, 0),
new MyDefectTag()
);
}
}
}
}
}

Cannot delete file from server folder

I'm working on a simple portfolio project. I would like to show images on a webpage that logged in users can edit. My problem is in the [HttpPost] Edit, more specifically this part:
if (ModelState.IsValid)
{
//updating current info
inDb = ModelFactory<ArtSCEn>.GetModel(db, artSCEn.ArtSCEnID);
inDb.LastModified = DateTime.Now;
inDb.TechUsed = artSCEn.TechUsed;
inDb.DateOfCreation = artSCEn.DateOfCreation;
inDb.Description = artSCEn.Description;
inDb.ArtSC.LastModified = DateTime.Now;
//validating img
if (Validator.ValidateImage(img))
{
inDb.ImageString = Image.JsonSerialzeImage(img);
}
else
{
//return to the UI becuase we NEED a valid pic
return View(artSCEn);
}
db.Entry(inDb).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
//[PROBLEMATIC PART STARTS HERE]
//updating the pic on the server
//getting the string info
string userArtImgFolder = Server.MapPath($"~/Content/Images/Artistic/{inDb.ArtSC.PersonID}");
string imgNameOnServer = Path.Combine(
userArtImgFolder,
$"{inDb.ArtSC.PersonID}_{inDb.ArtSC.ArtSCID}_{inDb.ArtSCEnID}{Path.GetExtension(img.FileName)}");
//deleting previous pic
System.IO.File.Delete(imgNameOnServer);
//creating a new pic
Image.ResizePropotionatelyAndSave(img, Path.Combine(
userArtImgFolder,
$"{inDb.ArtSC.PersonID}_{inDb.ArtSC.ArtSCID}_{inDb.ArtSCEnID}{Path.GetExtension(img.FileName)}"));
return RedirectToAction("Edit", "Art", new { id = inDb.ArtSCID });
}
When I get back the new picture and I want to delete the previous, System.IO.File.Delete() always triggers an exception that it cannot access the resource, because someone else is holding onto it. Any idea what that might be?
Maybe it's something simple, I'm new to ASP, but just can't figure it out.
UPDATE
Following on the suggestions in the comments section, I checked the processes with a tool called Process Monitor and it seems that indeed IIS is locking the resource:
This one appears 2 more times in the logs, by the way.
Judging by the fact that the operation is CreateFileMapping, I guess it has to do with either Server.MapPath() or Path.Combine(), however, the Server is an IDisposable (being derived from Controller), so can that be the one I should deal with?
Also, the resource I'm trying to delete is an image used on the website, which might be a problem, but that section of the website is not shown during this process.
I found the solution building on the comment of #Diablo.
The IIS was indeed holding on to the resource, but Server.MapPath() or any of that code had nothing to do with it: it was the Edit view my page returning the data to. With the help of this SO answer, it turns out I was careless with a BitMap that I used without a using statement in the view to get some image stats. I updated the helper function with the following code:
public static float GetImageWidthFromPath(string imgAbsolutPath, int offset)
{
float width = 0;
using (Bitmap b = new Bitmap(imgAbsolutPath))
{
width = b.Width - offset;
}
return width;
}
Now IIS does not hold on to the resource and I can delete the file.

Adding Multiple Stencils To Same Visio Sheet

I have C# code that creates a Visio Application instance, then opens some existing stencils so I can get the Shape Masters I need for my drawing. Visio 2013 changed things so I need 2 different stencils open. The issue is that I get 2 drawings open in Visio, 1 per stencil. When I build my document and save it, I can close it but there is still another empty drawing open. I also get an empty blank page in my active document where I am creating the drawing.
Visio.Application va = new Visio.Application();
va .Documents.Add(#"");
Visio.Documents vdocs = va.Documents;
const string templateNameU = "BASFLO_M.VSTX";
const string ConnectorStencilNameU = "BASFLO_M.VSSX";
const string RectangleStencilNameU = "BASIC_U.VSS";
const string stencilNameU = "CONNEC_U.VSSX";
const string connectorMasterNameU = "Dynamic Connector";
const string RectangleMasterNameU = "Rounded Rectangle";
Visio.Master connectorMaster = null;
Visio.Master rectangleMaster = null;
// open the templates we need
Visio.Document vc = vdocs.OpenEx(RectangleStencilNameU, short)Visio.VisOpenSaveArgs.visOpenDocked);
va.Documents.Add(templateNameU);
I have tried closing all the open drawings with:
foreach (Visio.Document d in va.Documents)
{
va.Documents[d.Name].Close();
}
va.ActiveDocument.Close();
but that is messy. The for loop for some reason doesn't close the active document. Is there a way to add multiple stencils to the same document and/or page so I am only working with one page? Is there a way to delete the blank page without resorting to a for loop to get the page name to delete it? I have looked through the API and don't see a way.
It looks like you are creating two documents.. i.e. the first (empty) one with va.Documents.Add("") and then the second one based on the template using va.Documents.Add(templateNameU).
If you don't want the first one, just don't create it.. Means, you can create new document, then open stencils, then draw, then close everything, like this:
var doc = va.Documents.Add(templateNameU)
var stn1 = va.Documents.Open(<first stencil>)
var stn2 = va.Documents.Open(<second stencil>)
// ... do stuff with the doc and then close everything...
doc.Close();
stn1.Close();
stn2.Close();
Am I missing something?
BTW, to get "Dynamic connector" you don't need to open the "Connector Stencil", it contains a specific dynamic connector. To get the default connector, you can just use Application.ConnectorToolDataObject
Also, you can connect shapes using shape.AutoConnect (this will also use the default connector)
Also, you don't need to open the stencil specifically actually. If it is part of the template, it will be opened automatically for you when you create a new drawing based on that template (so you can get them using Application.Documents[].
Maybe helpful? (draw 2 rectangles and connect them):
var doc = va.Documents.Add("BASICD_M.VSTX");
var stencil = va.Documents["BASIC_M.VSSX"];
var rectMaster = stencil.Masters["Rounded Rectangle"];
var rect1 = va.ActivePage.Drop(rectMaster, 1, 1);
var rect2 = va.ActivePage.Drop(rectMaster, 3, 1);
rect1.AutoConnect(rect2, Visio.VisAutoConnectDir.visAutoConnectDirNone);

Programmatically Get Page Layout being used by a SharePoint Page

Wondered if it's possible to iterate through the pages held in the pages library and determine the page layout being used by each page? any c# code examples appreciated.
Many thanks in advance
You can get a reference to a PublishingWeb object and throught that the PublishingPage object which has a Layout property.
Below I have butchered the two pages example code to get something close to what you need.
using (SPWeb web = site.OpenWeb(HttpUtility.UrlDecode(webUri.AbsolutePath)))
{
PublishingWeb pWeb = null;
if (!web.Exists || !PublishingWeb.IsPublishingWeb(web))
{
return;
}
pWeb = PublishingWeb.GetPublishingWeb(web);
PublishingPageCollection publishingPages = publishingWeb.GetPublishingPages();
foreach (PublishingPage publishingPage in publishingPages)
{
//do something here with publishingPage.Layout
}
}

How to feed WebBrowser control and manipulate the HTML document?

Good day
I have question about displaying html documents in a windows forms applications. App that I'm working on should display information from the
database in the html format. I will try to describe actions that I have taken (and which failed):
1) I tried to load "virtual" html page that exists only in memory and dynamically change it's parameters (webbMain is a WebBrowser control):
public static string CreateBookHtml()
{
StringBuilder sb = new StringBuilder();
//Declaration
sb.AppendLine(#"<?xml version=""1.0"" encoding=""utf-8""?>");
sb.AppendLine(#"<?xml-stylesheet type=""text/css"" href=""style.css""?>");
sb.AppendLine(#"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.1//EN""
""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"">");
sb.AppendLine(#"<html xmlns=""http://www.w3.org/1999/xhtml"" xml:lang=""en"">");
//Head
sb.AppendLine(#"<head>");
sb.AppendLine(#"<title>Exemplary document</title>");
sb.AppendLine(#"<meta http-equiv=""Content-Type"" content=""application/xhtml+xml;
charset=utf-8""/ >");
sb.AppendLine(#"</head>");
//Body
sb.AppendLine(#"<body>");
sb.AppendLine(#"<p id=""paragraph"">Example.</p>");
sb.AppendLine(#"</body>");
sb.AppendLine(#"</html>");
return sb.ToString();
}
void LoadBrowser()
{
this.webbMain.Navigate("about:blank");
this.webbMain.DocumentText = CreateBookHtml();
HtmlDocument doc = this.webbMain.Document;
}
This failed, because doc.Body is null, and doc.getElementById("paragraph") returns null too. So I cannot change paragraph InnerText property.
Furthermore, this.webbMain.DocumentText is "\0"...
2) I tried to create html file in specified folder, load it to the WebBrowser and then change its parameters. Html is the same as created by
CreateBookHtml() method:
private void LoadBrowser()
{
this.webbMain.Navigate("HTML\\BookPage.html"));
HtmlDocument doc = this.webbMain.Document;
}
This time this.webbMain.DocumentText contains Html data read from the file, but doc.Body returns null again, and I still cannot take element using
getByElementId() method. Of course, when I have text, I would try regex to get specified fields, or maybe do other tricks to achieve a goal, but I wonder - is there simply way to mainipulate html? For me, ideal way would be to create HTML text in memory, load it into the WebBrowser control, and then dynamically change its parameters using IDs. Is it possible? Thanks for the answers in advance, best regards,
Paweł
I've worked some time ago with the WebControl and like you wanted to load a html from memory but have the same problem, body being null. After some investigation, I noticed that the Navigate and NavigateToString methods work asynchronously, so it needs a little time for the control to load the document, the document is not available right after the call to Navigate. So i did something like (wbChat is the WebBrowser control):
wbChat.NavigateToString("<html><body><div>first line</div></body><html>");
DoEvents();
where DoEvents() is implemeted as:
[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public void DoEvents()
{
DispatcherFrame frame = new DispatcherFrame();
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
new DispatcherOperationCallback(ExitFrame), frame);
Dispatcher.PushFrame(frame);
}
and it worked for me, after the DoEvents call, I could obtain a non-null body:
mshtml.IHTMLDocument2 doc2 = (mshtml.IHTMLDocument2)wbChat.Document;
mshtml.HTMLDivElement div = (mshtml.HTMLDivElement)doc2.createElement("div");
div.innerHTML = "some text";
mshtml.HTMLBodyClass body = (mshtml.HTMLBodyClass)doc2.body;
if (body != null)
{
body.appendChild((mshtml.IHTMLDOMNode)div);
body.scrollTop = body.scrollHeight;
}
else
Console.WriteLine("body is still null");
I don't know if this is the right way of doing this, but it fixed the problem for me, maybe it helps you too.
Later Edit:
public object ExitFrame(object f)
{
((DispatcherFrame)f).Continue = false;
return null;
}
The DoEvents method is necessary on WPF. For System.Windows.Forms one can use Application.DoEvents().
Another way to do the same thing is:
webBrowser1.DocumentText = "<html><body>blabla<hr/>yadayada</body></html>";
this works without any extra initialization

Categories

Resources