Response a new word from a template without copy - openxml

`
System.IO.File.Copy(wordFullPath, wordFullPathCopy, true);
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(wordFullPathCopy, true))
{
string docText = null;
using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
{
docText = sr.ReadToEnd();
docText = docText.Replace("[$1-1-1$]", "TEST1");
docText = docText.Replace("[$1-1-2$]", "TEST2");
using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
{
sw.Write(docText);
}
}
}
byte[] byteArray = File.ReadAllBytes(wordFullPathCopy);
System.IO.File.Delete(wordFullPathCopy);
Page.Response.Clear();
Page.Response.AppendHeader("Content-Disposition", "attachment; filename=DocxDllTest.docx");
Page.Response.ContentType = "application/octet-stream";
Page.Response.BinaryWrite(byteArray);
Page.Response.OutputStream.Flush();
Page.Response.OutputStream.Close();
Page.Response.Flush();
Page.Response.End();`
Hi~I have a docX template(Of course,It must read only!!).
Now I make a copy and use WordprocessingDocument to replace text.
It worked fine.
But now,I want to not make a copy.
Just replace text and save to memorystream then Response to user.
Is it possible? and How??
Thx!!

byte[] wordByteArray = null;
string wordFullPath = FileHelper.FileFromFileDir("ATT1.docx"); //Template
wordByteArray = File.ReadAllBytes(wordFullPath);
using (MemoryStream ms = new MemoryStream())
{
string docText = null;
ms.Write(wordByteArray, 0, (int)wordByteArray.Length);
using (WordprocessingDocument doc = WordprocessingDocument.Open(ms, true))
{
using (StreamReader sr = new StreamReader(doc.MainDocumentPart.GetStream()))
{
docText = sr.ReadToEnd();
docText = docText.Replace("[$1-1-1$]", "TEST1");
docText = docText.Replace("[$1-1-2$]", "TEST2");
}
byte[] docTextByteArray = Encoding.UTF8.GetBytes(docText);
MemoryStream memDocText = new MemoryStream(docTextByteArray);
doc.MainDocumentPart.FeedData(memDocText);
}
Page.Response.Clear();
Page.Response.AppendHeader("Content-Disposition", "attachment; filename=DocxDllTest.docx");
Page.Response.ContentType = "application/octet-stream";
Page.Response.BinaryWrite(ms.ToArray());
Page.Response.OutputStream.Flush();
Page.Response.OutputStream.Close();
Page.Response.Flush();
Page.Response.End();
OK!!It work!!
Thank stackOverflow's users!!

Related

ITextSharp v5.5.13.0 XMLWorker Turkish character problem

I used iTextSharp and all Turkish character disappeared.
Also html inline css attributes work on table element but not working on div element.
I tried lots of encoding convert sample code but not found any results.
My sample code:
public static byte[] HtmlToPdfItextSharp(string HTMLCONTENTSTRING, List<string> cssFiles = null)
{
using (var ms = new MemoryStream())
{
Document pdfDoc = new Document(PageSize.A4.Rotate(), 10, 10, 10, 10);
BaseFont STF_Helvetica_Turkish = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, "CP1254", BaseFont.NOT_EMBEDDED);
Font fontNormal = new Font(STF_Helvetica_Turkish, 12, Font.NORMAL, BaseColor.BLACK);
string fontPath = Path.Combine(Path.Combine(Server.MapPath("~/App_Data/Pdf/arial.ttf")));
XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
fontProvider.UseUnicode = true;
fontProvider.Register(fontPath);
CssAppliers ca = new CssAppliersImpl(fontProvider);
var pdfWriter = PdfWriter.GetInstance(pdfDoc, ms);
pdfDoc.Open();
pdfWriter.DirectContent.SetFontAndSize(STF_Helvetica_Turkish, 12);
pdfWriter.CloseStream = false;
var htmlContext = new HtmlPipelineContext(null);
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
cssFiles.ForEach(e => cssResolver.AddCssFile(e, true));
var pp = new PdfWriterPipeline(pdfDoc, pdfWriter);
IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, pp));
XMLWorker worker = new XMLWorker(pipeline, true);
XMLParser parser = new XMLParser(worker);
parser.Parse(new MemoryStream(Encoding.UTF8.GetBytes(HTMLCONTENTSTRING)));
pdfDoc.Close();
return ms.GetBuffer();
}
}
I updated my code and add stylesheet file (with font familiy:arial;) and i solved character
but it takes too long time
My new updated function like:
public static byte[] HtmlToPdfItextSharp(string HTMLCONTENTSTRING, List<string> cssFiles = null)
{
using (var ms= new MemoryStream())
{
Document pdfDoc = new Document(PageSize.A4.Rotate(), 10, 10, 7, 10);
var pdfWriter = PdfWriter.GetInstance(pdfDoc, ms);
pdfWriter.CloseStream = false;
pdfDoc.Open();
var htmlContext = new HtmlPipelineContext(null);
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
cssFiles.ForEach(e => cssResolver.AddCssFile(e, true));
var pp = new PdfWriterPipeline(pdfDoc, pdfWriter);
IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, pp));
XMLWorker worker = new XMLWorker(pipeline, true);
XMLParser parser = new XMLParser(worker);
parser.Parse(new MemoryStream(Encoding.UTF8.GetBytes(pHtmlIcerik)));
pdfDoc.Close();
return ms.ToArray();
}
}
Css code :
body {
font-family:Arial;
}
table{
font-family:Arial;
}
td{
font-family:Arial;
}

