c# itextsharp how to get digital signature image - c#

Is it possible to get the image of any digital signatures in a pdf file with itextsharp using c# code?
PdfReader pdf = new PdfReader("location.pdf");
AcroFields acroFields = pdf.AcroFields;
List<string> names = acroFields.GetSignatureNames();
foreach (var name in names)
{
PdfDictionary dict = acroFields.GetSignatureDictionary(name);
}
With this simple lines i can get the signature dictionaries but from this object i am not able to get the content of the image.
Can anyone help?

I answer my own question... if it could be usefull to someone else i did it like this.
I found a Java class to do what i was looking for and I translated it in C#.
class XyzmoSignatureDataExtractor
{
private PdfReader reader;
public XyzmoSignatureDataExtractor(PdfReader reader)
{
this.reader = reader;
}
public PdfImageObject extractImage(String signatureName)
{
MyImageRenderListener listener = new MyImageRenderListener();
PdfDictionary sigFieldDic = reader.AcroFields.GetFieldItem(signatureName).GetMerged(0);
PdfDictionary appearancesDic = sigFieldDic.GetAsDict(PdfName.AP);
PdfStream normalAppearance = appearancesDic.GetAsStream(PdfName.N);
PdfDictionary resourcesDic = normalAppearance.GetAsDict(PdfName.RESOURCES);
PdfContentStreamProcessor processor = new PdfContentStreamProcessor(listener);
processor.ProcessContent(ContentByteUtils.GetContentBytesFromContentObject(normalAppearance), resourcesDic);
return listener.image;
}
class MyImageRenderListener : IRenderListener
{
public void BeginTextBlock() { }
public void EndTextBlock() { }
public void RenderImage(ImageRenderInfo renderInfo)
{
try
{
image = renderInfo.GetImage();
}
catch (Exception e)
{
throw new Exception("Failure retrieving image", e);
}
}
public void RenderText(TextRenderInfo renderInfo) { }
public PdfImageObject image = null;
}
}
To use the class and save the image i just do like that:
PdfReader reader = new PdfReader("location.pdf");
XyzmoSignatureDataExtractor extractor = new XyzmoSignatureDataExtractor(reader);
AcroFields acroFields = reader.AcroFields;
foreach (string name in acroFields.GetSignatureNames())
{
PdfImageObject image = extractor.extractImage(name);
var _image = image.GetDrawingImage();
string file_name = "sig." + image.GetFileType();
_image.Save(file_name);
}

Related

iText 7 - Extract Exact Word(s) In A Region

With PDF I understand that char/words can be positioned into multiple locations. I understand also this
(so part of the text may be outside rect, iText doesn't cut text
snippets in pieces)
I found this help also.
Extracting text from a rectangle using iText ( .Net ) does give me the entire line
And managed the code below, but I still couldn't extract the exact word inside my rectangle.
namespace iText
{
public static class Test
{
public static void Main(String[] args)
{
var reader = new PdfReader("mypdf.pdf");
PdfDocument pdfDoc = new PdfDocument(reader);
var addressRect = new Rectangle(0, 0, 0, 0);
var addressRegionFilter = new TextRegionEventFilter(addressRect);
var filterListener = new RectangleTextExtractionStrategy(new LocationTextExtractionStrategy(), addressRect);
var addressText = PdfTextExtractor.GetTextFromPage(pdfDoc.GetPage(1), filterListener);
pdfDoc.Close();
}
}
public class RectangleTextExtractionStrategy : ITextExtractionStrategy
{
private ITextExtractionStrategy innerStrategy = null;
private Rectangle rectangle;
public RectangleTextExtractionStrategy(ITextExtractionStrategy strategy, Rectangle rectangle)
{
this.innerStrategy = strategy;
this.rectangle = rectangle;
}
public void EventOccurred(IEventData iEventData, EventType eventType)
{
if (eventType != EventType.RENDER_TEXT)
return;
TextRenderInfo tri = (TextRenderInfo)iEventData;
foreach (TextRenderInfo subTri in tri.GetCharacterRenderInfos())
{
Rectangle r2 = new CharacterRenderInfo(subTri).GetBoundingBox();
if (Intersects(r2))
innerStrategy.EventOccurred(subTri, EventType.RENDER_TEXT);
}
}
public string GetResultantText()
{
return innerStrategy.GetResultantText();
}
public ICollection<EventType> GetSupportedEvents()
{
return innerStrategy.GetSupportedEvents();
}
private bool Intersects(Rectangle rectangle)
{
var addressRect = new Rectangle(62, 20, 6, 7);
bool intersect = rectangle.Contains(addressRect);
if(intersect)
return true;
return false;
}
}
}

