问题描述
我正在使用VB.net 2010和word2000.我正在尝试做的事情是获取要绘制的行以锚定到正在工作"的当前页面上.截至目前,我只是用第一页调用了这部分代码,而现在没有文字的情况下将其推到最后一页.
I''m using VB.net 2010 and word 2000. What I''m trrying to do is get the lines that I''m drawing to anchor to the current page that is being "worked on". As of right now I just have this portion of code being called with the first page and it''s getting pushed to the last page with no text on it.
wrdDoc.Shapes.AddLine(intBeginX, intBegY, intEndX, intEndY).Select()
根据endtil之后的intillisense有一个锚对象,但是它不是很直观.我尝试了MSDN Library,这更没有用了,我已经尝试过使用google搜索,到目前为止还没有运气.
我要做的就是让那些愚蠢的行在被调用时被锚定在当前页面上.在侧面请注意,我正在使用该文本文件生成字母的文本文件中没有行,这些行位于一个单独的文件中,该文件被称为包含坐标.
这就是我喜欢使用PDF的原因,您只需告诉它所需的位置,而不必担心漂移.
在此先感谢!!!
there is a anchor object according to the intillisense after the endY but it''s not very intuitive. I tried the MSDN Library and that is even more useless, I''ve tried searching with google and no such luck so far.
All I want to do is get those stupid lines to be anchored on the current page when it is called. on a side note the text file that I''m working with to produce the letter does not have the lines in them, the lines are in a seperate file that is being called containing the coordinates.
This is the reason I like working with PDF''s you can just tell it where you want it and not have to worry about drifting.
Thanks in advance!!!
推荐答案
Anchor
Optional Object. A Range object that represents the text to which the label is bound. If Anchor is specified, the anchor is positioned at the beginning of the first paragraph in the anchoring range. If this argument is omitted, the anchoring range is selected automatically and the label is positioned relative to the top and left edges of the page.
以下是我一起破解的一些快速而又肮脏的代码,它们创建了一个Word文档,该文档由两页组成,每页包含一点文字和一行.
Below is some quick and dirty code I hacked together, that creates a Word document consisting of two pages, each containing a little text and a line.
using System.Runtime.InteropServices;
using Word = Microsoft.Office.Interop.Word;
...
...
Word.ApplicationClass wrdApp = null;
const string TEXT =
"This is some random text.\n" +
"This is more random text.\n" +
"This is the final bit of random text.";
try
{
wrdApp = new Word.ApplicationClass();
// ----------Create new document----------
wrdApp.Documents.Add();
Word.Document wrdDoc = wrdApp.ActiveDocument;
// ----------Populate document----------
// Page 1
wrdDoc.Range(Start: 0, End: 0).Text = TEXT; // Insert text at start of document
wrdDoc.Shapes.AddLine(50, 50, 100, 100, wrdDoc.Range(Start: 0, End: TEXT.Length - 1)); // Insert line anchored to first paragraph within specified range
// Page break
wrdDoc.Range(TEXT.Length+1, TEXT.Length+1).Select(); // Put selection at end of inserted text.
wrdApp.Selection.InsertBreak(Word.WdBreakType.wdPageBreak); // Start a new page at current selection (i.e. after inserted text)
// Page 2
Word.Range startOfPage2 = wrdDoc.Range(Start: wrdApp.Selection.Start);
startOfPage2.Text = TEXT; // Insert text at beginning of 2nd page
wrdDoc.Shapes.AddLine(150, 50, 200, 100, startOfPage2); // Add shape on 2nd page, anchored to top of page
// ----------Save document----------
wrdDoc.SaveAs2(FileName: "Test.docx");
wrdDoc.Close();
wrdDoc = null;
}
finally
{
if (wrdApp != null)
{
wrdApp.Quit();
Marshal.FinalReleaseComObject(wrdApp);
}
}
因此,基于此,我建议创建一个Range对象,该对象代表要向其添加行的页面,然后像往常一样调用wrdDoc.Shapes.AddLine
,但是将range对象作为锚点传递,并且希望可以在正确的页面上插入该行.据我所知,文档的当前选择与行的位置无关.
希望这会有所帮助!
So based on this, I''d recommend creating a Range object representing the page you want to add a line to, and then calling wrdDoc.Shapes.AddLine
as you''ve been doing, but passing in the range object as your anchor, and that will hopefully insert the line on the correct page. The document''s current Selection has nothing to do with the line placement as far as I could tell.
Hope this helps!
这篇关于试图在Word文档上画线,出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!