iTextSharp SetListSymbol does not work - pdf-generation

I am trying to set styling in PDF generated from iTextSharp.
It seems the SetListSymbol is not working for me whenever I am trying to set Symbol to list items.
Below is the code that I used:
var elements = HTMLWorker.ParseToList(overviewReader,null);
foreach (var element in elements)
{
//element
var list = element as List;
if (list != null)
{
//list.Symbol.GetImage();
list.SetListSymbol("\u25A0");
list.IndentationLeft = 20f;
doc.Add(list);
}
else
{
doc.Add(element);
}
}

The HTMLWorker within iText and iTextSharp supports some very limited "stylesheets" via iTextSharp.text.html.simpleparser.StyleSheet. These stylesheets are loosely based on HTML/CSS properties but only the most basic (think HTML 3.2).
The three main things that you want to do are (1) load a font, (2) create a StyleSheet pointing to that font and (3) bind the StyleSheet to the HTMLWorker. I'm going to partially lift some code from my answer here.
iTextSharp doesn't automatically spider the entire system looking for fonts so you need to manually register them. (Actually, there is a method that you can call and tell iTextSharp to guess at loading fonts but this is much faster.)
Step #1, load the font, in this case Curlz
//Path to our font
string OurFont = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "CURLZ___.TTF");
//Register the font with iTextSharp
iTextSharp.text.FontFactory.Register(OurFont);
Step #2, create a StyleSheet and point it to our font. I'll also set some other properties just to show them off.
//Create a new stylesheet
iTextSharp.text.html.simpleparser.StyleSheet ST = new iTextSharp.text.html.simpleparser.StyleSheet();
//Set the default body font to our registered font's internal name
ST.LoadTagStyle(iTextSharp.text.html.HtmlTags.LI, iTextSharp.text.html.HtmlTags.FACE, "Curlz MT");
ST.LoadTagStyle(iTextSharp.text.html.HtmlTags.LI, iTextSharp.text.html.HtmlTags.COLOR, "FF0000");
ST.LoadTagStyle(iTextSharp.text.html.HtmlTags.LI, iTextSharp.text.html.HtmlTags.SIZE, "50");
Step #3, bind the StyleSheet to our HTMLWorker
//Use our StyleSheet from above when parsing
var elements = HTMLWorker.ParseToList(overviewReader, ST);
Below is a full-working C# WinForms app targeting iTextSharp 5.2.0 that shows off all of the above.
using System;
using System.IO;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
namespace WindowsFormsApplication2 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
//Path to our font
string FontArial = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "CURLZ___.TTF");
//Register the font with iTextSharp
iTextSharp.text.FontFactory.Register(FontArial);
//Create a new stylesheet
iTextSharp.text.html.simpleparser.StyleSheet ST = new iTextSharp.text.html.simpleparser.StyleSheet();
//Set the default body font to our registered font's internal name
ST.LoadTagStyle(iTextSharp.text.html.HtmlTags.LI, iTextSharp.text.html.HtmlTags.FACE, "Curlz MT");
ST.LoadTagStyle(iTextSharp.text.html.HtmlTags.LI, iTextSharp.text.html.HtmlTags.COLOR, "FF0000");
ST.LoadTagStyle(iTextSharp.text.html.HtmlTags.LI, iTextSharp.text.html.HtmlTags.SIZE, "50");
//Sample HTML
var html = #"<ul><li>Test</li></ul>";
//File to output
var outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf");
//Basic PDF creation, nothing special here
using (FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
using (Document doc = new Document(PageSize.LETTER)) {
using (PdfWriter writer = PdfWriter.GetInstance(doc, fs)) {
doc.Open();
//Bind a reader to our HTML
using (StringReader overviewReader = new StringReader(html)) {
//Use our StyleSheet from above when parsing
var elements = HTMLWorker.ParseToList(overviewReader, ST);
//Loop through each element
foreach (var element in elements) {
//See if the element is a list item
var list = element as List;
if (list != null) {
//Set some properties
list.SetListSymbol("\u25A0");
list.IndentationLeft = 20f;
}
//Add the element to the document
doc.Add(element);
}
}
doc.Close();
}
}
}
this.Close();
}
}
}

