How to give the option to change the Textbloack foreground-color,size in Windowsphone7 - silverlight-4.0

I am completely new in Windowsphone7.i have develop sample application in that i want give the option to change the font-color,Size,style(Italic/Bold) as Dynamically(like RadEditor) .please help me how to resolve this option.

If you Develop your Application MVVM style then it is not so hard to do this. You just need a property for every setting you want to set dynamically and then bind to this properties. And you create a Settings View where you can set the properties and if you change them you use INotifyPropertyChanged to broadcast that your properties value changed and so every control which is bound to that property will change and redraw.
GalaSoft MVVM
MVVM Codeplex
Easy MVVM sample Application for Windows Phone 7
The link you found to save an image looks ok, but i did it a bit different, actually from the CameraCaptureTask you already get a WritableBitmap Image and you can save it like this.
To save the Image:
private void SaveToIsolatedStorage(WriteableBitmap image, string fileName)
{
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (myIsolatedStorage.FileExists(fileName))
{
myIsolatedStorage.DeleteFile(fileName);
}
using (var stream = myIsolatedStorage.OpenFile(fileName, FileMode.Create))
{
Extensions.SaveJpeg(image, stream, image.PixelWidth, image.PixelHeight,0, 100);
}
}
}
To read the Image:
private WritableBitmap ReadFromIsolatedStorage(string fileName)
{
WriteableBitmap bitmap = new WriteableBitmap(200,200);
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (store.FileExists(fileName))
{
using (var stream = store.OpenFile(fileName,FileMode.Open))
{
bitmap.SetSource(stream);
}}
}
return bitmap;
}
I hope this will work because i wrote it from scratch. :)
And in your ViewModel you should have a WritableBitmap Property which is bound to your Image Control on your View.
To use a lot of images and work with them you should read a bit more about this, because somehow SL Images use a lot of memory so you will need to address this problem somehow in the future.

Related

Adding text items to an Existing PDF w/ Telerik DocumentProcessing Library

I want to open an existing PDF document and add different annotations to it. Namely bookmarks and some text
I am using the Telerik Document Processing Library (dpl) v2019.3.1021.40
I am new to dpl , but I believe the RadFlowDocument is the way to go.
I am having troubles creating the RadFlowDocument
FlowProvider.PdfFormatProvider provider = new FlowProvider.PdfFormatProvider();
using (Stream stream = File.OpenRead(sourceFile))
{
--> RadFlowDocument flowDoc = provider.Import(stream);
}
The line indicated w/ the arrow give the error "Import Not Supported"
There is a telerik blog post here
https://www.telerik.com/forums/radflowdocument-to-pdf-error
It seems relevant, but not 100% sure.
It cautions to be sure the providers are mated correctly, I believe they are in my example....
Again, ultimate goal is to open a PDF and add some stuff to it. I think the RadFlowDocument is the right direction. If there is a better solution, Im happy to hear that too.
I figured it out. The DPL is pretty good, but doc is still growing, hope this helps someone out...
This draws from a myriad of articles, I cant begin to cite them all.
There are 2 notions for working w/ PDFs in the DPL.
FixedDocument takes pages. I think this is meant for sewing docs together.
FlowDocument I believe lays things out like an HTML renderer would.
I am using Fixed, mainly b/c I can get that to work.
using System;
using System.IO;
using System.Windows; //nec for Size struct
using System.Diagnostics; //nec for launching the pdf at the end
using Telerik.Windows.Documents.Fixed.Model;
//if you have fixed and flow provider, you have to specify, so I make a shortcut
using FixedProvider = Telerik.Windows.Documents.Fixed.FormatProviders.Pdf;
using Telerik.Windows.Documents.Fixed.Model.Editing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace DocAggregator
{
[TestClass]
public class UnitTest2
{
[TestMethod]
public void EditNewFIle_SrcAsFixed_TrgAsFixed()
{
String dt = #"C:\USERS\greg\DESKTOP\DPL\";
String sourceFile = dt + "output.pdf";
//Open the sourceDoc so you can add stuff to it
RadFixedDocument sourceDoc;
//a provider parses the actual file into the model.
FixedProvider.PdfFormatProvider fixedProv = new FixedProvider.PdfFormatProvider();
using (Stream stream = File.OpenRead(sourceFile))
{
//'populate' the doc object from the file
//using the FLOW classes, I get "Import Not Supported".
sourceDoc = fixedProv.Import(stream);
}
int pages = sourceDoc.Pages.Count;
int pageCounter = 1;
int xoffset = 150;
int yoffset = 50;
//editor is the thing that lets you add elements into the source doc
//Like the provider, the Editor needs to match the document class (Fixed or Flow)
RadFixedDocumentEditor editor = new RadFixedDocumentEditor(sourceDoc);
foreach (RadFixedPage page in sourceDoc.Pages)
{
FixedContentEditor pEd = new FixedContentEditor(page);
Size ps = page.Size;
pEd.Position.Translate(ps.Width - xoffset, ps.Height - yoffset);
Block block = new Block();
block.HorizontalAlignment = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.HorizontalAlignment.Center;
block.TextProperties.FontSize = 22;
block.InsertText(string.Format("Page {0} of {1} ", pageCounter, pages));
pEd.DrawBlock(block);
pageCounter++;
}
string exportFileName = "addedPageNums.pdf";
if (File.Exists(exportFileName))
{
File.Delete(exportFileName);
}
File.WriteAllBytes(exportFileName, fixedProv.Export(sourceDoc));
//launch the app
Process.Start(exportFileName);
}
}
}

Get Contents Of Image Class From AutomationElement

