Update Chart Data while creating Dynamic Word Documents - c#

We have a requirement, in which we need to create dynamic word document and these word documents contains Table, Chart, text and images to be replace/update dynamically. So we decided to create a template and place text holders at dynamic places. To create new document by reading and updating Template we decided to go for OpenXML SDK 2.0.
I am successfully able to replace/update Text/Table and Image content (for place holder tagging we are using Content Controls and we assign a unique tag to each content control.)
I am still trying to figure out, how we will modify the chart data. As far as i know about chart part, it has its own underlying xlx data file, in which it stores the data. So now to update chart part i can think of three different solutions.
1.) Update underlying xlx file by using Open SDK. I am not sure if this can be done or not, but considering its just an xlx file, so i should be able to modify it using Open XML Sdk itself.
2.) Create a chart, convert into image and then replace the image.
3.) I have also read about Custom XML, in which we can bind Content Controls with that XML. So the idea is to create a chart with Custom XML as datasource and then update data xml dynamically.
Any suggestions or Code samples would be appreciated. I am using Open XML SDK 2.0.

I tried with approach1, I modified the underlying xlsx data, but seems it was not sufficient as under Chart.xml -> ChartSpace -> Chart-> PlotArea -> BarChart -> BarChartSeries -> Values, it was saving those numeric values. So i guess i am missing a component, which can read from xlsx and then updates chart accordingly. For now i have manually updated the both xlsx and Values so that it is reflected properly.

Related

itext create pdf based on existing one with changed content

I got quite complicated ready pdf file. It has got barcode and fancy looking table.
I have to create based on it application which will generate pdfs that will look the same but contain different records in the table and different barcode.
Is it possible to copy existing pdf and just change content of barcode and table ?
What would be the best approach to create the same looking pdf but with different content ?
Whank You very much for help
If the barcode and table are static I would open it in photoshop or illustrator delete everything I dont want, Then save it as a pdf again. Then follow this guide iText - add content to existing PDF file and use it as a template to put my custom content in.
If the table and bar code are dynamically generated (each one is different) and you need to crop out content on the fly I would pull some hacky crap and draw white squares over all the content I want gone. then proceed to use it as a template.
Just my 2 cents given the information provided.

How to dynamically populate RTF documents?

Is it possible to create an RTF template document and programmatically insert elements like tables or populate variables? If so, what do I need to do this?
I understand that there is a RichTextBox control that can represent the document, but I need to define a template (or more specifically, the sales guys will be defining the template and giving it to me) which I need to then populate with the data. The RichTextBox control is no good to them. We need RTF because not all clients have Word.
As #ToastyMallows suggested, it's come down to a dirty game of replacing strings.
I'm using the following to generate tables and formatting and stuff:
http://www.codeproject.com/Articles/30902/RichText-Builder-StringBuilder-for-RTF
I've done this, but in an old Windows Forms client-side application using Word automation. It works but not elegantly. We defined the templates in WordPad I think, then extracted the RTF which gets stored in a database. There are pre-defined fields that get populated on document creation.
One thing I have discovered in doing RTF stuff, is that the meaning of RichText (the format) and RichText (as in controls) are not necessarily the same.

Generate PDF based on a Word document for each data item

There're a data list with hundreds of data items (suppose each item is a customer), and a predefined word document as template, the requirement is - for each data item, fill corresponding data into template fields, and generate a readonly PDF file as result.
Prefered platform is ASP.NET with C#.
I found two solutions:
Change the word document into a PDF form, and use iTextSharp to fill the form fields. But create the PDF form with correct format (font, layout, etc.) is a difficult work, and it needs particular tool and new skill when system user wants to add new template (unless the PDF form is always created by developer).
Add text placeholder in the word file, and the program can read word file, replace text, and convert into PDF. But I'm not sure which components should be used.
I'd like to get some advices on this problem. tks.
Update 20130416:
After some searching & experiments, my conclusion is below:
Client solution: use Microsoft.Office.Interop.Word (Office2007+plugin or Office2012) to read data, convert to pdf, etc. But this method running on server side may be unsafe.
Server solution:
Make PDF form, and use iTextSharp to fill the form fields. The disadvantage has been mentioned above.
Make HTML template, and replace field placeholders, and use iTextSharp+XMLWorker to convert HTML to PDF. The difficulty is create the HTML template manually and optimize the PDF effect.
MS SharePoint Office Automation Service is a server solution based on MS Office, perhaps this method will be easier, but it needs license and SharePoint server cost.
Finally, I chose the HTML template solution for this request. QED.
Another option would be to use Tx Text Control for ASP.NET. They have a
mailmerge feature that allows you to fill data into a word template.
The merged document can easily be saved as a pdf.
For the second option you can use iTextsharp or Aspose which supports the placeholder replacement and generation PDF, it supports creating files based on templates of MSWord and Openoffice which could be usefull for user who do not want to buy MSWord only to create a template.
Another option, you can use nustache templates, fill them with list data and then use xmlworker from ItextSharp to render to pdf.

