modifying a print preview - c#

I'm building a c# app that displays a print preview (of a document) and then asks the user(s) to 'sign' the document via a InkPicture control. I've got no problems extracting the Bitmap from the inkpicture control and applying it to the PrintDocument (I do this earlier in the process before the print preview and paint those images to the printdocument) but the purpose of the print preview is to allow the user(s) to review the document as it would be printed and sign off on it.
I've tried resetting the document to a modified one
// MyDocumentType derives from PrintDocument and ipSignature is a
// user control derived from InkPicture that converts the ink to
// a gif
MyDocumentType doc = (MyDocumentType)ppcPreview.Document;
doc.AddSignature(ipSignature.Gif);
ppcPreview.Document = doc;
I've tried reconstructing the print preview control
MyDocumentType doc = (MyDocumentType)ppcPreview.Document;
doc.AddSignature(ipSignature.Gif);
ppcPreview = new PrintPreviewControl();
ppcPreview.Document = doc;
to no effect.
Invalidating the control after it's modified also does nothing.
I'm kind of stumped.

Did you try using the InvalidatePreview method instead of Invalidate?

Related

How to display a PDF in Winforms with no obstructions?

I want to display a PDF in my WinForms C# application. I have tried using a WebBrowser component, but it displays control bars from Adobe Reader. I have also tried a component Adobe PDF Reader control axAcroPDF, but using it crashed the responsiveness of my form, other components didn't move when resizing the form (I don't know why). What can I do to either change the WebBrowser component to not display the controls, or display the PDF in some other way?
With Magick.NET, you can convert your pdf to an image, and display it on the form. Also you need the GhostScript to be installed on your PC. Something like this:
using (var image = new MagickImage())
{
private MagickReadSettings _settings;
_settings = new MagickReadSettings()
{
FrameCount = 1, // return only one page
};
_settings.FrameIndex = 1; // return only the first page
_settings.Density = new Density(resolution); // set the resolution
image.Read(DocPath, _settings);
image.ColorAlpha(MagickColors.White);
bmp = image.ToBitmap();
}
Or, you can try this, but I have no experience with that. It is free and has some limitations, but may be it will fit your needs.

PDF Document does not display when creating control dynamically

I have an application that I want to display multiple PDF documents. If I define the control at design time I can load a document and display it, but when I dynamically create the control during run time I cannot get it to display. The document is being displayed in a tab.
Here is my code...
AxAcroPDF newPDF = new AxAcroPDF();
newPDF.CreateControl();
newPDF.Width = selectedTab.Width;
newPDF.Height = selectedTab.Height;
newPDF.LoadFile(filePath);
selectedTab.Controls.Add(newPDF);
newPDF.Show();
newPDF.Visible = true;
How do I get the PDF to display?
This is what worked for me...
AxAcroPDF newPDF = new AxAcroPDF();
selectedTab.Controls.Add(newPDF);
newPDF.CreateControl();
newPDF.Width = selectedTab.Width;
newPDF.Height = selectedTab.Height;
newPDF.LoadFile(filePath);
newPDF.Show();
For some reason it doesn't like the PDF control being added to the tab after the CreateControl() method is executed.
Don't use Width and Height but ActualWidth and ActualHeight from the SelectedTab. Under certain circumstances the non actuals may report zero sizes.
Otherwise hard code height and width to see if that provides an insight as to whether it is showing up, but hidden.

how to print .htm files in c#?

