Modify a printTicket - c#

I'm trying to modify a print ticket since many days. :(
Here is the code :
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Printing;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Drawing.Printing;
using System.Drawing;
using System.Xml;
// ....
// Get the ticket
var printQueue = new PrintServer("\\\\NetworkNameHere").GetPrintQueue("PrinterNameHere");
PrintTicket userPrintTicket = printQueue.UserPrintTicket;
// Modify the ticket to print in landscape (or any other option)
var xmlDoc = new XmlDocument();
xmlDoc.Load(userPrintTicket.GetXmlStream());
var manager = new XmlNamespaceManager(xmlDoc.NameTable);
manager.AddNamespace(xmlDoc.DocumentElement.Prefix, xmlDoc.DocumentElement.NamespaceURI);
string xpath = string.Format("//psf:Feature[#name='{0}']/psf:Option", "psk:PageOrientation");
XmlNode node = xmlDoc.SelectSingleNode(xpath, manager);
node.Attributes["name"].Value = "psk:Landscape";
PrintTicket modifiedPrintTicket = null;
using (var stream = new MemoryStream())
{
xmlDoc.Save(stream);
stream.Position = 0;
modifiedPrintTicket = new PrintTicket(stream);
}
System.Printing.ValidationResult result = printQueue.MergeAndValidatePrintTicket(printQueue.UserPrintTicket, modifiedPrintTicket);
printQueue.UserPrintTicket = result.ValidatedPrintTicket;
MessageBox.Show(result.ValidatedPrintTicket.PageOrientation.Value.ToString());
printQueue.Commit();
Byte[] myByteBuffer = UnicodeEncoding.Unicode.GetBytes("This is a test string for the print job stream.");
using (var job = printQueue.AddJob())
using (var stream = job.JobStream)
{
stream.Write(myByteBuffer, 0, myByteBuffer.Length);
stream.Close();
}
The problem :
Look like the ticket is correctly modified, but for some reasons it never use the information specified in the ticket for any option. HELP!!!!
Note that I'm using .Net 4.0 but I get the same result with the new PrintTicket parameter in addJob (4.5) : printQueue.AddJob("xx", result.ValidatedPrintTicket)
Thanks!

Related

Uploading large blobs to azure using azure-sdk-for-net is timing out

I am attempting to use c# and the Azure.Storage.Blobs.Specialized.BlockBlobClient to upload large files. I am encountering time-outs. I need a sample that shows how to upload large files (approx 300MB). I have not been able to find a good example.
using Azure.Storage.Blobs.Specialized;
...
using (var fs = fileInfo.Open(FileMode.Open,FileAccess.Read))
{
var blockBlobClient = new BlockBlobClient(
connectionString,
"ore",
fileInfo.Name);
await blockBlobClient.UploadAsync(fs);
}
I had to adjust the network timeout.
var uploadManager = new UploadManager();
await uploadManager.UploadBlob(connectionString, container, fileInfo);
This class handles large uploads, increases the network timeout and provides a Progress output.
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Azure.Storage.Blobs.Specialized;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Uploader
{
public class UploadManager
{
System.Collections.Concurrent.ConcurrentBag<long> progressBag = null;
Progress<long> progressHandler = null;
public UploadManager()
{
progressBag = new System.Collections.Concurrent.ConcurrentBag<long>();
progressHandler = new Progress<long>(progress => progressBag.Add(progress));
progressHandler.ProgressChanged += ProgressHandler_ProgressChanged;
}
public async Task UploadBlob(string connectionString, string container, FileInfo fileInfo)
{
using (var fs = fileInfo.Open(FileMode.Open, FileAccess.Read))
{
var clientOptions = new BlobClientOptions();
clientOptions.Retry.NetworkTimeout = new TimeSpan(0, 0, 600);
var blockBlobClient = new BlockBlobClient(connectionString, container, fileInfo.Name, clientOptions);
var uploadOptions = new BlobUploadOptions();
uploadOptions.ProgressHandler = progressHandler;
await blockBlobClient.UploadAsync(fs, uploadOptions);
}
}
private static void ProgressHandler_ProgressChanged(object sender, long e)
{
Debug.WriteLine($"Progress:{(e / 1000).ToString()} MB");
}
}
}

How to read HttpResponseContent as Stream using system.net.http

