WPF printing - PrintableAreaWidth & Height stuck in landscape? - wpf

I'm printing a FlowDocument using the following code:-
var printDialog = new PrintDialog();
var result = printDialog.ShowDialog();
if (!result.Value)
{
return;
}
var pageWidth = printDialog.PrintableAreaWidth;
var pageHeight = printDialog.PrintableAreaHeight;
flowDoc.ColumnWidth = pageWidth;
flowDoc.PageWidth = pageWidth;
flowDoc.PageHeight = pageHeight;
var paginator = ((IDocumentPaginatorSource)flowDoc).DocumentPaginator;
printDialog.PrintDocument(paginator, "Name");
The print dialog shows the page as being A4 Portrait, however the values of printDialog.PrintableAreaWidth and .PrintableAreaHeight are 1122 & 793 respectively, i.e. landscape.
Changing the orientation or the paper size via the dialog has no effect on these values. What's going on?
Update
I've added a screenshot showing the PrintDialog properties. Notice how the PrintTicket property reflects the correct page size and orientation, yet the two PrintableArea... properties are the wrong way around.
I'm starting to think this is a "funny" with the printer/driver. I've tried printing to the "XPS Document Writer" printer, and the pages render correctly when I view the created file. (And if I view the PrintDialog's properties, the PrintableArea... properties correctly reflect an A4 portrait page).

Related

Null values for PageMediaSize of a PrintTicket with Adobe PDF Creator using custom page size

I'm writing a WPF application to get and save print profiles (and another application using this print profiles to print documents).
It works fine except when I try to use Adobe PDF Creator has a printer with custom size (like 800mm by 1200mm). Then the PageMediaSize Width and Height are null in the print ticket.
Here's the code I use to get the PrintTicket :
PrintDialog pd = new PrintDialog();
if (pd.ShowDialog() == true)
{
PrintDocument doc = new PrintDocument();
doc.PrintPage += (o, a) =>
{
PrintQueue pq = pd.PrintQueue;
PrintTicket ticket = pd.PrintTicket;
...
a.Cancel = true;
};
doc.Print();
}
The PrindDialog contains the correct width and height for the page, but if I try to use the PrintTicket to print a document it crash, stating that PageMediaSize cannot contains null values.
Anyone have an idea on how to get a working PrintTicket ?
It sounds like a problem with Adobe PDF Creator's driver. The driver provides values for all the sizes, so if there is a problem, you might want to contact the makers of the printer/driver.
A way around this would be to figure out the sizes (800mm x 1200mm) and assume a resolution of 1/96 inches. Then do a conversion:
Width: ( 800mm) / (25.4 mm/in) / (1/96in) = 3023.62
Height: (1200mm) / (25.4 mm/in) / (1/96in) = 4535.43
And with those values you can say:
pd.PrintTicket.PageMediaSize = new PageMediaSize(Width, Height);

WPF - Getting Image Width/Height from internet URL

I've managed to get an image's width/height if its stored in my computer with the following code:(Fullpath is the file's full location)
var bitmapFrame = BitmapFrame.Create(new Uri(FullPath), BitmapCreateOptions.DelayCreation, BitmapCacheOption.None);
var width = bitmapFrame.PixelWidth;
var height = bitmapFrame.PixelHeight;
But the second I try to change the FullPath to an internet image (such as http://www.interweb.in/attachments/pc-wallpapers/16187d1222942178-nature-wallpaper-nature-summer-wallpaper.jpg) the width and height will not determine the real values needed and will just remain with the value of "1".
I've been sitting for a few hours here trying to figure out what went wrong and working a way around it but without success.
Thank you very much in advance!
Try this. For net Uri the ImageSource will download the image asynchronously, to avoid blocking.
var bitmapFrame = BitmapFrame.Create(new Uri(FullPath), BitmapCreateOptions.None, BitmapCacheOption.None);
if(bitmapFrame.IsDownloading)
{
bitmapFrame.DownloadCompleted += (e, arg) =>{
var width = bitmapFrame.PixelWidth;
var height = bitmapFrame.PixelHeight;
}
}
You want to wait for the DownloadCompleted event.

Printing WPF - no paging, adjust page size instead

I have a till roll printer and I want to print on it. I don't need paging but I do need to adjust the page size instead. No matter what code I use the printer page size always uses the settings in the Printer Preferences.
PrinterSettings ps = new PrinterSettings();
ps.DefaultPageSettings.PaperSize = new PaperSize("rec", 200, 900);
System.Windows.Controls.PrintDialog printDialog = new PrintDialog();
MyPaginator paginator = new MyPaginator(print);
paginator.PageSize = new Size(200, 900);
printDialog.PrintDocument(paginator, "My Receipt");
I'm using a DocumentPaginator as I thought this would allow be to be more specific about the page I wanted to print.
How can I set the page size?

WPF - Print content on same page

I have a requirement to print the WPF form contents on a Save button click. The content is plain text and will be name value pairs on each line. I don't need page breaks on consecutive Saves.
I have tried out the samples for PrintDialog, FlowDocument and FixedDocument and could not avoid the page break.
Is there any particular setting I am missing ?
Any alternative to PrintDialog ?
Code:
Paragraph myParagraph = new Paragraph();
myParagraph.Margin = new Thickness(0);
**myParagraph.BreakPageBefore = false;**
foreach (string line in textToPrint.Split(new string[] { Environment.NewLine }, StringSplitOptions.None))
{
myParagraph.Inlines.Add(new Run(line));
}
flowDocument.Blocks.Add(myParagraph);
DocumentPaginator paginator = ((IDocumentPaginatorSource)flowDocument).DocumentPaginator;
printDialog.PrintDocument(paginator, "Test Page");
A new Print job (Document) starts with a new page as far as I know. If you don't want page breaks gather all the values you want to print and then print them.

Best way to show huge text in WPF?

I need to show a really huge amount of text data in WPF code. First i tried to use TextBox (and of course it was too slow in rendering). Now i'm using FlowDocument--and its awesome--but recently i have had another request: text shouldnt be hyphenated. Supposedly it is not (document.IsHyphenationEnabled = false) but i still don't see my precious horizontal scroll bar. if i magnify scale text is ... hyphenated.
public string TextToShow
{
set
{
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(value);
FlowDocument document = new FlowDocument(paragraph);
document.IsHyphenationEnabled = false;
flowReader.Document = document;
flowReader.IsScrollViewEnabled = true;
flowReader.ViewingMode = FlowDocumentReaderViewingMode.Scroll;
flowReader.IsPrintEnabled = true;
flowReader.IsPageViewEnabled = false;
flowReader.IsTwoPageViewEnabled = false;
}
}
That's how i create FlowDocument - and here comes part of my WPF control:
<FlowDocumentReader Name="flowReader" Margin="2 2 2 2" Grid.Row="0" />
Nothing criminal =))
I'd like to know how to tame this beast - googled nothing helpful. Or you have some alternative way to show megabytes of text, or textbox have some virtualization features which i need just to enable. Anyway i'll be happy to hear your response!
It's really wrapping not hyphenation. And one can overcome this by setting FlowDocument.PageWidth to reasonable value, the only question was how to determine this value.
Omer suggested this recipe msdn.itags.org/visual-studio/36912/ but i dont like using TextBlock as an measuring instrument for text. Much better way:
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(value);
FormattedText text = new FormattedText(value, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(paragraph.FontFamily, paragraph.FontStyle, paragraph.FontWeight, paragraph.FontStretch), paragraph.FontSize, Brushes.Black );
FlowDocument document = new FlowDocument(paragraph);
document.PageWidth = text.Width*1.5;
document.IsHyphenationEnabled = false;
Omer - thanks for the direction.

Resources