I am using PrintTicket object (printTicket.Stapling = Stapling.StapleTopLeft;) and using PrintQueue.Addjob method in System.printing to enable stapling option. Stapling is working fine with XPS document, but not working with PDF documents. Here is my code:
PrintTicket printTicket = printQueue.DefaultPrintTicket;
printTicket.Stapling = Stapling.StapleTopLeft;
printQueue.CurrentJobSettings.CurrentPrintTicket = printTicket;
printQueue.AddJob("TEST", "C:\\TEST.XPS", false, printTicket);
TEST.XPS is printing and stapling, but changing the file to PDF is not working.
If you look at the MDSN for that AddJob method it specifically says it for XPS documents only.
https://msdn.microsoft.com/en-us/library/jj205516%28v=vs.110%29.aspx
Inserts a new print job for an XML Paper Specification (XPS) Document
into the queue, gives it the specified name and settings, and
specifies whether or not it should be validated.
Related
I want to save an IDocumentPaginatorSource, e.g. FixedDocument or XpsDocument, as a PDF by using the virtual printer "Microsoft Print to PDF":
var printServer = new System.Printing.PrintServer();
var queue = printServer.GetPrintQueue("Microsoft Print to PDF");
var writer = System.Printing.PrintQueue.CreateXpsDocumentWriter(queue);
writer.Write(Document.DocumentPaginator); // Document = IDocumentPaginatorSource
This works, but makes the printer open a file save dialog. I would like to set the file name programmatically and either suppress this dialog completely or at least set the initial file name in the dialog. Is this possible?
I know, that this can be done when using System.Drawing.Printing.PrintDocument by setting PrinterSettings.PrintFileName and PrinterSettings.PrintToFile (see 1, 2), but this is the old printing framework that does not support IDocumentPaginatorSource.
I checked all classes in the System.Printing namespace but did not find any way to set these two settings. Maybe it's possible to retrofit these seetings into the PrintTicket by extending the print schema? If so, how exactly would you do that?
I don't have a solution using the "Microsoft Print to PDF printer", but if you switched to using the Win2PDF printer driver you can set the file name programatically through the registry. To do this, see the documentation for the "PDFFileName" or "PDFDefaultFileName" registry settings.
I'm trying to convert HTML file to PDF with iText7 add-on html2Pdf. I have this code and always when I call method ConvertToPdf program failed with error: specified method is not supported.
I'm trying it with valid trial licence key. I also try it without ConverterProperties and the result was the same.
Is there another way how to create PDF from HTML file with iText ?
ConverterProperties props = new ConverterProperties();
props.SetMediaDeviceDescription(new MediaDeviceDescription(MediaType.ALL));
LicenseKey.LoadLicenseFile(#"C:\Users\blah\kerz.xml");
string ppa = #"C:\Users\blah\Desktop\html\index.html";
HtmlConverter.ConvertToPdf(new FileInfo(ppa), new FileInfo(#"C:\Users\blah\Desktop\html\a.pdf"),props);
After struggling whole day, I identified the issue but this didn't solve my problem.
On short:
I need to open a PDF, convert to BW (grayscale), search some words and insert some notes nearby found words. At a first look it seems easy but I discovered how hard PDF files are processed (having no "words" concepts and so on).
Now the first task, converting to grayscale just drove me crazy. I didn't find a working solution either commercial or free. I came up with this solution:
open the PDF
print with windows drivers, some free PDF printers
This is quite ugly since I will force the C# users to install such 3'rd party SW but.. that is fpr the moment. I tested FreePDF, CutePDF and PDFCreator. All of them are working "stand alone" as expected.
Now when I tried to print from C#, obviously, I don't want the print dialog, just select BW option and print (aka. convert)
The following code just uses a PDF library, shown for clarity only.
Aspose.Pdf.Facades.PdfViewer viewer = new Aspose.Pdf.Facades.PdfViewer();
viewer.BindPdf(txtPDF.Text);
viewer.PrintAsGrayscale = true;
//viewer.RenderingOptions = new RenderingOptions { UseNewImagingEngine = true };
//Set attributes for printing
//viewer.AutoResize = true; //Print the file with adjusted size
//viewer.AutoRotate = true; //Print the file with adjusted rotation
viewer.PrintPageDialog = true; //Do not produce the page number dialog when printing
////PrinterJob printJob = PrinterJob.getPrinterJob();
//Create objects for printer and page settings and PrintDocument
System.Drawing.Printing.PrinterSettings ps = new System.Drawing.Printing.PrinterSettings();
System.Drawing.Printing.PageSettings pgs = new System.Drawing.Printing.PageSettings();
//System.Drawing.Printing.PrintDocument prtdoc = new System.Drawing.Printing.PrintDocument();
//prtdoc.PrinterSettings = ps;
//Set printer name
//ps.PrinterName = prtdoc.PrinterSettings.PrinterName;
ps.PrinterName = "CutePDF Writer";
ps.PrintToFile = true;
ps.PrintFileName = #"test.pdf";
//
//ps.
//Set PageSize (if required)
//pgs.PaperSize = new System.Drawing.Printing.PaperSize("A4", 827, 1169);
//Set PageMargins (if required)
//pgs.Margins = new System.Drawing.Printing.Margins(0, 0, 0, 0);
//Print document using printer and page settings
viewer.PrintDocumentWithSettings(ps);
//viewer.PrintDocument();
//Close the PDF file after priting
What I discovered and seems to be little explained, is that if you select
ps.PrintToFile = true;
no matter C# PDF library or PDF printer driver, Windows will just skip the PDF drivers and instead of PDF files will output PS (postscript) ones which obviously, will not be recognized by Adobe Reader.
Now the question (and I am positive that others who may want to print PDFs from C# may be encountered) is how to print to CutePDF for example and still suppress any filename dialog?
In other words, just print silently with programmatically selected filename from C# application. Or somehow convince "print to file" to go through PDF driver, not Windows default PS driver.
Thanks very much for any hints.
I solved conversion to grayscale with a commercial component with this post and I also posted there my complete solution, in care anyone will struggle like me.
Converting PDF to Grayscale pdf using ABC PDF
I am using Amyuni PDF Creator .Net to print PDF using a Windows service.
Windows service is running under Local System user account. When I tried to print using above library, it prints the PDF in wrong font. See the attachment (Wrong font in PDF printing).
This issue persists with only some of the printers such as Brother MFC-8890DW Printer.
But for the same printer with above windows service, it prints the PDF properly when unchecked the Enable advanced printing features setting in above printer Properties. See the attachment (Disable Advanced printing features).
using (FileStream file1 = new FileStream(pdfFile, FileMode.Open, FileAccess.Read))
{
using (IacDocument doc1 = new IacDocument())
{
doc1.Open(file1, string.Empty);
doc1.Copies = 1;
bool printed = doc1.Print(printer, false);
}
}
But same windows service prints PDF correctly for some other printers such as HP LaserJet P1005 either Enable advanced printing features checked or unchecked.
Without having access to the same printer that you are using it is hard to know exactly what is happening. My best guess would be that the driver of this printer has issues dealing with process-level fonts (those that are registered using the GDI function AddFontResourceEx) when "Enable advanced printing features" is checked. This is how Amyuni PDF Creator uses fonts embedded in the PDF file, which is the case for the file that you have presented.
A possible workaround for this could be to use the attribute "PrintAsImage" of the Document class.
The code would look like this:
//set license key This is needed only with licensed version
acPDFCreatorLib.SetLicenseKey("your company", "your activation code");
//Create a new document instance
Amyuni.PDFCreator.IacDocument doc = new Amyuni.PDFCreator.IacDocument(null);
doc.AttributeByName("PrintAsImage").Value =1;
//Open the file here (...)
//Print to default printer
pdfCreator1.Document.Print("", false);
Another alternative would be to save your file as xps using Amyuni PDF Creator then send the xps file to the printer:
// Create print server and print queue.
LocalPrintServer localPrintServer = new LocalPrintServer();
PrintQueue defaultPrintQueue = LocalPrintServer.GetDefaultPrintQueue();
defaultPrintQueue.AddJob("my document", "c:\\temp\\mytempfile.xps", true);
Disclaimer: I work for Amyuni Technologies.
I need to populate XFA form fields in a PDF (created with Adobe LiveCycle Designer). We're attempting to use iText (actually iTextSharp with C#) to parse the PDF, populate the XFA fields and then save the modified PDF back out.
All the examples I can find with iText (very few iTextSharp examples) talk about modifying AcroForm fields. This PDF does NOT have AcroForm fields and uses XFA only.
Pointers to any non-standard resources would be helpful (I've already done the requisite Googling on the topic and haven't found anything useful).
Code examples here would be awesome from anyone who has actually done what I'm trying to do.
If you can get a data packet into the PDF, the XFA runtime in Acrobat would populate those fields with the data in the data packet.
If you want to see what one of these looks like, create a form in LiveCycle Designer (comes with Acrobat Pro), add some fields to it, and save it as a dynamic PDF. Open the form in Acrobat and type some values into the fields and save it.
Open the PDF with a tool that lets you peer at the PDF data and you'll find /Catalog/AcroForm/XFA a stream that has an <xfa:datasets> packet with the values you typed. That's what you'll need to create yourself and insert into the PDF.
The XDP spec includes a description of the data packet and the merge algorithm. You can find it here:
http://partners.adobe.com/public/developer/xml/index_arch.html
Alternately, you buy the LiveCycle server from Adobe which lets you do all this programmatically in a number of ways including through web service calls.
using (FileStream existingPdf = new FileStream("existing.pdf", FileMode.Open))
using (FileStream sourceXml = new FileStream("source.xml", FileMode.Open))
using (FileStream newPdf = new FileStream("new.pdf", FileMode.Create))
{
// Open existing PDF
PdfReader pdfReader = new PdfReader(existingPdf);
// PdfStamper, which will create
PdfStamper stamper = new PdfStamper(pdfReader, newPdf);
stamper.AcroFields.Xfa.FillXfaForm(sourceXml);
stamper.Close();
pdfReader.Close();
}
iTextSharp can work with XFA. To remove all doubts, please take a look at sample on iText website:
http://itextpdf.com/examples/iia.php?id=165
I have the same issue and I think I found the solution. I am using Powershell to inspect the pdf object.
Load the iTextSharp DLL.
Add-Type -Path "C:\Users\micah\Desktop\itextsharp.dll"
Load the PDF into a variable:
$PDF = New-Object iTextSharp.text.pdf.pdfreader -ArgumentList "C:\Users\micah\Desktop\test.pdf"
Inspect this object:
$PDF.AcroFields.XFA.DomDocument.XDP.DataSets.Data.TopMostSubForm | Get-Member
What you SHOULD see, is that all your fields on your PDF are in this object as a property.
You can get a quick view of all your fields like this:
$PDF.AcroFields.XFA.DomDocument.XDP.DataSets.Data.TopMostSubForm | Select-Object -Property "*"
That should be the magic ticket. This location is both fields from the original form AND the XFA portion of the form.
It says that in the book because itext does not do this. Can you convert your PDF?