I have a piece of code to read HttpResponseContent using windows.web.http:
using System.IO;
using Windows.Web.Http;
using Newtonsoft.Json;
var responseContent = JsonConvert.DeserializeObject<HttpResponseContent>(response.Content.ToString());
if (response.StatusCode == HttpStatusCode.Ok)
{
//convert only from this line
using (var input =
await response.Content.ReadAsInputStreamAsync()
.AsTask()
.ConfigureAwait(false))
using (var stream = input.AsStreamForRead())
using (StreamReader sr = new StreamReader(stream))
using (JsonReader reader = new JsonTextReader(sr)) //to this line
{
return DeserializeData(reader);
}
Question 1: How can I achieve this using system.net.http namespace?
Note: I wrote those codes in uwp using windows.web.http, what I want is to convert that code using different package/namespace, which is system.net.http which I am using in Azure Functions.
Reference Link: https://learn.microsoft.com/en-us/dotnet/api/system.net.http?view=netframework-4.7.2 https://learn.microsoft.com/en-us/uwp/api/windows.web.http
Thanks
using (var input =
await response.Content.ReadAsStreamAsync()
.ConfigureAwait(false))
using (StreamReader sr = new StreamReader(input))
using (JsonReader reader = new JsonTextReader(sr))
{
return DeserializeData(reader);
}

The Namespace X already contains a definition for XX?

I don't understand this error I am getting. I have tried to clean and build my project several times. Can anyone help me?
Program.cs
using System;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.IO;
using System.Threading.Tasks;
namespace HTTPrequestApp
{
class Program
{
static void Main(string[] args)
{
var lstWebSites = new List<string>
{
"www.mearstransportation.com",
"www.amazon.com",
"www.ebay.com",
"www.att.com",
"www.verizon.com",
"www.sprint.com",
"www.centurylink.com",
"www.yahoo.com"
};
string filename = #"RequestLog.txt";
{
using (var writer = new StreamWriter(filename, true))
{
foreach (string website in lstWebSites)
{
for (var i = 0; i < 4; i++)
{
MyWebRequest request = new MyWebRequest();
request.Request(website);
}
}
}
}
}
}
}
MyWebRequest.cs - the problem is here: public class MyWebRequest
Error is: "The namespace HttpRequestApp already contains a definition for MyWebRequest"
using HTTPrequestApp.MyWebRequest;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace HTTPrequestApp
{
public class MyWebRequest : IWebRequest
{
public void Request(string strWebSite)
{
List<string> lstWebSites = Program.GetList(strWebSite);
using (var client2 = new TcpClient(strWebSite, 80))
{
using (NetworkStream stream = client2.GetStream())
using (StreamWriter writer = new StreamWriter(stream))
using (StreamReader reader2 = new StreamReader(stream))
{
//writer.AutoFlush = true;
writer.WriteLine("GET / HTTP/1.1");
writer.WriteLine("HOST: {0}:80", lstWebSites[1]);
writer.WriteLine("Connection: Close");
writer.WriteLine();
writer.WriteLine();
string theresponse = reader2.ReadToEnd();
Console.WriteLine(theresponse);
}
}
}
}
}
IWebRequest.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using System.IO;
using System.Threading.Tasks;
//using MyWebRequest.Lib;
namespace HTTPrequestApp.MyWebRequest
{
public interface IWebRequest
{
Task<List<strWebSite>> GetList();
void Request();
}
}
To give an over view of what I am trying to accomplish here is: Send HTTP request to get the initial page. Get back the HTTP response and check that it is a 200 response code. And time how long it took to retrieve the response.
This is a console app but I need to not have it depend on the console, it needs to be an independent application so I can use it somewhere else.
If anyone has any suggestions on how I can simplify my code please let me know.
thanks.
You have a HTTPrequestApp.MyWebRequest namespace and a HTTPrequestApp.MyWebRequest class name: c# compiler get confused (and human too...)
Consider renaming the namespace in something such HTTPrequestApp.MyWebRequestNameSpace if you really want a different namespace.

How do import Xml file into Word Content Controls

I was able to create a Word Document with content controls mapped to an Xml schema and using the code from this blog: http://seroter.wordpress.com/2009/12/23/populating-word-2007-templates-through-open-xml/ I am able to insert data into the word document.
The question I have is, is it possible to replace the the code below so that I can use an Xml file instead of having to write this for each finding:
//create XML string matching schema custom XML path
string newXml = "<root>" +
"<FINDING>Adobe Flash Player contains multiple...</FINDING>" +
"<STATUS>Open</STATUS>" +
"<THREATLEVEL>High</THREATLEVEL>" +
"<RECOMMENDATION>Update Flash Player to version...</RECOMMENDATION>" +
"<DEVICEAFFECTED>UserPC</DEVICEAFFECTED>" +
"<SCANNER>XXXXXX</SCANNER>" +
"</root>";
I have tried replacing this with:
string newXml = #"C:\Users\Christopher\Desktop\BookData\TestReport.xml";
and created a nested using statement with StreamReader and the existing StreamWriter but the word document would not populate and there would not be any errors.
--I just tried to replace that code with this:
//create XML string matching schema custom XML path
string newXml = #"C:\Users\Christopher\Desktop\BookData\TestReport.xml";
using (StreamReader sr = new StreamReader (newXml))
{
newXml = sr.ReadToEnd();
}
and I no longer get the error when I open the document, but the content controls are not populating?
Thank you.
Turns out the problem I was running into was that my code and examples were not actually deleting the previous CustomXMLParts located in the "CustomXML" folder. I the code working in two ways, the first is a Windows Form App with one button (btnGenerate) which allows you to select both the template.docx and customXML.xml files. The second is the a Console Program.
Cheers
Windows Form Application:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DocumentFormat.OpenXml.Packaging;
using System.IO;
using System.Threading;
namespace TestReportCreator_Beta
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[STAThread]
private void btnGenerate_Click(object sender, EventArgs e)
{
string outFile = #"C:\Users\Christopher\Desktop\BookData\TestReportBetaEND.docx";
OpenFileDialog OFD = new OpenFileDialog();
OFD.Multiselect = false;
OFD.Title = "Open Word Document";
OFD.Filter = "Word Document|*.docx;*.domx";
OFD.ShowDialog();
string docPath = OFD.FileName;
OFD.Title = "Opne Xml Document";
OFD.Filter = "Xml Document|*.xml";
OFD.ShowDialog();
string xmlPath = OFD.FileName;
// convert template to document
File.Copy(docPath, outFile);
using (WordprocessingDocument doc = WordprocessingDocument.Open(outFile, true))
{
MainDocumentPart mdp = doc.MainDocumentPart;
if (mdp.CustomXmlParts != null)
{
mdp.DeleteParts<CustomXmlPart>(mdp.CustomXmlParts);
}
CustomXmlPart cxp = mdp.AddCustomXmlPart(CustomXmlPartType.CustomXml);
FileStream fs = new FileStream(xmlPath, FileMode.Open);
cxp.FeedData(fs);
mdp.Document.Save();
}
}
}
}
Here is the Console Program Version
using System; using System.Collections.Generic;
using System.Linq; using System.Text;
using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing; using System.IO;
namespace BookData
{
class Program
{
static void Main(string[] args)
{
string template = #"C:\Users\Christopher\Desktop\BookData\TestReportBeta.docx";
string outFile = #"C:\Users\Christopher\Desktop\BookData\TestReportBetaEND.docx";
string xmlPath = #"C:\Users\Christopher\Desktop\BookData\TestReport.xml";
// convert template to document
File.Copy(template, outFile);
using (WordprocessingDocument doc = WordprocessingDocument.Open(outFile, true))
{
MainDocumentPart mdp = doc.MainDocumentPart;
if (mdp.CustomXmlParts != null)
{
mdp.DeleteParts<CustomXmlPart>(mdp.CustomXmlParts);
}
CustomXmlPart cxp = mdp.AddCustomXmlPart(CustomXmlPartType.CustomXml);
FileStream fs = new FileStream(xmlPath, FileMode.Open);
cxp.FeedData(fs);
mdp.Document.Save();
}
}
}
}
I hope you fellow developers and office automation folks out there are able put this to good use!

