Get text by line number - visual-studio

What is the easiest way to get the whole line of text having line number/where the mouse caret is? (In currently open document.)
I'm getting line number using:
DTE dte = Package.GetGlobalService(typeof(DTE)) as DTE;
int line = ((EnvDTE.TextSelection)dte.ActiveDocument.Selection).ActivePoint.Line;
also using similar approach I can get selected text:
string line = ((EnvDTE.TextSelection)dte.ActiveDocument.Selection).Text;
but I'm struggling to find anything that could be useful.

To get the whole line of text where the caret is:
var activePoint = ((EnvDTE.TextSelection)dte.ActiveDocument.Selection).ActivePoint;
string text = activePoint.CreateEditPoint().GetLines(activePoint.Line, activePoint.Line + 1);

Related

MigraDoc Formatting

I am completely new to PDF creation including MigraDoc. I have gotten this far, which is really close to what I want for now. My question is that the text string (myMessage) that I pass to the "bodyParagraph" is up to 100 lines long, which causes three pages to be created, which is good. However the first page's Top margin is slightly greater than the second and third pages. I have no idea of why...
Basically, I am trying to create every page the same. Same header, footer and the body to take the same space regardless of the number of lines in the "bodyParagraph" content. If I have taken the completely wrong approach I would be open to suggestions.
Also, if there is a good tutorial to point me to that would be great. I can't really find anything but samples. I have learned everything from the samples, but sections, paragraph, etc is all new to me and I would like to get a better understanding of what I've done.
public static Document CreateWorkOrderPDF2(Document document, string filename, string WorkOrderHeader, string myMessage)
{
Section section = document.AddSection();
section.PageSetup.PageFormat = PageFormat.Letter;
section.PageSetup.StartingNumber = 1;
section.PageSetup.LeftMargin = 40;
//Sets the height of the top margin
section.PageSetup.TopMargin = 100;
section.PageSetup.RightMargin = 40;
section.PageSetup.BottomMargin = 40;
//MARGIN
HeaderFooter header = section.Headers.Primary;
header.Format.Font.Size = 16;
header.Format.Font.Color = Colors.DarkBlue;
MigraDoc.DocumentObjectModel.Shapes.Image headerImage = header.AddImage("../../Fonts/castorgate.regular.png");
headerImage.Width = "2cm";
Paragraph headerParagraph = section.AddParagraph();
headerParagraph = header.AddParagraph(WorkOrderHeader);
//BODY PARAGRAPH
Paragraph bodyParagraph = section.AddParagraph();
bodyParagraph = section.AddParagraph(myMessage);
bodyParagraph.Format.Font.Size = 10;
bodyParagraph.Format.Font.Color = Colors.DarkRed;
//paragraph.Format.Distancne = "3cm";
Paragraph renderDate = section.AddParagraph();
renderDate = section.AddParagraph("Work Order Generated: ");
renderDate.AddDateField();
return document;
}
The line Paragraph bodyParagraph = section.AddParagraph(); adds an empty paragraph. I assume that is the extra space on the first page.
Same issue with renderDate in the following code block.
Just remove the calls section.AddParagraph() to remove the empty paragraphs if you don't want them.
MigraDoc is much like Word and understanding sections, paragraphs, &c. in Word will also help you with MigraDoc. That knowledge along with the samples and IntelliSense should get you going.
You can use MigraDoc to create an RTF file, open the RTF in Word, and click the pilcrow to show formatting characters in Word.

NPOI XWPF how can I place text on a single line that is both left & right justified?

I'm new to using NPOI XWPF and trying to create my first document, so far it's going well. The only issue I have left is trying to place text on the same line that is both left and right justified, I want it to look like:
Area: 1(Left Jstfd) Grade Level/Course: 10th Grade Reading (Right Jstfd)
Below is the code snippet I'm using, it's just pushing all the text together on the left side of the page...blah
XWPFParagraph p2 = doc.CreateParagraph();
p2.Alignment = ParagraphAlignment.LEFT;
XWPFRun r3 = p2.CreateRun();
r3.SetBold(true);
r3.FontFamily = "Times New Roman";
r3.FontSize = 12;
r3.SetText("Area: " + ah.schoolArea);
XWPFRun r4 = p2.CreateRun();
r4.SetBold(true);
r4.FontFamily = "Times New Roman";
r4.FontSize = 12;
r4.SetText("Grade Level/Course: " + ah.filterParm);
Before trying to accomplish a task in (N)POI, it's always good to realize how said task is accomplished in Microsoft Word itself. You can't simply split a paragraph half-way a line, what you do is
Add a tab stop at the end of the line
Set it to right-aligned.
Type text on the left, hit tab, type text on the right
Unfortunately, it doesn't seem XWPFParagraph exposes tabstop functionality at this point. However, XWPFParagraph is a wrapper around the CT_P class, which maps 1:1 onto the underlying Office XML format. Using reflection, we can access this private field and use it to directly add the tabstop.
Sample code:
var paragraph = document.CreateParagraph();
var memberInfo = typeof(XWPFParagraph).GetField("paragraph", BindingFlags.NonPublic | BindingFlags.Instance);
if (memberInfo == null)
{
throw new Exception("Could not retrieve CT_P from XWPFParagraph");
}
var internalParagraph = (CT_P) memberInfo.GetValue(paragraph);
CT_PPr pPr = internalParagraph.AddNewPPr();
CT_Tabs tabs = pPr.AddNewTabs();
CT_TabStop tab = tabs.AddNewTab();
tab.pos = "9000";
tab.val = ST_TabJc.right;
var run = paragraph.CreateRun();
run.SetText("Left aligned");
run.AddTab();
run = paragraph.CreateRun();
run.SetText("Right aligned");
Result:

