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.

Why you should choose ComponentOne's PDFDocumentSource and FlexViewer

  1. Load and view PDFs easily. You can load the PDF into the viewer at design time without any code and view your PDF at run time. If you want to do this in the code-behind instead, you only need to write a few lines of code.
  2. There's no dependency on Adobe Reader on your system.
  3. These are supported across platforms: the same lines of code will work on WinForms, WPF, and UWP with minimal differences — which can be specific to each platform.
  4. Read, view, export, and print PDFs in .NET applications without any external dependencies.
  5. They supports many PDF and Viewer features:
    1. Support for embedded fonts (with minor limitations for CFF fonts).
    2. Text search and selection in FlexViewer, or text search from code.
    3. Load PDFs from files or streams.
    4. Support for PDF/A compliant documents.
    5. Landscape and portrait printing orientations.
    6. Print PDFs, with print options, from FlexViewer or from code.
    7. Export PDFs to HTML and image formats (TIFF, BMP, PNG, JPEG, GIF) from FlexViewer or from code.
    8. Navigate using outlines and hyperlinks in FlexViewer.

Different Use Cases Where These Components Help

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:

  • C1.Win.4
  • C1.Win.C1Document
  • C1.Win.Barcode.4
  • C1.Win.Chart.FlexChart.4
  • C1.Win.FlexViewer.4
  • C1.Win.ImportServices.4
  • C1.Win.Bitmap.4
  • C1.Win.C1DX.4
  • C1.C1Excel.4
  • C1.C1Pdf.4
  • C1.C1Word.4
  • C1.C1Zip.4

WPF:

  • C1.WPF.4
  • C1.WPF.Document.4
  • C1.WPF.FlexViewer.4
  • C1.WPF.BarCode.4
  • C1.WPF.Bitmap.4
  • C1.WPF.DX.4
  • C1.WPF.Excel.4
  • C1.WPF.FlexChart.4
  • C1.WPF.ImportServices.4
  • C1.WPF.Pdf.4
  • C1.WPF.Word.4
  • C1.WPF.Zip.4
  • C1.WPF.FlexViewer.4

UWP:

  • C1.UWP
  • C1.UWP.Document
  • C1.UWP.BarCode
  • C1.UWP.Bitmap
  • C1.UWP.DX
  • C1.UWP.Excel
  • C1.UWP.FlexChart
  • C1.UWP.Imaging
  • C1.UWP.Pdf
  • C1.UWP.Word
  • C1.UWP.Zip
  • C1.UWP.FlexViewer

Load and view PDF files from a stream

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:

  • C1PdfDocumentSource.LoadFromFile(string fileName)
  • C1PdfDocumentSource.LoadFromStream(System.IO.Stream stream)

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:

    • Step 1: Drop C1FlexViewer on the form.
    • Step 2: Drop C1PdfDocumentSource on the form.
    • Step 3: Add Invoice.pdf to the project.
    • Step 4: Go to Form_Load method in the code-behind.
    • Step 5: Paste the following code in Form_Load.

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;
}
  • Step 6: Run the application.

The PDF File gets loaded in C1FlexViewer. PDFInViewer

View PDF files with multi-language text in a PDF Viewer

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):

  • Step 1: Drop C1FlexViewer on the form.
  • Step 2: Drop C1PdfDocumentSource on the form.
  • Step 3: Add ArabicText.pdf to the project.
  • Step 4: Set C1PdfDocumentSource1.DocumentLocation="C:\ArabicText.pdf"
  • Step 5: Select the DocumentSource dropdown for C1FlexViewer and set it to c1PdfDocumentSource1.
  • Step 6: Run the application.

The PDF file loads in C1FlexViewer. ArabicText

Read and view PDF files in UWP applications

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:

    • Step 1: Create a UWP -> Blank App.
    • Step 2: Add C1FlexViewer to MainPage.xaml.
    • Step 3: Add a reference to C1.UWP.Document in the project.
    • Step 4: Create a Resources folder in the project and add a PDF file. For example, add Invoice.PDF to the folder.
    • Step 5: Go to the code-behind in Page_Loaded method.
    • Step 6: Include the following namespace:

using C1.UWP.Document
    • Step 7: Load the PDF file through a stream:

Stream stream = this.GetType().GetTypeInfo().Assembly.GetManifestResourceStream("PdfDocumentSourceSamples.Resources.DefaultDocument.pdf");
    • Step 8: Create a C1PdfDocumentSource object and load the file from the stream into it:

