Spire.PDF prints pdf only in grayscale - c#

I am using the free version of Spire.PDF to print a local pdf file but the print is in grayscale even though the pdf file is in color. Here is my code
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(fileName);
doc.ColorSpace = PdfColorSpace.RGB;
PrintDialog dialogPrint = new PrintDialog();
dialogPrint.AllowPrintToFile = true;
dialogPrint.AllowSomePages = true;
dialogPrint.PrinterSettings.MinimumPage = 1;
dialogPrint.PrinterSettings.MaximumPage = doc.Pages.Count;
dialogPrint.PrinterSettings.FromPage = 1;
dialogPrint.PrinterSettings.ToPage = doc.Pages.Count;
if (dialogPrint.ShowDialog() == DialogResult.OK)
{
//Set the pagenumber which you choose as the start page to print
doc.PrintFromPage = dialogPrint.PrinterSettings.FromPage;
//Set the pagenumber which you choose as the final page to print
doc.PrintToPage = dialogPrint.PrinterSettings.ToPage;
//Set the name of the printer which is to print the PDF
doc.PrinterName = dialogPrint.PrinterSettings.PrinterName;
PrintDocument printDoc = doc.PrintDocument;
printDoc.DefaultPageSettings.Color = true;
dialogPrint.Document = printDoc;
printDoc.Print();
}
I have checked dialogPrint.PrinterSettings.SupportsColor returns true

Related

Can't print jpg with Aspose.Pdf

I want to load in a JPEG file and print it with Aspose.Pdf in C# (.net Framework 4.8). The code I currently have is:
public void PrintImage(string fileToPrint, string printerName, string jobName)
{
System.Drawing.Image srcImage = System.Drawing.Image.FromFile(fileToPrint);
int h = srcImage.Height;
int w = srcImage.Width;
var doc = new Document();
var page = doc.Pages.Add();
var image = new Image();
image.File = (fileToPrint);
page.PageInfo.Height = (h);
page.PageInfo.Width = (w);
page.PageInfo.Margin.Bottom = (0);
page.PageInfo.Margin.Top = (0);
page.PageInfo.Margin.Right = (0);
page.PageInfo.Margin.Left = (0);
page.Paragraphs.Add(image);
var viewer = new PdfViewer(doc);
PrintUsingViewer(viewer, printerName, jobName);
}
private static void PrintUsingViewer(PdfViewer viewer, string printerName, string jobName)
{
viewer.AutoResize = true; // Print the file with adjusted size
viewer.AutoRotate = true; // Print the file with adjusted rotation
viewer.PrintPageDialog = false; // Do not produce the page number dialog when printing
var ps = new System.Drawing.Printing.PrinterSettings();
var pgs = new System.Drawing.Printing.PageSettings();
ps.PrinterName = printerName;
viewer.PrinterJobName = jobName;
viewer.PrintDocumentWithSettings(pgs, ps);
viewer.Close();
}
When I save the document instead of printing and look at it, it seems fine (the image is added). However, when trying to print the image it is not printed and the page is just blank..
I would like to print without first saving the document as a PDF and then trying to print that saved PDF. Does anyone see what I am doing wrong?
The solution was adding this line of code
doc.ProcessParagraphs();
right after this line:
page.Paragraphs.Add(image);
So the code now becomes
public void PrintImage(string fileToPrint, string printerName, string jobName)
{
System.Drawing.Image srcImage = System.Drawing.Image.FromFile(fileToPrint);
int h = srcImage.Height;
int w = srcImage.Width;
var doc = new Document();
var page = doc.Pages.Add();
var image = new Image();
image.File = (fileToPrint);
page.PageInfo.Height = (h);
page.PageInfo.Width = (w);
page.PageInfo.Margin.Bottom = (0);
page.PageInfo.Margin.Top = (0);
page.PageInfo.Margin.Right = (0);
page.PageInfo.Margin.Left = (0);
page.Paragraphs.Add(image);
doc.ProcessParagraphs();
var viewer = new PdfViewer(doc);
PrintUsingViewer(viewer, printerName, jobName);
}
private static void PrintUsingViewer(PdfViewer viewer, string printerName, string jobName)
{
viewer.AutoResize = true; // Print the file with adjusted size
viewer.AutoRotate = true; // Print the file with adjusted rotation
viewer.PrintPageDialog = false; // Do not produce the page number dialog when printing
var ps = new System.Drawing.Printing.PrinterSettings();
var pgs = new System.Drawing.Printing.PageSettings();
ps.PrinterName = printerName;
viewer.PrinterJobName = jobName;
viewer.PrintDocumentWithSettings(pgs, ps);
viewer.Close();
}
Now the image is printed correctly!

