Page 1 of 1

Corrupted file when adding an object

PostPosted: Wed Mar 28, 2012 9:05 pm
by SmithaRSanthosh
I tried to generate a docx document by using an xml string and unmarshalling it. I referred to a document.xml to generate this. The document.xml was :
Code: Select all
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml"><w:body><w:p w:rsidR="005517ED" w:rsidRDefault="00B076EB"><w:r><w:t xml:space="preserve">Testing </w:t></w:r><w:r w:rsidR="00214F89"><w:rPr><w:noProof/></w:rPr><w:drawing><wp:inline distT="0" distB="0" distL="0" distR="0"><wp:extent cx="5486400" cy="3200400"/><wp:effectExtent l="19050" t="0" r="19050" b="0"/><wp:docPr id="1" name="Chart 1"/><wp:cNvGraphicFramePr/><a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"><a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/chart"><c:chart xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" r:id="rId4"/></a:graphicData></a:graphic></wp:inline></w:drawing></w:r></w:p><w:sectPr w:rsidR="005517ED" w:rsidSect="00B66653"><w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0"/><w:cols w:space="720"/><w:docGrid w:linePitch="360"/></w:sectPr></w:body></w:document>


I used this code - i wanted to generate a document similar to the above document.
Code: Select all
String stringForChart = "<w:p  xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" w:rsidR=\"005517ED\" w:rsidRDefault=\"00B076EB\">";
      stringForChart = stringForChart + "<w:r>";
         stringForChart = stringForChart + "<w:t xml:space=\"preserve\">Testing </w:t>";
      stringForChart = stringForChart + "</w:r>";
      stringForChart = stringForChart + "<w:r xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\"  w:rsidR=\"00214F89\">";
      stringForChart = stringForChart + "<w:rPr>";
         stringForChart = stringForChart + "<w:noProof/>";
      stringForChart = stringForChart + "</w:rPr>";
      stringForChart = stringForChart + "<w:drawing>";
      stringForChart = stringForChart + "<wp:inline distT=\"0\" distB=\"0\" distL=\"0\" distR=\"0\">";
      stringForChart = stringForChart + "<wp:extent cx=\"5486400\" cy=\"3200400\"/><wp:effectExtent l=\"19050\" t=\"0\" r=\"19050\" b=\"0\"/>";
      stringForChart = stringForChart + "<wp:docPr id=\"1\" name=\"Chart 1\"/>";
      stringForChart = stringForChart + "<wp:cNvGraphicFramePr/>";
      stringForChart = stringForChart + "<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">";
      stringForChart = stringForChart + "<a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/chart\">";
      stringForChart = stringForChart + "<c:chart xmlns:c=\"http://schemas.openxmlformats.org/drawingml/2006/chart\" ";
      stringForChart = stringForChart + "xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" ";
      stringForChart = stringForChart + "r:id=\"rId4\"/>";
      stringForChart = stringForChart + "</a:graphicData>";
      stringForChart = stringForChart + "</a:graphic>";
      stringForChart = stringForChart + "</wp:inline>";
      stringForChart = stringForChart + "</w:drawing>";
      stringForChart = stringForChart + "</w:r>";
       stringForChart = stringForChart + "</w:p>";
      
       wordMLPackage.getMainDocumentPart().addObject(
             org.docx4j.XmlUtils.unmarshalString(stringForChart) );


My issue is that the code works fine and generates a docx file. When I open the file using Microsoft word it says that its corrupted , in open office I cannot see the chart which I was intending to create. If I add other objects like paragraphs and tables I am able to see them but not this chart. Can you help me with it?

Re: Corrupted file when adding an object

PostPosted: Wed Mar 28, 2012 10:14 pm
by SmithaRSanthosh
I checked my generated file (after unzipping ) with the docx I created manually.

I noticed that in the _rels package in the orginal (manul) file there is a xml - document.xml.rels
This xml contains references to the relationships :ex:

<Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart" Target="charts/chart1.xml" />


My generated folder does not contain this file. Instead the _rels folder only cntains a .rels the contents of which has :


<Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="word/theme/theme1.xml" />

In my code I have references to both themes and charts in the following way :