C1PdfDocumentSource _pdfDocSource=new C1PdfDocumentSource();
await _pdfDocSource.LoadFromStreamAsync(stream.AsRandomAccessStream());
  • Step 9: Assign FlexViewer’s DocumentSource object to the C1PdfDocumentSource object:

C1FlexViewer1.DocumentSource=_pdfDocSource;

Here's what your PDF file should look like in the UWP C1FlexViewer: yet to add

Convert PDF pages to JPEG in a specific resolution

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:

    • Step 1: Add Shipping Labels.pdf to the project.
    • Step 2: Go to the code-behind and add the following namespace:

using C1.Win.C1Document;
    • Step 3: Create a C1PDFDocumentSource object and load a PDF into it.

C1PdfDocumentSource pdf = new C1PdfDocumentSource();
pdf.LoadFromFile(@"..\..\ShippingLabels.pdf");
    • Step 4: Create a JPEG exporter object and specify the resulting FileName:

var exporter = ExportProvider.JpegExportProvider.NewExporter();
exporter.FileName = "TestPDF.jpg";
    • Step 5: Call ShowOptionsDialog of the exporter. This dialog is used to set the resolution for the JPEG image.

<ul>
<ul>
	<li><strong>Step 6:</strong> Use the PDF object to export to JPEG.</li>
</ul>
</ul>
[code]
pdf.Export(exporter);
  • Step 7: Run the application.
  • Step 8: Set the desired resolution and click OK.

Here's what the final JPEG file should look like:

JPEGResolution

Convert a batch of PDF files to JPEG

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);
}

View a PDF in Viewer with rotated pages

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:

    • Step 1: Add CustomerSuppliers.pdf to the project.
    • Step 2: Go to the code-behind and add the following namespace:

using C1.Win.C1Document;
    • Step 3: Create the C1PdfDocumentSource object and load a PDF file.

C1PdfDocumentSource pdf = new C1PdfDocumentSource();
pdf.LoadFromFile(@"..\..\CustomerSuppliers.pdf");
    • Step 4: Drop C1FlexViewer on the form in design mode.
    • Step 5: In the code-behind, set:

C1FlexViewer1.DocumentSource=pdf.DocumentSource;
    • Step 6: At design time, select C1FlexViewer on the form.
    • Step 7: In Properties, set the following property:

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: RotatedView

Navigate through a PDF Document in a WPF viewer

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:

      • Step 1: Drop C1FlexViewer on the form.
      • Step 2: Set the following property for C1FlexViewer in the XAML tag:

<c1:C1FlexViewer HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Name="flv" />
      • Step 3: Add a reference to C1.WPF.Document DLL in the project.
      • Step 4: Add Invoice.pdf to the project.
      • Step 5: Go to the code-behind in the Window_Loaded event.
      • Step 6: Include the following namespace for the C1.WPF.Document:

using C1.WPF.Document
      • Step 7: Create a C1PdfDocumentSource object and load a PDF file to it.

C1PdfDocumentSource pdf=new C1PdfDocumentSource();
Pdf.LoadFromFile(@”..\..\Invoice.pdf”);
      • Step 8: Set FlexViewer’s DocumentSource to the C1PdfDocumentSource object.

Flv.DocumentSource=pdf;
      • Step 9: Run the application.
      • Step 10: Click on the ‘Document Outlines’ button on the FlexViewer toolbar.
      • Step 11: Click on an outline in the Document Outlines panel that opens on the left to navigate through the document.

PDFInWPFFlexViewer

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.

Sample

You can find samples on PDFDocumentSource and SSRSDocumentSource at the following locations after installing C1Studio installer for WinForms, WPF, and UWP:

WinForms: C:\Users\<User>\Documents\ComponentOneSamples\Winforms\C1Document\CS<(orVB)>\

WPF:  C:\Users\<User>\Documents\ComponentOneSamples\WPF\C1.WPF.Document\CS<(orVB)>\

      • PDFDocumentSourceSamples
      • PrintAndExport

UWP: C:\Users\<User>\Documents\ComponentOneSamples\WPF\C1.WPF.Document\CS<(orVB)>\

      • PDFDocumentSourceSamples

Generate, View, and Print PDFs

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.

Limitations

C1PDFDocumentSource has certain limitations in every platform. Please refer to the respective documentation to view these limitations.

Read more about PDF DocumentSource.

Tags