I have a scenario in my application in which user can upload business case document which is word document, and other users(admin and super admin) can see that business case and approve the document.
What is happening is admins and super admins have to download word document , which i don't want , i want them to see that document in a pdf format.
I could do that by restricting the user to upload only pdf files but that is not i want , I want him to upload word document and at the time of saving that file in to server , i want to save as PDF formtat .
Current code is working fine but it is saving file as word file , i want to save the file as pdf format.
int count = 0;
foreach (HttpPostedFileBase file in emailasign.Files)
{
var filename = "";
//Checking file is available to save.
if (file != null)
{
var random = new Random();
filename = random.Next(111111, 999999).ToString() + Path.GetExtension(file.FileName);
var ServerSavePath = Path.Combine(Server.MapPath("~/UploadedFiles/") + filename);
//Save file to server folder
file.SaveAs(ServerSavePath);
count++;
}
if (count > 1)
{
TempData["OrignalFile"] += "," + file.FileName;
TempData["FileName"] += "," + filename;
}
else if (count == 1)
{
TempData["OrignalFile"] = file.FileName;
TempData["FileName"] = filename;
}
}
Note: Right now it is accepting all format but i will restrict it in to word document only , so at the the time of uploading the document , i want to convert it in to PDF format and than save it on server
So, I want to be able to get an XML file attachment from an email and store it in the database for later use.
I can retrieve the emails and store the SenderEmailAdress, sentOn and the body in the database. Now I want to add the XML file attachment to the database too, so I can use it later.
How can I get the attachment file (or the content) from the email and to a variable so I can add it to the DB?
Here's the code for the attachment I have now (so after I get the Mail Item):
//Check for attachments.
int AttachCnt = oMsg.Attachments.Count;
// Check if there are any attachments
if (AttachCnt > 0)
{
for (int i = 1; i <= AttachCnt; i++)
{
System.Diagnostics.Debug.WriteLine(i.ToString() + " - FileName: " + oMsg.Attachments[i].FileName);
String ext = Path.GetExtension(oMsg.Attachments[i].FileName);
// check if the file extention is .xml
if (ext == ".xml")
{
// Get the file ready to store in the DB. This is what I want to know!
return;
}
}
}
Save the attachment as a file (Attachment.SaveAsFile), then read the file data into a variable.
I have a form which uploads an image to the server, and I am abl to get the filename of the image and other data inside the form.
How would I convert the image to string to be included in a html email?
This is the code which gives me my filename.
string PostedFileName = "not_set";
foreach (string fileName in HttpContext.Current.Request.Files)
{
HttpPostedFile file = HttpContext.Current.Request.Files[fileName];
PostedFileName = fileName;
}
MessageSent("TRUE - " + PostedFileName);
**Important: question answered after code block**
Platform:
C#, OpenXML SDK (2.5), .NET 4.0
What I'm trying to achieve
I've been trying to generate a pptx presentation from some data and images coming from my database.
Any generated file gets corrupted, but it's really passing the OOXML validation. I really don't know what else I could do.
What I've already tried
I tried to remove the images, the text, then I've commented the code that deletes the first (template) slide but nothing changes my final result: a corrupted file.
The error
When I try to open the file: "PowerPoint was unable to display some of the text, images, or objects on slides in the file, "filename.pptx", because they have become corrupted.
Affected slides have been replaced by blank slides in the presentation and it is not possible to recover the lost information. To ensure that the file can be opened in previous versions of PowerPoint, use the Save As command (File menu) and save the file with either the same or a new name.
Code
Here's the code I'm using to generate the PPTX:
void GenerateSlides(string fullPath, string path, IEnumerable<Data> data)
{
var slidePath = fullPath;
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
// Copy the template file to generate new slides
File.Copy(string.Format("{0}{1}", path, "TemplateTF.pptx"), slidePath, true);
using (var presentationDocument = PresentationDocument.Open(slidePath, true))
{
var presentationPart = presentationDocument.PresentationPart;
var slideTemplate = (SlidePart)presentationPart.GetPartById("rId2");
// Recover the data to fullfill the slidepart
int i = 1;
foreach (var singleData in data)
{
(...)
// Creates the new image
var newSlide = CloneSlidePart(presentationPart, slideTemplate);
var imgId = "rIdImg" + i;
var imagePart = newSlide.AddImagePart(ImagePartType.Jpeg, imgId);
var stream = new MemoryStream();
using (var file = File.Open(string.Format("{0}{1}"
, WebConfigurationManager.AppSettings["pathImages"]
, singleData.ImageName), FileMode.Open))
{
var buffer = new byte[file.Length];
file.Read(buffer, 0, (int)file.Length);
stream.Write(buffer, 0, buffer.Length);
imagePart.FeedData(new MemoryStream(buffer));
}
// Important method to swap the original image
SwapPhoto(newSlide, imgId);
i++;
InsertContent(newSlide, (...));
SwapPhoto(newSlide, imgId);
newSlide.Slide.Save();
}
DeleteTemplateSlide(presentationPart, slideTemplate);
presentationPart.Presentation.Save();
}
}
void SwapPhoto(SlidePart slidePart, string imgId)
{
var blip = slidePart.Slide.Descendants<Drawing.Blip>().First();
blip.Embed = imgId;
slidePart.Slide.Save();
}
void DeleteTemplateSlide(PresentationPart presentationPart, SlidePart slideTemplate)
{
var slideIdList = presentationPart.Presentation.SlideIdList;
foreach (SlideId slideId in slideIdList.ChildElements)
{
if (slideId.RelationshipId.Value.Equals("rId2"))
{
slideIdList.RemoveChild(slideId);
}
}
presentationPart.DeletePart(slideTemplate);
}
SlidePart CloneSlidePart(PresentationPart presentationPart, SlidePart slideTemplate)
{
var newSlidePart = presentationPart.AddNewPart<SlidePart>("newSlide" + i);
i++;
newSlidePart.FeedData(slideTemplate.GetStream(FileMode.Open));
newSlidePart.AddPart(slideTemplate.SlideLayoutPart);
var slideIdList = presentationPart.Presentation.SlideIdList;
uint maxSlideId = 1;
SlideId prevSlideId = null;
foreach (SlideId slideId in slideIdList.ChildElements)
{
if (slideId.Id > maxSlideId)
{
maxSlideId = slideId.Id;
prevSlideId = slideId;
}
}
maxSlideId++;
var newSlideId = slideIdList.InsertAfter(new SlideId(), prevSlideId);
newSlideId.Id = maxSlideId;
newSlideId.RelationshipId = presentationPart.GetIdOfPart(newSlidePart);
return newSlidePart;
}
void InsertContent(SlidePart slidePart, (...))
{
SwapPlaceholderText(slidePart, "Title", "ReplacementString1");
SwapPlaceholderText(slidePart, "Text", "ReplacementString2");
}
void SwapPlaceholderText(SlidePart slidePart, string placeholder, string value)
{
var textList = slidePart.Slide.Descendants<Drawing.Text>().Where(
t => t.Text.Equals(placeholder)).ToList();
foreach (Drawing.Text text in textList)
{
text.Text = value;
}
}
Answer
Ok, I realized how different MS Office versions can be.
a) If I try to open the .pptx file with Office 2013: error message + opens perfectly, no logo image nor slidepart showing any aditional information
b) If I try to open the .pptx file with Office 2007: error message + empty slides, no information at all
c) If I try to open the .pptx file with Office 2010: error message + empty slides and the most important information I could ever have: corrupted icon in logo's place!!!
I removed the logo image from my template and voilĂ , the file is is perfectly generated. Now, if I really NEED to add the logo image, I can do it programatically.
Thanks! After one week trying to realize what the hell was happening, a great friend of mine opened the file using Office 2010, then I DID realize the logo image was corrupted in my original template file.
Thanks :)
Ok, I realized how different MS Office versions can be.
a) If I try to open the .pptx file with Office 2013: error message + opens perfectly, no logo image nor slidepart showing any aditional information
b) If I try to open the .pptx file with Office 2007: error message + empty slides, no information at all
c) If I try to open the .pptx file with Office 2010: error message + empty slides and the most important information I could ever have: corrupted icon in logo's place!!!
I removed the logo image from my template and voilĂ , the file is is perfectly generated. Now, if I really NEED to add the logo image, I can do it programatically.
Thanks! After one week trying to realize what the hell was happening, a friend of mine opened the file using Office 2010, then I DID realize the logo image was corrupted in my original template file.
I need to get and save the attachments(s) from a mail item, but using the code below returns all attachments - meaning it also returns the embedded images like the sender's signature with logo which is an image. How can I differentiate a true attachment vs. embedded images? I have seen a lot from forums but it is still unclear to me.
public static void SaveData(MailItem currentMailItem)
{
if (currentMailItem != null)
{
if (currentMailItem.Attachments.Count > 0)
{
for (int i = 1; i <= currentMailItem.Attachments.Count; i++)
{
currentMailItem.Attachments[i].SaveAsFile(#"C:\TestFileSave\" + currentMailItem.Attachments[i].FileName);
}
}
}
}
You can check whether an attachment is inline or not by using the following pseudo-code from MS Technet Forums.
if body format is plain text then
no attachment is inline
else if body format is RTF then
if PR_ATTACH_METHOD value is 6 (ATTACH_OLE) then
attachment is inline
else
attachment is normal
else if body format is HTML then
if PR_ATTACH_FLAGS value has the 4 bit set (ATT_MHTML_REF) then
attachment is inline
else
attachment is normal
You can access the message body format using MailItem.BodyFormat and the MIME attachment properties using Attachment.PropertyAccessor.
string PR_ATTACH_METHOD = 'http://schemas.microsoft.com/mapi/proptag/0x37050003';
var attachMethod = attachment.PropertyAccessor.Get(PR_ATTACH_METHOD);
string PR_ATTACH_FLAGS = 'http://schemas.microsoft.com/mapi/proptag/0x37140003';
var attachFlags = attachment.PropertyAccessor.Get(PR_ATTACH_FLAGS);