Get Paragraph with complete formatting

Using C# & OpenXML, I need to create a document. The document have series of paragraphs created under docs body. I need to make few paragraphs ready-only. To accomplish this the entire document made read-only. Edit permission to few paragraphs are provided using permStart and permEnd tags. The generated document sent to the user.
The user could edit the editable paras, could add lot of information using tables, paragraphs & could format the text using different color, font size, font type. After making these changes user sends the document back to the server, where I need to extract each editable area's text with complete format and structure & store into a database table. Each editable area's text goes to one column of the table. After saving all editable area's text into their corresponding columns, the docx file discarded.
In future when user requests the same document, we need to take the stored content from db and create a document, by recreating the content with all original formatting/structure.
My question is how can we extract the paragraph with all its formatting intact and store into a database table & recreate the content in another document in future request for the same document.
I'm using OpenXML SDK 2.0, MS-Visual Studio 2010, .NET 4.0 and Win7 OS. The user will use Office 2007/2010 to edit the content.
Finally (few months before) I was able to extract editable paragraphs from word document using Eric White's excellent OpenXML PowerTools OpenSource. Have a look at Eric's DocumentBuilder samples to extract paras from doc

How to read metadata information from docx documents?

what I need to achieve is to have a word document template(docx), which will contain Title, Author name, Date, etc.
This template then will be used by users to complete it. I need to create a c# program, that will take in the docx file and read all the information of interest(title, name, date, ..).
So my questions are:
How do I put the metadata into the template saying: this is Title, this is Date, this is Name, etc? (not programatically)
How do I programmatically read that information?
One way to approach this would be to use Content Controls. In Office, you can create your template, and then for each of your respective inputs of interest you can place one of these controls. They're under the Developer tab in Office.
After inserting your controls you'll need for each of them to have a unique name. Office will let them all have the same name, but you'll need to uniquely identify all of them in your template document.
You now need to get the data that's input in to these controls. Again, there's likely to be some better solutions but Eric White has all kinds of great OpenXML stuff, and so here's one of his: Iterating over Content Controls
I think there's problems with finding content controls nested within a table. So, if you do that, then I think you have to specifically loop over the elements of the table to find content controls within.
Also, you're probably going to want to save a .docx from your .doct file, which I don't think there's any built-in "one-liner" method in OpenXML; however, you can create a new Word document, and then write the file stream of the template in to the newly created docx file. Again, of course, there may be better solutions out there.
Have you been here? There's lots of good stuff:
Introduction to OpenXML
Additionally, Eric has been releasing more and more videos on the OpenXML YouTube channel
1) how do I put the metadata into the template saying: this is Title,
this is Date, this is Name, etc? (not programatically)
You could do that on Info tab in MS Word 2010 as shown below:
2) how do I programmatically read that information?
Once you created your document (or template) you could always look inside it with Open XML SDK 2.0 Productivity Tool (wich is installed with OpenXML SDK) to see where (what classes to use) to get/set some information from/to document.
Also I think this post might help you to solve your task:
Add and update custom document properties in a docx
UPDATE:
Hi Dave,
Please have a look at this MSDN Article - Retrieving Application Properties from Word 2010 Documents by Using the Open XML SDK 2.0
Hope this is exactly what you are looking for.
All OpenXML documents have built in core Metadata that will do what you need through System.IO.Packaging. Once you open the word file using the open xml sdk in c#, you can get to these values via the PackageProperties class. There are 11 Properties you can use.
You "encourage" your user to enter the metadata using Word's Document Information Panel (DIP).
You can force this on by default when they open your template, by a setting in the Developer Toolbar for the template. See the following article on how to set this in your template.
I wrote a quick Windows Form app that displays this information using open xml sdk call to the PackageProperties of the Word file that is displayed above.
Here is the full solution with the sample word file included.
Hope this helps.

Categories

Resources