Hi All, I am struggling in rest API where i need to post an XML in body with header and get the response, can anyone post an example of how to do it?

String reqURL = baseUrl + data_oauth.get(PropLoad.getTestXmlData("URL"));
Template template = new Template();
String updatedUrl = template.getUpdatedURL(reqURL);
Map<String, String> headers = Template.getRequestData(data_oauth,PropLoad.getTestXmlData("HEADER"));
headers.entrySet().toString();
String updatedAuthor = template.getAuthorizationHeader(headers, methodDesc);
headers.put("Authorization", updatedAuthor);
String xmlRequest = Template.generateStringFromResource(data_oauth,"xmlbody");
Response response = webCredentials_rest.postCallWithHeaderAndBodyParamForXml(headers, xmlRequest, updatedUrl);
// am getting Unmarshalled as in response, can any help me on posting an POST request with XML body in it
You can send it like this:
URL url = new URL(urlString);
URLConnection connenction = url.openConnection();
OutputStream output = connenction.getOutputStream();
InputStream input = new FileInputStream(xmlFile);
byte[] buffer = new byte[4096];
int len;
while ((len = input .read(buffer)) >= 0) {
out.write(buffer, 0, len);
}
input .close();
output.close();
And read the response like this:
StringBuilder stringBuilder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(connenction.getInputStream()));
String readLine = reader.readLine();
while (readLine != null) {
stringBuilder.append(readLine);
readLine = br.readLine();
}

Issue in converting HTML to PDF containing <pre> tag with Flying Saucer and ITEXT