List objects = HTMLWorker.ParseToList(new StringReader(format), null);
foreach (IElement element in objects)
{
List list = new List();
list.SetListSymbol("\u2022");
list.IndentationLeft = 20f;
list.Add(element);
if (list.Chunks.Count == 0)
{
doc1.Add(element);
}
else
{
doc1.Add(list);
}
}

Related

Find and use already embedded font?

I have create a PDF file with Adobe Illustrator that I have loaded into memory with itext7 pdfreader.
That PDF file already contains a embedded font named "Lato (Embedded)" Encoding:Ansi.
How do create a PDFFont object out of it so I and can use it to draw additional paragraphs?
First of all please note that you would only be able to write additional paragraphs with such a fond if the subset contains all the glyphs needed to write the text, or if the font was fully embedded into the PDF.
The solution below works in case the font you want to find is used to write at least one glyph in the content stream of any page in a document (including nested XObjects), and in case you don't have other fonts with similar names in the document.
Here is a small utility class that helps you extract the desired font from a document:
private static class FontFinder implements IEventListener {
private PdfFont suitableFont;
private String nameToLookFor;
private FontFinder(String nameToLookFor) {
this.nameToLookFor = nameToLookFor;
}
public static PdfFont findFont(PdfDocument pdfDocument, String fontName) {
FontFinder finder = new FontFinder(fontName);
PdfCanvasProcessor processor = new PdfCanvasProcessor(finder);
for (int i = 1; i <= pdfDocument.getNumberOfPages(); i++) {
processor.processPageContent(pdfDocument.getPage(i));
}
return finder.suitableFont;
}
#Override
public void eventOccurred(IEventData data, EventType type) {
if (data instanceof TextRenderInfo) {
PdfFont curFont = ((TextRenderInfo) data).getFont();
String fontName = curFont.getFontProgram().getFontNames().getFontName();
if (fontName != null && fontName.contains(nameToLookFor)) {
suitableFont = curFont;
}
}
}
#Override
public Set<EventType> getSupportedEvents() {
return new HashSet<>(Arrays.asList(EventType.RENDER_TEXT));
}
}
You will need to open the PdfDocument in stamping mode (passing both PdfReader and PdfWriter to the constructor).
PdfDocument pdfDocument = new PdfDocument(new PdfReader(inFile), new PdfWriter(outFile));
Then you can fetch your font in the following way (make sure result is not null):
PdfFont font = FontFinder.findFont(pdfDocument, "Lato");
After that you can use that font instance to draw any content, e.g. by passing it to setFont method of Paragraph, Div and so on.

How to remove Objects from PDF File using PDFClown