ASP.NET C# - get image from QueryString and save it to server as .ico

I'm newbie in asp.net so I need some help how to solve this.
Basically idea is:
get image from QueryString, for example: /Default.aspx?src=http://www.google.hr/images/logo.png
convert it and resize to 16x16 px ".ico" IE compilant
save it to server, and print/echo URL to ico
Using ASP.NET 3.5 C#
This is my try:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.IO;
using System.Net;
namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var source = Request.QueryString["src"];
if (source != null)
{
WebClient webclient = new WebClient();
using (Stream stream = webclient.OpenRead(source))
{
Bitmap iconbitmap = new Bitmap(System.Drawing.Image.FromFile(webclient));
var icon = Icon.FromHandle((iconbitmap).GetHicon());
FileStream fs = new FileStream("/test1.ico", FileMode.Create);
icon.Save(fs);
fs.Close();
}
}
}
}
}
EDIT:
Got some errors
(Error 1 The best overloaded method match for 'System.Drawing.Image.FromFile(string)' has some invalid arguments )
Thanks
Try this:
WebClient webclient = new WebClient();
using (Stream stream = webclient.OpenRead(source))
{
Bitmap iconbitmap = new Bitmap(System.Drawing.Image.FromStream(stream));
var icon = Icon.FromHandle((iconbitmap).GetHicon());
FileStream fs = new FileStream("/test1.ico", FileMode.Create);
icon.Save(fs);
fs.Close();
}
or if you don't need conversion:
WebClient webclient = new WebClient();
webclient.DownloadFile(source, "/test1.ico");
System.Drawing.Image.FromFile() is expecting a string, you are passing it a WebClient.

Categories

Resources