i have the code for saving a gridview as an html file using a savefiledialog. i want to save it to a specific path (without using the savefiledialog)... how can i do that?
here's my code:
SaveFileDialog dialog = new SaveFileDialog();
dialog.DefaultExt = "*.html";
dialog.Filter = "WORD Document (*.html)|*.html";
if (dialog.ShowDialog() == true)
{
RadDocument document = CreateDocument(rgvReportData);
document.LayoutMode = DocumentLayoutMode.Paged;
document.Measure(RadDocument.MAX_DOCUMENT_SIZE);
document.Arrange(new RectangleF(PointF.Empty, document.DesiredSize));
document.SectionDefaultPageMargin = new Telerik.Windows.Documents.Layout.Padding(2, 2, 2, 2);
document.SectionDefaultPageOrientation = PageOrientation.Landscape;
HtmlFormatProvider provider = new HtmlFormatProvider();
using (Stream output = dialog.OpenFile())
{
provider.Export(document, output);
}
}
how can i sve it without using a savefiledialog?
using(StreamWriter output = new StreamWriter("path\to\your\file")) {
provider.Export(document, output);
}
will do the same thing, but to a specific path. You can read more on file access on MSDN.
String fileName = "youfilename.html"; // give the full path if required
RadDocument document = CreateDocument(rgvReportData);
document.LayoutMode = DocumentLayoutMode.Paged;
document.Measure(RadDocument.MAX_DOCUMENT_SIZE);
document.Arrange(new RectangleF(PointF.Empty, document.DesiredSize));
document.SectionDefaultPageMargin = new Telerik.Windows.Documents.Layout.Padding(2, 2, 2, 2);
document.SectionDefaultPageOrientation = PageOrientation.Landscape;
HtmlFormatProvider provider = new HtmlFormatProvider();
Stream output = File.Open(filename, FileMode.Open, FileAccess.ReadWrite);
provider.Export(document, output);
}
using (var output = new FileStream("path", FileMode.Create, FileAccess.Write))
{
provider.Export(document, output);
}
OpenFileDialog displays the specific path that you have choosen.
You could set the path of the SaveFileDialog, is not necessary to show the dialog like dialog.ShowDialog(), you have just to set the path like dialog.Filename = "file name".
And replace it like :
SaveFileDialog dialog = new SavefileDialog();
dialog.FileName = "path"; // like "C:\\someFolder\\someFile.html"
You could try it
Related
I tried to convert png to xps. I fallow this answer .
My code:
XpsDocument xpsDocument = new XpsDocument(#"C:\pathRoot\fileName.xps", FileAccess.ReadWrite);
XpsDocumentWriter xpsDocumentWriter = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
xpsDocumentWriter.Write(#"C:\pathRoot\fileName.png");
Here I got an exception
System.IO.FileFormatException: 'File contains corrupted data.'
I assumed that answer's author by saying "YourImageYouWishToWrite" means a path to png file like 'C:\pathRoot\fileName.png'. Or I am getting it completely wrong.
the easiest way to do this through printing all Images into Microsoft XPS Writer Printer , then you can merge the output individual XPS files , like :
public void MergeXpsDocument(string outputXPS, string[] ListOfXPS)
{
if (File.Exists(outputXPS))
{
File.Delete(outputXPS);
}
XpsDocument[] XpsFiles = new XpsDocument[ListOfXPS.Length];
for (int i = 0; i < ListOfXPS.Length; i++)
{
XpsDocument xpsFile = new XpsDocument(ListOfXPS[i], FileAccess.Read);
XpsFiles[i] = xpsFile;
}
XpsDocument tempPackage = new XpsDocument(outputXPS, FileAccess.ReadWrite);
XpsDocumentWriter xpsDocWriter = XpsDocument.CreateXpsDocumentWriter(tempPackage);
FixedDocumentSequence fixedDocSequence = new FixedDocumentSequence();
foreach (XpsDocument XPSdoc in XpsFiles)
{
FixedDocumentSequence fixedDocSeq = XPSdoc.GetFixedDocumentSequence();
foreach (DocumentReference sourceSequence in fixedDocSeq.References)
{
DocumentReference docRef = new DocumentReference();
docRef.Source = sourceSequence.Source;
(docRef as IUriContext).BaseUri = (sourceSequence as IUriContext).BaseUri;
FixedDocument fd = docRef.GetDocument(true);
docRef.SetDocument(fd);
fixedDocSequence.References.Add(docRef);
}
}
xpsDocWriter.Write(fixedDocSequence);
tempPackage.Close();
My printing code given below
void SaveReport(Telerik.Reporting.Report report, string fileName)
{
ReportProcessor reportProcessor = new ReportProcessor();
Telerik.Reporting.InstanceReportSource instanceReportSource = new Telerik.Reporting.InstanceReportSource();
instanceReportSource.ReportDocument = report;
RenderingResult result = reportProcessor.RenderReport("PDF", instanceReportSource, null);
using (FileStream fs = new FileStream(fileName, FileMode.Create))
{
fs.Write(result.DocumentBytes, 0, result.DocumentBytes.Length);
}
// initialize PrintDocument object
PrintDocument doc = new PrintDocument()
{
PrinterSettings = new PrinterSettings()
{
// set the printer to 'Microsoft Print to PDF'
PrinterName = "Canon LBP3000",
// tell the object this document will print to file
// PrintToFile = true,
// set the filename to whatever you like (full path)
PrintFileName = fileName,
}
};
doc.DocumentName = "My";
doc.Print();
}
Then I call the function like
SaveReport(new ThermalPrint(), Server.MapPath(#"~\Report\123.pdf"));
The function executes without error but printer not print the pdf file.
The printer printing dialog like
I can't understand the problem.
If you want to print the report, call reportProcessor.PrintReport(typeReportSource, printerSettings) but make sure you provide a valid PrinterSettings instance.
I have such code that saves bin file, but user has to choose file
Stream myStream;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Binary File (*.bin)|*.bin";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = saveFileDialog1.OpenFile()) != null)
{
Question.PersistObject(questions, myStream);
myStream.Close();
}
}
But I want to choose file in code, and if a file with such name doesn't exist, then create it. How to set in myStream that file?
Replace all your logic related to the OpenFileDialog with File.Open:
using (var myStream = File.Open(someFilePath, FileMode.OpenOrCreate))
{
Question.PersistObject(questions, myStream); // do something with the stream
}
The OpenOrCreate file mode will open the file if it exists, or create it if it does not.
The using statement will take care of closing the stream for you.
One option would be to have a base name and append a number:
string templateName = "myfile{0}.bin";
string finalName;
int count = 0;
do {
finalName = String.Format(templateName, count++);
} while (File.Exists(finalName);
Or, if you don't care about the name, use Path.GetTempFileName
Then pass that name to StreamWriter:
using (StreamWriter writer = new StreamWriter(finalName))
{
// Write stuff
}
So currently in my WPF project, I have the user browse for an XML file and then I want to deserialize that XML file and display the data in a DataGrid.
I'm sure my deserialize function works. However, I currently have it set to deserialize only one XML file as shown below:
public static void DeSerializationXML()
{
XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "lot_information";
xRoot.IsNullable = false;
// Create an instance of analytes class.
LotInformation[] lotinfo;
// Create an instance of stream writer.
TextReader txtReader = new StreamReader(#"C:\~\lot-123456.xml");
// Create and instance of XmlSerializer class.
XmlSerializer xmlSerializer = new XmlSerializer(typeof(LotInformation[]), xRoot);
// DeSerialize from the StreamReader
lotinfo = (LotInformation[])xmlSerializer.Deserialize(txtReader);
// Close the stream reader
txtReader.Close();
Console.ReadLine();
}
In another function, I have the following that allows the user to browse for a file and upload it:
private void ChangeLotFilePath()
{
OpenFileDialog Dialog = new OpenFileDialog();
Dialog.Filter = "XML files (*.xml)|*.xml";
Dialog.ShowDialog();
if (!String.IsNullOrEmpty(Dialog.FileName))
{
LotFileCreationDirectory = Dialog.FileName.ToString();
}
DeSerializationXML();
}
Now I am wondering, how do I pass Dialog.FileName.ToString() to the StreamReader so that it will recognize the file path selected by the user?
Why don't you take in the path as a parameter?
public static void DeSerializationXML(string path)
{
...
TextReader txtReader = new StreamReader(path);
}
private void ChangeLotFilePath()
{
using (var dialog = new OpenFileDialog()) {
dialog.Filter = "XML files (*.xml) | *.xml";
if (dialog.ShowDialog() == DialogResult.OK) {
DeserializationXML(dialog.FileName);
}
}
}
my code is-
Stream myStream;
saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Ticket files (*.tkt)|*.tkt";
saveFileDialog1.FilterIndex = 1;
saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.Title = "Save text Files";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
myStream = saveFileDialog1.OpenFile();
if (myStream != null)
{
StreamWriter wText = new StreamWriter(myStream);
string st = gettxt();
wText.Write(st);
//wText.WriteLine("sdfsderfsdsf");
myStream.Close();
}
}
whenever i uncomment Writeline and comment write(st) notthing is written..
and the string should be of specific length then only data is saved in the tkt file..
and last problem is that if text is too large then part of the text is only written..
please help me
try with
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
TextWriter tw = new StreamWriter(saveFileDialog1.FileName);
tw.WriteLine(gettxt());
tw.Close();
}
Hope this helps.
There are very few ways to explain the problem, other than there's a problem with the app that reads the file. Beware that you are writing a .txt file, it might not necessarily be compatible with a .tkt file format. Whatever that looks like. For one, the file will contain a BOM, 3 bytes at the start of the file that says it is a utf-8 formatted text file.
One thing you could try to diagnose the problem better:
using (var wText = new StreamWriter(myStream, Encoding.Default)) {
string st = gettxt();
wText.Write(st);
//wText.WriteLine("sdfsderfsdsf");
}
The Encoding argument specifies that the characters should be written in the default code page and without a BOM.
saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Ticket files (*.tkt)|*.tkt";
saveFileDialog1.FilterIndex = 1;
saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.Title = "Save text Files";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
using (Stream myStream = saveFileDialog1.OpenFile())
{
using (StreamWriter wText = new StreamWriter(myStream))
{
wText.WriteLine(gettext());
}
}
}