Add and display FixedDocument to WPF form - c#

I'm programmatically generating a FixedDocument to help me print. FixedDocument.ActualWidth is coming out as 0. I suspect this is because I am not actually displaying the FixedDocument. How can I add and display a FixedDocument object?
This is a beginner question. I'm not skilled with WPF. I looked on MSDN/Goog. Sites make the assumption that I've already added the FixedDocument and just need to manipulate it.
I have:
private FixedDocument CreateFixedDocumentWithPages()
{
FixedDocument fixedDocument = CreateFixedDocument();
fixedDocument.DocumentPaginator.PageSize = size;
PageContent content = AddContentFromImage();
fixedDocument.Pages.Add(content);
return fixedDocument;
}
Pseudocode of what I want: myWpfFormObject.AddChild(fixedDocument)

for show FixedDocument:
in your Wpf window, add the DocumentViewer Controle, then set the Document property.
for ActualWidth pb:
I think you should call the methods Measure & Arrange for each FixedPage.
See the code below from the exapmle in msdn:
Size sz = new Size(8.5 * 96, 11 * 96);
fixedPage.Measure(sz);
fixedPage.Arrange(new Rect(new Point(), sz));
fixedPage.UpdateLayout();
see also https://stackoverflow.com/a/1695518/1271037

So I had a slightly different situation, but this answer got me close. I'm using the fixed document to display Tiffs from a scanner. Some of those Tiffs can be in legal letter format (so longer than the standard A4 8.5 by 11 size). The code below fixed my issue and this answer helped.
So ended up I'm taking a fixed document, creating a page content, creating a fixed page, creating an image.
Then taking the image and adding it to the fixed page, then taking the fixed page and adding it to the page content, then taking the page content and adding it to the fixed document.
System.Windows.Documents.FixedPage fixedPage = new System.Windows.Documents.FixedPage();
System.Windows.Documents.PageContent pageContent = new System.Windows.Documents.PageContent();
pageContent.Child = fixedPage;
if (fixedDocument == null)
{
fixedDocument = new System.Windows.Documents.FixedDocument();
}
fixedDocument.Pages.Add(pageContent);
System.Windows.Controls.Image image = new System.Windows.Controls.Image();
TiffBitmapDecoder decoder = new TiffBitmapDecoder(new Uri(tiffImage, UriKind.Relative), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnDemand);
image.Source = decoder.Frames[0];
fixedPage.Children.Add(image);
//Code to make the legal letter size work.
Size sz = new Size(decoder.Frames[0].Width, decoder.Frames[0].Height);
fixedPage.Width = sz.Width;
fixedPage.Height = sz.Height;
pageContent.Width = sz.Width;
pageContent.Height = sz.Height;

Related

How to place images dynamically using Aspose.PDF in C#?

