How to Print the generated PDF using aspose document class

Hi,

Am able to download the PDF into path using Doc.save(@“D:temp/”+ filename).
But the requirement is to print that document on a click. I am confused that whether we print the pdf using window.print() or not. Could you please suggest an approach to implement print or window.print() functionality.

Also can we implement window.print() instead downloading into the path?
Thanks in advance.

Regards,
Sai prasad

Hi Sai prasad,


Thanks for contacting support.

Aspose.Pdf for .NET also offers the feature of Printing PDF File to Default Printer using Printer and Page Settings. So once you have created the PDF file, pass Document instance to PdfViewer.Bind(…) object.

In the event of any further query, please feel free to contact.

Thanks for your reply.


I have tried with the Facade PDF viewer object. And created object for print document also. Got exception “no printers are installed” when used below lines of code.
Viewer.Printdocument();
Viewer.PrintdocumentwithSettings();
Viewer.PrintdocumentwithSetup();

I have done coding on Clients Bank Virtual machines. So my Ultimate aim is just to show a print dialog box of generated PDF.
Could you please suggest an approach to show print dailog box of generated PDF.

Thanks in advance.

Regards,
Sai Prasad




Hi Sai prasad,


Thanks for sharing the details.

It appears that no printer is installed/configured over machine where you are executing the printing code snippet. Furthermore, concerning to the code snippet for showing print dialog box during printing, we are working on creating the required code snippet and will get back to you soon.

Thanks for sharing the details.

Am trying to get option to Open/Save/Cancel generated pdf instead of printing it.

Could you please help me out for getting option to open/save/cancel it. Below is the code that am using.

String pdfHtmlTemplate =
Document doc = New Document(new MemoryStream(Encoding.UTF8.GetBytes(pdfHtmlTemplate)), objLoadoptions);
doc.save();
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.Charset = “UTF-8”;
Response.AddHeader(“content-disposition”, String.Format(“attachment;filename=FileName.pdf”, “FileName”));
Response.ContentType = “application/pdf”;
Response.Flush();
Response.End();

Hi Sai prasad,

Thanks for sharing the details.

From above statement, my understanding is that you need to render the output PDF in browser and want to show the dialog with option to Save/Open or Cancel the PDF display. Please note that in order to render the output in Web-browser, you need to save the output in Stream instance and then pass the stream to Response object. Or you can use a save overload of Document object which takes Response object as an argument.

Thanks for your response.

What you understand is correct. I want to get a dialog box with option to save/open or cancel. I have tried to implement the method that you recommended. Am getting exceptional saying that "cannot access a closed stream "

Could you please suggest the way to get download PDF.
Below is the code that am using.

String pdfHtmlTemplate =
Byte[] inputBytes = Encoding.UTF8.GetBytes(pdfHtmlTemplate);
MemoryStream stream = new MemoryStream (inputBytes );
Document doc = new Document(stream , new htmlloadoptions);
Response.Clear();

Response.ClearHeaders();

Response.ClearContent();

Response.Charset = “UTF-8”;

Response.AddHeader(“content-length”,stream.length.tostring());

Response.AddHeader(“content-disposition”, String.Format(“attachment;filename=FileName.pdf”, “FileName”));
Response.ContentType = “application/pdf”;
Response.BinaryWrite(stream.toarray());
Response.Flush();
Response.End()

Could you please have a look into it and help me out with working code ASAP.

Hi Sai prasad,

Thanks for sharing the details.

I think the reason you encountered above issue is because you tried to pass the same MemoryStream object to Response instance whereas in actual you have to save the Document output to a separate MemoryStream instance and then use that new object in Response instance. I have tested the scenario with a slight modified code snippet and did not notice any issue.

For your reference, I have attached the image file showing intended dialog box and I have also attached a sample project which I have used for testing purposes. Should you have any related query, please feel free to contact.