I have a pdf which contains a lot of invisible paths. Since the amount of path produces problems later on, I would like to remove the ones that have white colors.
So far I am trying to do this with a ContentScanner:
public class FilterWhitePathScanner implements Scanner {
private static final Logger LOG = LoggerFactory.getLogger(FilterWhitePathScanner.class);
private int count = 0;
public void scan(ContentScanner level) {
if (level == null)
return;
while (level.moveNext()) {
ContentObject object = level.getCurrent();
if (object instanceof ContainerObject) {
// Scan the inner level!
scan(level.getChildLevel());
} else if (object instanceof org.pdfclown.documents.contents.objects.Path) {
AffineTransform ctm = level.getState().getCtm();
Color<?> strokeColor = level.getState().getStrokeColor();
Color<?> fillColor = level.getState().getFillColor();
if (checkWhite(fillColor) && checkWhite(strokeColor)) {
level.remove();
} else {
LOG.info("Stroke Color " + strokeColor + " - Fill Color " + fillColor);
}
} else {
LOG.info("Object:" + object);
}
}
}
It recognizes the paths correctly, but in the end these are not removed from the PDF. Here the code handling the PDF (it extracts only one page from the source pdf):
Document targetDoc = new File().getDocument();
targetDoc.getPages().add(sourceDoc.getPages().get(pageNum).clone(targetDoc));
Page page = targetDoc.getPages().get(0);
Contents contents = page.getContents();
FilterWhitePathScanner filterWhitePathScanner = new FilterWhitePathScanner();
filterWhitePathScanner.scan(new ContentScanner(contents));
LOG.info("White Paths: " + filterWhitePathScanner.getCount());
targetDoc.getFile().save(tempFilePath.toFile(), SerializationModeEnum.Standard);
The saved PDF file still contains the paths I tried to remove. How can I remove objects from the PDF finally?
Thanks,
Thomas
Finally found the solution in the Java doc:
You have to call contents.flush(); to persist the changes into the pdf file.
So I added this line to the PDF handling code before calling save and it works!

Binding Image stored in the Isolated Storage to Image Control in Windows Phone

Is it possible to bind the image present in the Isolates storage to image control through xaml. I found some implementations like getting the image through the property and binding that into xaml control. But this is not the implementation what I am searching for. My question is like, writing an attach property and helper method to fetch the content from Isolated storage. I found a similar implementation in LowProfileImage class, used in windows phone 7. But I think it is deprecated now. If anyone tried similar implementations please help me to achieve the same. Also if implementation have any performance drains please mention that info too.
Yes, it is possible to use images from isolated storage in the app UI. It requires loading the image from the file into the BitmapImage and then binding ImageSource of your control to that BitmapImage. I'm using the following approach:
First, there's a method to load image asynchronously:
private Task<Stream> LoadImageAsync(string filename)
{
return Task.Factory.StartNew<Stream>(() =>
{
if (filename == null)
{
throw new ArgumentException("one of parameters is null");
}
Stream stream = null;
using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isoStore.FileExists(filename))
{
stream = isoStore.OpenFile(filename, System.IO.FileMode.Open, FileAccess.Read);
}
}
return stream;
});
}
Then it can be used like this:
public async Task<BitmapSource> FetchImage()
{
BitmapImage image = null;
using (var imageStream = await LoadImageAsync(doc.ImagePath))
{
if (imageStream != null)
{
image = new BitmapImage();
image.SetSource(imageStream);
}
}
return image;
}
And finally you just assign return value of FetchImage() method to some of your view model's property, to which the UI element is bound. Of course, your view model should properly implement INotifyPropertyChanged interface for this approach to work reliably.
If you want to use attached properties approach, here's how you do it:
public class IsoStoreImageSource : DependencyObject
{
public static void SetIsoStoreFileName(UIElement element, string value)
{
element.SetValue(IsoStoreFileNameProperty, value);
}
public static string GetIsoStoreFileName(UIElement element)
{
return (string)element.GetValue(IsoStoreFileNameProperty);
}
// Using a DependencyProperty as the backing store for IsoStoreFileName. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsoStoreFileNameProperty =
DependencyProperty.RegisterAttached("IsoStoreFileName", typeof(string), typeof(IsoStoreImageSource), new PropertyMetadata("", Changed));
private static void Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Image img = d as Image;
if (img != null)
{
var path = e.NewValue as string;
SynchronizationContext uiThread = SynchronizationContext.Current;
Task.Factory.StartNew(() =>
{
using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isoStore.FileExists(path))
{
var stream = isoStore.OpenFile(path, System.IO.FileMode.Open, FileAccess.Read);
uiThread.Post(_ =>
{
var _img = new BitmapImage();
_img.SetSource(stream);
img.Source = _img;
}, null);
}
}
});
}
}
}
And then in XAML:
<Image local:IsoStoreImageSource.IsoStoreFileName="{Binding Path}" />
Some limitations of this approach:
It only works on Image control, though you can change this to a whichever type you want. It's just not very generic.
Performance-wise, it will use a thread from the threadpool every time image source is changed. It's the only way to do asynchronous read from isolated storage on Windows Phone 8 right now. And you definitely don't want to do this synchronously.
But it has one one important advantage:
It works! :)
I like the above approach but there is a simpler more hacky way of doing it if you are interested.
You can go into your xaml and bind the image source to an string property then put the file path into the property dynamically.
<!-- XAML CODE -->
<Image Source="{Binding imagePath}"/>
//Behind property
public String imagePath { get; set; }
load your path into the image path then bind the image source to the image path string. You might have to do an INotifyPropertyChanged but this method should work with proper binding.

