using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HiQPdfClient;
namespace HiQPdf_Demo
{
public partial class AttachmentsAndFileLinks : System.Web.UI.Page
{
protected void buttonCreatePdf_Click(object sender, EventArgs e)
{
string serverIP = textBoxServerIP.Text;
uint serverPort = uint.Parse(textBoxServerPort.Text);
string serverPassword = textBoxServerPassword.Text;
// create a PDF document
PdfDocument document = new PdfDocument(serverIP, serverPort);
// use server password if necessary
if (serverPassword.Length > 0)
document.ServerPassword = serverPassword;
// set a demo serial number
document.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ==";
// display the attachments when the document is opened
document.Viewer.PageMode = PdfPageMode.Attachments;
// create a page in document
PdfPage page1 = document.AddPage();
// create the true type fonts that can be used in document
PdfFont pdfFontEmbed = document.CreateFontFromName("Times New Roman", 10, true);
// create an attachment with icon from file
string filePath1 = Server.MapPath("~") + @"\DemoFiles\Attach\TextAttach1.txt";
PdfAttachment pdfAttachment1 = document.CreateAttachmentFromFile(page1, new RectangleFloat(10, 30, 10, 20),
PdfAttachIconType.PushPin, filePath1);
pdfAttachment1.Description = "Attachment with icon from a file";
// write a description at the right of the icon
PdfText pdfAttachment1Descr = new PdfText(40, 35, pdfAttachment1.Description, pdfFontEmbed);
page1.Layout(pdfAttachment1Descr);
// create an attachment with icon from a data
byte[] fileData2 = System.IO.File.ReadAllBytes(filePath1);
PdfAttachment pdfAttachment2 = document.CreateAttachmentFromData(page1, new RectangleFloat(10, 60, 10, 20),
PdfAttachIconType.Paperclip, fileData2, "AttachFromData_WithIcon.txt");
pdfAttachment2.Description = "Attachment with icon from data";
// write a description at the right of the icon
PdfText pdfAttachment2Descr = new PdfText(40, 65, pdfAttachment2.Description, pdfFontEmbed);
page1.Layout(pdfAttachment2Descr);
// create an attachment without icon in PDF from a file
string filePath2 = Server.MapPath("~") + @"\DemoFiles\Attach\TextAttach2.txt";
document.CreateAttachmentFromFile(filePath2, "Attachment without icon from a file");
// create an attachment without icon in PDF from data
byte[] fileData1 = System.IO.File.ReadAllBytes(filePath2);
document.CreateAttachmentFromData(fileData1, "AttachFromData_NoIcon.txt", "Attachment without icon from data");
try
{
// write the PDF document to a memory buffer
byte[] pdfBuffer = document.WriteToMemory();
// inform the browser about the binary data format
HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf");
// let the browser know how to open the PDF document and the file name
HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment; filename=PdfAttachments.pdf; size={0}",
pdfBuffer.Length.ToString()));
// write the PDF buffer to HTTP response
HttpContext.Current.Response.BinaryWrite(pdfBuffer);
// call End() method of HTTP response to stop ASP.NET page processing
HttpContext.Current.Response.End();
}
finally
{
document.Close();
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Master.SelectNode("pdfAttachments");
Master.LoadCodeSample("AttachmentsAndFileLinks");
}
}
}
}