issue reading from xml file c#

I am trying to read from a xml file but it is not working eventhough I have been weating over it for two days, so any help would be greatly appreciated!
In Cookbook class:
public List<Recipe> readAll()
{
List<Recipe> newListRecipies = new List<Recipe>();
Recipe readRecipie = new Recipe();
TextReader reader = null;
try
{
var serializer = new XmlSerializer(typeof(Recipe));
reader = new StreamReader(path);
newListRecipies = BinarySerialization.ReadFromBinaryFile<List<Recipe>>(path);
reader.Close();
return newListRecipies;
}
catch (Exception e)
{
string error = $"An exception occured: " + e;
Log theLog = new Log();
theLog.LogMessage(error);
return newListRecipies;
}
}
In Recipe class:
public Recipe readOne(string name)
{
CookBook newCB = new CookBook();
List<Recipe> allRecipies = newCB.readAll();
foreach(Recipe oneRecipe in allRecipies)
{
if(oneRecipe.recipeName == name)
{
return oneRecipe;
}
}return newCB.defaultRecipie;
}
I am getting the default recipe as the result everytime. I can see the the recipies are saved correctly everytime but here the code anyways:
In Recipie class:
public void SaveRecipe(Recipe myRecepie)
{
CookBook theCookBook = new CookBook();
theCookBook.Save(myRecepie);
addFoodItem(myRecepie.recipeIngridients);
}
In CookBook class:
public void Save(Recipe newRecipie)
{
TextWriter writer = null;
try
{
var serializer = new XmlSerializer(typeof(Recipe));
writer = new StreamWriter(path, append: true);
serializer.Serialize(writer, newRecipie);
}
finally
{
if (writer != null)
writer.Close();
}
}
And the xml file (generated by the save function in the CookBook class)
<?xml version="1.0" encoding="utf-8"?>
<Recipe xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<recipeName>toast</recipeName>
<recipeType>snack</recipeType>
<recipeIngridients>
<string>bread</string>
<string>butter</string>
</recipeIngridients>
</Recipe><?xml version="1.0" encoding="utf-8"?>
<Recipe xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<recipeName>G&T</recipeName>
<recipeType>drink</recipeType>
<recipeIngridients>
<string>tonic</string>
<string>gin</string>
</recipeIngridients>
</Recipe><?xml version="1.0" encoding="utf-8"?>
<Recipe xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<recipeName>cake</recipeName>
<recipeType>snack</recipeType>
<recipeIngridients>
<string>butter</string>
<string>sugar</string>
</recipeIngridients>
</Recipe>
I believe the way you are deserializing the xml is incorrect with BinarySerialization.ReadFromBinaryFile....
Assuming your xml is correct I would do something like this.
// read file
List<Recipe> recipes;
using (var reader = new StreamReader("recipe.xml"))
{
XmlSerializer deserializer = new XmlSerializer(typeof(List<Recipe>),
new XmlRootAttribute("Recipe"));
recipes = (List<Recipe>)deserializer.Deserialize(reader);
}
These are the changes I made. It's best to load the previous recipes, add to the list, then re-write the XML from scratch.
public class Recipe
{
public string recipeName;
public string recipeType;
public List<string> recipeIngridients = new List<string>();
public Recipe readOne(string name)
{
CookBook newCB = new CookBook();
List<Recipe> allRecipies = newCB.readAll();
foreach(Recipe oneRecipe in allRecipies)
{
if(oneRecipe.recipeName == name)
{
return oneRecipe;
}
}
return newCB.defaultRecipe;
}
}
public class RecipeList
{
public List<Recipe> Recipes = new List<Recipe>();
}
public class CookBook
{
public Recipe defaultRecipe;
public string path;
public void Save(Recipe newRecipe)
{
TextWriter writer = null;
RecipeList recipeList = null;
try
{
// See if recipes exists
var serializer = new XmlSerializer(typeof(RecipeList));
if (File.Exists(path)) // Load the recipe list if it exists
{
using (var fileStream = File.OpenRead(path))
{
recipeList = (RecipeList)serializer.Deserialize(fileStream);
}
}
else
{
recipeList = new RecipeList();
}
// Add recipe to the list
recipeList.Recipes.Add(newRecipe);
writer = new StreamWriter(path, append: false);
serializer.Serialize(writer, recipeList);
}
finally
{
if (writer != null)
writer.Close();
}
}
public List<Recipe> readAll()
{
RecipeList temp = null;
var serializer = new XmlSerializer(typeof(RecipeList));
try
{
using (var fileStream = File.OpenRead(path))
{
temp = (RecipeList)serializer.Deserialize(fileStream);
}
return temp.Recipes;
}
catch (Exception e)
{
string error = #"An exception occured: " + e;
//Log theLog = new Log();
//theLog.LogMessage(error);
return new List<Recipe>();
}
}
}

