I want to Accept all the track changes from word document. I have written following codes to do so.(I am using PowerTools from codeplex.)
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(filePath, true))
{
OpenXmlPowerTools.RevisionAccepter.AcceptRevisions(wordDoc);
}
But above code is not working in some of the document. It shows System.Exception: Internal error 20, found element exception in some of the document.
So is there any issue with my word document? If yes then what should I look into document? In short I want to know that what is wrong with my document so that I can correct my document to run above code.
Another thing is that I am able to accept tracking changes in Word 2013/2010/2007 itself!!
Any help would be highly appreciated,
I have asked same question on https://powertools.codeplex.com/discussions and they have updated RevisionAccepter.cs file to get this resolved.
Thread from Power Tools Discussion
Related
I am having trouble updating a Hyperlink in a Word doc (Q How to update the body and a hyperlink in a Word doc ) and am zooming in on the Descendants<T>() call not working. Here is my code:
using DocumentFormat.OpenXml.Packaging; //from NuGet ClosedXML
using DocumentFormat.OpenXml.Wordprocessing; //from NuGet ClosedXML
WordprocessingDocument doc = WordprocessingDocument.Open(...filename..., true);
MainDocumentPart mainPart = doc.MainDocumentPart;
IEnumerable<Hyperlink> hLinks = mainPart.Document.Body.Descendants<Hyperlink>();
The doc is opened OK because mainPart gets a value. But hLinks has no elements. If I open the Word doc in Word, a hyperlink is present and working.
In the Immediate Window I see the following values:
mainPart.Document.Body
-->
{DocumentFormat.OpenXml.Wordprocessing.Body}
ChildElements: {DocumentFormat.OpenXml.OpenXmlChildElements}
ExtendedAttributes: {DocumentFormat.OpenXml.EmptyEnumerable<DocumentFormat.OpenXml.OpenXmlAttribute>}
FirstChild: {DocumentFormat.OpenXml.OpenXmlUnknownElement}
HasAttributes: false
HasChildren: true
InnerText: "
lots of data, e.g:
...<w:t>100</w:t>...
mainPart.Document.Body.Descendants<Text>().First()
-->
Exception: "Sequence contains no elements"
If I cannot even find the text parts, how should I ever find and replace the hyperlink?
If you are sure there are elements in your file that you are searching with linq, and nothing is returning or you are getting exceptions, that typically points to a namespace problem.
If you post your entire file, I can better help you, but check to see if you can alias your namespace like so:
using W = DocumentFormat.OpenXml.Wordprocessing;
and then in your Descendants call you do something like this:
var hLinks = mainPart.Document.Body.Descendants<W.Hyperlink>();
This answer demonstrates another namespace trick to try also.
Something seems to be wrong with my Word doc; it was generated with a tool. Testing with another Word doc, created with Word, gives better results. I am working on it ...
With a regular Word doc, looking at
doc.MainDocumentPart.Document.Body.InnerXml
the value starts with:
<w:p w:rsidR=\"00455325\" w:rsidRDefault=\"00341915\"
xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">
<w:r>
<w:t>Hello World!
but with the word doc I am testing with, which comes from a tool I myself made:
<w:body xmlns:w=\"http://schemas.openxmlforma...
This explains a lot.
I will have to fix my tool :-)
Update:
The fix was that this did not give the correct part of data to insert in the Word Doc:
string strDocumentXml = newWordContent.DocumentElement.InnerXml;
but instead this is the correct data:
string strDocumentXml = newWordContent.DocumentElement.FirstChild.OuterXml;
Inspection with the debugger of:
doc.MainDocumentPart.Document.Body.InnerXml
as mentioned above, confirmed it. The Descendants call now returns the expected data, and updating the hyperlink works.
Side note:
I clearly fixed a bug in my app, but, apart from updating the hyperlink, the app worked perfectly OK before, with that bug :-)
I am having trouble updating a Hyperlink in a Word doc (Q How to update the body and a hyperlink in a Word doc ) and am zooming in on the Descendants<T>() call not working. Here is my code:
using DocumentFormat.OpenXml.Packaging; //from NuGet ClosedXML
using DocumentFormat.OpenXml.Wordprocessing; //from NuGet ClosedXML
WordprocessingDocument doc = WordprocessingDocument.Open(...filename..., true);
MainDocumentPart mainPart = doc.MainDocumentPart;
IEnumerable<Hyperlink> hLinks = mainPart.Document.Body.Descendants<Hyperlink>();
The doc is opened OK because mainPart gets a value. But hLinks has no elements. If I open the Word doc in Word, a hyperlink is present and working.
In the Immediate Window I see the following values:
mainPart.Document.Body
-->
{DocumentFormat.OpenXml.Wordprocessing.Body}
ChildElements: {DocumentFormat.OpenXml.OpenXmlChildElements}
ExtendedAttributes: {DocumentFormat.OpenXml.EmptyEnumerable<DocumentFormat.OpenXml.OpenXmlAttribute>}
FirstChild: {DocumentFormat.OpenXml.OpenXmlUnknownElement}
HasAttributes: false
HasChildren: true
InnerText: "
lots of data, e.g:
...<w:t>100</w:t>...
mainPart.Document.Body.Descendants<Text>().First()
-->
Exception: "Sequence contains no elements"
If I cannot even find the text parts, how should I ever find and replace the hyperlink?
If you are sure there are elements in your file that you are searching with linq, and nothing is returning or you are getting exceptions, that typically points to a namespace problem.
If you post your entire file, I can better help you, but check to see if you can alias your namespace like so:
using W = DocumentFormat.OpenXml.Wordprocessing;
and then in your Descendants call you do something like this:
var hLinks = mainPart.Document.Body.Descendants<W.Hyperlink>();
This answer demonstrates another namespace trick to try also.
Something seems to be wrong with my Word doc; it was generated with a tool. Testing with another Word doc, created with Word, gives better results. I am working on it ...
With a regular Word doc, looking at
doc.MainDocumentPart.Document.Body.InnerXml
the value starts with:
<w:p w:rsidR=\"00455325\" w:rsidRDefault=\"00341915\"
xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">
<w:r>
<w:t>Hello World!
but with the word doc I am testing with, which comes from a tool I myself made:
<w:body xmlns:w=\"http://schemas.openxmlforma...
This explains a lot.
I will have to fix my tool :-)
Update:
The fix was that this did not give the correct part of data to insert in the Word Doc:
string strDocumentXml = newWordContent.DocumentElement.InnerXml;
but instead this is the correct data:
string strDocumentXml = newWordContent.DocumentElement.FirstChild.OuterXml;
Inspection with the debugger of:
doc.MainDocumentPart.Document.Body.InnerXml
as mentioned above, confirmed it. The Descendants call now returns the expected data, and updating the hyperlink works.
Side note:
I clearly fixed a bug in my app, but, apart from updating the hyperlink, the app worked perfectly OK before, with that bug :-)
First I would like to point out that stackowerflow helped me with many problems in the past, so thank you all. But now I have come to problem that I haven't fount a solution for yet and it's driving me crazy. I'm not native english speaker, so sorry for any language mistakes.
So here it is:
I'm generating pdf with itextsharp library(great library by the way). I'm starting with some kind of pdf form/template, to which i'm adding 'fill-out' data. I'm using PdfReader to read template pdf and by caling PdfStamper method GetOverContent(pageNum) for individual pages I get PdfContentByte. With that PdfContentByte I'm adding my text/data (BeginText and EndText is used on every page). Most of text I add with method ShowTextAligned. That all ok, generated pdf contains my text. The problem begins where i have to add 'columned' text. I do that with following code:
ColumnText ct = new ColumnText(cb);//cb is PdfContentByte
Phrase p = new Phrase(txt, FontFactory.GetFont(DEFAULT_FONT, BaseFont.CP1250, true, font_size));
ct.SetSimpleColumn(p, x, y, x+width, y+height, 10, alignment);
ct.Go();
setDefaultFont();//sets font to PdfContentByte again with setFontAndSize and SetColorFill
Columned text is added with this code OK, but the text(on that same page/same PdfContentByte) added AFTER this with ShowTextAligned is not visible in Acrobat Reader.
Here is the 'fun' part - that text in same pdf file opened with foxit reader is fine/visible/ok.
So text added with ShowTextAligned after adding ColumnText is not visible in acrobat reader but visible in foxit reader just fine. This problem exists inside one page, new page resets this problem (PdfContentByte for next page is new).
My workaround for that was to add all ColumnText AFTER all calls of ShowTextAligned. That worked till today, when customer printed out generated pdf with acrobat reader, which after printing the document, displayed message that pdf contains error and that author of pdf should be contacted. Version of Adobe Reader is 10.1.1. Problem is not in customer computer, same thing hapens on my computer.
After researching the web I installed Adobe Acrodat Pro Trial which contains tool Preflight, which is purposed for analyzing pdfs (as far I understand). This tool outputs warning "Invalid content state stream for operator". And here I'm stucked. I belive the problem exists inside added ColumnText, because document generated without them causes no problem displaying/printing and Preflight states "No problem found".
It is possible that i'm missing some fact and that the problem is in my code...
Please help me, because i'm runnig out of ideas.
I hope this post will help someday someone else with the same problem.
I cannot attach sample pdf because it contains sensitive data, but if there is no other way, i'll recreate the scenario/code.
So to answer my question/problem:
When writing to pdf using PdfContentByte and using method ShowTextAligned you have to call BeginText before writing and after you are finished you have to call EndText. So i did. BUT if you want to add some other element(like ColumnText, Image and probably anything else) you can't do that before you call EndText. If you do, generated pdf will be 'problematical'/corrupted.
So in pseudocode following is wrong:
BeginText();
ShowtextAligned();
AddImage();
ShowtextAligned();
EndText();
Correct usage is:
BeginText();
ShowtextAligned();
EndText();
AddImage();
BeginText();
ShowtextAligned();
EndText();
I hope this will help someone someday somewhere.
Okay... I'm trying to use the most recent version of ITextSharp to turn an XML file into a PDF. It isn't working.
The documentation on SourceForge doesn't seem to have kept up with the actual releases; the code in the provided example won't even compile under the newest version.
Here is my test XML:
<Remittance>
<RemitHeader>
<Payer>BlueCross</Payer>
<Provider>Maricopa</Provider>
<CheckDate>20100329</CheckDate>
<CheckNumber>123456789</CheckNumber>
</RemitHeader>
<RemitDetail>
<NPI>NPI_GOES_HERE</NPI>
<Patient>Patient Name</Patient>
<PCN>0034567</PCN>
<DateOfService>20100315</DateOfService>
<TotalCharge>125.57</TotalCharge>
<TotalPaid>55.75</TotalPaid>
<PatientShare>35</PatientShare>
</RemitDetail>
</Remittance>
And here is the code I'm attempting to use to turn that into a PDF.
Document doc = new Document(PageSize.LETTER, 36, 36, 36, 36);
iTextSharp.text.pdf.PdfWriter.GetInstance(doc,
new StreamWriter(fileOutputPath).BaseStream);
doc.Open();
SimpleXMLParser.Parse((ISimpleXMLDocHandler)doc,
new StreamReader(fileInputPath).BaseStream);
doc.Close();
Now, I was pretty sure the (ISimpleXMLDocHandler)doc piece wasn't going to work, but I can't actually find anything in the source that both a) implements ISimleXMLDocHandler and b) will accept a standard XML document and parse it to PDF.
FYI- I did try an older version which would compile using the example code from sourceforge, but it wasn't working either.
After browsing the 5.0.2 code, it looks like there is no document that will just take XML and turn it into a PDF for you. So, unless you can find similar code on the web or in an old release of iTextSharp, you'll need to write it yourself.
iText XML to PDF requires that the XML be formatted according to itext.dtd (please Google responsibly). You can also use a tagmap.xml to map your XML entities to those appropriate according to the DTD.
I get the “Command Failed” exception when using the following code to insert content from one word 2007 document into another using bookmarks in c# :
string filePath = #“C:\temp\one.doc”;
object trueObj = true;
object falseObj = false;
wordApp.Selection.InsertFile( filePath, ref missing, ref falseObj, ref trueObj, ref falseObj );
"one.doc" is another word document containing table content.
Error code: -2146824090. This error generally comes when the target object is disposed or unavailable. Not sure why I am getting it here.
Also when I remove table content from the target document and I just add formatted text, the operation succeeds. When the same operation is performed through word GUI, the operation works fine. Have scoured the internet for pointers on this issue, but none were helpful in resolving this.
Thanks in advance,
Bharath K.
We have solved this problem by defining a macro that performs the above actions and invoking the macro using c# from my program. That worked!
I encountered this problem and I noticed that range where to insert the file contains some locked content controls. Before using InsertFile command make sure that range (Selection here) does not contain locked content controls.
Regards.