Find and use already embedded font? - itext7

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.

Related

Not able to set Redaction Color in iText7 (C#)

I'm not able to change PDF redaction's color in iText7 + PDFSweep using the C# code below. The RED redaction box takes effect only on the first page of the PDF file, then on subsequent pages the color of the redaction box reverts back to BLACK
String input = SRC_FOLDER + "/report.pdf";
String output = SRC_FOLDER + "/report_redacted.pdf";
CompositeCleanupStrategy strategy = new CompositeCleanupStrategy();
strategy.Add(new RegexBasedCleanupStrategy(#"(\d\d\d\d)").SetRedactionColor(ColorConstants.RED));
PdfDocument pdf = new PdfDocument(new PdfReader(input), new PdfWriter(output));
PdfAutoSweep autoSweep = new PdfAutoSweep(strategy);
autoSweep.CleanUp(pdf);
pdf.Close();
It's a bug in pdfSweep.
ìText handles documents on a page by page basis.
In order to be able to re-use the same strategy on different pages, every ICleanupStrategy needs to provide a reset method.
The current implementation for that reset method for RegexBasedCleanupStragegy is
public ICleanupStrategy reset() {
return new RegexBasedCleanupStrategy(this.pattern);
}
Which copies the strategy's pattern, but not its color. As a result, on every page but the first one, the color will default back to black.
To fix this, simply create your own implementation that overrides this behavior to also copy the color.
I will report this as a bug (iText developer here)
for the sake of completion, this would be the fixed approach:
public class RegexBasedCleanupStrategy extends
RegexBasedLocationExtractionStrategy implements ICleanupStrategy {
private Pattern pattern;
private Color redactionColor = ColorConstants.BLACK;
public RegexBasedCleanupStrategy(String regex) {
super(regex);
this.pattern = Pattern.compile(regex);
}
public RegexBasedCleanupStrategy(Pattern pattern) {
super(pattern);
this.pattern = pattern;
}
#Override
public Color getRedactionColor(IPdfTextLocation location) {
return redactionColor;
}
public RegexBasedCleanupStrategy setRedactionColor(Color color) {
this.redactionColor = color;
return this;
}
public ICleanupStrategy reset() {
RegexBasedCleanupStrategy copy = new RegexBasedCleanupStrategy(pattern);
copy.redactionColor = redactionColor;
return copy;
}
}

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.

iTextSharp SetListSymbol does not work

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);
}
}

Adding custom metadata tags using LibTiff.Net

I now how to add a custom tag to an image but it's not showing up as the tag name in image viewer. I only see the number I assigned and its value.
Why there is no proper name for my custom tag?
using BitMiracle.LibTiff.Classic;
namespace WindowsFormsApplication1
{
class Program
{
private const TiffTag IMG_GUID = (TiffTag)666;
private static Tiff.TiffExtendProc m_parentExtender;
public static void TagExtender(Tiff tif)
{
TiffFieldInfo[] tiffFieldInfo =
{
new TiffFieldInfo(IMG_GUID, -1, -1, TiffType.ASCII, FieldBit.Custom, true, false, "IMG_GUID"),
};
tif.MergeFieldInfo(tiffFieldInfo, tiffFieldInfo.Length);
if (m_parentExtender != null)
m_parentExtender(tif);
}
static void Main(string[] args)
{
// Register the extender callback
// It's a good idea to keep track of the previous tag extender (if any) so that we can call it
// from our extender allowing a chain of customizations to take effect.
m_parentExtender = Tiff.SetTagExtender(TagExtender);
byte[] buffer = new byte[25 * 144];
string outputFileName = writeTiffWithCustomTags(buffer);
// restore previous tag extender
Tiff.SetTagExtender(m_parentExtender);
}
private static string writeTiffWithCustomTags(byte[] buffer)
{
string existingTiffName = "..\\..\\tifimages\\cramps.tif";
string outputFileName = existingTiffName;
using (Tiff image = Tiff.Open(outputFileName, "a"))
{
// set custom tags
image.SetDirectory(0);
string value = "test";
image.SetField(IMG_GUID, value);
image.CheckpointDirectory();
// Write the information to the file
image.WriteEncodedStrip(0, buffer, 25 * 144);
}
return outputFileName;
}
}
}
The application you use for viewing your TIFFs should know about your custom tags beforehand in order to be able to display its names.
It's not gonna happen! (Because you may select almost arbitrary integer for your custom tag).
So, there is nothing wrong with custom tags being displayed as (an integer, a value) pair. It's just the way custom tag work.

Resources