0

I am using iTextSharp to write my data to the template file. The PDF document is then rendered using MemoryStream in C#.

Everything works as expected with Google Chrome and partially with firefox.

var document = new Document();
//document open and content settings are done here
document.AddTitle("My Document Title"); //Statement-1
document.AddHeader("content-disposition", "inline;filename=MyFileName.pdf");

Google chrome shows the title as "My Document Title" and when downloading, it sets the file name as "MyFileName". The same is not the case with IE and Firefox.

IE displays the site name as title and is assigning the query parameters as file name. Firefox also has a random behavior. Even javascript (window.open settings) does not work. I want the browsers to display the same title and file name as Google chrome.

Please guide if I have missed any important steps to normalize the title and file name across browsers.

Aditya Alle
  • 57
  • 1
  • 11
  • FYI: that's not really an iTextSharp problem that's a browser or browser plugin problem. I'm not sure what you expect with this question. The PDFs are OK, there's very little you can do to "normalize" accross browsers. – Bruno Lowagie Apr 05 '16 at 11:58
  • There is no specification how and what a browser shall display in the title bar during loading of the external plugin content, so anything can go there. (Displaying PDF title once it's loaded is different question) IHMO it's wrong to assume that it shall display content-displosition content there (since document is still loading). Bruno, blaming browser for something they shall not do or answering by promoting your iText products is not cool. – async5 Apr 05 '16 at 14:11

1 Answers1

0

This question consists of two parts:

Showing the title

Right now, you are adding metadata to a PDF document: you are adding a title for the document. When you open the PDF in a PDF viewer, you want to see this title in the title bar. To achieve this, you must set the DisplayDocTitle viewer preference:

writer.AddViewerPreference(PdfName.DISPLAYDOCTITLE, new PdfBoolean(true));

Now when you open the PDF in Adobe Reader, the title will be shown in the title bar. When you open the PDF in Chrome PDF viewer (which is not the same as Adobe Reader), you don't need to set this viewer preference. Chrome PDF viewer chooses to show the title anyway. When you open the PDF in Firefox, you're using pdf.js to view the PDF. I don't know if pdf.js respects the viewer preferences. I doubt if MSIE looks at the viewer preferences of a PDF opened in a PDF browser plugin.

Summarized: if you open the downloaded PDF using Adobe Reader as a standalone tool (outside the browser) and you see the title in the title bar, you're doing it right. If the title doesn't show in the title bar of a browser, there's nothing you can do about it, because the browser doesn't support what you need.

Defining the filename

To define the file name, you need to use this line:

document.AddHeader("Content-Disposition", "inline;filename=MyFileName.pdf");

If you have this line, you're doing it right. If a browser doesn't repect the Content-Disposition, then the browser is doing it wrong.

See the following posts on StackOverflow for inspiration for a workaround:

(Your question has been answered many times before.)

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165