Extended printer properties - c#

I'm working with a WinForms app. I have an RDLC report that will be printed on 11x17 and then folded (printer supports folding). I'm rendering to EMF and drawing to pages of a PrintDocument. This works fine except for folding.
What I'd like to do is store the settings that make the printer fold. The users would select a preset from a dropdown and the app would select the printer, the paper size, the tray, whether to duplex, and whether to fold. Storing the PrinterSettings object covers most of this, but doesn't save the folding option.
I first attempted to store/retrieve something I read about called DEVMODE. For reference: http://nicholas.piasecki.name/blog/2008/11/programmatically-selecting-complex-printer-options-in-c-shar/. What I found is that even though I had extra data specific to the driver, all the bytes were 0 regardless of what driver-specific settings I changed. I'm not sure where I went wrong with this, but I abandoned it and looked at the printing capabilities in WPF.
I found that I could configure a PrintTicket for my settings, store it, and retrieve it later. It seems a bit convoluted just to save the settings, but I think I have it working. At least it seems to show up correctly in the PrintDialog. However, I'm now stuck trying to figure out how to print my report.
As I understand it, I can't take a PrintDocument from WinForms printing and use it in WPF. I also read EMF format is not supported in WPF. I thought I would render each EMF to a bitmap, then print those. But the text in my report is fuzzy and I'm not having any luck clearing it up.
Starting with a stream that contains EMF bytes that I know will render sharply with PrintDocument, I test trying to save to a file. It seems no settings that I provide will save with crisp text.
var pageImage = new Metafile(stream);
pageImage.Save(filename);
All this just to add the ability to fold. Am I just completely on the wrong track? I don't see how this should be so hard. I guess I either need to find another way to save/restore custom printer settings or I need a way to render these EMF files better.
I also tried rendering the report directly to BMP format and it's also poor quality.

I tried something slightly different and it worked! I reused my original PrintDocument code and printed to an XPS file. Then I printed the XPS file using my PrintTicket and it works fine.

Related

Embed word document into another WITHOUT icon

How to embed a word document into another word document via OpenXML SDK, but showing content, not an icon of word? Such, as we do it manually in word: Insert object from file -> WITHOUT checking "Dispaly as icon"?
I've found this article, but it uses an icon. I've also tried to use OpenXML SDK Productivity Tool, but shows only generated binary data.
EDITED:
I use the following code:
DrawAspect = OleDrawAspectValues.Content
and then i add image part:
var imagePart = mainDocumentPart.AddNewPart<ImagePart>("image/x-emf", imagePartId);
GenerateImagePart(imagePart);
But my image part - is just an array of bytes of word's icon.
So, in this case happens the following: when i open generated document, it shows embedded document as an icon, but when i double click this embedded document, edit it and save changes, the embedded document is shown as a content, so maybe it's possible in some way to show this content without editing embedded document? Should i use instead of array of bytes of word's icon an array of bytes of doc's screenshot?
Not sure i described it clear, so please ask
I'm afraid what you are asking for is almost impossible.
The only difference as far as the word file is concerned between the icon and the embedded file, is the image.
When you don't use a icon Word pretty much just take a screenshot of the document you are embedding and inserts that in place of the Icon graphic.
I've uploaded an example I grabbed from a Word file I made. Found this little gem in the /media folder inside the .docx file.
So basicly, your only choice in resolving this if you can't live with the Icon is to somehow grab a picture of the word-file you want to embed and insert that instead of the Icon image.
How you'd go about that can't be pretty. First of all the open xml sdk contains no such functionality. I tried playing a bit around with office interop as well, but no luck.
I only see two possible ways to achieve this.
First one is via Interop. You'll need to install a "pretend printer" like the ones that print to PDF instead of sending it to a printer. This one however needs to print to an image format. The format of the file in the Media folder was .emf but I'm not positive thats a requirement.
Anyways, should the above somehow be possible you could embed that picture, pretty much using the example you link from Microsoft, and just change this size of the "icon" which now would be an image of the document.
Second possibility would be to open the word document as a process, set the document size to 72% (or whatever makes the document be the only one on screen on your desktop) and the grab a print screen and cut it down to just the document and the use that as your image for the embedding.
For the record, I don't recommend you do any of the above, but thoose are the only options I see.
Should someone have a better solution to this I'm all ears.
Finally, should you decide that you want to push on with this, I'll be happy to code up an example of option number 2 if you reply and tell me you'd like that.
Kaspar
There is a nice wrapper API (Document Builder 2.2) around open xml specially designed to merge documents, with flexibility of choosing the paragraphs to merge etc. You can download it from here.
Using this tool you can embed a paragraph of another word document or entire word document as per your requirement.
The documentation and screen casts on how to use it are here.
Hope this helps.

