title: Remove content control from a word document read from sharepoint
body: <p>I have a contentcontrol with the text draft in a word document.
  In sharepoint there is an application page to generate pdf documents from word documents, however I have to  remove that draft text.
  The draft text is inside a content control called draft as well.  I cant use office API's, it has to e use OpenXML, I already read about the OpenXMLHelper and the method RemoveContentCOntrolANdKeepContents but that code is extremely confusing, so I am not sure what to do exactly.</p>
  
  <p>I have the following code which I need to complete,</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
          {
              Logger.LogDebug("ConvertToPdf", "Page_Load(object sender, EventArgs e)", "BEGIN");
  
              string source = Request.QueryString["Source"];
              string messageSource = Request.Url.ToString();
              int id = Convert.ToInt32(Request.QueryString["ID"]);
              string state = Request.GetQueryStringValue(MeetingsCommon.Constants.QUERYSTRINGPARAMETER_STATE_NAME);
              string statusMessage = Request.GetQueryStringValue(MeetingsCommon.Constants.QUERYSTRINGPARAMETER_MESSAGE_NAME);
              this.litMessage.Text = statusMessage;
  
              if (!string.IsNullOrEmpty(state))
                  return;
  
              using (SPLongOperation operation = new SPLongOperation(this.Page))
              {
                  try
                  {
                      operation.Begin();
                      SPWeb currentWeb = SPContext.Current.Web;
                      SPSite currentSite = currentWeb.Site;
                      SPSecurity.RunWithElevatedPrivileges(delegate()
                      {
                          using (SPSite elevatedSite = new SPSite(source))
                          {
                              using (SPWeb elevatedWeb = elevatedSite.OpenWeb())
                              {
                                  elevatedWeb.AllowUnsafeUpdates = true;
                                  SPList drafts = elevatedWeb.GetSafeListByName(MeetingsCommon.Constants.LISTS_HIDDENCONVERSION_NAME);
                                  SPDocumentLibrary draftsDL = elevatedWeb.GetSafeDocumentLibraryByName(MeetingsCommon.Constants.LISTS_HIDDENCONVERSION_NAME);
  
                                  SPListItem item = drafts.GetItemById(id);
  
                                  ConversionJobSettings pdfConversionJobSettings = new ConversionJobSettings();
                                  pdfConversionJobSettings.OutputFormat = SaveFormat.PDF;
                                  pdfConversionJobSettings.OutputSaveBehavior = SaveBehavior.AlwaysOverwrite;
  
                                  string serviceName = "Word Automation Services";
                                  ConversionJob pdfConversionJob = new ConversionJob(serviceName, pdfConversionJobSettings);
                                  pdfConversionJob.UserToken = elevatedWeb.CurrentUser.UserToken;
  
                                  string docxFile = elevatedWeb.Url + "/" + item.Url;
                                  string pdfFile = docxFile.Replace("docx", "pdf");
                                  SPList destinationList = null;
  
  
                                  pdfConversionJob.AddFile(docxFile, pdfFile);
                                  Stream document = draftsDL.GetDocumentTemplate(item.Name);
                                  RemoveDraft(document);
  
  
  
  
  private void RemoveDraft(Stream wordFile)
          {
              Logger.LogDebug("GenerateRefuseLetter", "BuildDocument(Stream templateFile, RefuseLetter refuseLetter)", "BEGIN");
              Stream returnValue = null;
              try
              {
                  using (Package package = Package.Open(wordFile, FileMode.Open, FileAccess.ReadWrite))
                  {
                      string relationshipType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
  
                      //  Get the main document part (workbook.xml, document.xml, presentation.xml).
                      var relationShipCollection = package.GetRelationshipsByType(relationshipType);
                      if (relationShipCollection.Count() == 0)
                          throw new Exception(string.Format("Could not find a relationship with type:{0} in the package", relationshipType));
  
                      //  There should only be one document part in the package. 
                      PackageRelationship relationship = relationShipCollection.First();
  
                      Uri documentUri = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative), relationship.TargetUri);
                      PackagePart documentPart = package.GetPart(documentUri);
  
                      Uri uriData = null;
  
                      List&lt;Uri&gt; lstPackageParts = new List&lt;Uri&gt;();
                      foreach (PackagePart p in package.GetParts())
                      {
                          XmlDocument doc = new XmlDocument();
                          doc.Load(p.GetStream());
  
  
                      }
  
                  }
              }
              catch(Exception ex)
              {
                      throw ex;
              }        
          }
  </code></pre>