I would like to know if there is a way to "play" with sentences in a .docx.
Here's what I need to do:
I have a paragraph in a document
exemple of my paragraph:
This is a paragraph that I need to format based on some conditions and I can't figure how to do this with openxml sdk.
end of exemple.
So based on a condition that I evaluate in c# I would like to add/remove the text. Another thing that you should know id that I would like the product owner to change the text of the document.
Basicly what I want to acheive is having a template document that my product owner can edit at will but in this document the text might change based on for whom the document is produce.
Thanks
You can create a template document in Word and create a content control in each place you want the text to be dynamically inserted. For each created content control, you should set unique Tag property value.
In your c# application code, you can then find a content control by its tag quite easily. After you find it, you can save its parent node, remove the content control and insert the text you want as a child paragraph element for the parent node saved earlier.
You can add content controls by using Developer tab. If you can't see it, you can use steps described here in order to show it: http://msdn.microsoft.com/en-us/library/bb608625.aspx .
There are some complexities involved in replacing text because text can be broken into multiple runs. If you want your owner to be able to edit the document, this is a problem you must solve. Luckily, there is sample code that you can use as part of the PowerTools for Open XML. PowerTools were written as cmdlets for PowerShell, but you can take the core C# code for your own programs. The TextReplacer.cs module should give you a good starting point. You will need some of the other modules that it depends on, like PtOpenXmlDocument.cs, PtOpenXmlUtil.cs and PtUtil.cs. I hope that helps.
Related
I am using Silverlight RichTextEditor ( reference ) that does not have bulleting or numbering of text. Is there any way to do so in a simpler manner.
If you need to apply bullets/numbering/indent to selected-text in a RichTextBox, then you have to programmatically find Paragraph elements inside selected-text and insert the corresponding character (bullet or number of tab) at the start of each Paragraph element.
Check out here for detailed explanation.
Or,
Check out the ordered/unordered list controls explained in this link. You can extend its functionality to acheive what you intend to do. I have used it to show hierarchical data using nested lists and found it fairly easy to modify it to my needs.
I want a way to manipulate a word document.
The document is a word template .DOTX file, and I need to hide and/or show specific paragraphs of the document based on conditions on my ASP.NET/C# application.
For example, if i entered the first and last names in my form, the generated word doc will show both fields, else of the last name is not entered it will not be shown in the generated document. I already know how to generate the doc but I dont know how to hide and/or show specific elements.
I am using Docentric Toolkit for this kind of scenario. The toolkit itself is using OpenXML in the background, so no Word installation is necessary. I suggest you take a look at the example for conditional content here.
In your ASP.NET application you prepare a data model. This model should also include a boolean field where you set true/false value depending on whether you want to show conditional content in the document or not. The document template contains conditional tag, which is used as a placeholder for conditional content. Other tags (placeholders) can be nested in conditional content.
What I would like to do is to be able to write some hidden marks in the document, so that when the user fills in some information, then I can process each part of the document according to the marks or sections that surrounded it. I'm using .NET, any ideas? Thanks
Take a look at SdtElements tag, OpenXml Sdk and Word 2007 Content Control Toolkit
The essence is:
Create a Word document that will be your template
In Word, turn on Developer tab
In the Developer tab there is a Controls group. Click the "Aa" ones to insert a new content tag, click "Properties" to edit the tag alias and data, turn on Design Mode to see the content tags
The steps below apply if your app can output an XML you would bind to your document:
Open the Word Content Control Toolkit
Open the document you created
Attach an xml in the right-side panel
double-click on content tags to edit their XPath binding or use drag-drop; see WCCT manual for that
Once you have your template prepared, depending on your actual task at hand you can do many things with these content tags, including:
replace the custom xml part in your document to update the data users will see when they open it
use openxml sdk to replace the data without opening it in Word
use the template to collect data (possible but unreliable, not recommended)
delete and insert content tags from code via openxml sdk
In your code you can find the content tags by using LINQ queries over OpenXmlSdk objects like so:
var contentTags = document.MainDocumentPart.Document.Body
.Descendants<SdtElement>()
.Where(x => x.SdtProperties.GetFirstChild<Tag>() != null && x.SdtProperties.GetFirstChild<DataBinding>() != null)
.Select(x => x.SdtProperties.GetFirstChild<Tag>().Val.InnerText =="Whatever Tag you're looking for or other condition to match several")
.ToList();
You can use custom document properties as described here or here
I am creating a blog and I am wondering how to allow an admin user to add styles to their post. For instance, if they would like to bold a few words in a paragraph or add an image in the middle of the paragraph.
I am using MVC 4 and entity framework. The plan is to have a text area for the user to create a post and save to the database. And also allow the user to do basic formatting to the text.
Thanks in advance.
You will want to implement a (possibly jQuery?) HTML WYSIWYG editor, such as Redactor
. There are many WYSIWYG plugins out there that are configurable and hook to your textarea.
First try to make a simple textarea that can save your text in your database then after that you can change that textarea and use some plug-ins like ckeditor (references: http://drupal.org/project/ckeditor, http://drupal.org/project/ckeditor), you may also like to use nicEdit which I'm also using right now (http://nicedit.com/), or you may want to use WYSIWYM Markdown Editor[wmd] (https://github.com/derobins/wmd, http://code.google.com/p/wmd/) like this one on stack overflow.. And another one Rich Text Editor [RTE] (demo is here: http://www.kevinroth.com/rte/demo.htm, main site is here: http://www.kevinroth.com/rte/)
I am working on a project in C# that will produce a Word document using the Word Automation API.
I would like to insert page breaks at specific points in the generated document and I am currently doing this successfully with the following code:
// Generate page break
object pageBreak = WdBreakType.wdPageBreak;
wordApp.Selection.InsertBreak(ref pageBreak);
However, if the document has naturally wrapped onto the next page anyway after running out of room on the previous page then I don't really want to be generating a page break or else I will end up with a blank page.
What I would really like is the ability to find out exactly where the cursor is and if it is on the first line and column of the current page then I can safely assume that it is not necessary to insert a page break.
Is there a way to access the position of the cursor? Or another solution that would do the same thing? It seems like a simple requirement so I apologize in advance if I have missed the obvious.
Assuming that you are programmatically building the document in a way that would cause wordApp.Selection to properly reflect your actual (and relevant) position in the document, you can determine its line and column on its starting page using its Information property and the following two WdInformation enums (shown here as VBA; not sure what the .NET PIA syntax is offhand):
line = wordApp.Selection.Information(wdFirstCharacterLineNumber)
col = wordApp.Selection.Information(wdFirstCharacterColumnNumber)
These values correspond to the Ln and Col values seen in Word's status bar. If they are both equal to 1, then you are in the first position of the page on which the Selection begins.
Good luck!
You could try setting property on the paragraph that causes it to have a page break before it. I don't if Word Automation API allows it to be set, but it seems exactly what you need.