Enabling browser to begin rendering initial pages quickly on large PDF files - c#

My application currently allows direct links to PDF files on the server, but the files now need to be secured and direct links will have to be phased out because of security concerns.
I have tried the following lines to send PDF file to the client. It works fine.
Response.Clear();
Response.ContentType = "Application/pdf";
Response.WriteFile(filepath);
Response.End();
But there is a catch.
When a PDF file is linked, the browser renders the first few pages right away while downloading the rest. But with the approach above, the browser waits until the entire file has been downloaded and then proceeds to render the PDF.
So is there a way to trick the browser into mimicking opening a directly linked PDF?

After reading your post I realized my application had the same behavior (although given the typical sizes of files and the formats used it hasn't been an issue - at least not yet...) Upon some research, you'll have to choose from a couple of options depending upon how much complexity you want to introduce:
If you want a simpler implementation, you'll need to change your approach a bit to set up a loop that reads a certain amount of the file into a buffer in memory and then performs a Response.Write for that buffer. In the loop you'd then do a Response.Flush to push that data towards the client. Since this is synchronous, you'll have to wait until the flush is complete before you can push down the next block of the file. But, if the primary goal is getting the first couple of blocks out there so they client application can begin working with them, this might meet your needs.
If you want potentially better server-side scalability and are willing to accept a bit more complexity, you'll need to explore using Response.BeginFlush. I believe asynchronous writing was introduced in .Net 4.5. Here's a link that should get you started:
http://www.slideshare.net/pragyanshis/asynchronous-reading-and-writing-http-r-equest
I'm adding exploring both approaches to my "to do" list for my own application...

Related

Best way to generate PDF in c# using Word or InDesign?

I'm comfortable generating Word documents using Aspose.Word (which can also save as a PDF) but I've recently been asked to do the same thing using a PDF as the starter template. We recently bought Aspose.Total and whilst Aspose.Pdf looks like it can do some manipulations it doesn't look to be all that flexible/easy (like adding a big line of text and getting it to wrap, and shifting other content down the page if it takes up more space).
What would be the best way of using a PDF as a template for what is basically a bit like a mail merge from a database? Should I turn it into a PDF form and merge it from an XML data source? Is this even viable or would such a form still have a limitation on spacing (so that longer lines/paragraphs of text won't reflow the document where necessary)?
From what I can tell it doesn't look like InDesign can be manipulated in c# even via a COM object (which would be nasty on a web server anyway).
If I recreated the InDesign/PDF as a Word document I'm sure I could work wonders, but you know what these publishing types are like, who think Word documents are the tool of the devil. These PDFs are never going to a professional printer anyway; they're just brochures for a client to download from a web page (based on information in a database) for printing/use at home.
You have indeed many solutions for such a web to print project. Choosing one is a matter of budget, requirements and users count. Placing dynamic contents can be done at the simpliest with PDF forms fillable with xml data.
On the other hand you can work with InDesign Server and output PDF based on InDesign templates. That's generally a good choice when a large amount of users needs to get rich pdf files in parallel. But the costs are heavy.
You can also envision A pitstop server or Callas PDFToolBox Server to place dynamic texts based on variables as supplied by you. The good point here is that you don't need much coding here. Those apps are ready to use.
You can at last consider command line tools. A few of them may have some useful commands such as pdfTk or cPdf to merge texts.

csv file upload on asp.net

User need to upload 30,000 csv values, these values may contain 16 digit numbers or zipcodes.
Now our asp.net project doesn't use AJAX tool kit controls or jquery or any other 3rd party controls or open source code. This is restriction as per companies privacy policy.
So I have to come with a best way to accommodate file upload feature. This has to be versatile in regards to re-usability some thing like a custom control
This file upload feature should not be saved on the server, instead I need to read data into stream buffer and send them to UI then once user verify these values he/she'll have to hit submit thus values will be saved to DB.
what are the best ways to implement this,
Can I make use of System.Net.Webclient? or
Is there any other alternative ways by using Async HttpHandlers?
Can I show file upload progress to UI ?
Appreciate if you could help me with proper guidence on this.
Thanks in advance
krish
Well 30,000 values of zipcode or any other 16 digit code should be uploadable normally using file control(i mean via postback).
if you have to read and show 30,000 on the UI, i presume you are at risk of freezing your UI.
Moreover if you are not on HTML5, there is no way that you can read the content on the client side, except if you fiddle around with flash.
HTML 5 file api reference
http://www.html5rocks.com/en/tutorials/file/filesystem/
How to read and write files with Flash(Action script)
http://www.mikechambers.com/blog/2008/08/20/reading-and-writing-local-files-in-flash-player-10/

Word/PDF Generation in C# with hundreds of pages is too slow

I'm having speed issue generating documentation in C#.
I am basically trying to create documents with 600+ pages. But the tools I have used handle this very slowly.
I first tried using DocX by Novacode. Creation of this document with 600+ pages takes upwards to 3 minutes. I learned that there could be an issue with the function "InsertDocument" so I tried to find a different solution.
I started looking into opening a HTML document into word. While this is a fast solution, images are not embedded into the document. The HTML syntax (src="data:image/png;base64,xxxx") is not supported in MS Word.
I could use URLs to the images, but then if the internet connection is down, the images would not display.
I then started looking into a HTML->PDF solution. iTextSharp is a little faster than the DocX solution, but still takes 1-2 minutes to generate this document.
I am simply out of ideas. I'm not sure a commercial product would be better, and I don't want to shell out that kind of cash, to just have the same speed issue.
Has anyone had experience with creating Word/PDF documents with 600+ pages in C# that is fairly quick (1-5 seconds).
If you are trying to do this from a web server, you should be careful about resources consumption of this process, since you may run out of memory for example quite easily.
If at some point you decide to consider commercial libraries, maybe you could give Amyuni PDF Creator .Net a try. Amyuni PDF Creator .Net provides a "page by page" mode that saves resources when processing exceptionally long PDF documents. The idea is to save each page to the output file as soon as it is generated, maybe keeping a few pages in memory in case they need to be modified.
Take a look on these links for more details:
SartSave Method
EndSave Method
Processing large PDF files
usual disclaimer applies
You should be able to create a rich formatted DOCX file with 600+ pages in that time frame, but for PDF file I'm not sure... it will probably depend on your document content.
Anyway, I'm able to create a rather large DOCX file with GemBox.Document in just few seconds (0-4 sec), and PDF file as well, but it does take a bit more time then DOCX output.
You can also convert HTML to DOCX or HTML to PDF really fast, but that can depend on the HTML content itself.
If possible, you should prefer having well written HTML content that's "printer-friendly", doesn't have too much nesting levels, has optimized images, has single CSS file, etc. Also, if you're providing an URL as an input path then I think it's better to have embedded base64 images then links in order to avoid making additional web requests.
Last, I don't think there is much difference in Flat OPC XML vs DOCX. Basically they both generate the same content, it's just that DOCX file is additionally zipped which is a neglectable performance penalty.

Sending PDF with Fast Web View in ASP.NET

I am using ASP.NET 4.0 + IIS 7 to serve up a number of large PDFs via Response.TransmitFile.
The PDFs are all linearized, i.e. "Fast Web View". However the browser still requires the entire PDF to be downloaded before displaying any of it. All I want to do is show the first page (at least) without having to wait several minutes for the entire PDF to download.
From what I have read, the response header should include ["Accept-Ranges", "bytes"] but this does not seem to help.
Can anyone give me some pointers?
Thanks in advance!
I don't think you can do that easily. For byte range to work you would need many requests to serve the same file. How would you associate all these requests with a single file instance? You would probaly need to save the file to disk or somehonw maintain the file in memory... it could get tricky...
Much simpler would be to save PDFs in a shared folder (a first in first out cache) and let the HTTP 1.1 do the rest.

Resume in upload file control

I did read the following posts:
Pause/Resume Upload in C#
resume uploads using HTTP?
But didn't got a perfect solution to my problem.
In the above posts, one of the answers says "client and server needs to identify the file some how i suggest the use of a Guid so the server knows what file to append the extra data too." Request you to plz visit the first link of the aobve and find that answer. This answer is all about streaming. Can someone plz provide links using which I can build such kind of code?
In these posts one of the answer said "you can send several small file pieces and rebuild them server side"...HOW?
Can't I use something like checksum etc to detect how much part is uploaded and how much more needs to be and append it to that file? If yes, how?
Streams are a fairly fundemental concept in working with files on the .NET platform (as it is in Java, C and other languages). You should start by reading about them and how to use them. See the Stream class on MSDN.
HOW? By using streaming - you stream parts of the file, in small chunks (using an offset into the file and the size of chunk). Again, see Stream documentation.
You could, but checksums of different files may be the same - with a GUID the chance of a collision is pretty small compared to checksums.

Categories