I am trying to make a small program to read a URLs from a DB and attach them to SAP B1 Deliveries.
Example source URL :
http://xxxx/xx/xxx/xxx/xxx/x/xxxx.pdf
Code:
oAttDelv.Lines.SourcePath = Path.GetDirectoryName(row.AttachURL);
oAttDelv.Lines.FileName = Path.GetFileNameWithoutExtension(row.AttachURL);
oAttDelv.Lines.FileExtension = Path.GetExtension(row.AttachURL);
int iErr = oAttDelv.Add();
int AttEntry = 0;
int temp_int = lErrorCode;
string temp_string = sErrorMsg;
oCompany.GetLastError(out temp_int, out temp_string);
The error shown in temp_string is as in the image. How do I resolve the two periods before pdf and get just one?
Errors:
temp_int = -5002
temp_string = "Source file does not exist , 'http:\\*****************\\jspui\\bitstream\\123456789\\2444\\1\\500393..pdf'"
Try this:
var temp_string = #"Source file does not exist , 'http:\*****************\jspui\bitstream\123456789\2444\1\500393..pdf'";
var file_name = temp_string.Substring(temp_string.LastIndexOf(#"\") + 1).Replace("..", ".");
Related
I am using Gembox.Documents to insert an HTML file into a Word or PDF document.
Unfortunately, in the resulting Word (or pdf), the height of the contents of the rows (cells) in the table is too high and does not correspond to the original one in the HTML file and I cannot change this with the help of CSS or HTML.
Can you, please, suggest solutions to the problem?
string fileName="zzzz";
var destinationDocument = new DocumentModel();
var section = new Section(destinationDocument);
destinationDocument.Sections.Add(section);
var srcDocument = DocumentModel.Load(TempPath + fileName + ".html");
var pageSetup = srcDocument.Sections[0].PageSetup;
var destpagesPageSetup = destinationDocument.Sections[0].PageSetup;
destpagesPageSetup.Orientation = Orientation.Landscape;
destpagesPageSetup.PageWidth = 1000;
destpagesPageSetup.PageHeight = 1000;
destpagesPageSetup.RightToLeft = true;
destpagesPageSetup.PageMargins.Left = 20;
destpagesPageSetup.PageMargins.Right = 0;
destpagesPageSetup.PageMargins.Bottom = 0;
destpagesPageSetup.PageMargins.Top = 0;
destpagesPageSetup.PageMargins.Gutter = 0;
destpagesPageSetup.PageMargins.Footer = 0;
var mapping = new ImportMapping(srcDocument, destinationDocument, false);
var blocks = srcDocument.Sections[0].Blocks;
foreach (Block b in blocks)
{
//b.ParentCollection.TableFormat.DefaultCellSpacing = 1;
Block b1 = destinationDocument.Import(b, true, mapping);
section.Blocks.Add(b1);
}
var pageSetup1 = section.PageSetup;
destinationDocument.Save(TempPath + fileName + ".pdf");
thanks
This issue occurred because of the cell margins appearing from the HTML content.
After investigating that HTML, the issue was resolved, the fix is available in the current latest bugfix version:
https://www.gemboxsoftware.com/document/downloads/bugfixes.html
Or in the current latest NuGet package:
https://www.nuget.org/packages/GemBox.Document/
I want to convert my resulting txt file into a UTF8 formatted file so I can load it into my Azure SQL DW via Polybase. It is required the source file be in UTF8.
MSDN has an "IO Streaming example" HERE works perfectly for a single job. I am trying to architect an SSIS solution for around 30 tables though. I believe using this method would cause a race condition where the PS script will be locked by 1 SSIS package when another SSIS package needs it.
I am a sql dev, not a .NET dev so please forgive me. How would one convert the above to an SSIS C# Script task assuming I know how to pass parameters into the Script task?
PowerShell Code from MSDN
#Static variables
$ascii = [System.Text.Encoding]::ASCII
$utf16le = [System.Text.Encoding]::Unicode
$utf8 = [System.Text.Encoding]::UTF8
$ansi = [System.Text.Encoding]::Default
$append = $False
#Set source file path and file name
$src = [System.IO.Path]::Combine("<MySrcFolder>","<MyUtf8stage>.txt")
#Set source file encoding (using list above)
$src_enc = $ascii
#Set target file path and file name
$tgt = [System.IO.Path]::Combine("<MyDestFolder>","<MyFinalstage>.txt")
#Set target file encoding (using list above)
$tgt_enc = $utf8
$read = New-Object System.IO.StreamReader($src,$src_enc)
$write = New-Object System.IO.StreamWriter($tgt,$append,$tgt_enc)
while ($read.Peek() -ne -1)
{
$line = $read.ReadLine();
$write.WriteLine($line);
}
$read.Close()
$read.Dispose()
$write.Close()
$write.Dispose()
Update
I found a similar post which I was able to tweak to my needs, I swear I searched high and low before posting. Anyway here is what IS working for me. If you see anyway to improve it please share:
public void Main()
{
//$Package::SourceSQLObject = tablename
//$Package::StageFile_DestinationFolderPath = rootpath eg "C:\temp\"
string path = (string)Dts.Variables["$Package::StageFile_DestinationFolderPath"].Value;
string name = (string)Dts.Variables["$Package::SourceSQLObject"].Value;
string from = Path.Combine(path, name) + ".csv";
string to = Path.ChangeExtension(from, "txt");
Dts.Log("Starting " + to.ToUpper(), 0, null);
using (StreamReader reader = new StreamReader(from, Encoding.ASCII, false, 10))
using (StreamWriter writer = new StreamWriter(to, false, Encoding.UTF8, 10))
{
while (reader.Peek() >= 0)
{
writer.WriteLine(reader.ReadLine());
}
}
Dts.TaskResult = (int)ScriptResults.Success;
Your code indicates that your are trying to convert an ASCII file to UTF-8 however that article also states the following:
As UTF-8 uses the same character encoding as ASCII PolyBase will also
support loading data that is ASCII encoded.
So my advice to you is to try the file first with Polybase, check for any conversion issues before you spend any time trying to convert the files.
var mySrcFolder = ""; // something from user variables?
var myUtf8stage = ""; // something from user variables?
var myFinalstage = ""; // something from user variables?
// Static variables
var ascii = System.Text.Encoding.ASCII;
var utf16le = System.Text.Encoding.Unicode;
var utf8 = System.Text.Encoding.UTF8;
var ansi = System.Text.Encoding.Default;
var append = false;
// Set source file path and file name
var src = System.IO.Path.Combine(
mySrcFolder,
String.Format("{0}.txt", myUtf8stage));
// Set source file encoding (using list above)
var src_enc = ascii;
// Set target file path and file name
var tgt = System.IO.Path.Combine(
mySrcFolder,
String.Format("{0}.txt", myFinalstage));
// Set target file encoding (using list above)
var tgt_enc = utf8;
using (var read = new System.IO.StreamReader(src, src_enc))
using (var write = new System.IO.StreamWriter(tgt, append, tgt_enc))
{
while (read.Peek() != -1)
{
var line = read.ReadLine();
write.WriteLine(line);
}
}
public void Main()
{
//$Package::SourceSQLObject = tablename
//$Package::StageFile_DestinationFolderPath = rootpath eg "C:\temp\"
string path = (string)Dts.Variables["$Package::StageFile_DestinationFolderPath"].Value;
string name = (string)Dts.Variables["$Package::SourceSQLObject"].Value;
string from = Path.Combine(path, name) + ".csv";
string to = Path.ChangeExtension(from, "txt");
Dts.Log("Starting " + to.ToUpper(), 0, null);
using (StreamReader reader = new StreamReader(from, Encoding.ASCII, false, 10))
using (StreamWriter writer = new StreamWriter(to, false, Encoding.UTF8, 10))
{
while (reader.Peek() >= 0)
{
writer.WriteLine(reader.ReadLine());
}
}
Dts.TaskResult = (int)ScriptResults.Success;
I'm trying to test this Program using a test.txt file in my Documents folder. I'm having trouble getting the paths right. can some one give me some ideas? its a homework assignment and I'm almost done with it!!
using System;
using System.IO;
class Program
{
// declare constants to use in wind chill factor equation - no magic numbers
const double EQUATION_NUMBER_ONE = 35.74;
const double EQUATION_NUMBER_TWO = 0.6215;
const double EQUATION_NUMBER_THREE = 35.75;
const double EQUATION_NUMBER_FOUR = 0.4275;
const double EQUATION_EXPONENT = 0.16;
const int DEGREE_SYMBOL = 176;
static void Main()
{
// declare some variables for the main method
string fileName = "";
string line = "";
double t = 0.0;
double v = 0.0;
double wchillf = 0.0;
char degreeSymbol = (char)DEGREE_SYMBOL;
string environment = System.Environment.GetFolderPath
(System.Environment.SpecialFolder.Personal) + "c:\\";
// Give student Info and ask user for a file name.
// we will read this file and use the info for our input.
Console.WriteLine("Wind Chill Calculator Braught to you by Teancum");
Console.WriteLine("for CS 1400 01X");
Console.WriteLine("11/11/2012");
Console.WriteLine("Project 8");
Console.Write("Please enter the file name in your My Documents folder: ");
fileName = Console.ReadLine();
string path = environment + fileName;
//we will create a new instance of the StreamReader class
//as well find the file in the documents folder
//this will read the Info and orginise it.
StreamReader windChillinfo = new StreamReader(path);
// start the do loop to keep readding the file untill there is no more information
do
{
// read in a line and save it as a string variable
line = windChillinfo.ReadLine();
//this if is to make sure there is no invalid info for example if the file is empty.
if (line != null)
{
string[] values = line.Split();
t = double.Parse(values[0]);
v = double.Parse(values[1]);
//here we call the windchillmagic Method
wchillf = WindChillMagic(t, v);
//here will be the Results of the windchillmagic method
Console.WriteLine("\nFor a temperature {0:f2} F{1}", t, degreeSymbol);
Console.WriteLine("\nand a wind speed of {0:f2}mph", v);
Console.WriteLine("\nThe wind chill factor would be = {0:f2}{1}\n", wchillf, degreeSymbol);
}
} while (line != null);
windChillinfo.Close();
Console.WriteLine("\nThank you for and keep Warm! Press enter to EXIT");
Console.ReadLine();
}//End Main()
static double WindChillMagic(double t, double v)
{
double wci = 0.0;
wci = EQUATION_NUMBER_ONE + (EQUATION_NUMBER_TWO * t) - (EQUATION_NUMBER_THREE * (Math.Pow(v, EQUATION_EXPONENT))) + (EQUATION_NUMBER_FOUR * t * (Math.Pow(v, EQUATION_EXPONENT)));
return wci;
}
}//End class Program
How about you do something along the lines of following:
String path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "filename.txt");
using (StreamWriter sw = new StreamWriter(path, false))
{
sw.WriteLine("Hello, file.");
}
This worked for me - now I have the file "filename.txt" in my Documents folder, with text "Hello, file." inside.
Your version doesn't work as you're doing this:
string environment = System.Environment.GetFolderPath
(System.Environment.SpecialFolder.Personal) + "c:\\";
This means, if your personal folder was "C:\Users\Username\Documents", the environment string will now contain the value `C:\Users\Username\Documentsc:\", and after you combine it into the path with
fileName = Console.ReadLine();
string path = environment + fileName;
if you entered "test.txt", the path will now contain C:\Users\Username\Documentsc:\test.txt. You should use a debugger to find these kinds of errors.
This looks backwards:
System.Environment.GetFolderPath
(System.Environment.SpecialFolder.Personal) + "c:\\";
If GetFolderPath returns "SomeFolder\SomeOtherFolder" then what you produced is "SomeFolder\SomeOtherFolderc:\"
If you put a breakpoint here and stepped pass the line and then mouse over the environment variable, you'd see this problem.
1) It is backwards.
2) You should probably use Path.Combine instead.
string path = environment + fileName;
Should probably use Path.Combine instead so a slash is added between. What if environment doesn't end in a slash? Then you'd get "C:\SomeFolder\SomeOtherFoldersomeUsersFilename"
Can anyone give me a solution for the below problem since am new to c sharp.
Am having a number of files in directory. Intially i want to load the first file without passing query string, with the first file there should have next file link. Once the next link button is clicked then only have to pass the query string.
how to pass the query string from second file to end of the file,
DirectoryInfo oImageFolderInfo = new DirectoryInfo(this.ImagePath);
FileInfo[] oFileList = oImageFolderInfo.GetFiles("*.*");
string fileName=string.Empty;
if (Request.QueryString["filename"] != null)
{
fileName=(Request.QueryString["filename"]);
}
else
{
fileName = oFileList[0].Name;
}
HtmlImage imag = new HtmlImage();
imag.Src = Url + fileName;
HtmlAnchor nextAnchor = new HtmlAnchor();
nextAnchor.Href=??
nextAnchor.InnerText = "Next>>";
HtmlAnchor prevAnchor = new HtmlAnchor();
prevAnchor.Href=??
How to proceed this same upto reaching the end of the file?
You could use the index of the file for the next and previous button instead of the filename.
DirectoryInfo oImageFolderInfo = new DirectoryInfo(this.ImagePath);
FileInfo[] oFileList = oImageFolderInfo.GetFiles("*.*");
string fileName=string.Empty;
int index = 0;
if (Request.QueryString("i") != null)
index = Request.QueryString("i");
fileName = oFileList[index].Name;
HtmlImage imag = new HtmlImage();
imag.Src = Url + fileName;
if (index > 0)
prevAnchor.Href = String.Format("{0}?i={1}", Url, Index - 1);
if (index < oFileList.Count(
nextAnchor.Href = String.Format("{0}?i={1}", Url, Index + 1);
I have the following code,
I am sending an array of data to a web service, the response from the web service gets displayed currently in a web form as shown below.
private void Form1_Load(object sender, EventArgs e)
{
int ASent;
int CSent;
webservice.Results returned = convert();
txtResult.Text = System.Convert.ToString(returned.status);
txtMoreRes1.Text = returned.errorDetails[0].errorDetails;
txtMoreRes2.Text = returned.errorDetails[1].errorDetails;
txtMoreRes3.Text = returned.errorDetails[2].errorDetails;
txtMoreRes4.Text = returned.errorDetails[3].errorDetails;
date.Text = System.Convert.ToString(DateTime.Today);
time.Text = System.Convert.ToString(DateTime.Now.TimeOfDay);
}
What I now need is for the results 'returned' to be converted from an xml response into a .csv file. Then have the code save that .csv to a location on the C drive. How can I change my code to do this?
Thanks for your time!
StringBuilder SbCSV = new StringBuilder();
int ActualSent;
int CommitmentSent;
webservice.summaryResults returned = convert(out ActualSent, out CommitmentSent);
//txtResult.Text = System.Convert.ToString(returned.status);
SbCSV.Append(System.Convert.ToString(returned.status));
//txtMoreRes1.Text = returned.errorDetails[0].errorDetails;
SbCSV.Append("," + returned.errorDetails[0].errorDetails);
//txtMoreRes2.Text = returned.errorDetails[1].errorDetails;
SbCSV.Append("," + returned.errorDetails[1].errorDetails);
// Similary ...
txtMoreRes3.Text = returned.errorDetails[2].errorDetails;
txtMoreRes4.Text = returned.errorDetails[3].errorDetails;
actualSum.Text = System.Convert.ToString(returned.actualSum);
commitmentSum.Text = System.Convert.ToString(returned.commitmentSum);
date.Text = System.Convert.ToString(DateTime.Today);
time.Text = System.Convert.ToString(DateTime.Now.TimeOfDay);
actualsumsent.Text = ActualSent.ToString();
commitmentsumsent.Text = CommitmentSent.ToString();
errorposition1.Text = System.Convert.ToString(returned.errorDetails[0].errorPosition);
errorposition2.Text = System.Convert.ToString(returned.errorDetails[1].errorPosition);
errorposition3.Text = System.Convert.ToString(returned.errorDetails[2].errorPosition);
errorposition4.Text = System.Convert.ToString(returned.errorDetails[3].errorPosition);
TextWriter Tw = new StreamWriter("Your_FilePath_Name.csv");
Tw.Write(SbCSV.ToString());
Tw.Close();
Tw.Dispose();
You could also use FileHelpers to do this stuff.
You must create a class named MyClass with attributes that match your .csv format (each column = one attribute)
you build a List of such class objects and convert to csv this way using FileHelper :
FileHelperEngine engine = new FileHelperEngine(typeof(MyClass));
MyClass[] res = null; //Fill your array here from your code.
// To Write Use:
engine.WriteFile("FileOut.txt", res);
You can also do this with an XML Transformation.