unable to create document border using itext 7 - itext7

Using the code below I try to set a border around my document, but nothing shows up:
PdfWriter pdfWriter = new PdfWriter(toFile);
PdfDocument pdfDoc = new PdfDocument(pdfWriter); //Initialize PDF document
var pageSize = PageSize.LETTER;
var document = new iText.Layout.Document(pdfDoc, pageSize); // Initialize document
var fontTimesRoman = PdfFontFactory.CreateFont(iText.IO.Font.Constants.StandardFonts.TIMES_ROMAN);
var fontTimesRomanBold = PdfFontFactory.CreateFont(iText.IO.Font.Constants.StandardFonts.TIMES_BOLD);
var navy = new DeviceRgb(0, 0, 128);
var red = new DeviceRgb(139, 0, 0);
document.SetBorder(new SolidBorder(red, 18));
document.Add( new Paragraph(DateTime.Now.ToString("MMMM dd, yyyy"))
.SetFont(fontTimesRoman)
.SetTextAlignment(iText.Layout.Properties.TextAlignment.RIGHT)
//.SetHorizontalAlignment(iText.Layout.Properties.HorizontalAlignment.RIGHT)
.SetFontSize(11)
);

This won't work since the document is the size of the page and the Border will be outside of the page.
If you are trying to add a border to the edges of a Document you would need to do it in a PdfCanvas object
pdfDoc.AddNewPage();
var canvas = new PdfCanvas(pdfDoc, 1);
canvas.SetStokeColor(red);
canvas.SetLineWidth(18f);
canvas.Rectangle(0,1,pageSize.GetWidth()-1,pageSize.GetHeight()-1);
canvas.Stroke();
also don't forget to run
pdfDoc.Close();

Related

Saving a polygon object into a bitmapimage in WIndows phone 8.1

I am working on a windows phone 8.1 app where I need to convert a polygon object to an image and ultimately save it as a png file. Till now, I created a polygon object with various properties. Now I'm clueless about the other parts.
pol.Opacity = 0.5;
System.Windows.Point Point1 = new System.Windows.Point(10, 200);
System.Windows.Point Point2 = new System.Windows.Point(60, 140);
System.Windows.Point Point3 = new System.Windows.Point(130, 140);
System.Windows.Point Point4 = new System.Windows.Point(180, 200);
System.Windows.Point Point5 = new System.Windows.Point(130, 260);
System.Windows.Point Point6 = new System.Windows.Point(60, 260);
PointCollection myPointCollection = new PointCollection();
myPointCollection.Add(Point1);
myPointCollection.Add(Point2);
myPointCollection.Add(Point3);
myPointCollection.Add(Point4);
myPointCollection.Add(Point5);
myPointCollection.Add(Point6);
pol.Points = myPointCollection;
var imageBrush = new ImageBrush();
imageBrush.ImageSource = image.Source;
pol.Fill = imageBrush;
pol.Height = image.Height;
pol.MaxHeight = image.Height;
pol.MaxWidth = image.Width;
pol.Width = image.Width;
pol.Stroke = new SolidColorBrush(Colors.Red);
pol.StrokeThickness = 2;
pol.Margin = image.Margin;
You can use the WritableBitmap class in order to achieve this. I have a similar post on Silverlight, which you can refer: How to Crop an Image based on a Shape or Path control?. Hope that helps, at least gives some basic concepts. Let me know, if you need further help on this.
To save the shape as PNG, you can utilize the following code snippet:
WriteableBitmap bmp = GetAsWritableBitmap();
using (var mediaLibrary = new MediaLibrary())
{
using (var stream = new MemoryStream())
{
var fileName = string.Format("Gs{0}.jpg", Guid.NewGuid());
bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
stream.Seek(0, SeekOrigin.Begin);
var picture = mediaLibrary.SavePicture(fileName, stream);
if (picture.Name.Contains(fileName)) return true;
}
}
Hope that helps.

C# Winforms how to resize image and save

hi I want to open a image file with openfiledialog and resize to upload to database so please help me with this code which I got from some where but I don't know how to make it working thanks
private void ResizeImg(double scaleFactor, Stream sourcePath, string tragetPath)
{
using (var image = System.Drawing.Image.FromStream(sourcePath))
{
var newWidth = (int)(image.Width * scaleFactor);
var newHeight = (int)(image.Height * scaleFactor);
var resizingImg = new Bitmap(newWidth, newHeight);
var resizeGraph = Graphics.FromImage(resizingImg);
resizeGraph.CompositingQuality = CompositingQuality.HighQuality;
resizeGraph.SmoothingMode = SmoothingMode.HighQuality;
resizeGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
resizeGraph.DrawImage(image, imageRectangle);
resizingImg.Save(targetPath, image.RawFormat);
}
}
Without specifying source and target rectangle, it doesn't stretch.
Change
resizeGraph.DrawImage(image, imageRectangle);
to
var srcRectangle = new Rectangle(0, 0, image.Width, image.Height);
resizeGraph.DrawImage(image, imageRectangle,srcRectangle, GraphicsUnit.Pixel);
BTW: Consider ImageFormat.Jpeg or .Png for Save, because you cannot write all image formats you can read.