Have embedded images open with FancyBox, not in new window

This section of code in my /js/global.js file activate each image to open in a new window when clicked. Is it possible to alter this code to have each open in a FancyBox instead? I have downloaded a FancyBox plugin for a Vanilla forum I am running, and it currently only targets images embedded in posts After You Click On The Post Itself. On the main page, clicking on an image opens a new window.
// Shrink large images to fit into message space, and pop into new window when clicked.
// This needs to happen in onload because otherwise the image sizes are not yet known.
jQuery(window).load(function() {
var props = ['Width', 'Height'], prop;
while (prop = props.pop()) {
(function (natural, prop) {
jQuery.fn[natural] = (natural in new Image()) ?
function () {
return this[0][natural];
} :
function () {
var
node = this[0],
img,
value;
if (node.tagName.toLowerCase() === 'img') {
img = new Image();
img.src = node.src,
value = img[prop];
}
return value;
};
}('natural' + prop, prop.toLowerCase()));
}
jQuery('div.Message img').each(function(i,img) {
var img = jQuery(img);
var container = img.closest('div.Message');
if (img.naturalWidth() > container.width() && container.width() > 0) {
img.wrap('');
}
});
// Let the world know we're done here
jQuery(window).trigger('ImagesResized');
});
Add an specific class to your wrapped images, modifying this line
img.wrap('');
... into this :
img.wrap('<a class="fancybox" href="'+$(img).attr('src')+'"></a>');
Then bind fancybox to that selector (".fancybox") in a custom script like :
$(".fancybox").fancybox();
This assumes that you have properly loaded the fancybox js and css files.

Is it possible to rename Outlook Category programmatically?