I am using Flying Saucer library to convert html to pdf. It is working fine with the all the HTML files.
But for some HTML files which include some tags in pre tag, generated PDF file has tags displayed.
If I remove pre tags then the formatting of data is lost.
My code is
org.w3c.dom.Document document = null;
try {
Document doc = Jsoup.parse(new File(htmlFile), "UTF-8", "");
Whitelist wl = new RelaxedPlusDataBase64Images();
Cleaner cleaner = new Cleaner(wl);
doc = cleaner.clean(doc);
Tidy tidy = new Tidy();
tidy.setShowWarnings(false);
tidy.setXmlTags(false);
tidy.setInputEncoding("UTF-8");
tidy.setOutputEncoding("UTF-8");
tidy.setPrintBodyOnly(true);
tidy.setXHTML(true);
tidy.setMakeClean(true);
tidy.setAsciiChars(true);
if (doc.select("pre").html().contains("</")) {
doc.select("pre").unwrap();
}
Reader reader = new StringReader(doc.html());
document = (tidy.parseDOM(reader, null));
Element element = (Element) document.getElementsByTagName("head").item(0);
element.getParentNode().removeChild(element);
NodeList elements = document.getElementsByTagName("img");
for (int i = 0; i < elements.getLength(); i++) {
String value = elements.item(i).getAttributes().getNamedItem("src").getNodeValue();
if (value != null && value.startsWith("cid:") && value.contains("#")) {
value = value.substring(value.indexOf("cid:") + 4, value.indexOf("#"));
elements.item(i).getAttributes().getNamedItem("src").setNodeValue(value);
System.out.println(value);
}
}
document.normalize();
System.out.println(getNiceLyFormattedXMLDocument(document));
} catch (Exception e) {
System.out.println(e);
}
Method to create PDF is :
try {
org.w3c.dom.Document doc = CleanHtml.cleanNTidyHTML("b.html");
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(doc, null);
renderer.setPDFVersion(new Character('7'));
String outputFile = "test.pdf";
OutputStream os = new FileOutputStream(outputFile);
renderer.layout();
renderer.createPDF(os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
By using itext XMLWorker :
try {
org.w3c.dom.Document doc = CleanHtml.cleanNTidyHTML("a.html");
String k = CleanHtml.getNiceLyFormattedXMLDocument(doc);
OutputStream file = new FileOutputStream(new File("test.pdf"));
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, file);
document.open();
ByteArrayInputStream is = new ByteArrayInputStream(k.getBytes());
XMLWorkerHelper.getInstance().parseXHtml(writer, document, is);
document.close();
file.close();
} catch (Exception e) {
e.printStackTrace();
}
public static String getNiceLyFormattedXMLDocument(org.w3c.dom.Document doc) throws IOException, TransformerException {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
// transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
Writer stringWriter = new StringWriter();
StreamResult streamResult = new StreamResult(stringWriter);
transformer.transform(new DOMSource(doc), streamResult);
String result = stringWriter.toString();
return result;
}

How to specify parameters in box-view API header?

I am using the following piece of C# code to upload, convert and download a .pptx file, via Box view API.
var boxViewID = "";
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
String url_ = #"https://upload.view-api.box.com/1/documents";
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url_);
wr.ContentType = "multipart/form-data; boundary=" + boundary;
wr.Headers.Add("Authorization:Token " + "MY_CODE"/*Configuration.BoxViewAPIKey*/);
wr.Method = "POST";
wr.KeepAlive = true;
wr.Credentials = System.Net.CredentialCache.DefaultCredentials;
wr.Timeout = 1000000;
wr.SendChunked = true;
DateTime start = DateTime.Now;
Exception exc = null;
Stream rs = wr.GetRequestStream();
try
{
rs.Write(boundarybytes, 0, boundarybytes.Length);
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; non_svg=\"true\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
string header = string.Format(headerTemplate,"file", file, contentType);
Console.WriteLine(header);
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
rs.Write(headerbytes, 0, headerbytes.Length);
FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[40960];
int bytesRead = 0;
int totalSent = 0;
int totalLength = (int)fileStream.Length;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
totalSent += bytesRead;
var percent = new decimal(100.0 * totalSent / totalLength);
if (progress != null)
{
progress("Box processing", percent);
}
rs.Write(buffer, 0, bytesRead);
}
fileStream.Close();
}
catch(Exception ex)
{
exc = ex;
}
DateTime end = DateTime.Now;
int seconds = (int)(end - start).TotalSeconds;
if(seconds>=0)
{
if(exc!=null)
{
throw exc;
}
}
byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
rs.Write(trailer, 0, trailer.Length);
rs.Close();
WebResponse wresp = null;
try
{
wresp = wr.GetResponse();
Stream stream2 = wresp.GetResponseStream();
StreamReader reader2 = new StreamReader(stream2);
var res = reader2.ReadToEnd();
var docRes = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(res);
if (docRes["id"] != null)
boxViewID = docRes["id"];
}
catch (Exception ex)
{
if (wresp != null)
{
wresp.Close();
wresp = null;
}
}
finally
{
wr = null;
}
return boxViewID;
Specifying "non_svg" parameter should create .png images for every slide in the presentation (instead of .svg + .html pairs). However, the API seems to ignore this part of the request and I am always getting svg files.
Any idea on what am I doing wrong? Thanks!
The non_svg option causes PNG representations to be generated for each page, but the SVG representation is still generated. The viewer will only load the PNG files if SVG is not supported in the browser (basically only IE 8). Try changing page-1.svg to page-1.png in the browser (e.g., https://view-api.box.com/1/sessions/465c5d45caf04752a6113b0e5df593a5/assets/page-1.png vs https://view-api.box.com/1/sessions/465c5d45caf04752a6113b0e5df593a5/assets/page-1.svg). All of the assets will exist in content.zip if you use the documents content endpoint.

saving email attachments as binary to the database using c#

I can get email attachments from exchange server 2003 (MAPI.Attachment). How can I save the pdf attachment file as binary into the database?
Here's some code to understand better. For testing purposes I am saving the attachment pdf file into a filesystem. How ca I go about saving that into a database? Or how can I convert that into a byte array? Also, how can I get the size of the file since I need that when I declare the byte array "fileData"...
byte[] fileData = new byte[10000];
string[] fileTokens = new string[2];
string[] result = new string[3];
message.Unread = false;
emailSubject = message.Subject.ToString();
emailBody = message.Text.ToString();
MAPI.Attachments test = null;
test = (MAPI.Attachments)message.Attachments;
int attachmentCount = (int)test.Count;
for (int loopCounter = 1; loopCounter <= attachmentCount; loopCounter++)
{
MAPI.Attachment test2 = (MAPI.Attachment)test.get_Item(loopCounter);
bool temp = (test2.Name.ToString().Contains(".pdf") && test2.Name.ToString().IndexOf('Q') == 0);
if (test2.Name.ToString().Contains(".pdf") && test2.Name.ToString().IndexOf('Q') == 0)
{
//test2.ReadFromFile(fileData);
test2.WriteToFile("d:\\data\\" + test2.Name);
PDFParser pdfParser = new PDFParser();
pdfParser.ReadPdfFile("d:\\data\\" + test2.Name, result);
sentTime = (DateTime)message.TimeSent;
string fileName = (string)test2.Name;
fileTokens = fileName.Split('.');
}
RequestHistorySet historySet = new RequestHistorySet(1, sentTime, fileData, fileTokens[1]);
bool res = historySet.Update();
message.Unread = false;
message.Update();
And here's the Update function from historySet Class
public bool Update()
{
using (SqlConnection mySqlConnection = ...))
{
// Set up the Command object
SqlCommand myCommand = new SqlCommand("CONNECTION STRING..", mySqlConnection);
// Set up the OriginalName parameter
SqlParameter prmId = new SqlParameter("#id", SqlDbType.Int);
prmId.Value = id;
myCommand.Parameters.Add(prmId);
SqlParameter prmRequsetDate = new SqlParameter("#requestDate", SqlDbType.DateTime);
prmRequsetDate.Value = requestDate;
myCommand.Parameters.Add(prmRequsetDate);
// Set up the FileData parameter
SqlParameter prmFileData = new SqlParameter("#uplodedQuote_File ", SqlDbType.VarBinary);
prmFileData.Value = fileData;
prmFileData.Size = fileData.Length;
myCommand.Parameters.Add(prmFileData);
SqlParameter prmFileExtension = new SqlParameter("#uplodedQuote_Ext", SqlDbType.NVarChar);
prmFileExtension.Value = fileExtension;
myCommand.Parameters.Add(prmFileExtension);
// Execute the command, and clean up.
mySqlConnection.Open();
bool result = myCommand.ExecuteNonQuery() > 0;
mySqlConnection.Close();
return result;
}
}
As of now this is what I have done. I save it to a filesystem and then read from there.
test2.WriteToFile("d:\\data\\" + test2.Name);
fileData = FileToByteArray("d:\\data\\" + test2.Name);
PDFParser pdfParser = new PDFParser();
pdfParser.ReadPdfFile("d:\\data\\" + test2.Name, result);
TryToDelete("d:\\data\\" + test2.Name);
sentTime = (DateTime)message.TimeSent;
string fileName = (string)test2.Name;
fileTokens = fileName.Split('.');
historySet = new RequestHistorySet(1, sentTime, fileData, fileTokens[1]);