Deserialize object from binary file

Hey guys I am working on a program that will take a person's name and comment and then serialize it to a binary file. It must also be able to deserialize and load the data to the form. I need it to be able to go up and down the list of files(names and comments) by using buttons on the form.
In my code I have it separated into a class that builds the object and the code for the form, and no i'm not allowed to separate it. (Just in case anyone thought that'd work)
the form code:
private void btnAdd_Click(object sender, EventArgs e)
{
bool blnValid = true;
if (string.IsNullOrWhiteSpace(txtName.Text))
{
MessageBox.Show("Please enter a valid name");
}
if (string.IsNullOrWhiteSpace(txtComment.Text))
{
MessageBox.Show("Please enter a valid comment");
blnValid = false;
}
if (blnValid)
{
//create class instance and assign property values
//set file name
DateTime CurrentDate = DateTime.Now;
string strFileName;
strFileName = CurrentDate.ToString("dd-MM-yy-hh-mm-ss") + ".bin";
// serialize object to binary file
MessageBox.Show(strFileName);
Enquiry newEnquiry = new Enquiry();
newEnquiry.Name = txtName.Text;
newEnquiry.Comment = txtComment.Text;
newEnquiry.DOB = dteDOB.Value;
newEnquiry.WriteToFile(strFileName, newEnquiry);
}
}
private void btnLoad_Click(object sender, EventArgs e)
{
}
private void btnPrevious_Click(object sender, EventArgs e)
{
}
and the class code:
[Serializable]
class Enquiry
{
//stores the various values into the enquiry class.
public string Name { get; set; }
public DateTime DOB { get; set; }
public string Comment { get; set; }
//creates the file, if there isn't one, writes the file, and
//disables sharing while the program is active. It also formats
//the file into binary
public void WriteToFile(string strFileName, Enquiry newEnquiry)
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(strFileName, FileMode.Create, FileAccess.Write, FileShare.None);
formatter.Serialize(stream, newEnquiry);
stream.Close();
}
// this loads the files and translates them to plain text
public void ReadFromFile(string strFileName, Enquiry newEnquiry)
{
Stream stream = File.OpenRead(strFileName);
BinaryFormatter formatter = new BinaryFormatter();
newEnquiry = (Enquiry)formatter.Deserialize(stream);
stream.Close();
}
I don't know what you are asking, but if you need methods to serialize and deserialize, use these:
public static void BinarySerializeObject(string path, object obj)
{
using (StreamWriter streamWriter = new StreamWriter(path))
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
try
{
binaryFormatter.Serialize(streamWriter.BaseStream, obj);
}
catch (SerializationException ex)
{
throw new SerializationException(((object) ex).ToString() + "\n" + ex.Source);
}
}
}
public static object BinaryDeserializeObject(string path)
{
using (StreamReader streamReader = new StreamReader(path))
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
object obj;
try
{
obj = binaryFormatter.Deserialize(streamReader.BaseStream);
}
catch (SerializationException ex)
{
throw new SerializationException(((object) ex).ToString() + "\n" + ex.Source);
}
return obj;
}
}

Windows Form XML Serialization Save Dialog