I can't seem to find a good way to print .htm files in c# using .net 4.0, visual studio 2010 and windows forms. When i tried to print it directly, it printed the raw html data instead of printing the "page" itself.
The only way i know to print it, is to use a WebBrowser control. When i print the document, it doesn't print colors and the page isn't printed correctly. For example, the edges are not drawn and so on.
Code for Web Browser :
public void Print()
{
// Create a WebBrowser instance.
WebBrowser webBrowserForPrinting = new WebBrowser();
// Add an event handler that prints the document after it loads.
webBrowserForPrinting.DocumentCompleted +=
new WebBrowserDocumentCompletedEventHandler(PrintDocument);
// Set the Url property to load the document.
webBrowserForPrinting.Url = new Uri(Core.textLog);
}
private void PrintDocument(object sender, WebBrowserDocumentCompletedEventArgs e)
{
((WebBrowser)sender).ShowPrintDialog();
//// Print the document now that it is fully loaded.
//((WebBrowser)sender).Print();
//// Dispose the WebBrowser now that the task is complete.
((WebBrowser)sender).Dispose();
}
What can i do?
Thank you!
Printing web pages will forever be the bane of your existence. There just isn't a solution out there that prints HTML directly to your printer really, really well. And even if you do find a program that does it well, it's only a matter of time until you try to print a page with some unsupported formatting, in which case you're right back where you started.
What we do is print HTML to a pdf file with a program called wkhtmltopdf. Then we open it in Acrobat (which has excellent printing support) and print from there. I can't say enough good things about wkhtmltopdf. It's command line driven, and its super, super fast. Best of all, its free. It has a companion program called wkhtmltoimage that will print to most popular image formats, too (bmp, jpg, png, etc).
After downloading/installing the program, you can run a quick test by going to your command prompt, navigating to the install folder, and typing:
wkhtmltopdf "http://YouWebAddress.com" "C:/YourSaveLocation.pdf"
It also has a ton of command line switches that give you greater control over the outputs (headers, footers, page numbering, etc etc).
Ok, as i said, problem was that edge are not drawn and neither are the backgrounds.
Here is how i solved it.
Hashtable values = new Hashtable();
values.Add("margin_left", "0.1");
values.Add("margin_right", "0.1");
values.Add("margin_top", "0.1");
values.Add("margin_bottom", "0.1");
values.Add("Print_Background", "yes");
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(#"Software\Microsoft\Internet Explorer\PageSetup", true))
{
if (key == null) return;
foreach (DictionaryEntry item in values)
{
string value = (string)key.GetValue(item.Key.ToString());
if (value != item.Value.ToString())
{
key.SetValue(item.Key.ToString(), item.Value);
}
}
}
So before i print, i go to regedit, change the values, and the document gets printed perfectly. Hope this helps other people that have the same problem when printing from webbrowser control in windows forms.

WPF DocumentViewer - Print with no confirmation

I have a WPF application where I use a document viewer. I also start printing programmatically with documentviewer.Print(); However, when that is pressed it brings up the screen with the Windows printers and makes the user have to click "OK" again on that screen to start. Is there a way to avoid the confirmation and make documentviewer.Print(); immediately start the print job on the default Windows printer?
All you need is the default print queue, which you can get via
var pq = LocalPrintServer.GetDefaultPrintQueue()
From this, you can create an XpsDocumentWriter:
var writer = PrintQueue.CreateXpsDocumentWriter(pq);
Now, you can get the DocumentPaginator from your DocumentViewer via the Document property, which returns an IDocumentPaginatorSource that has a DocumentPaginator property:
var paginator = documentviewer.Document.DocumentPaginator;
and you can send that right to the XpsDocumentWriter's Write method:
writer.Write(paginator);
Simple, isn't it?

Why does Print Preview show properly formatted pages that won't actually print?

I am writing an app to print formatted data using Visual Studio 2008/C#. I have formatted the data in the fashion that I want it to display. I am using two Print Documents and event handlers, because the first page of the report carries formatting requirements that differs from pages 2 through N.
Print Preview shows me properly formatted data for all pages that I try to print. Nevertheless, pages 2 through N will not actually print.
I've stepped through my code and the data is being passed correctly to the event handler. This is the block of code that calls the second print document's event handler. What am I doing wrong?
// First page print limit has been reached. Do we
// still have unprinted items in the arraylist? Call the second
// print handler event and print those items.
if (((alItemsToPrint.Count) - iItemPrintedCount) > 0)
{
// Getting a look at my formating
PrintPreviewDialog printPreview2 = new PrintPreviewDialog();
printPreview2.Document = ItemsPrintDocument;
printPreview2.ShowDialog();
printPreview2.Dispose();
// Print item overflow pages
ItemsPrintDocument.Print();
// Release the resources consumed by this print document
ItemsPrintDocument.Dispose();
}
Thanks for your time, everyone.
To Print a Document, you use:
PrintDocument.Print
When Preview, You assign The PrintDocument to PrintPreviewDialog
printPreview2.Document = ItemsPrintDocument;
When You show PrintPreviewDialog, it replaces the PrintDocument' PrintController to PreviewPrintController and call PrintDocument.Print.
This action generates a List of images (metafiles) one on each page.
Next, it restore the original PrintController on PrintDocument and show images.
When You press the PrintButton on PrintPreviewDialog, it calls PrintDocument.Print with original PrintController.
Note that for correct behavior you can use BeginPrint' PrintDocument event to initialize vars to new PrintDocument.Print.
If you use PrintPreviewDialog, you do´nt need call PrintDocument.Print.

Categories