<%@ Page Language="C#" AutoEventWireup="true" CodeFile="drawimageinpdf.aspx.cs" Inherits="drawimageinpdf"%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
.CS Page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PdfFileWriter;
using System.Diagnostics;
using System.Drawing;
public partial class drawimageinpdf : System.Web.UI.Page
{
static PdfDocument Document;
static PdfPage Page;
static PdfContents Contents;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Test(false, Server.MapPath("~/PdfFiles/abc.pdf"));//Generate Pdf Doc in PdfFiles Folder
}
public void Test(Boolean Debug, String FileName)
{
Document = new PdfDocument(PaperType.Letter, false, UnitOfMeasure.Inch);
Document.Debug = Debug;
Page = new PdfPage(Document);
Contents = new PdfContents(Page);
PdfImage Image1 = new PdfImage(Document, Server.MapPath("~/images/Flower.jpg")); //Image to show in Pdf
PdfImage Image1 = new PdfImage(Document, Server.MapPath("~/images/Flower.jpg")); //Image to show in Pdf
Image1.SetImageQuality(50);
Contents.SaveGraphicsState();
Contents.Translate(0.00, 1.0);
ImageSizePos NewSize = Image1.ImageSizePosition(8.27, 9.02, ContentAlignment.MiddleCenter);
Contents.DrawImage(Image1, NewSize.DeltaX, NewSize.DeltaY, NewSize.Width,
NewSize.Height);
Contents.RestoreGraphicsState();
Document.CreateFile(FileName);
Process Proc = new Process();
Proc.StartInfo = new ProcessStartInfo(FileName);
//Download
Image in Pdf Document
Response.ClearContent(); //Clears
all content output from Buffer Stream
Response.ClearHeaders(); //Clears all headers from Buffer Stream
//Adds
an HTTP header to the output stream
Response.AddHeader("Content-Disposition", "inline;filename="+ DateTime.Now.ToString("yyyyMMddhhmm"));
Response.ContentType = "application/pdf"; //Gets
or Sets the HTTP MIME type of the output stream
//Writes
the content of the specified file directory to an HTTP response output stream
as a file block
Response.WriteFile(FileName);
Response.Flush(); //sends all currently buffered output to the client
Response.Clear(); //Clears all content output from Buffer Stream
}
}
More than
One Image in PDF Document
Document
= new PdfDocument(8.27,
11.02, UnitOfMeasure.Inch);
Document.Debug = Debug;
for (int i = 0; i < dss.Rows.Count; i++) //Load Image Path's Into DataSet
{
Page = new PdfPage(Document);
Contents = new PdfContents(Page);
string path = Server.MapPath("~/" + dss.Rows[i]["filepath"].ToString());
PdfImage Image1 = new PdfImage(Document, path);
Image1.SetImageQuality(50);
Contents.SaveGraphicsState();
Contents.Translate(0.00, 1.0);
ImageSizePos NewSize = Image1.ImageSizePosition(8.27, 9.02, ContentAlignment.MiddleCenter);
Contents.DrawImage(Image1,
NewSize.DeltaX, NewSize.DeltaY, NewSize.Width, NewSize.Height);
Contents.RestoreGraphicsState();
}
Document.CreateFile(FileName);
Process Proc = new Process();
Proc.StartInfo = new ProcessStartInfo(FileName);
Reference:
http://www.codeproject.com/Articles/570682/PDF-File-Writer-Csharp-Class-Library-Version?rp=/KB/files/570682/PdfFileWriter_dll.zip
http://www.codeproject.com/Articles/570682/PDF-File-Writer-Csharp-Class-Library-Version?rp=/KB/files/570682/PdfFileWriter_dll.zip
thnx for sharing such a valuable information….I am using zetpdf to generate pdf in asp.net…I would also suggest you to use zetpdf…It makes the job easy..
ReplyDeleteThis code does implement Generate Images in PDF, which is great. But I used to use a C# PDF to image SDK, which also includes the function of Generate Images in PDF, and it is free, and the charging function is more powerful.
ReplyDelete