im stuck with this stupid form... heres my code. It saves it where the streamwriter wants it to but when i save it where the user wants via the savedialog box is creates the XML but doesnt put anything in it! Can someone have a look as it's starting to wind me up!
void SavebuttonClick(object sender, EventArgs e)
{
Stream myStream ;
SaveFileDialog savefile1 = new SaveFileDialog();
savefile1.Filter = "xml files |*.xml" ;
savefile1.FilterIndex = 2 ;
savefile1.RestoreDirectory = true ;
if(savefile1.ShowDialog() == DialogResult.OK)
{
if((myStream = savefile1.OpenFile()) != null)
{
Values v = new Values();
v.task1_name = this.task1_name.Text;
v.task1_desc = this.task1_desc.Text;
v.task1_date = this.task1_date.Value;
v.task1_time = this.task1_time.Value;
SaveValues(v);
}
myStream.Close();
}
}
This is the streamwriter...
public void SaveValues(Values v)
{
XmlSerializer serializer = new XmlSerializer(typeof(Values));
using(TextWriter textWriter = new StreamWriter(#"E:\TheFileYouWantToStore.xml"))
{
serializer.Serialize(textWriter, v);
}
...
}
EDIT:
public class Values
{
public string task1_name { get; set;}
public string task1_desc { get; set;}
public DateTime task1_date { get; set;}
public DateTime task1_time { get; set;}
}
I presume this is the code you meant, im fairly new to coding though mate :(
You must call textWriter.close(); after serialize. If you don't close writer it won't apply chenges to file.
By the way you are writing values to E:\TheFileYouWantToStore.xml. your save save method does not use users file.
public void SaveValues(Values v)
{
XmlSerializer serializer = new XmlSerializer(typeof(Values));
using(TextWriter textWriter = new StreamWriter(#"E:\TheFileYouWantToStore.xml"))
{
serializer.Serialize(textWriter, v);
textWriter.close();
}
...
}
EDIT:
if(savefile1.ShowDialog() == DialogResult.OK)
{
Values v = new Values();
v.task1_name = this.task1_name.Text;
v.task1_desc = this.task1_desc.Text;
v.task1_date = this.task1_date.Value;
v.task1_time = this.task1_time.Value;
SaveValues(savefile1.FileName, v);
}
-
public void SaveValues(string fileName, Values v)
{
XmlSerializer serializer = new XmlSerializer(typeof(Values));
using(TextWriter textWriter = new StreamWriter(fileName))
{
serializer.Serialize(textWriter, v);
textWriter.close();
}
...
}

How can I write HTML in a Word document?

How can I write HTML in a Word document using C#?
I made a class to help writing a document
using System;
using System.IO;
using Microsoft.Office.Interop.Word;
namespace WordExporter
{
public class WordApplication : IDisposable
{
private Application application;
private Document document;
private string path;
private bool editing;
public WordApplication(string path)
{
this.path = path;
this.editing = File.Exists(path);
application = new Application();
if (editing)
{
document = application.Documents.Open(path, ReadOnly: false, Visible: false);
}
else
{
document = application.Documents.Add(Visible: false);
}
document.Activate();
}
public void WriteHeader(string text)
{
foreach (Section wordSection in document.Sections)
{
var header = wordSection.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
header.Font.ColorIndex = WdColorIndex.wdDarkRed;
header.Font.Size = 20;
header.Text = text;
}
}
public void WriteFooter(string text)
{
foreach (Section wordSection in document.Sections)
{
var footer = wordSection.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
footer.Font.ColorIndex = WdColorIndex.wdDarkRed;
footer.Font.Size = 20;
footer.Text = text;
}
}
public void Save()
{
if (editing)
{
application.Documents.Save(true);
}
else
{
document.SaveAs(path);
}
}
#region IDisposable Members
public void Dispose()
{
((_Document)document).Close(SaveChanges: true);
((_Application)application).Quit(SaveChanges: true);
}
#endregion
}
class Program
{
static void Main(string[] args)
{
using (var doc = new WordApplication(Directory.GetCurrentDirectory() + "\\test.docx"))
{
doc.WriteHeader("<h1>Header text</h1>");
doc.WriteFooter("<h1>Footer text</h1>");
doc.Save();
}
}
}
}
In the WriteHeader I write some text on the document header, but I need to use HTML. How can I say the contents are HTML? I will also need to insert HTML in the document content...
I can just insert the html file on the section I want using:
range.InsertFile("file.html");

Categories