i’ve got a task to convert docx document to Pdf. I’ve decided to take this approach:
convert docx to html, then pass html to ItextSharp. Few week i’ve been looking all over google, codeplex, sourceforge and stackoverflow and so on for a solution thow to make this conversion, until I found Eric White blog. At firs impression he made great tool for working with OpenXml documents. But when I tried to test it I’ve got an error about null reference. Error occurs when reading header (RevisionAccepter class)
public static void AcceptRevisions(WordprocessingDocument doc)
{
AcceptRevisionsForPart(doc.MainDocumentPart);
foreach (var part in doc.MainDocumentPart.HeaderParts) //part is null
AcceptRevisionsForPart(part); //null ref exception here
foreach (var part in doc.MainDocumentPart.FooterParts)
AcceptRevisionsForPart(part);
if (doc.MainDocumentPart.EndnotesPart != null)
AcceptRevisionsForPart(doc.MainDocumentPart.EndnotesPart);
if (doc.MainDocumentPart.FootnotesPart != null)
AcceptRevisionsForPart(doc.MainDocumentPart.FootnotesPart);
}
code I use for conversion (same as in example)
private void conv()
{
byte[] byteArray = File.ReadAllBytes(textBox1.Text);
using (MemoryStream memoryStream = new MemoryStream())
{
memoryStream.Write(byteArray, 0, byteArray.Length);
using (WordprocessingDocument doc =
WordprocessingDocument.Open(memoryStream, true))
{
HtmlConverterSettings settings = new HtmlConverterSettings()
{
PageTitle = "My Page Title"
};
XElement html = HtmlConverter.ConvertToHtml(doc, settings);
File.WriteAllText("Test.html", html.ToStringNewLineOnAttributes());
}
}
}
nameSpaces:
using System.Xml;
using System.Xml.Xsl;
using OpenXmlPowerTools;
using System.Xml.Linq;
using DocumentFormat.OpenXml.Packaging;
I tried to pass documents created by word 2010, with header and without and still getting errors in the same place. Maybe I’m passing document incorrectly or something with the document itself.
Maybe there is another way to convert docx to pdf without using comercial components, like Apose.
found the problem. Errors was occuring because of different references between power tools project and main project.
DocumentFormat.OpenXml version on my project was 2.5.5513.0 and on power tools – 2.0.5022.0
After making references to the same resource everything went fine.