We have a business requirement which is similar to word automation service but this requirement we have used ASPOSE and SharePoint CSOM.
Requirement:
Requirement:
- Get the document from SharePoint Library
- Read the document data in the format of Byte[]
- Get the data from workflow history List
- Append the history data to document in the format of Table
- Convert Word to PDF
- Upload PDF to another SharePoint Library
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aspose.Words;
using Aspose.Words.Saving;
using Microsoft.SharePoint.Client;
using ClientOM = Microsoft.SharePoint.Client;
using System.IO;
namespace DocumentToPDF
{
class Program
{
static private void CopyStream(Stream source, Stream destination)
{
try
{
byte[] buffer = new byte[32768];
int bytesRead;
do
{
bytesRead = source.Read(buffer, 0, buffer.Length);
destination.Write(buffer, 0, bytesRead);
} while (bytesRead != 0);
}
catch (Exception ex)
{
throw ex;
}
}
static void Main(string[] args)
{
try
{
//Update this Info accordingly
string siteUrl = "http://contoso:1515/sites/OldRC";
string sourceLibraray = "Shared Documents";
string targetLibrary = "http://contoso:1515/sites/OldRC/Shared%20Documents/";
string tempFileDirectory = "F:/Nagaraju/Shared/";
string workflowHistoryList = "PollQuestions";
ClientContext clientContext =
new ClientContext(siteUrl);
List sharedDocumentsList = clientContext.Web.Lists
.GetByTitle(sourceLibraray);
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = @"<View>
<Query>
<Where><Eq><FieldRef Name='Status' /><Value Type='Choice'>2</Value></Eq></Where>
</Query>
</View>";
ListItemCollection listItems = sharedDocumentsList.GetItems(camlQuery);
clientContext.Load(sharedDocumentsList);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
foreach (ClientOM.ListItem item in listItems)
//if (listItems.Count == 1)
{
//ClientOM.ListItem item = listItems[0];
Console.WriteLine("FileLeafRef: {0}", item["FileLeafRef"]);
string fileName = item["FileLeafRef"].ToString();
Console.WriteLine("FileDirRef: {0}", item["FileDirRef"]);
Console.WriteLine("FileRef: {0}", item["FileRef"]);
Console.WriteLine("File Type: {0}", item["File_x0020_Type"]);
Console.WriteLine("File Name: {0}", item["FileRef"]);
FileInformation fileInformation =
ClientOM.File.OpenBinaryDirect(clientContext, (string)item["FileRef"]);
using (MemoryStream memoryStream = new MemoryStream())
{
CopyStream(fileInformation.Stream, memoryStream);
Aspose.Words.Document DOC = new Aspose.Words.Document(memoryStream);
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.Compliance = PdfCompliance.PdfA1b;
//Get Approvers info
//ClientContext clientContext = new ClientContext(siteUrl);
List approverList = clientContext.Web.Lists.GetByTitle(workflowHistoryList);
camlQuery.ViewXml = string.Format(@"<View>
<Query>
<Where><Eq><FieldRef Name='ParentId' /><Value Type='Lookup'>{0}</Value></Eq></Where>
</Query>
</View>", item.Id.ToString());
ListItemCollection approverItems = approverList.GetItems(camlQuery);
clientContext.Load(approverList);
clientContext.Load(approverItems);
clientContext.ExecuteQuery();
ClientOM.ListItem approverItem = approverItems[0];
DocumentBuilder builder = new DocumentBuilder(DOC);
//First Table
// We call this method to start building the table.
builder.StartTable();
builder.InsertCell();
builder.Write("Header 1");
// Build the second cell
builder.InsertCell();
builder.Write("Header 2");
// Build the third cell
builder.InsertCell();
builder.Write("Header 3");
// Call the following method to end the row and start a new row.
builder.EndRow();
// Build the first cell of the second row - Update List column internal names.
builder.InsertCell();
builder.Write(item["FileLeafRef"].ToString());
// Build the second cell.
builder.InsertCell();
builder.Write(item["Title"].ToString());
// Build the third cell.
builder.InsertCell();
builder.Write(item["ID"].ToString());
builder.EndRow();
//Second Table
builder.MoveToDocumentEnd();
// We call this method to start building the table.
builder.StartTable();
builder.InsertCell();
builder.Write("Header 1");
// Build the second cell
builder.InsertCell();
builder.Write("Header 2");
// Build the third cell
builder.InsertCell();
builder.Write("Header 3");
// Build the fourth cell
builder.InsertCell();
builder.Write("Header 4");
// Call the following method to end the row and start a new row.
builder.EndRow();
// Build the first cell of the second row- Update List column internal names.
builder.InsertCell();
builder.Write(approverItem["Question"].ToString());
// Build the second cell.
builder.InsertCell();
builder.Write(approverItem["Answer1"].ToString());
// Build the second cell.
builder.InsertCell();
builder.Write(approverItem["Answer2"].ToString());
// Build the second cell.
builder.InsertCell();
builder.Write(approverItem["Answer3"].ToString());
builder.EndRow();
// Signal that we have finished building the table.
builder.EndTable();
DOC.Save(tempFileDirectory + fileName.Split('.')[0] + ".pdf", saveOptions);
FileStream fstream = System.IO.File.OpenRead(tempFileDirectory + fileName.Split('.')[0] + ".pdf");
byte[] content = new byte[fstream.Length];
fstream.Read(content, 0, (int)fstream.Length);
fstream.Close();
FileCreationInformation fi = new FileCreationInformation();
fi.Url = targetLibrary + fileName.Split('.')[0] + ".pdf";
fi.Content = content;
sharedDocumentsList.RootFolder.Files.Add(fi);
clientContext.ExecuteQuery();
Console.WriteLine("Document uploaded to SharePoint Library.");
}
}
//else
// Console.WriteLine("Document not found.");
}
catch (Exception ex)
{
throw ex;
}
}
}
}
No comments:
Post a Comment