ComponentOne introduces C1PDFDocumentSource, a PDF Component that can read, print, and export your PDF documents without any third party PDF reader or software. It also acts as a key component to load and view your PDFs in C1FlexViewer — a cross-platform viewer that can load reports and document types. With C1PDFDocumentSource, you can view PDFs at run time, print them on hard paper, or convert them into other formats without having Acrobat Reader on your system.
You might need to work with PDFs in .NET applications in various situations. The following lists are some of the use cases where you might need a PDF component and a viewer. Please note that all these examples are supported across WinForms, WPF, and UWP platforms with minimal code differences. Please visit the respective documentation for more help. In the following examples, make sure you have the following DLLs in your respective platform applications: WinForms:
WPF:
UWP:
You might want to load a file from a memory stream instead of the disk to improve the application’s speed. C1PdfDocumentSource provides two overloads for loading PDF files:
While the first overload helps you directly load a PDF file from a disk, the second overload helps you easily load and view PDF files in a stream using the following steps in a WinForms application:
using (System.IO.FileStream fileStream = File.OpenRead(@"..\\..\\Invoice.pdf"))
{
//create new MemoryStream object
MemoryStream memStream = new MemoryStream();
memStream.SetLength(fileStream.Length);
//read file to MemoryStream
fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);
c1PdfDocumentSource1.LoadFromStream(memStream);
c1FlexViewer1.DocumentSource = c1PdfDocumentSource1;
}
The PDF File gets loaded in C1FlexViewer.
You can produce PDF files from documents written in any language. By supporting different PDF fonts, C1PDFDocumentSource also supports multi-language text. The component supports TrueType, Type0, Type1, OpenType, and Unicode fonts. Because of this support, C1PDFDocumentSource can read most PDF files with different fonts — with the exception of currently having only partial support for CFF fonts. Here's how you can load a PDF file with Arabic text in FlexViewer in a WinForms application (as well as how you can load a PDF in FlexViewer through C1PDFDocumentSource at design time without any lines of code):
The PDF file loads in C1FlexViewer.
Because it has cross-platform support, C1PDFDocumentSource can load a PDF file on any platform. In addition to WinForms and WPF, C1PDFDocumentSource and C1FlexViewer are also supported on UWP applications. Load a PDF through a stream in C1FlexViewer in a UWP application using the following steps:
using C1.UWP.Document
Stream stream = this.GetType().GetTypeInfo().Assembly.GetManifestResourceStream("PdfDocumentSourceSamples.Resources.DefaultDocument.pdf");
C1PdfDocumentSource _pdfDocSource=new C1PdfDocumentSource();
await _pdfDocSource.LoadFromStreamAsync(stream.AsRandomAccessStream());
C1FlexViewer1.DocumentSource=_pdfDocSource;
Here's what your PDF file should look like in the UWP C1FlexViewer: yet to add
The easiest way to view PDF files on your system without Acrobat Reader is to convert them to JPEG files. Even if you want to view PDFs in your browser, you might need an external plug-in to do so, while viewing JPEG files doesn't require any plug-ins. It's easy to convert PDF files to JPEG with C1PDFDocumentSource: you can load a PDF file and export the pages to JPEG files. Now it's time to set the resolution. You can specify more resolution if you want to see less loss in the clarity of the image,but these JPEGs would be larger in size. Simply write the following code in your WinForms application and run it:
using C1.Win.C1Document;
C1PdfDocumentSource pdf = new C1PdfDocumentSource();
pdf.LoadFromFile(@"..\\..\\ShippingLabels.pdf");
var exporter = ExportProvider.JpegExportProvider.NewExporter();
exporter.FileName = "TestPDF.jpg";
export.ShowOptionsDialog();
pdf.Export(exporter);
Here's what the final JPEG file should look like:
The above example showed you how you can convert a single PDF file to JPEG. However, you might want to convert to JPEG images all at once. With C1PDFDocumentSource, you can convert a batch of files easily using the following code:
String inputDirectory = @"C:\\PDF\\";
String[] files = Directory.GetFiles(inputDirectory, "*.pdf");
foreach (String filePath in files)
{
int startIdx = filePath.LastIndexOf("\\\");
int endIdx = filePath.LastIndexOf(".");
String docName = filePath.Substring(startIdx + 1, endIdx - startIdx - 1);
C1PdfDocumentSource pdf = new C1PdfDocumentSource();
var exporter = ExportProvider.JpegExportProvider.NewExporter();
exporter.FileName = "PDFtoJPEG”+”docName+”.jpg";
pdf.LoadFromFile(@"C:\\PDF\\"+docName+".pdf");
pdf.Export(exporter);
}
FlexViewer is a powerful tool when it comes to offering display preferences to users for the documents it supports. View a PDF with rotated pages at design time or at run time. To load a PDF rotated 90 degrees clockwise, follow these steps in a WinForms application:
using C1.Win.C1Document;
C1PdfDocumentSource pdf = new C1PdfDocumentSource();
pdf.LoadFromFile(@"..\\..\\CustomerSuppliers.pdf");
C1FlexViewer1.DocumentSource=pdf.DocumentSource;
C1FlexViewer.RotateView= C1.Win.FlexViewer.FlexViewerRotateView.Rotation90Clockwise;
This will rotate all of your PDF pages 90 degrees clockwise. You can also set the same property in the code-behind. Here's what your PDF should like:
One of the uses of C1PdfDocumentSource, as mentioned earlier, is that it can help you load a PDF in FlexViewer. With its 2017v2 release, this component also supports outlines and hyperlinks, making it possible to navigate through these documents in FlexViewer. You only need to load a PDF with outlines or hyperlinks, and FlexViewer can help you navigate through the documents. If you want to view a PDF with hyperlinks in a WPF application, use the following steps:
<c1:C1FlexViewer HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Name="flv" />
using C1.WPF.Document
C1PdfDocumentSource pdf=new C1PdfDocumentSource();
Pdf.LoadFromFile(@”..\\..\\Invoice.pdf”);
Flv.DocumentSource=pdf;
You might come across other instances where you would need to work with PDF files in a .NET application. For more information across platforms, please visit the following documentation: add links to documentation when it's available C1Document for Winforms, C1Document for WPF, C1Document for UWP.
You can find samples on PDFDocumentSource and SSRSDocumentSource at the following locations after installing C1Studio installer for WinForms, WPF, and UWP: WinForms: C:\Users\
WPF: C:\Users\
UWP: C:\Users\
Apart from loading, viewing, and printing, you can also generate a PDF in your application. Please visit this blog this links to another blog for more details.
C1PDFDocumentSource has certain limitations in every platform. Please refer to the respective documentation to view these limitations. Read more about PDF DocumentSource. <!-- p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #454545} --><!-- p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Calibri} -->