[C#]

String pdfHtmlTemplate = “<
table >< tr >< td >…</ td ></ tr ></
table >”;
Byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(pdfHtmlTemplate);

System.IO.MemoryStream stream = new MemoryStream(inputBytes);

Document doc = new Document(stream, new HtmlLoadOptions());

MemoryStream ms = new MemoryStream();

doc.Save(ms);

Response.Clear();

Response.ClearHeaders();

Response.ClearContent();

Response.Charset = "UTF-8";

Response.AddHeader("content-length", ms.Length.ToString());

Response.AddHeader("content-disposition", String.Format("attachment;filename=FileName.pdf", "FileName"));

Response.ContentType = "application/pdf";

Response.BinaryWrite(ms.ToArray());

Response.Flush();

Response.End();

Thanks for your fast response.

I have changed the code and tried. New code is executed without exception but Dialog box is not coming for me. I have tried in both IE and chrome

Actually on click of print I have written a javascript function and from that function am calling below method using ajax call .

Public string PDFprint(int noteId, Notemodel model)
{

    String pdfHtmlTemplate = “< table >< tr >< td >…</ td ></ tr ></ table >”;
    

    < o:p > </ o:p >

    Byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(pdfHtmlTemplate);
    

    [System.IO](http://system.io/).MemoryStream stream = new MemoryStream(inputBytes);<o:p></o:p>

    Document doc = new Document(stream, new HtmlLoadOptions());

    MemoryStream ms = new MemoryStream();

    doc.Save(ms);

    Response.Clear();

    Response.ClearHeaders();

    Response.ClearContent();

    Response.Charset = “UTF - 8”;

    Response.AddHeader(“content - length”, ms.Length.ToString());

    Response.AddHeader(“content - disposition”, String.Format(“attachment; filename = FileName.pdf”, “FileName”));

    Response.ContentType = “application / pdf”;

    Response.BinaryWrite(ms.ToArray());

    Response.Flush();

    Response.End();

}

Please you please help me out .

Thanks

Hi Sai prasad,

Thanks for sharing the details.

In my earlier attempt, I tested the scenario with FireFox and in recent attempt, I have used Internet Explorer 11.0.9600 I am still unable to notice any issue. For your reference, I have also attached the image showing desired behavior.

Please note that Internet Explorer is currently set as my default browser for default.aspx (which contains following code snippet).

[C#]

String pdfHtmlTemplate = “<
table >< tr >< td >…</ td ></ tr ></
table >”;


Byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(pdfHtmlTemplate);

System.IO.MemoryStream stream = new MemoryStream(inputBytes);

Document doc = new Document(stream, new HtmlLoadOptions());

MemoryStream ms = new MemoryStream();

doc.Save(ms);

Response.Clear();

Response.ClearHeaders();

Response.ClearContent();

Response.Buffer = true;

Response.Charset = "UTF-8";

Response.AddHeader("Accept-Header", ms.Length.ToString());

Response.AddHeader("Content-Length", ms.Length.ToString());

Response.AddHeader("Expires", "0″");

Response.AddHeader("Pragma", "cache");

Response.AddHeader("Cache-Control", "private");

Response.AddHeader("content-disposition", String.Format("attachment;filename={0}", "article" + "docId" + ".pdf"));

Response.ContentType = "application/pdf";

Response.AddHeader("Accept-Ranges", "bytes");

Response.BinaryWrite(ms.ToArray());

Response.Flush();

Response.End();

Hi Sai prasad,

Adding more to my previous comments, in case you still face any issue, please try using following code snippet.

[C#]

String pdfHtmlTemplate = “<
table >< tr >< td >…</ td ></ tr ></
table >”;
Byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(pdfHtmlTemplate);

System.IO.MemoryStream stream = new MemoryStream(inputBytes);

Document doc = new Document(stream, new HtmlLoadOptions());

MemoryStream ms = new MemoryStream();

PdfSaveOptions saveoption = new PdfSaveOptions();

doc.Save(Response,“sample.pdf”, ContentDisposition.Attachment,saveoption
);

Save Editcancel

Hi

Thanks for sharing the details.

I have tried both the codes but still dailog box is not coming. Did I need to return anything to js file in the method. Or Is there any browser settings for that.

Hi Sai prasad,


Thanks for contacting support.

Can you please share a sample application, so that we can test the scenario in our environment. We are sorry for your inconvenience.

Hi team,

Thanks​ for your help. I have tried with your code and now am.able to generate the pdf. It’shelped me a lot. Thanks a lot.
Now I want to display a text with a underline (irrespective of text size) at the header in every page. How can I implement that.
I have tried with the Text fragment class, with this only text with underline (based on text size) was printing​. Just I want a full line in header below the header text. How can I implement this.?

Thanks in advance.

Hi Sai prasad,


Thanks for contacting support.

As per my understanding, when using TextFragment, the underline appears under the text. However you need to have a line larger than TextFragment width. If so is the case, then you may try Adding line object to PDF.

In case I have not properly understood your requirement, please share some further details.

Hi,


In the downloaded PDF , I want to display a text (for example “Header text Details”) at the left corner of the header and below this text I want to display a line based on page with i.e., width = 100% at the header itself.

Thanks in advance.

Hi Sai prasad,


Thanks for sharing the details.

In order to add text to PDF file, please follow the instructions specified over Add Text to a PDF file where you can specify the location for text element and in order to add Line by following instructions specified earlier.

In case I am still unable to understand the requirement, please share some image file which can help us in further replying this query.