Word 文書にテキスト ドラフトを含む contentcontrol があります。SharePoint には、Word ドキュメントから PDF ドキュメントを生成するためのアプリケーション ページがありますが、その下書きテキストを削除する必要があります。下書きテキストも下書きと呼ばれるコンテンツ コントロール内にあります。オフィス API を使用できません。OpenXML を使用する必要があります。OpenXMLHelper とメソッド RemoveContentCONtrolANdKeepContents については既に読みましたが、そのコードは非常に紛らわしいので、正確に何をすべきかわかりません。
完了する必要がある次のコードがあります。
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<Uri> lstPackageParts = new List<Uri>();
foreach (PackagePart p in package.GetParts())
{
XmlDocument doc = new XmlDocument();
doc.Load(p.GetStream());
}
}
}
catch(Exception ex)
{
throw ex;
}
}