How to print directly from URI

Hi,

I need to print documents directly from a network location (URI) for my customer. Documents can be either word of PDF format. I am using the following code:

Two questions:

1- How do I actually print the document without opening?

2- Is the code same for printing word as well PDF document from URI?

System.Net.WebRequest request = System.Net.WebRequest.Create(@"http://localhost:5555/AllDocuments/helloworldout.docx");

System.Net.WebResponse response = request.GetResponse();

byte[] responseData = new byte[response.ContentLength];
int bytesRead = 0;

Document doc = null;
while (bytesRead < response.ContentLength)
{

    int bytesToRead = (int)response.ContentLength - bytesRead;
    Console.WriteLine("BytesToRead:" + bytesToRead + " || bytesRead " + bytesRead);
    bytesRead += response.GetResponseStream().Read(responseData, bytesRead, bytesToRead);
}
response.Close();

Thanks

@UmerJaved

Thanks for your inquiry.

You can not print the document without loading the document into Aspose.Words DOM. Please read about Aspose.Words.Document’s Constructor from here:

Please note that Aspose.Words mimics the same behavior as MS Word do. You can not load PDF document into Aspose.Words DOM and MS Word. You can load file formats mentioned at following documentation link into Aspose.Words DOM.

Once you have loaded a file of LoadFormat into Aspose.Words DOM, you can print it by using Document.Print method.

Please use the following code example to print document from a web request. Hope this helps you. Please let us know if you have any more queries.

WebClient client = new WebClient();

try

{

client.UseDefaultCredentials = true;

Byte[] bytes = client.DownloadData("http://localhost:5555/AllDocuments/helloworldout.docx");

MemoryStream stream = new MemoryStream(bytes);

Document doc = new Document(stream);

//Print document

doc.Print();

}

finally

{

client.Dispose();

}

Dear Tahir,

Thanks for the reply. The loadformat link you send does not seem to work! Please can you copy the code example instead for downloading PDF and printing?

Another question:

Our customer is a reknowned educational institute and they have requirement where we need to provde a print button for each individal application which contains about 8 docments to print(6 word/pdf document +2 SSRS reports running and printing). And they can perform this print operation on single Application or batch of applications.

Now before we suggest them to finally buy Aspos, do we need to be aware of any technical bottleneck regarding above requirement in terms of Aspos code?

Thanks

Umer

@UmerJaved

Thanks for your interest in our products.

