Generate, View, and Print a PDF File in .NET Applications with a Single Control

Generate, view, and print PDF files in your .NET applications in one place with ComponentOne PDF Tools.

ComponentOne PDF is a .NET library that supports advanced features that are part of PDF specification, which can help you create a PDF file in your application.

After creating a PDF file, you can load that file using ComponentOne PDFDocumentSource, a library that can load and view your PDF in FlexViewer in your .NET application. PDFDocumentSource can also independently search keywords in your PDF, as well as export and print your PDF files. In addition to PDFs generated from C1PDF, FlexViewer can also load any PDF.

FlexViewer is a flexible viewer that can load reports and documents like C1FlexReport, SSRS Reports, and PDF files. It also offers powerful features for viewing documents. Please see more information about FlexViewer to flexviewer page

Because all of the above tools are cross-platform, you can generate, view, and print PDF files in any of the Winforms, WPF, and UWP applications.

Suppose you want to put various images of your office locations in a single file and store it in a PDF. You also want to load this PDF at run time and print it. You can do this in a WinForms application using the following steps (as well as a WPF or UWP application):

  • Step 1: Add the following references to refer to C1PdfDocumentSource and C1Pdf in your application:
    • C1.Win.C1Document.4
    • C1.C1Pdf.4
  • Step 2: Drop C1FlexViewer on the form.
  • Step 3: In Form_Load method, copy the following code to generate a PDF file using the C1PdfDocument class. In this example, various office location images are drawn in a rectangle using the DrawImage method on every new page. When you use the DrawString method, the office name is defined at a particular location. In the end, the PDF is saved in a file.

//Create C1PdfDocument  
C1.C1Pdf.C1PdfDocument pdf = new C1.C1Pdf.C1PdfDocument();  

//Create Font that would add Office Location title.  
Font font = new Font("Calibri", 16, FontStyle.Underline);  

//Create Rectangle area in which the image would be drawn  
RectangleF rc = pdf.PageRectangle;  
rc.Inflate(-72, -72);  

//Define Image to be used  
var image = Image.FromFile(@"..\\..\\GrapeCity Sendai.jpg");  

//Draw the Image  
pdf.DrawImage(image, rc,ContentAlignment.TopLeft, C1.C1Pdf.ImageSizeModeEnum.Scale);  

//Add Office title  
pdf.DrawString("GrapeCity Sendai", font, Brushes.Black, new PointF(220, 20));  

//Create new page  

//Create Rectangle area, define the image, draw the image and add Office Title  
RectangleF rc1 = pdf.PageRectangle;  
var image1 = Image.FromFile(@"..\\..\\GrapeCity Sendai in Winters.jpg");  
rc.Inflate(-72, -72);  
pdf.DrawImage(image1, rc1, ContentAlignment.TopLeft, C1.C1Pdf.ImageSizeModeEnum.Scale);  
pdf.DrawString("GrapeCity Sendai in Winters", font, Brushes.Black, new PointF(20, 20));  

//Create new page  

//Create Rectangle area, define the image, draw the image and add Office Title.  
RectangleF rc2 = pdf.PageRectangle;  
rc.Inflate(-72, -72);  
var image2 = Image.FromFile(@"..\\..\\GrapeCity Sendai Wine Studios.jpg");  
pdf.DrawImage(image2, rc2, ContentAlignment.TopLeft, C1.C1Pdf.ImageSizeModeEnum.Scale);  
pdf.DrawString("GrapeCity Sendai Wine Studios", font, Brushes.Black, new PointF(20, 20));  

//Create new page  

//Create Rectangle area, define the image, draw the image and add Office Title.  
RectangleF rc3 = pdf.PageRectangle;  
rc.Inflate(-72, -72);  
var image3 = Image.FromFile(@"..\\..\\GrapeCity Sendai Outside View.jpg");  
pdf.DrawImage(image3, rc3, ContentAlignment.TopLeft, C1.C1Pdf.ImageSizeModeEnum.Scale);  
pdf.DrawString("GrapeCity Sendai Outside View", font, Brushes.Black, new PointF(20, 20));  
string fileName = "GrapeCity Sendai.pdf";  

  • Step 4: Now that you've created and generated a PDF file, you are just a few steps away from viewing it in a Viewer at run time. Create a C1PDFDocumentSource object and load the file in it.

C1.Win.C1Document.C1PdfDocumentSource pdfdoc = new C1.Win.C1Document.C1PdfDocumentSource();  
pdfdoc.LoadFromFile("GrapeCity Sendai.pdf");  

  • Step 5: Load the C1PDFDocumentSource in FlexViewer.

c1FlexViewer1.DocumentSource = pdfdoc;  

This is what your PDF file should look like: GrapeCityOfficeLocation* Step 6: To print the file, simply click the Print button on FlexViewer.

Please note that your application references the following DLLs when running the above example:

  • C1.Win.C1Document.4
  • C1.C1Pdf.4
  • C1.Win.4
  • C1.Win.Barcode.4
  • C1.Win.Chart.FlexChart.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
  • C1.Win. FlexViewer.4
  • C1.Win.C1Ribbon.4

Learn more about C1PDF here. Learn more about the C1PDFDocumentSource component here.should link to corresponding pages.


GrapeCity Developer Tools
comments powered by Disqus