|
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Security.Permissions;
using System.Collections;
using System.Drawing;
using System.Collections.Generic;
using System.IO;
using System.Drawing.Imaging;
[assembly: System.Security.AllowPartiallyTrustedCallers, FileIOPermission(SecurityAction.RequestMinimum, Unrestricted = true)]
namespace SQLCLR
{
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Assert, Unrestricted = true)]
public partial class UserDefinedFunctions
{
[Microsoft.SqlServer.Server.SqlFunction(
FillRowMethodName = "GetPDF_FillRow",
TableDefinition = "PDFPageImage Varbinary(max)",
DataAccess = DataAccessKind.Read)]
public static IEnumerable GetPDF(SqlString DocumentName)
{
ArrayList items = new ArrayList();
List<Image> pages = new List<Image>();
object[] images;
images = new object[1];
MemoryStream pageStream = new MemoryStream();
PDFParser.Parse pdfParser = new PDFParser.Parse();
using (SqlConnection conn = new SqlConnection("context connection = true"))
{
conn.Open();
SqlPipe pipe = SqlContext.Pipe;
SqlCommand cmd = new SqlCommand("GetDocument", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@Name", DocumentName));
SqlDataReader reader = cmd.ExecuteReader();
byte[] pdfContent = null;
while (reader.Read())
{
pdfContent = (byte[])reader.GetSqlBinary(0);
pages = pdfParser.Split(pdfContent);
for (int i = 0; i < pages.Count; i++)
{
MemoryStream ms = new MemoryStream();
pages[i].Save(ms, ImageFormat.Png);
items.Add((SqlBinary)ms.ToArray());
}
}
reader.Close();
reader = null;
pdfContent = null;
}
return items;
}
private static void GetPDF_FillRow(Object obj, out SqlBinary sItem)
{
SqlBinary sTemp = (SqlBinary)obj;
sItem = sTemp;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.