Adding custom metadata tags using LibTiff.Net - libtiff

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.

Related

SWT: Hyperlink in TableViewerColumn with MouseListener and correct column width

I want to add a Hyperlink (or a text that is styled like a Hyperlink) in a TableViewerColumn Cell.
I tried this and this.
Summarize: I either get a clickable Hyperlink for which the text is cropped (the column width is too small) or I get a link that is not clickable (MouseListener missing, no cursor).
Both tries are making use of a StyledCellLabelProvider for the TableViewerColumn. The first try does this:
TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
column .getColumn().setText(TITLE);
column .setLabelProvider(new MyHyperlinkLabelProvider());
However, the text in the cell is cropped and I have no idea how to set the column width so that the text fits in the cell. I tried with using pack(), but it had no effect.
private final class MyHyperlinkLabelProvider extends StyledCellLabelProvider {
private MyHyperlinkLabelProvider() {
}
#Override
public void update(ViewerCell cell) {
TableItem item = (TableItem)cell.getItem();
String myText= "Hyperlink text, unfortunately cropped";
link = new MyHyperlink((Composite)cell.getViewerRow().getControl(), SWT.NONE);
toolkit.adapt(link);
link.setText(myText);
TableEditor editor = new TableEditor(item.getParent());
editor.grabHorizontal = true;
editor.grabVertical = true;
editor.setEditor(link, item, cell.getColumnIndex());
GridDataFactory.fillDefaults().applyTo(editor.getEditor());
editor.layout();
link.addMouseListener(new MouseAdapter() {
#Override
public void mouseUp(MouseEvent event) {
super.mouseUp(event);
if (event.getSource() instanceof MyHyperlink) {
MyHyperlink link = (MyHyperlink)event.getSource();
System.out.println("Label was clicked: " + link.getText());
}
}
});
super.update(cell);
}
}
private class MyHyperlink extends Hyperlink {
public MyHyperlink(Composite parent, int style) {
super(parent, style);
this.setUnderlined(true);
}
}
The less preferred alternative is given in the second link. If I cannot get the column width correct, I'd go with this.
The second link suggests to try it with StyledString instead of Hyperlink. The StyledString at least is shown in full width and the column has the correct width. However, you cannot add a MouseListener to a StyledString. Only on the table, but that doesn't help.
private final class MyHyperlinkLabelProvider extends StyledCellLabelProvider {
private MyHyperlinkLabelProvider() {
}
#Override
public void update(ViewerCell cell) {
TableItem item = (TableItem)cell.getItem();
String mytext= "This is the hyperlink text";
/* make text look like a link */
StyledString text = new StyledString();
StyleRange myStyledRange =
new StyleRange(0, phase.length(), Display.getCurrent().getSystemColor(SWT.COLOR_BLUE), null);
myStyledRange.underline = true;
text.append(mytext, StyledString.DECORATIONS_STYLER);
cell.setText(text.toString());
StyleRange[] range = {myStyledRange };
cell.setStyleRanges(range);
super.update(cell);
}
}
To have the column size computed correctly, the cell text has to be set. Although the Hyperlink is what should be displayed link.setText(myText); is not enough. cell.setText(myText) is needed, too.
One can see it in the second snippet, for which the column size is correct. It is because of the cell.setText() call.

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.

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

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.

Javafx textfield resize to text length?

