Foxit Corporation Forums  

Go Back   Foxit Corporation Forums > Portable Document Format (PDF) Tools > SDK ( DLL or ActiveX)

SDK ( DLL or ActiveX) Integrate PDF functions into your application easily. The SDK shares the same technology that powers Foxit Reader.

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old July 14th, 2006, 02:40 PM
Daniel Daniel is offline
Junior Member
 
Join Date: Jul 2006
Posts: 2
Daniel is an unknown quantity at this point
Default C# sample code

Would it be possible to have the C# sample code (as mentioned in the Programming Guide) posted in this forum. Or even better add it to the list of sample applications on the Foxit Reader SDK page.

If posting the code is not an option, I would appreciate some pointers on how to render an PDF in C# (e.g. paint it on a PictureBox).
Reply With Quote
  #2  
Old July 17th, 2006, 07:53 PM
william's Avatar
william william is offline
Foxit Developer
 
Join Date: Dec 2005
Posts: 192
william is a glorious beacon of lightwilliam is a glorious beacon of lightwilliam is a glorious beacon of lightwilliam is a glorious beacon of lightwilliam is a glorious beacon of lightwilliam is a glorious beacon of light
Default Here is the C# sample code

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

// Sample C# program for calling FPDFVIEW.DLL from Managed Code
// The wrapper class is Foxit.FPDFView.
// Please take a look at PDFView.Form1.button1_Click method at the bottom for
// an example on how to call the wrapped functions.

// Namespace created for Foxit PDF Reader SDK
namespace Foxit
{
// Class for calling FPDFView.DLL
public class FPDFView
{
// FPDF_UnlockDLL: Unlock the DLL with license key. Not used in evaluation version.
[DllImport("fpdfview.dll")]
public static extern void FPDF_UnlockDLL(String license_id, String unlock_code);

// FPDF_LoadDocument: Load a document
[DllImport("fpdfview.dll")]
public static extern int FPDF_LoadDocument(String file_path, String password);

// FPDF_GetPageCount: Get number of pages in the document
[DllImport("fpdfview.dll")]
public static extern int FPDF_GetPageCount();

// FPDF_LoadPage: Load a page
[DllImport("fpdfview.dll")]
public static extern int FPDF_LoadPage(int pdf_doc, int page_index);

// FPDF_GetPageWidth: Get page width in points
[DllImport("fpdfview.dll")]
public static extern double FPDF_GetPageWidth(int pdf_page);

// FPDF_GetPageHeight: Get page width in points
[DllImport("fpdfview.dll")]
public static extern double FPDF_GetPageHeight(int pdf_page);

// FPDF_RenderPage: Render a page onto specified area of a device
[DllImport("fpdfview.dll")]
public static extern void FPDF_RenderPage(System.IntPtr hdc, int pdf_page, int start_x,
int start_y, int size_x, int size_y, int rotate, int flags);

// FPDF_ClosePage: Close a loaded page
[DllImport("fpdfview.dll")]
public static extern int FPDF_ClosePage(int pdf_page);

// FPDF_CloseDocument: Close a loaded document
[DllImport("fpdfview.dll")]
public static extern int FPDF_CloseDocument(int pdf_doc);
}
}

// Namespace created for PDFView sample application
namespace PDFView
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.PictureBox pdfView;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.pdfView = new System.Windows.Forms.PictureBox();
this.SuspendLayout();
//
// button1
//
this.button1.Font = new System.Drawing.Font("SimSun", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button1.Location = new System.Drawing.Point(288, 760);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(256, 40);
this.button1.TabIndex = 0;
this.button1.Text = "Show PDF";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// pdfView
//
this.pdfView.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.pdfView.Location = new System.Drawing.Point(32, 0);
this.pdfView.Name = "pdfView";
this.pdfView.Size = new System.Drawing.Size(760, 752);
this.pdfView.TabIndex = 1;
this.pdfView.TabStop = false;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(8, 18);
this.ClientSize = new System.Drawing.Size(824, 808);
this.Controls.Add(this.pdfView);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
// Open a file dialog
OpenFileDialog dlg = new OpenFileDialog();
dlg.DefaultExt = "pdf";
dlg.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*";
if (dlg.ShowDialog(this) != DialogResult.OK) return;

// First open file
int pdf_doc = Foxit.FPDFView.FPDF_LoadDocument(dlg.FileName);
if (pdf_doc == 0) return; // load PDF failed
// Then load first page
int pdf_page = Foxit.FPDFView.FPDF_LoadPage(pdf_doc, 0);
if (pdf_page == 0) return; // page loading failed
// Get the destination control size
int size_x = pdfView.DisplayRectangle.Width;
int size_y = pdfView.DisplayRectangle.Height;
// White fill the destination area
Graphics graphics = pdfView.CreateGraphics();
SolidBrush brush = new SolidBrush(Color.FromArgb(255, 255, 255, 255));
graphics.FillRectangle(brush, 0, 0, size_x, size_y);
// Get a HDC (handle to device context) to be used in DLL call
IntPtr hdc = graphics.GetHdc();
// OK! We can render the page now
Foxit.FPDFView.FPDF_RenderPage(hdc, pdf_page, 0, 0, size_x, size_y, 0, 0);
graphics.ReleaseHdc(hdc);
// Finally, we destroy page and document objects
Foxit.FPDFView.FPDF_ClosePage(pdf_page);
Foxit.FPDFView.FPDF_CloseDocument(pdf_doc);
}
}
}
Reply With Quote
  #3  
Old July 18th, 2006, 12:53 AM
Daniel Daniel is offline
Junior Member
 
Join Date: Jul 2006
Posts: 2
Daniel is an unknown quantity at this point
Default

Thanks! That does the trick.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 06:15 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2005-2008 Foxit Corporation. All rights reserved.