I would like to share the information from Aspose.Pdf perspective. Aspose.Pdf for .NET provides the capability to create as well as manipulate existing PDF files. It also offers the feature to print the PDF documents. From your above requirement, my understand is that without viewing the file, you need to print it programmatically. If so is the case, then I am pleased to share that you can use Aspose.Pdf for .NET to print the documents programmatically. For further details, please visit [Printing PDF to an XPS File and Hiding Print Dialog (Facades)

Aspose.Pdf for .NET only deals with PDF printing. However as shared by my fellow worker, you can use Aspose.Words to print the Word documents.

In case I can be of any further assistance, please feel free to contact.

Hi Nayyer,

Thanks i understand we will have to use Aspos.Pdf. But when i try to test print into XPS File, the table in my pdf is not full shown in printable area?

Please see page 3 and 4 in attached document.

I have also raised a question on Aspos. pdf reporting service extension forum, please see if you could help with that as well.

We are getting closer to finalised the Aspos for our customer!

Please help ASAP

Thanks

Umer

@UmerJaved

Regarding your above requirement, in order to convert SSRS (.RDL) files to PDF format, you may consider using Aspose.Pdf for Reporting Services which is a rendering extension of SQL Reporting Services. You can also use Aspose.Pdf for Reporting Services with Microsoft Report Viewer in local mode or you can call the conversion procedure in your .NET application. Once the PDF files are generated, you can use Aspose.Pdf for .NET to print the PDF files.

For further details, please visit

@UmerJaved

Can you please share the source PDF file so that we can test the scenario at our end. Furthermore, when printing the PDF file, you can specify the margin information resultant file. For more information, please visit

Printing PDF File to Default Printer using Printer and Page Settings
Printing PDF to an XPS Printer (Facades)

Thanks Nayyer, do you have an example code which demonstrates:

Running of an SSRS report by Aspos reporting service extension and then conversion for the sake of printing?

I need to try any sample code for our customer reports to see if we can achieve desired results!

Regards

Umer

Thanks Nayyer.

Attached are both original pdf and converted XPS files(since i did not have printer to test the print out), as you can see:

From first converted file, table and footer are extending out of printable area, also on some pages footer is missing.

From second converted file, i have used the Autoresize property set to true, which is better but it is leaving too much margin from all side specially bottom!

I am wondering how the conversion code will cope with variable types of Pdf files? as our customer will get some times transcript in pdf, some times CV in PDF etc from all over the world.

Code:

Byte[] bytes = client.DownloadData("http://vir-ukd-lbs-01:5555/AllDocuments/The Education Zone - Catalogue (full).pdf");

MemoryStream stream = new MemoryStream(bytes);
Document doc = new Document(stream);

Aspose.Pdf.Facades.PdfViewer pdfViewer = new Aspose.Pdf.Facades.PdfViewer(doc);


System.Drawing.Printing.PrinterSettings printerSetttings = new System.Drawing.Printing.PrinterSettings();
printerSetttings.Copies = 1;
PageSettings settings = new PageSettings(printerSetttings);


pdfViewer.AutoResize = true;

printerSetttings.PrinterName = "Microsoft XPS Document Writer";
pdfViewer.PrintDocumentWithSettings(settings,printerSetttings);

i have been running above from a console app for testing.

Can i run the above code from a class library project? by replacing following line:

printerSetttings.PrinterName = "Microsoft XPS Document Writer";

with for E.g.
printerSetttings.PrinterName = "HP Deskjet 2050 J510 series";

As my requirement is to run the code from a .net class library(called plugin for our Microsoft Dynamics Customer relationship management product) which resides on platform layer and executes when client performs some operation on Aspx pages of this application.

I tried running from plugin class library as a result of trigger on CRM page but nothing happened.

Upon debugging i can see file being downloaded from URI but nothing happens when actually code reaches on following line:

printerSetttings.PrinterName = "HP Deskjet 2050 J510 series";

Is it the limitation of .net class library project? of am i missing something.

Our class library can be registered in DB or disc or Gac!

Regards

Umer

Hi Umer,


Sorry for the delayed response.

I am working over this query and will get back to you soon.

@UmerJaved

Thanks for your inquiry.

Umer Javed:

The loadformat link you send does not seem to work!

I have checked the load format link and it is working. Please try again to check the load formats from here:

Umer Javed:

Please can you copy the code example instead for downloading PDF and printing?

Please note that Aspose.Words does not load Pdf file into Aspose.Words DOM. Please use the Aspose.Pdf component to work with Pdf files. My colleague has already explained you about Pdf printing.

Umer Javed:

Our customer is a reknowned educational institute and they have requirement where we need to provde a print button for each individal application which contains about 8 docments to print(6 word/pdf document +2 SSRS reports running and printing). And they can perform this print operation on single Application or batch of applications.

In your case, you need to use Aspose.Words and Aspose.Pdf components. Please use the Aspose.Words to convert your MS Word document into Pdf and use Aspose.Pdf to merge/join Pdf documents and printing. My colleagues from Aspose.Pdf team will reply you shortly about your query related to Aspose.Pdf.

Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.

Hi Tahir,

Attached is the error i get when trying to click the link.

Yes we will need to merge all these documents into 1 print job request for individual applicant.Also i guess i can find an example regarding this on your forum?

Looking forward to reply from your colleague regarding other outstanding question.

Regards

Umer

Hi Umer,

Thanks for your inquiry. Please accept my apologies for your inconvenience. The LoadFormat link works for me. Please try following documentation link. Hope this works at your end.

Following is the detail of LoadFormat Enumeration.

LoadFormat Enumeration

public enum LoadFormat

Indicates the format of the document that is to be loaded.

Members

Member Name Value Description
Auto 0 Indicates the format of the document that is to be loaded.
Doc 1 Indicates the format of the document that is to be loaded.
Dot 2 Indicates the format of the document that is to be loaded.
DocPreWord97 3 Indicates the format of the document that is to be loaded.
Docx 4 Indicates the format of the document that is to be loaded.
Docm 5 Indicates the format of the document that is to be loaded.
Dotx 6 Indicates the format of the document that is to be loaded.
Dotm 7 Indicates the format of the document that is to be loaded.
FlatOpc 8 Indicates the format of the document that is to be loaded.
FlatOpcMacroEnabled 9 Indicates the format of the document that is to be loaded.
FlatOpcTemplate 10 Indicates the format of the document that is to be loaded.
FlatOpcTemplateMacroEnabled 11 Indicates the format of the document that is to be loaded.
Rtf 12 Indicates the format of the document that is to be loaded.
WordML 13 Indicates the format of the document that is to be loaded.
Html 14 Indicates the format of the document that is to be loaded.
Mhtml 15 Indicates the format of the document that is to be loaded.
Odt 16 Indicates the format of the document that is to be loaded.
Ott 17 Indicates the format of the document that is to be loaded.
Text 18 Indicates the format of the document that is to be loaded.
TestDocxDml 19
Unknown 20 Indicates the format of the document that is to be loaded.
Umer Javed:

Thanks Nayyer, do you have an example code which demonstrates:

Running of an SSRS report by Aspos reporting service extension and then conversion for the sake of printing?

I need to try any sample code for our customer reports to see if we can achieve desired results!

Hi Umer,

Thanks for your patience. Regarding above stated requirement, please notice my comments in another forum thread which you have created here. I have also attached a sample solution.

Umer Javed:
Attached are both original pdf and converted XPS files(since i did not have printer to test the print out), as you can see:

From first converted file, table and footer are extending out of printable area, also on some pages footer is missing.

From second converted file, i have used the Autoresize property set to true, which is better but it is leaving too much margin from all side specially bottom!

I am wondering how the conversion code will cope with variable types of Pdf files? as our customer will get some times transcript in pdf, some times CV in PDF etc from all over the world.

Hi Umer,


Thanks for your patience.


In order to accurately render the contents in PDF file based upon the contents and file size, as you have already observed, please try using pdfViewer.AutoResize = true; code line. However in order to eliminate the extra margin appearing around the contents, please try using following code lines.


[C#]

//set PageSize (if required)<o:p></o:p>

pgs.PaperSize = new
System.Drawing.Printing.PaperSize(“A4”, 827, 1169);<o:p></o:p>

//set PageMargins (if required)<o:p></o:p>

pgs.Margins = new
System.Drawing.Printing.Margins(0, 0, 0, 0);

//pass printer settings object to the
method
<o:p></o:p>

pdfViewer.PrintDocumentWithSettings(pgs,printerSetttings);


In case the problem still persists or you have any further query, please feel free to contact. We apologize for your inconvenience.

Dear Nayyer,

I have been waiting for over a week and a half for answer from you but unfortunately no reply yet for the query!

Did you test the pdf files for printing from code,i sent you?

As i said before when i try to print it from the code(to XPS Writer), the document is getting shrinked whereas original pdf gets printed perfetly.

Please reply ASAP as we have already asked our customer to purchase Aspose.pdf and Aspose.word for documents printing.

Regards

Umer

@UmerJaved

Thanks for your patience and sorry for the delayed response.

As shared earlier over 497106, in order to generate correct output, please set the value of pdfViewer.AutoResize to true. I have used the following code snippet which retrieves PDF document from URL and prints it using PdfViewer. As per my observations, the margin is properly being set as specified against PageSettings object.

Furthermore, I am afraid I did not receive any PDF documents from your side. In case I am still unable to understand the issue, please do let me know. We are sorry for your inconvenience.

[C#]

Byte[] bytes = new
System.Net.WebClient().DownloadData(“http://www.standardchartered.com.pk/pk/thegoodlife/images/home-app.pdf”);

MemoryStream stream = new MemoryStream(bytes);
Document doc = new Document(stream);

Aspose.Pdf.Facades.PdfViewer pdfViewer = new Aspose.Pdf.Facades.PdfViewer(doc);

System.Drawing.Printing.PrinterSettings printerSetttings = new System.Drawing.Printing.PrinterSettings();

System.Drawing.Printing.PageSettings pgs = new System.Drawing.Printing.PageSettings();

printerSetttings.Copies = 1;

PageSettings settings = new PageSettings(printerSetttings);

pdfViewer.AutoResize = true;

printerSetttings.PrinterName = "Microsoft XPS Document Writer";

pdfViewer.PrintDocumentWithSettings(settings, printerSetttings);

//set PageSize (if required)
pgs.PaperSize = new System.Drawing.Printing.PaperSize("A4", 827, 1169);

//set PageMargins (if required)
pgs.Margins = new System.Drawing.Printing.Margins(0, 0, 0, 0);

//pass printer settings object to the method
pdfViewer.PrintDocumentWithSettings(pgs, printerSetttings);