How to remove header and footer in pdf

generate html content to pdf using IronPdf, but in pdf generate 3 pages header content and footer in separate pages , how to display in single page hole content.
var Renderer = new IronPdf.HtmlToPdf();
//Renderer.PrintOptions.Header.DrawDividerLine = false;
//Renderer.PrintOptions.Footer.DrawDividerLine = false;
Renderer.PrintOptions.PaperSize = PdfPrintOptions.PdfPaperSize.A4;
Renderer.PrintOptions.CssMediaType = PdfPrintOptions.PdfCssMediaType.Screen;
Renderer.PrintOptions.PaperOrientation = PdfPrintOptions.PdfPaperOrientation.Portrait;
// Renderer.PrintOptions.MarginTop = 10; //millimeters
// Renderer.PrintOptions.MarginBottom = 10;
Renderer.PrintOptions.Zoom = 125;
//Renderer.PrintOptions.FirstPageNumber = 1;
Renderer.PrintOptions.CreatePdfFormsFromHtml = true;
Renderer.PrintOptions.FitToPaperWidth = true;
Renderer.PrintOptions.InputEncoding = Encoding.UTF8;
Renderer.PrintOptions.FitToPaperWidth = true;
pdf will be generate like this: enter image description here
Add the header and footer as follows:
var PDF = Renderer.RenderHTMLFileAsPdf("HTML Body");
PDF.AddHTMLHeaders(new HtmlHeaderFooter() {
HtmlFragment = "HTMLHeader"
});
PDF.AddHTMLFooters(new HtmlHeaderFooter()
{
Height=20,
HtmlFragment = "HTML Footer"
});
https://ironpdf.com/object-reference/api/IronPdf.PdfDocument.html?q=AddHTMLHeaders#IronPdf_PdfDocument_AddHTMLHeaders_IronPdf_HtmlHeaderFooter_System_Double_System_Double_System_Double_System_Boolean_System_Collections_Generic_IEnumerable_System_Int32__

How to print .docx silently with c#

I want to print a .docx file silently and being able to choose the tray of the printer.
At first I tried to print the .docx with the Microsoft.Office.Interop.Word but word is opening...
After I converted the .docx file to an image and printed it with ProcessStartInfo but it shows a printing window to the user.
ProcessStartInfo info = new ProcessStartInfo(imageFilePath);
info.Verb = "Print";
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(info);
I tried another method it print the image silently BUT the image is blured and not scaled correctly.
PrinterSettings settings = new PrinterSettings();
string defaultPrinter = settings.PrinterName;
FileInfo fileInfo = new FileInfo(imageFilePath);
PrintDocument pd = new PrintDocument();
pd.DocumentName = fileInfo.Name;
pd.PrintPage += (sender, args) =>
{
Image i = Image.FromFile(imageFilePath);
PrintPageEventArgs arguments = args;
System.Drawing.Rectangle m = new System.Drawing.Rectangle()
{
Y = 0,
X = 0,
Location = new System.Drawing.Point(0, 0),
Height = args.MarginBounds.Height,
Size = args.MarginBounds.Size,
Width = args.MarginBounds.Width
};
if ((double)i.Width / (double)i.Height > (double)m.Width / (double)m.Height)
{
m.Height = (int)((double)i.Height / (double)i.Width * (double)m.Width);
}
else
{
m.Width = (int)((double)i.Width / (double)i.Height * (double)m.Height);
}
args.Graphics.DrawImage(i, m);
};
pd.Print();
So is it possible to print a .docx silently and being able to choose the tray of the printer ?
Did anyone face the same issue. Any help in this regard. Thanks in advance.
I did something very similar to this myself but I never looked up the documentation if you could choose the tray. I believe these are set on the print server itself (if you are using one) and would be able to reference those if your application has the access rights.
string PrinterName = #"\\Server\nameOfThePrinter";
ProcessStartInfo printProcessInfo = new ProcessStartInfo()
{
Verb = "PrintTo",
CreateNoWindow = true,
FileName = pdfFileName,
Arguments = "\"" + PrinterName + "\"",
WindowStyle = ProcessWindowStyle.Hidden
};
Process printProcess = new Process();
printProcess.StartInfo = printProcessInfo;
printProcess.Start();
printProcess.WaitForInputIdle();
printProcess.WaitForExit(10000);
if (printProcess.HasExited)
{
}else
{
printProcess.Kill();
}
return true;
Also, you may want to investigate this article here https://www.codeproject.com/Tips/598424/How-to-Silently-Print-PDFs-using-Adobe-Reader-and
Cheers!
I found a solution I couldn't print a .docx silently so I converted it as a .png image before.
Link to convert .docx to .png
Here is the code to print the image :
PrinterSettings settings = new PrinterSettings();
string PrinterName = settings.PrinterName;
//set paper size
PaperSize oPS = new PaperSize
{
RawKind = (int)PaperKind.A4
};
//choose the tray here
PaperSource oPSource = new PaperSource
{
RawKind = (int)PaperSourceKind.Upper
};
PrintDocument printDoc = new PrintDocument
{
PrinterSettings = settings,
};
//set printer name here it's the default printer
printDoc.PrinterSettings.PrinterName = PrinterName;
printDoc.DefaultPageSettings.PaperSize = oPS;
printDoc.DefaultPageSettings.PaperSource = oPSource;
printDoc.PrintPage += new PrintPageEventHandler((sender, args) =>
{
System.Drawing.Image img = System.Drawing.Image.FromFile(imageFilePath);
int printHeight = (int)printDoc.DefaultPageSettings.PrintableArea.Height;
int printWidth = (int)printDoc.DefaultPageSettings.PrintableArea.Width;
int leftMargin = 0;
int rightMargin = 0;
args.Graphics.DrawImage(img, new System.Drawing.Rectangle(leftMargin, rightMargin, printWidth, printHeight));
});
printDoc.Print();
printDoc.Dispose();