in = new FileInputStream(System.getProperty("user.dir") + "/Files/theme/theme1.xml");
attachForeignPart(wordMLPackage, wordMLPackage,
externalCtm, "word/theme/theme1.xml", in);


in = new FileInputStream(System.getProperty("user.dir") + "/Files/charts/chart1.xml");
attachForeignPart(wordMLPackage, wordMLPackage.getMainDocumentPart(),
externalCtm, "word/charts/chart1.xml", in);

If I remove the theme relaetd lines from the code - then the Relationship Id="rId4" lines are not present in the .rels file then.

Re: Corrupted file when adding an object

PostPosted: Wed Mar 28, 2012 10:52 pm
by jason
If your attachForeignPart method (based on the ImportForeignPart sample) is doing what you intend, then it should be adding the chart part as a rel of the main document part. Please have a look again in the unzipped file - it ought to be there.

You'll need to alter your attachForeignPart method to return the relId the part is allocated. You need to include this relId in your stringForChart variable where it currently says rId4.

Re: Corrupted file when adding an object

PostPosted: Wed Mar 28, 2012 11:15 pm
by SmithaRSanthosh
Thank You. I was using the IMportForeignPart method as u mentioned.

I have changed it as suck :

Code: Select all
public static String attachForeignPart( WordprocessingMLPackage wordMLPackage,
         Base attachmentPoint,
         ContentTypeManager foreignCtm,
         String resolvedPartUri, InputStream is) throws Exception{
      
      
      Part foreignPart = Load.getRawPart(is, foreignCtm,  resolvedPartUri, null);
         // the null means this won't work for an AlternativeFormatInputPart
      Relationship r = attachmentPoint.addTargetPart(foreignPart);
      // Add content type
      ContentTypeManager packageCtm = wordMLPackage.getContentTypeManager();
      packageCtm.addOverrideContentType(foreignPart.getPartName().getURI(), foreignPart.getContentType());
      
      System.out.println("Attached foreign part: " + resolvedPartUri);
      return r.getId();
   }


And I am using it as :

Code: Select all
in = new FileInputStream(System.getProperty("user.dir") + "/Files/charts/chart1.xml");
      String rid = attachForeignPart(wordMLPackage, wordMLPackage.getMainDocumentPart(),
            externalCtm, "word/charts/chart1.xml", in);
               stringForChart = stringForChart + "r:id='"+rid+ "' />";


It still gives me the same issue with rels :


<Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="word/theme/theme1.xml" />

And the file is still corrupt.

Re: Corrupted file when adding an object

PostPosted: Wed Mar 28, 2012 11:41 pm
by SmithaRSanthosh
I put a few log statements and found that after invoking Load.getRawPart for the chart , it returned rId3 . I was expecting rId4.

The part was recognized as a JaxbXmlPart

In the rels document the mapping was :


<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml" />
<Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="word/theme/theme1.xml" />

In the original document there is a reference to the charts in the document.xml.rels file :
Code: Select all
  <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart" Target="charts/chart1.xml" />



Again as my mentioned before : the _rels folder contains the document.xml.rels in the original docx. But mine has rels file.

Re: Corrupted file when adding an object

PostPosted: Thu Mar 29, 2012 12:02 am
by SmithaRSanthosh
This is my whole code. I am not adding relationships explicity , do I need to add the document.xml.rels also
My code :

Code: Select all
package com;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.Unmarshaller;

import org.docx4j.XmlUtils;
import org.docx4j.jaxb.Context;
import org.docx4j.model.datastorage.CustomXmlDataStorage;
import org.docx4j.model.datastorage.CustomXmlDataStorageImpl;
import org.docx4j.openpackaging.Base;
import org.docx4j.openpackaging.contenttype.ContentTypeManager;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.exceptions.InvalidFormatException;
import org.docx4j.openpackaging.exceptions.PartUnrecognisedException;
import org.docx4j.openpackaging.io.Load;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.Part;
import org.docx4j.openpackaging.parts.PartName;
import org.docx4j.openpackaging.parts.WordprocessingML.BibliographyPart;
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart;
import org.docx4j.openpackaging.parts.WordprocessingML.EmbeddedPackagePart;
import org.docx4j.openpackaging.parts.opendope.ComponentsPart;
import org.docx4j.openpackaging.parts.opendope.ConditionsPart;
import org.docx4j.openpackaging.parts.opendope.QuestionsPart;
import org.docx4j.openpackaging.parts.opendope.XPathsPart;
import org.docx4j.openpackaging.parts.relationships.Namespaces;
import org.docx4j.relationships.Relationship;