Custom ShellTile

Is there a way to create a custom ShellTile that displays at start (after user pins our app)? I would like to resize and reposition a count number (label), but I can't find a way to do that.
You can't change the position or style of title or counter, but you can generate an image to display on the tile, for example:
WriteableBitmap bitmap = new WriteableBitmap(173, 173);
TextBlock textBlock = new TextBlock();
textBlock.TextWrapping = TextWrapping.Wrap;
textBlock.Foreground = new SolidColorBrush(Colors.White);
textBlock.FontSize = 22.0;
textBlock.Margin = new Thickness(12.0, 8.0, 8.0, 45.0);
textBlock.Text = "Lorem ipsum";
textBlock.HorizontalAlignment = HorizontalAlignment.Stretch;
textBlock.VerticalAlignment = VerticalAlignment.Stretch;
Grid layoutRoot = new Grid();
layoutRoot.Background = (Brush)App.Current.Resources["PhoneAccentBrush"];
layoutRoot.Width = 173.0;
layoutRoot.Height = 173.0;
layoutRoot.Children.Add(textBlock);
layoutRoot.Measure(new Size(173, 173));
layoutRoot.Arrange(new Rect(0, 0, 173, 173));
layoutRoot.UpdateLayout();
bitmap.Render(layoutRoot, null);
bitmap.Invalidate();
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
string fileName = "/Shared/ShellContent/BackgroundImage.jpg";
using (Stream fileStream = storage.CreateFile(fileName))
{
bitmap.SaveJpeg(fileStream, 173, 173, 0, 100);
}
}
StandardTileData tileData = new StandardTileData
{
BackgroundImage = new Uri("isostore:"/Shared/ShellContent/BackgroundImage.jpg, UriKind.Absolute),
Title = "Lorem Ipsum,
};
ShellTile.Create(new Uri("/MainPage.xaml", UriKind.Relative), tileData);

How to set WriteableBitmap back color for a live tile in Mango?

I'm trying to dynamically build a live tile.
It runs fine thanks to some SO suggestions and I have this code:
WriteableBitmap wbmp = new WriteableBitmap(173, 173);
TextBlock text = new TextBlock() { FontSize = (double)Resources["PhoneFontSizeLarge"], Foreground = new SolidColorBrush(Colors.White) };
text.Text = "my text";
wbmp.Render(text, new TranslateTransform() { Y = 20 });
wbmp.Invalidate();
// save image to isolated storage
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream imageStream = new IsolatedStorageFileStream("/Shared/ShellContent/MyImage.jpg", System.IO.FileMode.Create, isf))
{
wbmp.SaveJpeg(imageStream, wbmp.PixelWidth, wbmp.PixelHeight, 0, 100);
}
}
The problem is that the tile has a black (or, better, transparent) background. I would like to use accent background color, how can I do it?
Solved in this way:
Canvas can = new Canvas();
can.Background = (SolidColorBrush)Application.Current.Resources["PhoneAccentBrush"];
can.Width = 173;
can.Height = 173;
wbmp.Render(can, null);
You're better to use Resources["TransparentBrush"] as the background, and then save to png, otherwise, your tile will be the wrong color on a theme change.

How add a background picture for a DIV, Google maps Custom Controls

I am trying to create a image for google's custom controls. When I assign a background picture in a css, it breaks, otherwise it works.
var latlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var myLocationControlDiv = document.createElement('DIV');
var controlUI = document.createElement('DIV');
//controlUI.style.borderWidth = '20px';
//controlUI.style.backgroundColor = 'black';
//controlUI.style.borderStyle = 'solid';
//controlUI.style.cursor = 'pointer';
//controlUI.style.backgroundImage = "url(/img/icons/pin.png)";
controlUI.title = 'Click to set the map to Home';
myLocationControlDiv.appendChild(controlUI);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(controlUI);
You can also see it here: http://jsfiddle.net/k3Jjw/
Two simple problems:
You need an explicit height and width on your control - background images don't cause the DIV to grow to fit them.
The image URL you used does not exist (I get 404).
If you use the following code, you should see an image control in the top left:
var controlUI = document.createElement('DIV');
controlUI.style.cursor = 'pointer';
controlUI.style.backgroundImage = "url(http://dummyimage.com/100x100)";
controlUI.style.height = '100px';
controlUI.style.width = '100px';
controlUI.title = 'Click to set the map to Home';
myLocationControlDiv.appendChild(controlUI);
Great work posting the JSFiddle link - it made this question easy to answer.

Resources