Hello guys I am building a chat server where I use a textfield on the screen to type in the chat message that the user writes, the idea is that it works like a bubble over a persons head when he types a message.
my question is in order to not make a textbox that is too large or too small is there a way to make the textbox resize (trim if you will) so it adjust to the text written in the textfield?
P.S. I'm using JavaFx scenebuilder to do all of this.
You can use computeTextWidth method in the com.sun.javafx.scene.control.skin.Utils. the method is used in javafx.scene.control.Label class to calculate the minimum width for label content.
I solved my problem as below:
field.textProperty().addListener(new ChangeListener<String>() {
#Override
public void changed(ObservableValue<? extends String> ob, String o,
String n) {
// expand the textfield
field.setPrefWidth(TextUtils.computeTextWidth(field.getFont(),
field.getText(), 0.0D) + 10);
}
});
I have added a listener to textProperty, and with every text change i change the prefWidth of textfield.
Note: as long as the Utils.computeTextWidth() is not public, I have copied the source code to a new class (TextUtils).
Here is the full source code:
package me.jone30rw.fxcontrol;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextBoundsType;
public class TextUtils {
static final Text helper;
static final double DEFAULT_WRAPPING_WIDTH;
static final double DEFAULT_LINE_SPACING;
static final String DEFAULT_TEXT;
static final TextBoundsType DEFAULT_BOUNDS_TYPE;
static {
helper = new Text();
DEFAULT_WRAPPING_WIDTH = helper.getWrappingWidth();
DEFAULT_LINE_SPACING = helper.getLineSpacing();
DEFAULT_TEXT = helper.getText();
DEFAULT_BOUNDS_TYPE = helper.getBoundsType();
}
public static double computeTextWidth(Font font, String text, double help0) {
// Toolkit.getToolkit().getFontLoader().computeStringWidth(field.getText(),
// field.getFont());
helper.setText(text);
helper.setFont(font);
helper.setWrappingWidth(0.0D);
helper.setLineSpacing(0.0D);
double d = Math.min(helper.prefWidth(-1.0D), help0);
helper.setWrappingWidth((int) Math.ceil(d));
d = Math.ceil(helper.getLayoutBounds().getWidth());
helper.setWrappingWidth(DEFAULT_WRAPPING_WIDTH);
helper.setLineSpacing(DEFAULT_LINE_SPACING);
helper.setText(DEFAULT_TEXT);
return d;
}
}
In JavaFX 8, there is a solution for that, here is the code:
TextField tf = new TextField();
// Set Max and Min Width to PREF_SIZE so that the TextField is always PREF
tf.setMinWidth(Region.USE_PREF_SIZE);
tf.setMaxWidth(Region.USE_PREF_SIZE);
tf.textProperty().addListener((ov, prevText, currText) -> {
// Do this in a Platform.runLater because of Textfield has no padding at first time and so on
Platform.runLater(() -> {
Text text = new Text(currText);
text.setFont(tf.getFont()); // Set the same font, so the size is the same
double width = text.getLayoutBounds().getWidth() // This big is the Text in the TextField
+ tf.getPadding().getLeft() + tf.getPadding().getRight() // Add the padding of the TextField
+ 2d; // Add some spacing
tf.setPrefWidth(width); // Set the width
tf.positionCaret(tf.getCaretPosition()); // If you remove this line, it flashes a little bit
});
});
tf.setText("Hello World!");
In JavaFX 2.2 this code works with little limitations. You can't set the Font(so if you do not use the std-font, you must set it manually).
You can't get the padding from a TextField(so if you know the padding, write it hardcoded).
Happy Coding,
Kalasch
Since JavaFX 8, this is by far the simplest:
textField.prefColumnCountProperty().bind(textField.textProperty().length());
It is time to do some coding behind the scenes(builder) :).
The following code chunk is not a neat solution but better than none. :)
// define width limits
textField.setMinWidth(50);
textField.setPrefWidth(50);
textField.setMaxWidth(400);
// add listner
textField.textProperty().addListener(new ChangeListener<String>() {
#Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
textField.setPrefWidth(textField.getText().length() * 7); // why 7? Totally trial number.
}
});
No font dependent magic required if you use setPrefColumnCount
tf.textProperty().addListener(new ChangeListener<String>() {
#Override
public void changed(ObservableValue<? extends String> ob, String o, String n) {
tf.setPrefColumnCount(tf.getText().length() +1);
}
});
The best / easiest way to do this is to use JavaFX's "USE_COMPUTED_SIZE" option. You can either define it in the FXML, or programatically like this:
TextField textField = new TextField("hello");
textField.setPrefWidth(Control.USE_COMPUTED_SIZE);

Resources