can i make html editor of asp.net ajax 2.0/3.5 to read a rtf file from a directory?
can i if yes then how please?
Yes, first; read file content.
// Specify file.
FileStream file = new FileStream(Server.MapPath("~\\files\\test.rtf"), FileMode.OpenOrCreate, FileAccess.Read);
StreamReader sr = new StreamReader(file);
// Read contents of file into a string
string cval = sr.ReadToEnd();
// Second; Set Content property of HTMLEditor.
htmlEditor.Content = cval;
// Close StreamReader
sr.Close();
// Close file
file.Close();
Related
I am trying to make a one page PDF from a 3 page PDF document using memory stream. But when the code below executes, all 3 pages get added and not just the first page.
What am I missing here? Please help.
Note: I am using iTextSharp v5.5.13
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
MemoryStream ms = new MemoryStream();
byte[] fileToBeEncrypted = null;
string sSourcePDF = "C:\\my3pageFile.pdf;
PdfReader pdfReader = new PdfReader(sSourcePDF);
Document document = new Document();
PdfCopy copy = new PdfCopy(document, ms) {CloseStream = false};
document.Open();
copy.AddPage(copy.GetImportedPage(pdfReader, iPage));
document.Close();
fileToBeEncrypted = ms.ToArray(); //returns the ENTIRE DOCUMENT AND NOT JUST PAGE 1
Can anyone help?
Thanks
Tom
The above code pdfCopy.GetImportedPages does works and I found an error further down in my own code.
How to write Text on different pages on exiting pdf which have more then 1 page.
For example 'Hitesh Second Page' I want to write this word on second page
'Hitesh Third Page' I want to write this word on Third page
Below code work only for pdf which have one page.
string fileName = "test.pdf";
string oldFile = System.Web.Hosting.HostingEnvironment.MapPath("~/AuthDoc/CoverPage.pdf");
string newFile = System.Web.Hosting.HostingEnvironment.MapPath(fileName);
// open the reader
PdfReader reader = new PdfReader(oldFile);
Rectangle size = reader.GetPageSizeWithRotation(1);
Document document = new Document(size);
// open the writer
FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
document.Open();
// the pdf content
PdfContentByte cb = writer.DirectContent;
// select the font properties
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.SetColorFill(BaseColor.DARK_GRAY);
cb.SetFontAndSize(bf, 8);
string text = "";
cb.BeginText();
text = "Hitesh Third Page";
cb.ShowTextAligned(3, text,500,500, 0);
cb.EndText();
cb.BeginText();
text = "Hitesh Second Page";
cb.ShowTextAligned(2, text,500,500, 0);
cb.EndText();
PdfImportedPage page = writer.GetImportedPage(reader, 1);
cb.AddTemplate( page, 0, 0);
document.Close();
fs.Close();
writer.Close();
reader.Close();
byte[] bytes = System.IO.File.ReadAllBytes(newFile);
return bytes;
Thanks,
Hitesh
When using a Document / PdfWriter pair to create a document, you create the pages in their final order, i.e. you first create the first page, then the second, then the third, ...
In your code you appear to try to start by creating the third page and then continue by creating the second one. You'll have to sort the code accordingly.
As soon as you have sorted your code, you can use the Document method NewPage to switch to the next page:
document.NewPage();
Beware, though, iText ignores the NewPage call if there is no content whatsoever on the page in question. To override this, you can make iText believe it is not empty using the PdfWriter property PageEmpty before calling NewPage:
writer.PageEmpty = false;
document.NewPage();
That been said, though...
How to write Text on different pages on exiting pdf
For such a task you should not use a Document / PdfWriter pair to start with, use a PdfReader / PdfStamper pair! In a PdfStamper you can quite freely jump between pages...
I have a C# program that is looping through all of the forms (browser enabled) in my forms library and injecting an XML node into each of them (for a newly promoted field). For some reason, when the XML is saved back to the form, the first few tags are being stripped off. Specifically, these tags are:
<?xml version="1.0"?>
<?mso-infoPathSolution name="urn:schemas-microsoft-com:office:infopath:Contractor-DB-Form:-myXSD-2009-09-10T18-19-55" solutionVersion="1.0.1.1100" productVersion="12.0.0.0" PIVersion="1.0.0.0" href="http://echouat.rbs.us/npe/FormServerTemplates/Contractor_DB_Form.xsn"?>
<?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.2"?>
<?mso-infoPath-file-attachment-present?>"
My code to update the XML is as follows:
private static SPListItem InsertXmlNode(SPListItem infoPathForm, string nodeToUpdateStr, string nodeToInsertStr,
string nodeInnerXmlStr, string firstNode)
{
//load form into xml document
byte[] fileBytes = infoPathForm.File.OpenBinary();
MemoryStream itemStream = new MemoryStream(fileBytes);
//Stream itemStream = infoPathForm.File.OpenBinary();
XmlDocument xmlDoc = new XmlDocument();
XmlNamespaceManager xmlNameSpaceMgr = new XmlNamespaceManager(xmlDoc.NameTable);
xmlNameSpaceMgr.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-09-10T18:19:55");
xmlDoc.Load(itemStream);
itemStream.Close();
//inject xml
XmlNode nodeToUpdate = xmlDoc.SelectSingleNode(firstNode + nodeToUpdateStr, xmlNameSpaceMgr);
//only insert if doesn't already exist
if (xmlDoc.SelectSingleNode(firstNode + nodeToUpdateStr + "/" + nodeToInsertStr, xmlNameSpaceMgr) == null)
{
updateCounter++;
XmlNode nodeToInsert = xmlDoc.CreateNode(XmlNodeType.Element, nodeToInsertStr, "http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-09-10T18:19:55");
nodeToInsert.InnerText = nodeInnerXmlStr;
nodeToUpdate.AppendChild(nodeToInsert);
//get binary data for updated xml
byte[] newXmlData = Encoding.UTF8.GetBytes(xmlDoc.DocumentElement.OuterXml);
MemoryStream newMemStream = new MemoryStream(newXmlData);
//write updated binary data to the form
infoPathForm.File.SaveBinary(newMemStream);
newMemStream.Close();
infoPathForm.File.Update();
}
return infoPathForm;
}
The addition of the new node is working properly; I can see that the new XML is properly formed. It's just that the tags get stripped off once the file is loaded from the MemoryStream into the XmlDocument object. And once these tags are missing, the forms won't open anymore in IP.
PLEASE HELP!
Thank you!
Change the line which reads:
byte[] newXmlData = Encoding.UTF8.GetBytes(xmlDoc.DocumentElement.OuterXml);
to read:
byte[] newXmlData = Encoding.UTF8.GetBytes(xmlDoc.OuterXml);
What's the easiest way to convert a BMP file to a single page PDF using ASP.net? I'm going to generate the 8.5" x 11" BMP in Flash (which means I can manipulate it to make it as easy as possible), then use a POST to upload it to an ASP page, which will convert it to a PDF and redirect the user to the PDF. I don't want to add any margins or anything else, it will be laid out properly in the BMP for full-bleed.
Would it be easier to convert it to PDF in Flash, then upload?
Thanks!
You can use iTextSharp to create a PDF and insert the image into the document. This can be done all in memory with a final PDF produced to client.
The following is an MVC method, stripped for display, but should see how to do this.
[HttpGet]
public FileStreamResult Export(int? ID)
{
MemoryStream stream = new MemoryStream();
Document pdf = new Document();
PdfWriter writer = PdfWriter.GetInstance(pdf, stream);
pdf.Open();
PdfPTable tblImage = new PdfPTable(1);
tblImage.AddCell(Image.GetInstance(LogChart())); //The LogChart method returns image
pdf.Add(Image);
pdf.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Log.pdf");
Response.Buffer = true;
Response.Clear();
Response.OutputStream.Write(stream.GetBuffer(), 0, stream.GetBuffer().Length);
Response.OutputStream.Flush();
Response.End();
return new FileStreamResult(Response.OutputStream, "application/pdf");
}
I'm currently trying to write data (client machine) into a xml file where the user can save. However, I want the users to be able to decide on where they want to save this written xml file. Is there any controls or codes that I can use to allow the users to save the file?.
Update:
is that right way to do?
**HttpContext.Current.Response.Write(xw.ToStroing()); <<< ??????**
HttpContext.Current.Response.End();
update:
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
MemoryStream ms = new MemoryStream();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "text/xml";
HttpContext.Current.Response.AddHeader("Content-Disposition:", "attachment;filename=" + HttpUtility.UrlEncode(fileName));
using (StringWriter sw = new StringWriter())
{
using (XmlWriter xw = XmlWriter.Create(ms, settings))
{
xw.WriteStartDocument();
xw.WriteStartElement("Name");
xw.WriteStartElement("Application");
................
......................
HttpContext.Current.Response.Write(xw.ToStroing());
HttpContext.Current.Response.End();
When you begin a file download, the client's browser will handle the file saving location.
If you want to force the file to download instead of open, add this HTTP header to your response:
Content-Disposition: attachment; filename="myFile.xml"
here is the link helps me :)
Where is the MemoryStream being used? You should pass the StringWriter to the XmlTextWriter constructor then Response.Write() the contents of the StringWriter
Add this after the Response.AddHeader line:
Response.ContentType = "application/octet-stream";
This will force download of files, regardless the file type.