How to Generate Pdf with Atalasoft that opens in Print Mode in the Browser?
Atalasoft is a tool for providing Image and Pdf Management related operations for managing and modifying image and image files. We can modify the image and pdf files to apply different type of operations like viewing, creating thumbnails view, adding annotations to make it more secure by applying redactions, password, and customize pdf and image files in different ways to make office process usable.
One of the important operations is to print the pdf. For applying the print operations, a new pdf is generated with printing properties.
In general, if we open the pdf from physical location (or with window.open from javascript), it opens as below in the chrome browser:
But a physical pdf file generated via pdfencoder will open in browser as below:-
Sample Code below describes how one can generate a new pdf file using the Atalasoft classes:
PdfEncoder pdfEncoder = new PdfEncoder(); pdfEncoder .CreateSelfPrintingPdf = true; // sets the printing property of the pdf file pdfEncoder.SizeMode = PdfPageSizeMode.FitToPage; pdfEncoder.UseAdvancedImageCompression = false; FileInfo fiSource = new FileInfo(sourceFile); using (FileStream fs = fiSource.OpenRead()) { Atalasoft.Imaging.ImageSources.PdfImageSource pdfSource = new Atalasoft.Imaging.ImageSources.PdfImageSource(fs); using (FileStream _fs = new FileStream(<targetFilePath>, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { pdfEncoder.Save(_fs, pdfSource, null); } }
This approach uses the compression method for saving the files.
Another faster approach is to generate pdf files on the fly without using any kind of image encoding (pdfencoder) is:
PdfDocument pdfDoc = new PdfDocument( <sourceFilePath> ); pdfDoc .SelfPrintingPdf = true; pdfDoc .Save( <targetFilePath> );
Printing application by Atalasoft is not only limited to “All pages” printing for a document, but you can also print the “Current page” and print the pdf with “Page range”. For this case we can use the same pdfencoder object as mentioned in the above sample code, and instead of PdfSource, the ImageCollection object will be used.
Sample Code below shows the way to generate a new pdf file in print format with chosen page range like (page 3-6) :
ImageCollection images = new ImageCollection(); AtalaImage image = null; FileInfo fiSource = new FileInfo( <sourceFilePath> ); using (FileStream fs = fiSource.OpenRead()) { PdfDecoder pdfDecoder = new PdfDecoder(); // Ranges is a list of int type that contains starting page index value and page upto you want to print, like if you want to print from 3-6 then List will contains 3 and 6 number foreach (var index in ranges) { image = pdfDecoder.Read(fs, index, null); images.Add(image); } } using (FileStream _fs = new FileStream(<targetFilePath>, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { pdfEncoder.Save(_fs , images, null); }