How can I print XPS without dialog?

I try to print to XPS printer (it's not my default printer), but the program opens me a dialog.
Can I skip the dialog? This is the code:
pSettings = new PrinterSettings();
pSettings.PrintFileName = "test.xps";
RawPrinterHelper.SendStringToPrinter(pSettings.PrinterName, toSend);
spcn = new StandardPrintController();
printDocument1.PrinterSettings.PrinterName = "Microsoft XPS Document Writer";
printDocument1.PrintController = spcn;
printDocument1.PrintPage +=
new PrintPageEventHandler(printDocument1_PrintPage);
printDocument1.Print();
You can print PDF to XPS without a dialog using Aspose API
Aspose API
//Create PdfViewer object and bind PDF file
PdfViewer pdfViewer = new PdfViewer();
pdfViewer.OpenPdfFile("input.pdf");
//Set PrinterSettings and PageSettings
System.Drawing.Printing.PrinterSettings printerSetttings = new System.Drawing.Printing.PrinterSettings();
printerSetttings.Copies = 1;
printerSetttings.PrinterName = "Microsoft XPS Document Writer";
//Set output file name and PrintToFile attribute
printerSetttings.PrintFileName = "C:\\tempfiles\\printoutput.xps";
printerSetttings.PrintToFile = true;
**//Disable print page dialog**
**pdfViewer.PrintPageDialog = false;**
//Pass printer settings object to the method
pdfViewer.PrintDocumentWithSettings(printerSetttings);
pdfViewer.ClosePdfFile();

Ignored Paper Size in PrintDialog/XPS Document Writer

I am trying to print with WPF's PrintDialog class (namespace System.Windows.Controls in PresentationFramework.dll, v4.0.30319). This is the code that I use:
private void PrintMe()
{
var dlg = new PrintDialog();
if (dlg.ShowDialog() == true)
{
dlg.PrintVisual(new System.Windows.Shapes.Rectangle
{
Width = 100,
Height = 100,
Fill = System.Windows.Media.Brushes.Red
}, "test");
}
}
The problem is no matter what Paper Size I select for "Microsoft XPS Document Writer", the generated XPS, always, has the width and height of "Letter" paper type:
This is the XAML code I can find inside XPS package:
<FixedPage ... Width="816" Height="1056">
Changing the paper size in the print dialog only affects the PrintTicket, not the FixedPage content. The PrintVisual method produces Letter size pages, so in order to have a different page size you need to use the PrintDocument method, like so:
private void PrintMe()
{
var dlg = new PrintDialog();
FixedPage fp = new FixedPage();
fp.Height = 100;
fp.Width = 100;
fp.Children.Add(new System.Windows.Shapes.Rectangle
{
Width = 100,
Height = 100,
Fill = System.Windows.Media.Brushes.Red
});
PageContent pc = new PageContent();
pc.Child = fp;
FixedDocument fd = new FixedDocument();
fd.Pages.Add(pc);
DocumentReference dr = new DocumentReference();
dr.SetDocument(fd);
FixedDocumentSequence fds = new FixedDocumentSequence();
fds.References.Add(dr);
if (dlg.ShowDialog() == true)
{
dlg.PrintDocument(fds.DocumentPaginator, "test");
}
}

Categories

Resources