We are developing Outlook 2007 add-in. For testing outlook category renaming I've added the following code block
var session = Application.Session;
var categories = session.Categories;
var category1 = session.Categories[1];
//catefory1.Name is "Group1" before executing line below
category1.Name = "TEST!!!";
Marshal.ReleaseComObject(category1);
Marshal.ReleaseComObject(categories);
Marshal.ReleaseComObject(session);
to the end of add-in private void ThisAddIn_Startup(object sender, EventArgs e) method.
Category is renamed but if Outlook is closed, the above lines are commented, and outlook is started again - the category name is not "TEST!!!" as I expected. It is "Group1" as is was before renaming. Is it possible to rename outlook category "forever" by code? Microsoft.Office.Interop.Outlook.Category has no Save() or Update() or Persist() methods.
P.S. We are developing Outlook 2007 add-in using Visual Studio 2008, .net 3.5, C# 3.
The problem is reproduced with Outlook 2007 SP1 and SP2. Other outlook versions were not tested.
I have solved the problem (the problem itself seems to be Outlook 2007 bug) using a hack.
The following links helped me to create the hack (oops, not enough reputation to post more then 1 link):
http ://blogs.officezealot.com/legault/archive/2009/08/13/21577.aspx
http://www.officekb.com/Uwe/Forum.aspx/outlook-prog-addins/3142/Apply-Categories-to-other-users-Outlook-2007
http ://help.wugnet.com/office/set-master-category-list-Outlook-2007-ftopict1095935.html
http ://forums.slipstick.com/showthread.php?t=18189
http ://msdn.microsoft.com/en-us/library/ee203806%28EXCHG.80%29.aspx
The hack itself is show below:
using System;
using System.Text;
using System.Xml;
using System.IO;
using Microsoft.Office.Interop.Outlook;
namespace OutlookHack
{
public static class OutlookCategoryHelper
{
private const string CategoryListStorageItemIdentifier = "IPM.Configuration.CategoryList";
private const string CategoryListPropertySchemaName = #"http://schemas.microsoft.com/mapi/proptag/0x7C080102";
private const string CategoriesXmlElementNamespace = "CategoryList.xsd";
private const string XmlNamespaceAttribute = "xmlns";
private const string CategoryElement = "category";
private const string NameAttribute = "name";
public static void RenameCategory(string oldName, string newName, Application outlookApplication)
{
MAPIFolder calendarFolder = outlookApplication.Session.GetDefaultFolder(
OlDefaultFolders.olFolderCalendar);
StorageItem categoryListStorageItem = calendarFolder.GetStorage(
CategoryListStorageItemIdentifier, OlStorageIdentifierType.olIdentifyByMessageClass);
if (categoryListStorageItem != null)
{
PropertyAccessor categoryListPropertyAccessor = categoryListStorageItem.PropertyAccessor;
string schemaName = CategoryListPropertySchemaName;
try
{
// next statement raises Out of Memory error if property is too big
var xmlBytes = (byte[])categoryListPropertyAccessor.GetProperty(schemaName);
// the byte array has to be translated into a string and then the XML has to be parsed
var xmlReader = XmlReader.Create(new StringReader(Encoding.UTF8.GetString(xmlBytes)));
// xmlWriter will write new category list xml with renamed category
XmlWriterSettings settings = new XmlWriterSettings { Indent = true, IndentChars = ("\t") };
var stringWriter = new StringWriter();
var xmlWriter = XmlWriter.Create(stringWriter, settings);
xmlReader.Read(); // read xml declaration
xmlWriter.WriteNode(xmlReader, true);
xmlReader.Read(); // read categories
xmlWriter.WriteStartElement(xmlReader.Name, CategoriesXmlElementNamespace);
while (xmlReader.MoveToNextAttribute())
{
if (xmlReader.Name != XmlNamespaceAttribute) // skip namespace attr
{
xmlWriter.WriteAttributeString(xmlReader.Name, xmlReader.Value);
}
}
while (xmlReader.Read())
{
switch (xmlReader.NodeType)
{
case XmlNodeType.Element: // read category
xmlWriter.WriteStartElement(CategoryElement);
while (xmlReader.MoveToNextAttribute())
{
if ((xmlReader.Name == NameAttribute) && (xmlReader.Value == oldName))
{
xmlWriter.WriteAttributeString(NameAttribute, newName);
}
else
{
xmlWriter.WriteAttributeString(xmlReader.Name, xmlReader.Value);
}
}
xmlWriter.WriteEndElement();
break;
case XmlNodeType.EndElement: // categories ended
xmlWriter.WriteEndElement();
break;
}
}
xmlReader.Close();
xmlWriter.Close();
xmlBytes = Encoding.UTF8.GetBytes(stringWriter.ToString());
categoryListPropertyAccessor.SetProperty(schemaName, xmlBytes);
categoryListStorageItem.Save();
}
catch (OutOfMemoryException)
{
// if error is "out of memory error" then the XML blob was too big
}
}
}
}
}
This helper method must be called prior to category renaming, e.g.:
var session = Application.Session;
var categories = session.Categories;
var category1 = session.Categories[1];
//catefory1.Name is "Group1" before executing line below
OutlookCategoryHelper.RenameCategory(category1.Name, "TEST!!!", Application);
category1.Name = "TEST!!!";
Marshal.ReleaseComObject(category1);
Marshal.ReleaseComObject(categories);
Marshal.ReleaseComObject(session);
This is an Outlook bug introduces with Outlook 2007 SP2.
"Consider the following scenario. You have a custom application that can be run to create new categories in Outlook 2007.
You run the application to create a new category in Outlook 2007. Then, if you restart Outlook 2007, the category that you created is removed unexpectedly. This problem occurs after you install the Februarycumulative update or SP2."
There is a hotfix available since June 30, 2009:
http://support.microsoft.com/default.aspx/kb/970944/en
Regards,
Tim

Resources