public class CreateWordExample {
public static void main(String[] args) throws Exception {
      
      System.out.println( "Creating package..");
      
      WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
      
      // Need to know how what type of part to map to      
      InputStream in = new FileInputStream(System.getProperty("user.dir") + "/Files/[Content_Types].xml");
      ContentTypeManager externalCtm = new ContentTypeManager();
      externalCtm.parseContentTypesFile(in);
      
      // Example of a part which become a rel of the word document
      in = new FileInputStream(System.getProperty("user.dir") + "/Files/settings.xml");
      attachForeignPart(wordMLPackage, wordMLPackage.getMainDocumentPart(),
            externalCtm, "word/settings.xml", in);
      
      // Example of a part which become a rel of the package
//      in = new FileInputStream(System.getProperty("user.dir") + "/Files/_rels/document.xml.rels");
//      attachForeignPart(wordMLPackage, wordMLPackage,
//            externalCtm, "word/rels/.rels", in);
//      org.docx4j.relationships.ObjectFactory factory =
//         new org.docx4j.relationships.ObjectFactory();
//      
//      org.docx4j.relationships.Relationship rel = factory.createRelationship();
//      rel.setType( Namespaces.HYPERLINK  );
//      rel.setTarget(System.getProperty("user.dir") + "charts/chart1.xml");
//      rel.setTargetMode("External"); 
                        
      wordMLPackage.getMainDocumentPart().getRelationshipsPart().addRelationship(rel);
      
      // Example of a part which become a rel of the package
      in = new FileInputStream(System.getProperty("user.dir") + "/Files/theme/theme1.xml");
      attachForeignPart(wordMLPackage, wordMLPackage,
            externalCtm, "word/theme/theme1.xml", in);
      
      // Example of a part which become a rel of the word document
      in = new FileInputStream(System.getProperty("user.dir") + "/Files/charts/chart1.xml");
      String rid = attachForeignPart(wordMLPackage, wordMLPackage.getMainDocumentPart(),
            externalCtm, "word/charts/chart1.xml", in);
      System.out.println("");
      System.out.println(rid);
      System.out.println("");
      // Example of a part which become a rel of the package
      in = new FileInputStream(System.getProperty("user.dir") + "/Files/docProps/app.xml");
      attachForeignPart(wordMLPackage, wordMLPackage,
            externalCtm, "docProps/app.xml", in);
      
   
   
      
      
      
      wordMLPackage.getMainDocumentPart()
      .addStyledParagraphOfText("Title", "Hello world");
      
      org.docx4j.openpackaging.parts.DrawingML.Chart chartPart
      = new org.docx4j.openpackaging.parts.DrawingML.Chart(
            new PartName("/word/charts/chart1.xml"));
      
      EmbeddedPackagePart embeded = new EmbeddedPackagePart(new PartName("/word/embeddings/Microsoft_Office_Excel_Worksheet1.xlsx"));
      
      embeded.setBinaryData(new FileInputStream(System.getProperty("user.dir") + "/Files/embeddings/Microsoft_Office_Excel_Worksheet1.xlsx"));

      wordMLPackage.getParts().getParts().put(new PartName("/word/embeddings/Microsoft_Office_Excel_Worksheet1.xlsx"), embeded);
      
      
      String stringForChart = "<w:p  xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" w:rsidR=\"005517ED\" w:rsidRDefault=\"00B076EB\">";
      stringForChart = stringForChart + "<w:r>";
         stringForChart = stringForChart + "<w:t xml:space=\"preserve\">Testing </w:t>";
      stringForChart = stringForChart + "</w:r>";
      stringForChart = stringForChart + "<w:r xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\"  w:rsidR=\"00214F89\">";
      stringForChart = stringForChart + "<w:rPr>";
         stringForChart = stringForChart + "<w:noProof/>";
      stringForChart = stringForChart + "</w:rPr>";
      stringForChart = stringForChart + "<w:drawing>";
      stringForChart = stringForChart + "<wp:inline distT=\"0\" distB=\"0\" distL=\"0\" distR=\"0\">";
      stringForChart = stringForChart + "<wp:extent cx=\"5486400\" cy=\"3200400\"/><wp:effectExtent l=\"19050\" t=\"0\" r=\"19050\" b=\"0\"/>";
      stringForChart = stringForChart + "<wp:docPr id=\"1\" name=\"Chart 1\"/>";
      stringForChart = stringForChart + "<wp:cNvGraphicFramePr/>";
      stringForChart = stringForChart + "<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">";
      stringForChart = stringForChart + "<a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/chart\">";
      stringForChart = stringForChart + "<c:chart xmlns:c=\"http://schemas.openxmlformats.org/drawingml/2006/chart\" ";
      stringForChart = stringForChart + "xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" ";
      stringForChart = stringForChart + "r:id='"+rid+ "' />";
      stringForChart = stringForChart + "</a:graphicData>";
      stringForChart = stringForChart + "</a:graphic>";
      stringForChart = stringForChart + "</wp:inline>";
      stringForChart = stringForChart + "</w:drawing>";
      stringForChart = stringForChart + "</w:r>";
       stringForChart = stringForChart + "</w:p>";
      
       wordMLPackage.getMainDocumentPart().addObject(
             org.docx4j.XmlUtils.unmarshalString(stringForChart) );
      
       // Here is an easier way:
       String str = "<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" ><w:r><w:rPr><w:b /></w:rPr><w:t>Bold, just at w:r level</w:t></w:r></w:p>";
      
       wordMLPackage.getMainDocumentPart().addObject(
                org.docx4j.XmlUtils.unmarshalString(str) );
   wordMLPackage.getMainDocumentPart().addParagraphOfText("from docx4j!");
      // Now save it
      wordMLPackage.save(new java.io.File(System.getProperty("user.dir") + "/cOut1.docx") );
            
      System.out.println("Done.");
            
   }
   
   
   public static String attachForeignPart( WordprocessingMLPackage wordMLPackage,
         Base attachmentPoint,
         ContentTypeManager foreignCtm,
         String resolvedPartUri, InputStream is) throws Exception{
      
      
      Part foreignPart = Load.getRawPart(is, foreignCtm,  resolvedPartUri, null);
         // the null means this won't work for an AlternativeFormatInputPart
      Relationship r = attachmentPoint.addTargetPart(foreignPart);
      // Add content type
      ContentTypeManager packageCtm = wordMLPackage.getContentTypeManager();
      packageCtm.addOverrideContentType(foreignPart.getPartName().getURI(), foreignPart.getContentType());
      
      System.out.println("Attached foreign part: " + resolvedPartUri);
      return r.getId();
   }
   
   
}