How to connect to a print driver in C#?

I have an task of converting bunch of formats like .pdf, .doc, .jpg, .xls, .txt, .bmp file types into .png format. I found a print driver that does that.
But how do I connect to that printer driver in .net? This will a server side component. I need to print documents into a folder using this print driver.
I am wondering how that can be done.
Thanks
Based on your updated comments, it sounds as if you are looking to convert a variety of images and document types to a single common image type. The process of taking one of the several possible source formats you mention and convert it to a bitmapped format such as .PNG is referred to as RENDERING or RASTERIZING. You want to take one of the input formats, render it to a bitmap representation, then write it to a file in .PNG format. While it certainly might be possible to do this using a print driver, to do so, you would typically be relying on an installed application that would allow you to pass the source document to it for printing to the driver. For this to work, each of the source file types you want to be able to handle this way needs to have an application installed which can take actions from the shell and do what you request. So for example if you want to do this with a .DOC file, you need Microsoft Word installed as it does properly respond to the PRINT shell command. However, the limitation with the shell based method is that it is always going to print to the DEFAULT system printer. So your driver would need to be setup as the default printer for the machine you are going to run your process on. Therefore you would need to see if each of the source types you want to be able to handle have an installed or installable application which will allow you to print them using the shell and the PRINT action verb.
Reference URLs:
Windows Shell Verbs and File Associations
Creating Shortcut Menu Handlers
The problem with this technique is not all applications respond to the PRINT verb correctly or at all. This usually works with all the major Microsoft applications, but you should test any other document types you want to support before going much further with this technique.
This also raises other questions that this doesn't even begin to address such as what to do about multiple page formats. You listed a few image types that are straight-forward and can be converted to PNG files pretty directly. But how do you want to render a multiple page Word document files into PNG format? Do you intend for only one very large PNG with all the pages one after another? Or do you intend for one PNG file per corresponding source document page? The print driver method might not give you very much control over that.
Depending on some of these details and just how much control and reliability you need in the process, you might want to consider a completely different route to your process. Maybe you should consider using tools/libraries that can read the source file formats you want to support and render them directly, after which you can save into your PNG files. One library I have used in the past that would seem to fit and allow you a high degree of control over the conversion (rendering/rasterization) process is LeadTools. It is a fairly pricey product, but my experience with it has been that it does support a wide variety of formats reliably.
LeadTools PDF and Document Readers SDK
There may be some other open source tools available that you could pull together to support this type of functionality, but I'm not familiar with any to point you to anything specific. But hopefully this helps give you some information to look at putting together a process that might be more reliable and give you greater control than trying to coerce a printer driver to do something you might not quite be able to make work reliably.
Server-side component implies something that doesn't have a human sitting at it (at least, not the human that is trying to use that printer). If this is the case then a print driver will not work - Print drivers that write their output to disk instead of a device always, in my experience, ask the user to select a place to save the file (present a Save As dialog).
To elaborate a little bit on what Boo mentioned :
Depending on the printer driver you are using, you may be able to tell it where to save your file.
The problem is by using a printer, how it normally works is that you can print from any application to a .png file. But the application itself has to know how to open and render (not talk to the printer) the content of the original file.
To continue down this path, you have to make sure your server component knows how to read and render content of each file type (.jpg, .pdf, .doc, etc.).
Assuming your server component knows how to render the content, the next step from here is to use the .NET Printing namespace to print your content to the .png printer.
For more details go to : http://msdn.microsoft.com/en-us/magazine/cc188767.aspx

.NET component for color PDF to grayscale conversion

