Problema con header di un documento OOXML

giovedì 05 marzo 2009 - 12.01

lucaformisano Profilo | Newbie

Salve a tutti e grazie per l'attenzione....
sono alle prese ancora con un documento .docx ...
il documento e' composto da un body contenuto nel document.xml e un header
contenuto nel file header1.xml...
con il body non ho problemi....riesco tranquillamente a modificarlo e il
documento generato funziona perfettamente....
invece ho grossi problemi con l'header quando cerco di modificarlo...il file
docx generato risulta corrotto e word non me lo apre restituendomi l'errore:
"Una dichiarazione di testo o xml puo' apparire solo all'inizio dell'input in /word/header1.xml riga 324 colonna 63"
in quella posizione non c'e' niente ho provato anche a fargli inserire nel pacchetto l'header originale invece di quello modificato da me...(quindi solo rimozione e reinserimento) ma mi da lo stesso errore.
segue il codice (le parti commentate sono quelle che uso x la gestione
dell'header...) :


XDocument doc = XDocument.Parse(body);

XDocument head = XDocument.Parse(header);

string SaveLocation = Server.MapPath("IncaricoOccasionale") + "\\" +
"document.xml";

doc.Save(SaveLocation);

string SaveLocationHeader = Server.MapPath("IncaricoOccasionale") + "\\"
+ "header1.xml";

head.Save(SaveLocationHeader);

Uri uridel = new Uri("/word/document.xml", UriKind.Relative);

Uri urirel = new Uri("/word/_rels/document.xml.rels", UriKind.Relative);

Uri uridelheader = new Uri("/word/header1.xml", UriKind.Relative);

Uri urirelheader = new Uri("/word/_rels/header1.xml.rels",
UriKind.Relative);

Package p =
System.IO.Packaging.Package.Open(Server.MapPath("IncaricoOccasionale") +
"\\" + "contratto.docx");

p.DeletePart(uridel);

p.DeletePart(uridelheader);

p.DeleteRelationship("rId1");

PackagePart part =
p.CreatePart(uridel,"application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml");

PackagePart rela = p.CreatePart(urirel,
"application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml");

PackagePart hed = p.CreatePart(uridelheader,
"application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml");

PackagePart relhed = p.CreatePart(urirelheader,
"application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml");

StreamWriter stream = new StreamWriter(part.GetStream(FileMode.Create,
FileAccess.ReadWrite));

StreamWriter stream2= new StreamWriter(rela.GetStream(FileMode.Create,
FileAccess.ReadWrite));

StreamWriter stream3 = new StreamWriter(hed.GetStream(FileMode.Create,
FileAccess.ReadWrite));

StreamWriter stream4 = new StreamWriter(relhed.GetStream(FileMode.Create,
FileAccess.ReadWrite));

System.Xml.XmlDocument newdoc = new System.Xml.XmlDocument();

newdoc.Load(SaveLocation);

newdoc.Save(stream);

System.Xml.XmlDocument newdoc2 = new System.Xml.XmlDocument();

newdoc2.Load(Server.MapPath("IncaricoOccasionale") + "\\" +
"document.xml.rels");

newdoc2.Save(stream2);

stream2.Close();

System.Xml.XmlDocument newdoc3 = new System.Xml.XmlDocument();

newdoc3.Load(SaveLocationHeader);

newdoc3.Save(stream3);

System.Xml.XmlDocument newdoc4 = new System.Xml.XmlDocument();

newdoc4.Load(Server.MapPath("IncaricoOccasionale") + "\\" +
"header1.xml.rels");

newdoc4.Save(stream3);

stream3.Close();

stream4.Close();

p.Flush();

p.CreateRelationship(uridel, TargetMode.Internal,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",
"rId1");

p.Close();







GRAZIE


Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5