Re: Corrupted file when adding an object

PostPosted: Thu Mar 29, 2012 1:22 am
by sureshbabubv
Hi,

Please use this code

P objP = (P) XmlUtils.unmarshalString(stringForChart , Context.jc,P.class);

bean.getDoc().getMainDocumentPart().addObject(objP);

Thanks & Regards,
B.V.Suresh Babu.

Re: Corrupted file when adding an object

PostPosted: Thu Mar 29, 2012 3:58 pm
by SmithaRSanthosh
I did not get the meaning of the code snippet that you posted. What does P refer to here? and the bean?

Re: Corrupted file when adding an object

PostPosted: Thu Mar 29, 2012 5:21 pm
by SmithaRSanthosh
I can now get the files and folders including document.xml.rels in the correct folders. The document.xml also looks good and almost similar to the manually created file. But I still cannot render the chart. On opening the created file I get the error stating "Error Reading the File"

The difference I noted in the document.xml between the two files was:
In the original file there was :
xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006"
In the docx4j created file there was
xmlns:ns25="http://schemas.openxmlformats.org/drawingml/2006/compatibility"

Again , in my manual file there was a embedded folder with an xlsx containing chart info. In my file that was created via docx4j this folder was not there but the entire contents were there in the chart1.xml .

I want to know how I can debug the error that is causing the error reading file.