I am walking controls in a third party app that contains images. The automation element returns the class name Image, Any ideas on how I can get the contents of that Image as a bitmap object, or even bytes?
Although this question is old, I wanted to add an answer as I came across this problem as well.
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Automation;
namespace AutomationElementExtension
{
public static class AutomationElementExtension
{
public static Bitmap ToBitmap(this AutomationElement automationElement)
{
var boundingRectangle = automationElement.Current.BoundingRectangle;
var bitmap = new Bitmap(Convert.ToInt32(boundingRectangle.Width), Convert.ToInt32(boundingRectangle.Height), PixelFormat.Format32bppArgb);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.CopyFromScreen(Convert.ToInt32(boundingRectangle.X), Convert.ToInt32(boundingRectangle.Y), Point.Empty.X, Point.Empty.Y, bitmap.Size);
}
return bitmap;
}
}
}
You can then get the image as a bitmap by calling it as
var bitmap = myAutomationElement.ToBitmap();
This discussion can be useful: Capturing an image behind a rectangle .
Just use BoundingRectangle property of the AutomationElement to make a snapshot.

How to set background image in PdfPCell in iText?

I am currently using iText to generate PDF reports. I want to set a medium size image as a background in PdfPCell instead of using background color. Is this possible?
You can find an example on how to do this with iText 5.5.1 here. You need to create your own implementation of the PdfPCellEvent interface, for instance:
class ImageBackgroundEvent implements PdfPCellEvent {
protected Image image;
public ImageBackgroundEvent(Image image) {
this.image = image;
}
public void cellLayout(PdfPCell cell, Rectangle position,
PdfContentByte[] canvases) {
try {
PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS];
image.scaleAbsolute(position);
image.setAbsolutePosition(position.getLeft(), position.getBottom());
cb.addImage(image);
} catch (DocumentException e) {
throw new ExceptionConverter(e);
}
}
Then you need to create an instance of this event and declare it to the cell that needs this background:
Image image = Image.getInstance(IMG1);
cell.setCellEvent(new ImageBackgroundEvent(image));
This code was tested with the most recent version of iText and the result looks like this. You're using a version of iText with my name (Lowagie) in the package names (com.lowagie). This means that this sample may or may not work. We don't know and we won't test as the version you're using has been declared EOL years ago. It is no longer supported.

Loading Xaml via XamlReader only for Preview

just got some Problems with Loading Xaml Files by Runtime.
For your Information my Code-Snippet to Load the File as Content of a Usercontrol:
public UserControl LoadXaml(FileInfo paramFile)
{
FileInfo _XamlFile = paramFile;
UIElement rootElement;
FileStream s = new FileStream(_XamlFile.FullName, FileMode.Open);
rootElement = (UIElement)XamlReader.Load(s);
s.Close();
UserControl uc = new UserControl();
if (rootElement.GetType() == typeof(Window))
{
uc.Content = (rootElement as Window).Content;
}
else
{
uc = rootElement as UserControl;
}
return uc;
}
private void lstPDFDokumente_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var _XamlFile = ((System.Windows.Controls.ListBox)sender).SelectedItem as FileInfo;
if (_XamlFile != null)
{
layoutGrid.Children.Clear();
System.Windows.Controls.UserControl rootElement;
rootElement = XamlController.LoadXaml(_XamlFile);
layoutGrid.Children.Add(rootElement);
}
}
This works fine while Events and x:Class="..." are deleted by hand.
The Problems I try to solve are:
If there is a x:Class="..." at the root element the XamlReader throws the first exception.
When the XamlReader reaches a Control which contains an event, for Example Click or TextChanged, it throws another Exception.
What i try to figure out is how to load a XamlFile, show it inside of a Control in the main Window and to show some of the attributes like Name,Height,Width and so on.
Just read dozens of Websites but never found a topic about to make a preview or things like that.
One of the solutions i tried is to read the Xaml File as XML and delete that code.
The Problem was to get a list of all possible Events in C#.
If there are some Questions to that Code, feel free to ask :)
Greetings
Daniel

Problem with generating XPS documents with images in a WCF class library

I have a Silverlight application which uses a web service in order to create XPS documents. The document templates are created as XAML controls in a WCF class library.
public void GenerateXPS()
{
Type typeofControl = Type.GetType(DOCUMENT_GENERATOR_NAMESPACE + "." + ControlTypeName, true);
FrameworkElement control = (FrameworkElement)(Activator.CreateInstance(typeofControl));
control.DataContext = DataContext;
FixedDocument fixedDoc = new FixedDocument();
PageContent pageContent = new PageContent();
FixedPage fixedPage = new FixedPage();
//Create first page of document
fixedPage.Children.Add(control);
((IAddChild)pageContent).AddChild(fixedPage);
fixedDoc.Pages.Add(pageContent);
XpsDocument xpsd = new XpsDocument(OutputFilePath + "\\" + OutputFileName, FileAccess.ReadWrite);
System.Windows.Xps.XpsDocumentWriter xw = XpsDocument.CreateXpsDocumentWriter(xpsd);
xw.Write(fixedDoc);
xpsd.Close();
SaveToDocumentRepository();
}
In order to bind the actual data to my document template I set the DataContext property of the control. The problem is that when I look at my XPS, the images (I bind the Source of my Image controls to a string property that represents the URL of my image) are not displayed as if they were not loaded. How can I solve this problem? Thanks!
The binding infrastructure probably needs a push along because you are operating outside the intended use of WPF.
Try adding the following code after setting the datacontext:
control.DataContext = DataContext;
// we need to give the binding infrastructure a push as we
// are operating outside of the intended use of WPF
var dispatcher = Dispatcher.CurrentDispatcher;
dispatcher.Invoke(
DispatcherPriority.SystemIdle,
new DispatcherOperationCallback(delegate { return null; }),
null);
I cover this and other XPS related stuff in this blog post.