I'm using Delphi 7 and want to know if the file is printing to a PDF device so I can do some special processing. This is what I have to get it to work.
// If the printer is a PDF type it will ask for the file name here.
Printer.BeginDoc;
// The next line returns the file extension of the original file name and
// not the extension of the file being printed to.
Extension := AnsiUpperCase(ExtractFileExt(Printer.Title));
// I've added this line to get it to work but want to be able
// just check if the current child is being printed to a pdf device.
if CustomPrintDialog.ActivePrinter.Caption = 'Adobe PDF' then
Related
I am using the Export plugin in Grails for generating PDF/Excel report. I am able to generate single report on PDF/Excel button click. But, now I want to generate multiple reports on single button click. I tried for loop, method calls but no luck.
Reference links are ok. I don't expect entire code, needs reference only.
If you take a look at the source code for the ExportService in the plugin you will notice there are various export methods. Two of which support OutputStreams. Using either of these methods (depending on your requirements for the other parameters) will allow you to render a report to an output stream. Then using those output streams you can create a zip file which you can deliver to the HTTP client.
Here is a very rough example, which was written off the top of my head so it's really just an idea rather than working code:
// Assumes you have a list of maps.
// Each map will have two keys.
// outputStream and fileName
List files = []
// call the exportService and populate your list of files
// ByteArrayOutputStream outputStream = new ByteArrayOutputStream()
// exportService.export('pdf', outputStream, ...)
// files.add([outputStream: outputStream, fileName: 'whatever.pdf'])
// ByteArrayOutputStream outputStream2 = new ByteArrayOutputStream()
// exportService.export('pdf', outputStream2, ...)
// files.add([outputStream: outputStream2, fileName: 'another.pdf'])
// create a tempoary file for the zip file
File tempZipFile = File.createTempFile("temp", "zip")
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(tempZipFile))
// set the compression ratio
out.setLevel(Deflater.BEST_SPEED);
// Iterate through the list of files adding them to the ZIP file
files.each { file ->
// Associate an input stream for the current file
ByteArrayInputStream input = new ByteArrayInputStream(file.outputStream.toByteArray())
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(file.fileName))
// Transfer bytes from the current file to the ZIP file
org.apache.commons.io.IOUtils.copy(input, out);
// Close the current entry
out.closeEntry()
// Close the current input stream
input.close()
}
// close the ZIP file
out.close()
// next you need to deliver the zip file to the HTTP client
response.setContentType("application/zip")
response.setHeader("Content-disposition", "attachment;filename=WhateverFilename.zip")
org.apache.commons.io.IOUtils.copy((new FileInputStream(tempZipFile), response.outputStream)
response.outputStream.flush()
response.outputStream.close()
That should give you an idea of how to approach this. Again, the above is just for demonstration purposes and isn't production ready code, nor have I even attempted to compile it.
I have looked everywhere for this solution. The code below allows me to print to the printer, Adobe PDF, but what I want to do is automate the file name save as screen with a generic name and in a specific folder. For example, the file would be saved to C:\temp\tmpResize.pdf and I am having problems there.
var params = this.getPrintParams();
params.interactive=params.constants.interactionLevel.silent;
params.pageHandling=params.constants.handling.none;
params.fileName = "/c/temp/tmpResize.pdf";
params.printerName="Adobe PDF"
this.print(params);
Thanks for your help.
I've created a MigraDoc/PdfSharp document and now need to send it to a specific printer without any user interaction.
What do I need to use as a Renderer and how do I set the printer path/name to the MigraDocPrintDocument?
MigraDocPrintDocument is the correct class.
// Creates a PrintDocument that simplyfies printing of MigraDoc documents
MigraDocPrintDocument printDocument = new MigraDocPrintDocument();
// Attach the current printer settings
printDocument.PrinterSettings = printerSettings;
We use System.Windows.Forms.PrintDialog() to let the user select the printer (this dialog fills the printerSettings structure).
Use
internal PrinterSettings printerSettings = new PrinterSettings();
for the default printer. Change this structure to print with different settings or on a different printer.
Please note that with PDFsharp 1.31, printing will work with the GDI+ build only (the WPF build will not print the document correctly).
This one very simple thing I can't find the right technique. What I want is to open a .dotx template, make some changes and save as the same name but .docx extension. I can save a WordprocessingDocument but only to the place it's loaded from. I've tried manually constructing a new document using the WordprocessingDocument with changes made but nothing's worked so far, I tried MainDocumentPart.Document.WriteTo(XmlWriter.Create(targetPath)); and just got an empty file.
What's the right way here? Is a .dotx file special at all or just another document as far as the SDK is concerned - should i simply copy the template to the destination and then open that and make changes, and save? I did have some concerns if my app is called from two clients at once, if it can open the same .dotx file twice... in this case creating a copy would be sensible anyway... but for my own curiosity I still want to know how to do "Save As".
I would suggest just using File.IO to copy the dotx file to a docx file and make your changes there, if that works for your situation. There's also a ChangeDocumentType function you'll have to call to prevent an error in the new docx file.
File.Copy(#"\path\to\template.dotx", #"\path\to\template.docx");
using(WordprocessingDocument newdoc = WordprocessingDocument.Open(#"\path\to\template.docx", true))
{
newdoc.ChangeDocumentType(WordprocessingDocumentType.Document);
//manipulate document....
}
While M_R_H's answer is correct, there is a faster, less IO-intensive method:
Read the template or document into a MemoryStream.
Within a using statement:
open the template or document on the MemoryStream.
If you opened a template (.dotx) and you want to store it as a document (.docx), you must change the document type to WordprocessingDocumentType.Document. Otherwise, Word will complain when you try to open the document.
Manipulate your document.
Write the contents of the MemoryStream to a file.
For the first step, we can use the following method, which reads a file into a MemoryStream:
public static MemoryStream ReadAllBytesToMemoryStream(string path)
{
byte[] buffer = File.ReadAllBytes(path);
var destStream = new MemoryStream(buffer.Length);
destStream.Write(buffer, 0, buffer.Length);
destStream.Seek(0, SeekOrigin.Begin);
return destStream;
}
Then, we can use that in the following way (replicating as much of M_R_H's code as possible):
// Step #1 (note the using declaration)
using MemoryStream stream = ReadAllBytesToMemoryStream(#"\path\to\template.dotx");
// Step #2
using (WordprocessingDocument newdoc = WordprocessingDocument.Open(stream, true)
{
// You must do the following to turn a template into a document.
newdoc.ChangeDocumentType(WordprocessingDocumentType.Document);
// Manipulate document (completely in memory now) ...
}
// Step #3
File.WriteAllBytes(#"\path\to\template.docx", stream.GetBuffer());
See this post for a comparison of methods for cloning (or duplicating) Word documents or templates.
If a user prints a report, and they happen to be using the Microsoft XPS printer, i would like the default the filename to something meaningful.
i would have thought that the XPS printer would take the name of the print job, and use that as the default filename - but it doesn't.
Is there some other, programatic, way to default the name of the generated XPS file when i print to that printer? i was thinking there might be something like:
a registry key
global shared memory
API call like SetDefaultXPSFilename()
extended attributes about a print job
Example
Automate Excel to create a spreadsheet:
Excel xl = new ExcelApplication();
Workbook wb = xl.Workbooks.Add();
GenerateReport(wb);
wb.PrintOut();
Now if the user's default printer is the Microsoft XPS Document Writer, then the user will get:
i would like a way for that File name to be defaulted to something useful, such as:
20110729 - Chip Bank Settlement Sheet.xps
The user will accept the default filename, and files will organized automatically, rather than the user typing:
asdfadf.xps
References
eggheadcafe: XPS Default File Name
MSDN: XPS Name when Sent to Printer
Bump: 20110729 (12 months later)
Well,
here is a simple way (at least in my case):
(myPrintPage inherits from System.Drawing.Printing.PrintDocument)
With myPrintPage
With .PrinterSettings
If .PrinterName = "Microsoft XPS Document Writer" Then
.PrintToFile = True
.PrintFileName = "c:\test.pdf"
End If
End With
.Print()
End With
I haven't found a way, yet, to determine whether or not the printer I have chosen is going to print into a file, hence the test on the printer's name.
In addition to above, here is a piece of code I found useful:
Let's say that my default printer is NOT the XPS Document Writer. My code needs to archive automatically some data, print a report in XPS, then offer the user to print the report on the default printer. In the second step, I need to change the PrinterSettings of myPrintPage.
Here is how:
'save xps results
'is the XPS printer installed?
Dim myXPSfound As Boolean = False
For Each s As String In System.Drawing.Printing.PrinterSettings.InstalledPrinters
If s.Contains("XPS") Then
myXPSfound = True
Exit For
End If
Next
If myXPSfound Then
'Manual settings of the XPS printerSettings
Dim myXPSPrinterSettings As New Drawing.Printing.PrinterSettings
myXPSPrinterSettings.Collate = False
myXPSPrinterSettings.Copies = 1
myXPSPrinterSettings.Duplex = Drawing.Printing.Duplex.Simplex
myXPSPrinterSettings.FromPage = 0
myXPSPrinterSettings.MaximumPage = 9999
myXPSPrinterSettings.MinimumPage = 0
myXPSPrinterSettings.PrinterName = "Microsoft XPS Document Writer"
myXPSPrinterSettings.PrintRange = Drawing.Printing.PrintRange.AllPages
myXPSPrinterSettings.PrintToFile = True
myXPSPrinterSettings.ToPage = 1
myPrintPage.PrinterSettings = myXPSPrinterSettings
myPrintPage.PrinterSettings.PrintToFile = True
myPrintPage.PrinterSettings.PrintFileName = mytargetFileName & ".xps"
Try
myPrintPage.Print()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, "Error Printing the XPS File")
End Try
Else
MsgBox("The Microsoft XPS Writer was no found on this computer", MsgBoxStyle.Information, "Error Printing the XPS File")
End If
It can be handy sometimes.
The Microsoft XPS Document Writer (MXDW) will generate an output file path without prompting the user if the application that prints sets lpszOutput in DOCINFO.
If you don't have access to the code of the application then another option is to build an XPS driver that generates a file path even when lpszOutput hasn't been set. The Windows Driver Kit (WDK) is the place to start.
For more details and links see this post.
Win2PDF 7 can save as XPS, and does default to the name of the print job. If you don't want to use the print job as the name displayed in the File Save dialog, you can change the default file name by setting a registry value named "PDFTitle".
You can also set the output file without prompting either using the lpszOutput field of DOCINFO, or by setting a registry setting named "PDFFileName" as described in the Win2PDF documentation. The file will be created in the XPS format if the file name contains an .xps extension.