I have a C# script where I am using the Aspose.PDF library.
I am trying to place multiple images with a bit of a spacing vertically.
Here is what I am doing.
// Create pdf document
Aspose.Pdf.Document document = new Aspose.Pdf.Document();
Aspose.Pdf.Page page = document.Pages.Add();
Aspose.Pdf.Text.TextFragment text = new Aspose.Pdf.Text.TextFragment("Einstein Picture");
page.Paragraphs.Add(text);
Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(100, 600, 300, 800);
Aspose.Pdf.Rectangle rectangle1 = new Aspose.Pdf.Rectangle(100, 400, 300, 800);
page.AddImage("C:/Users/Alan/Desktop/image.gif", rectangle);
page.AddImage("C:/Users/Alan/Desktop/s.jpeg", rectangle1);
document.Save("C:/Users/Alan/Desktop/Testpdf.pdf", Aspose.Pdf.SaveFormat.Pdf);
How can I perfectly align pictures vertically with a bit of gap no matter how many pictures there are?
Currently the picture looks like this.
I request you to use below code snippet on your end and then share your kind feedback with us. This will enable you to place multiple images with a bit vertical spacing.
// Instantiate Document object
var pdf = new Aspose.Pdf.Document();
//Add a page to the document
var pdfImageSection = pdf.Pages.Add();
DirectoryInfo dir = new DirectoryInfo(#"D:\Aspose Files\images\");
FileInfo[] files = dir.GetFiles("*.jpg");
//Iterate through multiple images
foreach (var file in files)
{
FileStream stream = new FileStream(file.FullName, FileMode.Open);
System.Drawing.Image img = new System.Drawing.Bitmap(stream);
var image = new Aspose.Pdf.Image { ImageStream = stream };
//Set appearance properties
image.FixHeight = 300;
image.FixWidth = 300;
//Set margins for proper spacing and alignment
image.Margin = new MarginInfo(5, 5, 5, 5);
//Add the image to paragraphs of the document
pdfImageSection.Paragraphs.Add(image);
}
//Save resultant document
pdf.Save(#"D:\Aspose Files\Image2Pdf_out.pdf");
You were simply adding an image on a PDF page whereas this code snippet adds an image to Paragraphs collection and setting the margin property of image object fixes the alignment and spacing of the images.
Please let us know if you need any further assistance. We will be glad to help. For Aspose documentation to manipulate images is here.
I work with Aspose as Developer Evangelist.

How to get the HEIGHT of the Run or Paragraph

I found the Run or Paragraph in FlowDocument and now I need to know the HEIGHT of it.
i.e.
while (navigator.CompareTo(flowDocViewer.Document.ContentEnd) < 0)
{
TextPointerContext context = navigator.GetPointerContext(LogicalDirection.Backward);
Run run = navigator.Parent as Run;
// I need to get HEIGHT of Run in pixels somehow
Is it possible to do in fact?
Thank you!
A little function i am using. The input is a string containing a Section. You can easily render other blockelements like Paragraph.
You also can omit the second parameter of the Parse method.
The trick is not to measure the Paragraph, but the ViewBox which contains a RichTextBox. This is needed to actually render the Flowdocument. The ViewBox dynamically gets the size of the rtb. Maybe you even can do this without the ViewBox. I spent some time to figure this out and it works for me.
Note that Width of the RichTextBox is set to double.MaxValue. This means when you want to measure a single paragraph it has to be very long or everything is in one line. So this only makes sense when you know the Width of your output device. As this is a FlowDocument there is no Width, it flows ;)
I use this to paginate a FlowDocument where i know the paper size.
The returned Height is device independent units.
private double GetHeaderFooterHeight(string headerFooter)
{
var section = (Section)XamlReader.Parse(headerFooter, _pd.ParserContext);
var flowDoc = new FlowDocument();
flowDoc.Blocks.Add(section);
var richtextbox = new RichTextBox { Width = double.MaxValue, Document = flowDoc };
var viewbox = new Viewbox { Child = richtextbox };
viewbox.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
viewbox.Arrange(new Rect(viewbox.DesiredSize));
var size = new Size() { Height = viewbox.ActualHeight, Width = viewbox.ActualWidth };
return size.Height;
}

How to generate a image with text and images in C#

I have several paragraphs of text and couple of pictures between these paragraphs.
Now, I want to generate a picture using these materials, merging them vertically. But all the blocks of the text and pictures can not have bigger width than that of the generating picture, which means I have to zoom out the origin pictures, and fill each paragraph of text into a rectangle to fit the width.
Here is the tough thing:
To figure out the size of the rectangle to contain the text, I need use Graphics.MeasureString() method, which needs an instance of Graphics used to generate my picture(now, I'm using a blank template picture). But I do not know the exact size of this Graphics until I figure out all the sizes of rectangles and pictures.
Is there any method to get an instance of Graphics without source image?
Or is there any other method to do this work?
hope this could help dude .
http://chiragrdarji.wordpress.com/2008/05/09/generate-image-from-text-using-c-or-convert-text-in-to-image-using-c/
https://web.archive.org/web/20131231000000/http://tech.pro/tutorial/654/csharp-snippet-tutorial-how-to-draw-text-on-an-image
http://www.codeproject.com/Questions/388845/HOW-TO-MAKE-HIGH-QAULITY-IMAGE-WITH-TEXT-IN-Csharp
thank you
For people how are intrested in a WPF solution (as asked):
public static BitmapSource CreateImage(string text, double width, double heigth)
{
// create WPF control
var size = new Size(width, heigth);
var stackPanel = new StackPanel();
var header = new TextBlock();
header.Text = "Header";
header.FontWeight = FontWeights.Bold;
var content = new TextBlock();
content.TextWrapping = TextWrapping.Wrap;
content.Text = text;
stackPanel.Children.Add(header);
stackPanel.Children.Add(content);
// process layouting
stackPanel.Measure(size);
stackPanel.Arrange(new Rect(size));
// Render control to an image
RenderTargetBitmap rtb = new RenderTargetBitmap((int)stackPanel.ActualWidth, (int)stackPanel.ActualHeight, 96, 96, PixelFormats.Pbgra32);
rtb.Render(stackPanel);
return rtb;
}

WPF: Creating image of dynamically created usercontrol

I want to create the image of a dynamically created usercontrol and show it in a window.
I am creating the usercontrol using the Below code .
MyViews.MyViewsUserControl myViewsCanvas = new MyViews.MyViewsUserControl(AllFoundationMyViewsViewModel,item.Id);
//myViewsCanvas.Height = 5;
//myViewsCanvas.Width = 5;
Size size = new Size(50, 50);
myViewsCanvas.Measure(size);
double width = myViewsCanvas.DesiredSize.Width;
double height = myViewsCanvas.DesiredSize.Height;
myViewsCanvas.Arrange(new Rect(new Point(), size));
Then i am creating the image of the myViewsCanvas and adding it to a view box of another usercontrol called _DashBoardUserControl using the below code.
_DashBoardUserControl.Viewbox2.Child = CreateImage(myViewsCanvas);
Then i am adding the _DashBoardUserControl to a window.
UserControls.Controls.PopupWindow popup = new UserControls.Controls.PopupWindow();
popup.PopupContent = _DashBoardUserControl;
popup.ShowDialog();
The problem is, I can only see a portion of the Image. I guess that is because of the measure() and arrange() methods. Can anybody tell me about these methods or what size should i pass to these methods. Do i need to scale down the image? If yes how do i do that?
The easiest way I know of is this:
Viewbox v = new Viewbox();
v.Child = uielem;
uielem.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
v.Measure(uielem.DesiredSize);
v.Arrange(new Rect(new Point(), uielem.DesiredSize));
v.UpdateLayout();
r.Render(v);
where uielem is the element you want to render and r is the RenderTargetBitmap. (v.UpdateLayout might not be needed there, but I'm not sure anymore).

How do I resize a System.Windows.Forms.ToolBar?

I've not bothered with panels, docking, or anchors. I've simply thrown together a ToolBar control (not ToolStrip) and seem unable to size it.
System.Windows.Forms.ToolBar tb = new System.Windows.Forms.ToolBar();
// Reports 292x28 (approx) if I check width and height
// Basically the width of the form and I assume a default height
tb.Size = new System.Drawing.Size(195, 48);
// Reports 48x48, but does not actually create buttons of that size
// (It reports 48x48 because I'm retrieving 48x48 icons from a ResourceManager (resx))
tb.ButtonSize = new System.Drawing.Size(48, 48); //
The closest thing I found to making my ToolBar taller was:
http://bytes.com/topic/c-sharp/answers/241614-changing-height-toolbar-button
Although it's rather dated. And I didn't understand it. ToolBarButtons don't have Height, Width, or Size properties.
I'm using SharpDevelop, coding completely by hand on Vista, with all the .NET frameworks.
EDIT:
Here is the EXACT code that I am currently using.
#region ImageList/Toolbar
ImageList toolbarImages = new ImageList();
Image wizardToolbarImage = (Bitmap) rm.GetObject("wizard");
Image optionsToolbarImage = (Bitmap) rm.GetObject("configure");
toolbarImages.Images.Add(wizardToolbarImage);
toolbarImages.Images.Add(optionsToolbarImage);
ToolBar toolbarMain = new ToolBar();
toolbarMain.Size = new Size(195, 25); // no effect
ToolBarButton wizardToolbarButton = new ToolBarButton();
ToolBarButton optionsToolbarButton = new ToolBarButton();
wizardToolbarButton.ImageIndex = 0;
wizardToolbarButton.ToolTipText = "Wizard!";
optionsToolbarButton.ImageIndex = 1;
optionsToolbarButton.ToolTipText = "Options!";
toolbarMain.Buttons.Add(wizardToolbarButton);
toolbarMain.Buttons.Add(optionsToolbarButton);
toolbarMain.Appearance = ToolBarAppearance.Normal;
toolbarMain.ButtonSize = new System.Drawing.Size(48, 48); // no effect
toolbarMain.ImageList = toolbarImages;
toolbarMain.ButtonClick += new ToolBarButtonClickEventHandler(toolbarMain_Click);
Controls.Add(toolbarMain);
#endregion
In just about every winforms application I've written, regardless of language or framework, the toolbar could only be made taller by using larger icons.
You can also put the toolstrip inside a Panel and set the Dock property of the tool strip to Fill. And then you can size the Panel to whatever size you need.

Categories