How do I get the line of text under the cursor in a TextView in gtk#?

I have a GTK# TextView and I want to read the line of text under the cursor. I don't see a single method that will do that, so I think I need to combine several method calls, like Buffer.GetText, Buffer.GetIterAtOffset, Buffer.CursorPosition, but it's not obvious to me what the right combination is.
TextIter are a bit odd to use. Buffer.CursorPosition gives you the current position.
It's easy to find the end of the line:
var end = Buffer.CursorPosition;
end.ForwardToLineEnd();
To get the first character, there's not symetrical method, so you might try:
var start = Buffer.CursorPosition;
start.BackwardChars(start.LineOffset); // LineOffset gives you the iter offset on the current line.

Break line in long label text

Is there any trick to break a label text? Because '\n' '\r' '\n\r' don't work.
Many Thanks
if you use those 2 parameters you do what you want don't you ?
app.createLabel(text).setWidth(width).setWordWrap(true)
here is an example (among other widgets ;-):
function showurl() {
var app = UiApp.createApplication();
app.setTitle("Anchor in a popup ;-)");
var panel = app.createFlowPanel()
var image = app.createImage('https://sites.google.com/site/appsscriptexperiments/home/photo.jpg').setPixelSize(50, 50)
var link = app.createAnchor('This is your link', 'https://sites.google.com/site/appsscriptexperiments/home');
var lab = app.createLabel("wrap it because it's too narrow").setWidth(90).setWordWrap(true);
var quit = app.createButton('quit');
panel.add(image).add(link).add(lab).add(quit);
app.add(panel);
var doc = SpreadsheetApp.getActive();
doc.show(app);
}
EDIT : I found an old post(on the Google group forum, thanks again Henrique ;-) about breaking lines in toast messages and here is the code I used for that case... the principle should work for Labels too but I didn't try.
To use it, just use \n (where you want to break the line) in a variable containing your text and pass it through this function. (there are some comment in the script to explain)
function break_(msg){
var temp = escape(msg);// shows codes of all chars
msg = unescape(temp.replace(/%20/g,"%A0")); // replace spaces by non break spaces
temp = msg.replace("\n"," "); // and replace the 'newline' by a normal space
return temp; // send back the result
}
Would something like this work?
//takes a line of text and returns a flex table broken by \n
function breakLabel(text) {
var app = UiApp.getActiveApplication();
var flexTable = app.createFlexTable();
text = text.split('\n'); // split into an array
for (var i=0; i<text.length; i++){
flexTable.setWidget(i, 0, app.createLabel(text[i].toString()));
}
return flexTable;
}
Adding them to a vertical panel helps as well (not the way you want, but still..):
var vPanel = app.createVerticalPanel().setSize(100,100);
var label = app.createLabel('predominantly blabla blala blabla');
app.add(vPanel.add(label));
See reference
For anyone just now stumbling upon this, the best solution seems to be creating an HTML output for anything that needs line breaks.
Documentation
var htmlApp = HtmlService
.createHtmlOutput('<p>A change of speed, a change of style...</p>')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setTitle('My HtmlService Application')
.setWidth(250)
.setHeight(300);
SpreadsheetApp.getActiveSpreadsheet().show(htmlApp);
// The script resumes execution immediately after showing the dialog.

Can we paste lines into visual studio "without carriage return"?

Lets say we have these lines in the editor
int a = 10;
print(a,b);
string b = "hello";
So what i would like to do is.. shift the 3rd line to the 2nd position similar to
int a = 10;
string b = "hello";
print(a,b);
I use ctrl+x or Shift + del to cut the line to the clipboard.
But on pasting the line at 2nd position i get
int a = 10;
string b = "hello";
print(a,b);
The extra blank line at the 3rd line. is there any way to paste without that 3rd blank line. or for the matter an easier way to move and cut and paste lines. ?
Shift+Alt+T swaps the current line with the line below it. So you'd put the caret (or cursor) on the second line, then use that keyboard shortcut to swap it with the third line.
Thank you so much for the answer!.
i just stumbled on to a easier method and more flexible.
With a Addon - Pro tools
http://visualstudiogallery.msdn.microsoft.com/en-us/d0d33361-18e2-46c0-8ff2-4adea1e34fef
we can now use Alt + Arrow keys to move the lines up or down.

Resources