Currently i use Ghostscript to convert color PDF's to grayscale PDF's. Now i'm looking for reliable .NET commercial or not commercial component/library for ghostscript replacement. I googled and I did not find any component/library that is able to do that easily or to do that at all.
EDIT #1:
Why Ghostscript does not work for me:
I implemented Ghostscript and I'm using it's native API's. The problem is that Ghostscript does not support multiple instances of the interpreter within a single process. -dJOBSERVER mode also does not work for me because i don't collect all job and them process them all at once. It happens that Ghostscript is processing large job which takes around 20 minutes and meanwhile i get some smaller job which has to be processed ASAP and cannot wait 20 minutes. Other problem is that Ghostscript page processed events are not easily to catch. I wrote a parser for ghostscript stdout messages and i can read out processed page number but not for each page when it's processed as ghostscript pushes message for group of processed pages. There are couple of more problems with Ghostscript like producing bad pdf's, duplicating font problems.....
You can find one more problem i had with ghostscript here: Ghostscript - PS to PDF - Inverted images problem
-
a year after UPDATE:
Before a year a go i asked this question. Later i made my own solution by using iTextSharp.
You can take a look at the converting PDF to grayscale solution here:
http://habjan.blogspot.com/2013/09/proof-of-concept-converting-pdf-files.html
or
https://itextsharpextended.codeplex.com/
Works for me in most cases :)
Not quite an answer, but I think you dismiss Ghostscript too quickly.
Are you aware of the GhostScript API (for in-process Ghostscript)? Or of the -dJOBSERVER mode that can take a series of PS commands piped to its standard in?
That still won't get you your callbacks however, and it's still not multi-threaded.
As previously stated, iText could do it, but it would be a matter of walking through all the content and images looking for non-grayscale color spaces and converting them in a space-specific manner.
You'd also have to replace the pixel data in any images you might find.
The good news is that iText[Sharp] is capable of operating in multiple threads, provided each document is used from one thread at a time.
I suspect this is also the case for the suggested commercial library, which isn't such a good deal.
And then a light went on above my head... drawn in gray scale.
Blending modes and transparency groups!
Take all the current page content and stick it in a transparency group that is blended with a solid black rectangle that covers the page. I think there's even a luminosity to alpha blend mode... lets see here.
Yep, PDF reference section 11.6.5.2 "Soft Mask Dictionaries". You'll want a "luminosity" group.
Now, the bad news. If your goal in switching to gray scale is to save space, this will fail utterly. It'll actually make each file a little larger... say a 100 bytes per page, give or take.
The software rendering the PDF better be pretty hot stuff too. Your cousin's undergrad rendering project need not apply. This is advanced graphics stuff here, infrequently used by Common PDF Files, so the last sort of thing to be implemented.
So... For each original page
Create a new page.
Cover it with a black background.
Cover it with a white rectangle (had it backwards earlier) in a transparency group that uses a soft mask dictionary set to be the luminosity of the original page's content (now stashed in an XObject Form).
Because this is all your own code, you'll have ample opportunity to do whatever it is you want to do at the beginning or end of each page.
By golly, that's just crazy enough to work! It does require some PDF-Fu, but not nearly as much as the "convert each color space and image in various ways as I step through the document". Deeper knowledge, less code to write.
This isn't a .net library, but rather a potential work-around. You could install a virtual printer that is capable of writing PDF files. I would suggest CutePDF, as it's free, easy to use and does a great job 'printing' a large number of file formats to PDF. You can do nearly everything with CutePDF that you can do with a normal printer, including printing to grayscale.
After the virtual printer is installed, you can use c# to 'print' a greyscale version.
Edit: I just remembered that the free version is not silent. Once you print to the CutePDF printer, it will ask you to 'Save As'. They do have an SDK available for purchase, but I couldn't say whether it would be able to help you convert to grayscale.
If a commercial product is a valid option for you, allow me to recommend Amyuni PDF Creator .Net. By using it you will be able to enumerate all items inside the page and change their colors accordingly, images can also be set as grayscale. Usual disclaimers apply
Sample code using Amyuni PDF Creator ActiveX, the .Net version would be similar:
pdfdoc.ReportState = ReportStateConstants.acReportStateDesign;
object[] page_items = (object[])pdfdoc.get_ObjectAttribute("Pages[1]", "Objects");
string[] color_attributes = new string[] { "TextColor", "BackColor", "BorderColor", "StrokeColor" };
foreach (acObject page_item in page_items)
{
object _type = page_item["ObjectType"];
if ((ACPDFCREACTIVEX.ObjectTypeConstants)_type == ACPDFCREACTIVEX.ObjectTypeConstants.acObjectTypePicture)
{
page_item["GrayScale"] = true;
}
else
foreach (string attr_name in color_attributes)
{
try
{
Color color = System.Drawing.ColorTranslator.FromWin32((int)page_item[attr_name]);
int grayColor = (int)(0.3 * color.R + 0.59 * color.G + 0.11 * color.B);
int newColorRef = System.Drawing.ColorTranslator.ToWin32(Color.FromArgb(grayColor, grayColor, grayColor));
page_item[attr_name] = newColorRef;
}
catch { } //not all items have all kinds of color attributes
}
}
Before a year a go i asked this question. Later i made my own solution by using iTextSharp.
You can take a look at the converting PDF to grayscale solution here: https://itextsharpextended.codeplex.com/
iTextPdf a good product for creating/managing pdf it has got both commercial and free versions.
Have a look at aspose.pdf for .net it provides below features and a lot more.
Add and remove watermarks from PDF document
Set page margin, size, orientation, transition type, zoom factor and appearance of PDF document
..
And here is a list of open source pdf libraries.
After a lot of investigation i found out about ABCpdf from Websupergoo. Their component can easily convert any PDF page to grayscale by simple call to Recolor method. The component is commercial.

