PK
I ManipulatePdfFiles/PK
Ċ
I ( ManipulatePdfFiles/MakePdfGrayscaleDemo/PK
IU
C ManipulatePdfFiles/MakePdfGrayscaleDemo/MakePdfGrayscaleDemo.csproj
Debug
AnyCPU
{543B07EC-325F-4963-8479-EC97E1F6BAED}
Exe
Properties
MakePdfGrayscaleDemo
MakePdfGrayscaleDemo
v4.0
512
AnyCPU
true
full
false
bin\Debug\
DEBUG;TRACE
prompt
4
AnyCPU
pdbonly
true
bin\Release\
TRACE
prompt
4
..\..\..\..\..\WPF_Scrum\Current\Binaries\WPF\Telerik.Windows.Documents.Core.dll
..\..\..\..\..\WPF_Scrum\Current\Binaries\WPF\Telerik.Windows.Documents.Fixed.dll
..\..\..\..\..\WPF_Scrum\Current\Binaries\WPF\Telerik.Windows.Zip.dll
Always
PK
q
Iu 2 ManipulatePdfFiles/MakePdfGrayscaleDemo/Program.csusing System;
using System.Diagnostics;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Telerik.Windows.Documents.Fixed.FormatProviders.Pdf;
using Telerik.Windows.Documents.Fixed.Model;
using Telerik.Windows.Documents.Fixed.Model.ColorSpaces;
using Telerik.Windows.Documents.Fixed.Model.Common;
using Telerik.Windows.Documents.Fixed.Model.Graphics;
using Telerik.Windows.Documents.Fixed.Model.Objects;
using Telerik.Windows.Documents.Fixed.Model.Text;
namespace MakePdfGrayscaleDemo
{
class Program
{
static void Main()
{
PdfFormatProvider provider = new PdfFormatProvider();
RadFixedDocument document = provider.Import(System.IO.File.ReadAllBytes("RedTemplate.pdf"));
foreach (RadFixedPage page in document.Pages)
{
foreach (ContentElementBase element in page.Content)
{
MakeGrayscale(element);
}
}
string resultFile = "grayscale.pdf";
if (System.IO.File.Exists(resultFile))
{
System.IO.File.Delete(resultFile);
}
System.IO.File.WriteAllBytes(resultFile, provider.Export(document));
Process.Start(resultFile);
}
private static void MakeGrayscale(ContentElementBase element)
{
TextFragment text = element as TextFragment;
if (text != null)
{
text.Stroke = MakeGrayscale(text.Stroke);
text.Fill = MakeGrayscale(text.Fill);
}
Path path = element as Path;
if (path != null)
{
path.Stroke = MakeGrayscale(path.Stroke);
path.Fill = MakeGrayscale(path.Fill);
}
Image image = element as Image;
if (image != null)
{
BitmapSource originalImage = image.ImageSource.GetBitmapSource();
BitmapSource grayscaleImage = MakeGrayscale(originalImage);
image.ImageSource = new Telerik.Windows.Documents.Fixed.Model.Resources.ImageSource(grayscaleImage);
}
}
private static BitmapSource MakeGrayscale(BitmapSource source)
{
byte?[,] intensities = GetPixelsIntensity(source);
BitmapSource grayscaleSource = CreateImageFromGrayPixels(intensities);
return grayscaleSource;
}
private static ColorBase MakeGrayscale(ColorBase color)
{
RgbColor rgb = (RgbColor)color;
byte gray = GetGrayIntensity(rgb.R, rgb.G, rgb.G);
return new RgbColor(rgb.A, gray, gray, gray);
}
private static BitmapSource CreateImageFromGrayPixels(byte?[,] pixels)
{
double dpi = 96;
int height = pixels.GetLength(0);
int width = pixels.GetLength(1);
byte[] pixelData = new byte[width * height];
for (int y = 0; y < height; ++y)
{
int yIndex = y * width;
for (int x = 0; x < width; ++x)
{
pixelData[x + yIndex] = pixels[y, x] ?? 255;
}
}
BitmapSource bmpSource = BitmapSource.Create(width, height, dpi, dpi, PixelFormats.Gray8, null, pixelData, width);
return bmpSource;
}
private static byte?[,] GetPixelsIntensity(BitmapSource bitmapSource)
{
int width = bitmapSource.PixelWidth;
int height = bitmapSource.PixelHeight;
byte?[,] intensities = new byte?[height, width];
int[] pixels = GetPixels(bitmapSource);
int pixelIndex = 0;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
byte a, r, g, b;
GetComponentsFromPixel(pixels[pixelIndex++], out a, out r, out g, out b);
byte? intensity;
if (a == 0)
{
intensity = null;
}
else
{
intensity = (byte)((a / 255.0) * GetGrayIntensity(r, g, b));
intensity = intensity.Value < 255 ? ((byte)(intensity.Value + 1)) : intensity.Value;
}
intensities[i, j] = intensity;
}
}
return intensities;
}
private static void GetComponentsFromPixel(int pixel, out byte a, out byte r, out byte g, out byte b)
{
b = (byte)(pixel & 0xFF);
g = (byte)((pixel >> 8) & 0xFF);
r = (byte)((pixel >> 16) & 0xFF);
a = (byte)((pixel >> 24) & 0xFF);
}
private static int[] GetPixels(BitmapSource source)
{
int[] pixels = new int[source.PixelWidth * source.PixelHeight];
if (source.Format == PixelFormats.Bgr32 || source.Format == PixelFormats.Bgra32 || source.Format == PixelFormats.Pbgra32)
{
checked
{
source.CopyPixels(pixels, source.PixelWidth * 4, 0);
}
}
else if (source.Format == PixelFormats.Indexed8)
{
byte[] indices = new byte[source.PixelWidth * source.PixelHeight];
source.CopyPixels(indices, source.PixelWidth, 0);
for (int i = 0; i < indices.Length; ++i)
{
Color c = source.Palette.Colors[indices[i]];
pixels[i] = (c.A << 24) | (c.R << 16) | (c.G << 8) | (c.B << 0);
}
}
else
{
FormatConvertedBitmap converted = new FormatConvertedBitmap(source, PixelFormats.Bgra32, null, 0);
converted.CopyPixels(pixels, source.PixelWidth * 4, 0);
}
return pixels;
}
private static byte GetGrayIntensity(byte r, byte g, byte b)
{
return (byte)(0.2126 * r + 0.7152 * g + 0.0722 * b);
}
}
}
PK
#
I 3 ManipulatePdfFiles/MakePdfGrayscaleDemo/Properties/PK
#
I>\ B ManipulatePdfFiles/MakePdfGrayscaleDemo/Properties/AssemblyInfo.csusing System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("MakePdfGrayscaleDemo")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("MakePdfGrayscaleDemo")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("fc652f43-5656-43a9-a4bd-7ac6a9ef7ee1")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
PK
-
I$b b 7 ManipulatePdfFiles/MakePdfGrayscaleDemo/RedTemplate.pdf%PDF-1.7
%
2 0 obj
<>
stream
FExif MM * 4 ( 1 2 ҇i , , Adobe Photoshop CS6 (Windows) 2015:06:08 16:48:43 0221 n v( ~ H H Adobe_CM Adobe d
k" ?
3 !1AQa"q2B#$Rb34rC%Scs5&DTdE£t6UeuF'Vfv7GWgw 5 !1AQaq"2B#R3$brCScs4%&5DTdEU6teuFVfv'7GWgw ? TI%)$IJI$RPm=Ŭ{\4I%)$IJI$STI%)+.J]uc$V}V 7wם̇ٵ[%oo:OѵA:@]ީ2
}2-?nP
ZOHWjx y cS52|%AA,
Pv|?Ddb [d,ǡ}l$<9\ū;rm0
s-k}OͫE̡jK.|{IsZ+ݱ5s.H[_P]_Hŵ$fc7N]s4sLj GW HoH̆21M>wn1N+;?'2'N>'ɣ*v;ŕuȫ0[mmA YoZ5n5hr3i{ uɌD0/ 2I$U::s+?+ N "kEpX]Oh4c]ӳ2?Içy09f"43wXQ~+N%.;-1iՙ?K{RZw[Ru6nvK*W|Kw*r||Kh$,C;J`$ "tJ,GɁO+u51EB~u/s5 2zbɞS֣K580Èй >r-hƦKDw?k6_\Svk[QQUSYO$5 PQ:`oH76chA-Yt??tˉ?m o1eWUVV`?n *]Wӊݧi?y,!^KX=h_.7ޮᆑ ֖lF\G8~uy}Fk>DJ">uoNʹ~
>[7][^7c㵭/a->km/џ 7u3[v:]aɪcx|O_ v@\^o*ŭM7I/^!YS?譞FL,Yș młi[? Jo>{ݍhsnGp*aeַs-onS{ƇN }K?Y❈u<o89fxqb;p qSN~x?V]o'4вcG*o/$I˖]Zu{`kg<