Display dynamic QR Code in Crystal Report in asp .net page C# code-behind

I'm using CRv9 and want to make use of Google Charts API for generating QR code on fly (in asp .net) and display it in the Crystal Report in a PDF format.
I have spent the whole day looking for solution with no luck. The way we output the report is we use .rpt file, feed it with data and use Response.OutputStream to feed to browser. No CrystalReportViewer control hence CSS solution is not an option.
Now, I got as far as added an OLE Object from file with Link, which I would be overwriting every time the new QR code is generated. I apreciate that CR requires it to be a bitmap, so I was planning to download and convert the google's generated PNG file to BMP, that's not an issue. The problem is that Image in the report does not update after I replace the file. Meaning, it displays the original image, which was added as an OLE Object.
If I open this report in CR designer, the image gets refresh/updated and I'd have to save changes to the report to see this new image next time I generate a PDF file.
The question is really how to achieve a dynamic image in Crystal Reports 9? Remember, Picture object did not have a Graphic Location property until vXI, so I cannot use that.
Please help, I'm kinda stuck here. Manipulations with DataSets is not an option either as we're not giving report a datasource, instead we just map the fields with FormulaFieldDefinitions.
sample qr code url: https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=Hello%20world&choe=UTF-8
Try this:
Insert a picture; use a dummy QR code or something about the same size
Right-click the image and select 'Format Graphic...'
Select the Picture tab
Add your URL, in double-quotation marks, to the Graphic Location's conditional formatting
Refresh the report
My original posting: Crystal Reports: Dynamic Images
This technique worked with versions prior to XI.
Another idea: create a user-function library (UFL):
Creating a Crystal Reports Custom Function Library
You can also create a UFL in Java. In the UFL, you could make the call to Google's service and return the resulting image.
Or purchase a QR UFL: QR Code Font kit
No idea about anything in crystal reports, but the traditional way of embedding barcodes is to use a font, not an image. So it should be pretty doable if you have the ability to use custom fonts here.
the answer to my question is "it's impossible" :(
I had similar issue with dynamic QR-code as image. The problem with CrystalReport is that it flattens image rendering. My solution was to use the rdlc reporting option, though am not an expert in it. It solved the issue because it renders the image as the original file.
Add image to the report and set the source to database in the property. Set it to conform to original size. I think SAP should look into the way image is rendered because I had to change lots of design to rdlc.

Is there a way to replace a text in a PDF file with itextsharp?

I'm using itextsharp to generate the PDFs, but I need to change some text dynamically.
I know that it's possible to change if there's any AcroField, but my PDF doen's have any of it. It just has some pure texts and I need to change some of them.
Does anyone know how to do it?
Actually, I have a blog post on how to do it! But like IanGilham said, it depends on whether you have control over the original PDF. The basic idea is you setup a form on the page and replace the form fields with the text you want. (You can style the form so it doesn't look like a form)
If you don't have control over the PDF, let me know how to do it!
Here is a link to the full post:
Using a template to programmatically create PDFs with C# and iTextSharp
I haven't used itextsharp, but I have been using PDFNet SDK to explore the content of a large pile of PDFs for localisation over the last few weeks.
I would say that what you require is absolutely achievable, but how difficult it is will depend entirely on how much control you have over the quality of the files. In my case, the files can be constructed from any combination of images, text in any random order, tables, forms, paths, single pixel graphics and scanned pages, some of which are composed from hundreds of smaller images. Let's just say we're having fun with it.
In the PDFTron way of doing things, you would have to implement a viewer (sample available), and add some code over a text selection. Given the complexities of the format, it may be necessary to implement a simple editor in a secondary dialog with the ability to expand the selection to the next line (or whatever other fundamental object is used to make up text). The string could then be edited and applied by copying the entire page of the document into a new page, replacing the selected elements with your new string. You would probably have to do some mathematics to get this to work well though, as just about everything in PDF is located on the page by means of an affine transform.
Good luck. I'm sure there are people on here with some experience of itextsharp and PDF in general.
This question comes up from time to time on the mailing list. The same answer is given time and time again - NO. See this thread for the official answer from the person who created iText.
This question should be a FAQ on the itextsharp tag wiki.

Categories