how to print a pdf file without using adobe reader in C#? - c#

I have a pdf in ticket format of an 80mm invoice. How can I print without using adobe reader.
Is there a library that can help me work?
I tried various tools but none gave me a solution, even using adobe reader there are times when it does not send to print and it is uncomfortable for the client.

You may try Spire.PDF nuget package which supports multiple printing scenarios.
The following code shows how to print a PDF using the default printer:
using Spire.Pdf;
namespace PrintWithDefaultPrinter
{
class Program
{
static void Main(string[] args)
{
//Create PdfDocument object
PdfDocument doc = new PdfDocument();
//Load a PDF file
doc.LoadFromFile(#"C:\Users\Administrator\Desktop\sample.pdf");
//Print with default printer
doc.Print();
}
}
}
More printing options can be found in this documentation: C#/VB.NET: Print PDF Documents.

Related

How to add custom property/metadata to PDF 2.0 file

I am trying to add custom property/metadata to PDF 2.0 file (like this customprop). I have used PDFSharp, ITextSharp and PDFBox but I couldn't. Is there any free package that can fix the problem.
link for sample pdf 2.0
Have you tried SpirePDF? There is a free Nuget that you can try, it worked fine for me with that snippet:
using Spire.Pdf;
namespace App
{
class Program
{
static void Main(string[] args)
{
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(#"C:\Users\User\Downloads\output.pdf");
doc.DocumentInformation.SetCustomProperty("Name", "Test");
doc.DocumentInformation.SetCustomProperty("Company", "StackOverflow");
doc.SaveToFile(#"C:\Users\User\Downloads\result.pdf");
}
}
}
You can add custom metadata using Docotic.Pdf library like that:
// NOTE:
// When used in trial mode, the library imposes some restrictions.
// Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx for more information.
BitMiracle.Docotic.LicenseManager.AddLicenseData("temporary or permanent license key here");
using (var pdf = new PdfDocument("Simple_pdf_2.0_file.pdf"))
{
pdf.Metadata.Custom.Properties.Add("CustomKey", "CustomValue");
pdf.Save("SetCustomXmpProperties.pdf");
}
Here is the more comprehensive sample: https://github.com/BitMiracle/Docotic.Pdf.Samples/tree/master/Samples/Metadata/SetCustomXmpProperties
Disclaimer: I am the co-author of Docotic.Pdf library.

How to open a PDF automatically using itext in C#?

I have a Windows Form App which automatically generates a PDF using itext. I was using the following code previously to generate the PDF:
namespace ConsoleApp2 {class Program{static void Main(string[] args){
Document document = new Document();
PdfWriter.GetInstance(document, new FileStream("C:/Users/bra/800C.pdf", FileMode.Create));
document.Open();
document.Add(Chunk.NEWLINE);
Paragraph p17 = new Paragraph("Use formulations of Penzien (2000) assuming full slip condition. ");
document.Add(p17);
document.Close();
Console.ReadLine();
Is there a way that my code could be modified so that the PDF can be opened automatically when the program is run? (Rather than being saved on the C drive)? I have seen some examples with memoryStream online but couldn't wrap my head around how this works.

Using C# iText 7 to flatten an XFA PDF

Is it possible to use iText 7 to flatten an XFA PDF? I'm only seeing Java documentation about it (http://developers.itextpdf.com/content/itext-7-examples/itext-7-form-examples/flatten-xfa-using-pdfxfa).
It seems like you can use iTextSharp, however to do this.
I believe it's not an AcroForm PDF because doing something similar to this answer How to flatten pdf with Itext in c#? simply created a PDF that wouldn't open properly.
It looks like you have to use iTextSharp and not iText7. Looking at the NuGet version it looks like iTextSharp is essentially the iText5 .NET version and like Bruno mentioned in the comments above, the XFA stuff simply hasn't been ported to iText7 for .NET.
The confusion stemmed from having both iText7 and iTextSharp versions in NuGet and also the trial page didn't state that the XFA worker wasn't available for the .NET version of iText7 (yet?)
I did the following to accomplish what I needed at least for a trial:
Request trial copy here: http://demo.itextsupport.com/newslicense/
You'll be emailed an xml license key, you can just place it on your desktop for now.
Create a new console application in Visual Studio
Open the Project Manager Console and type in the following and press ENTER (this will install other dependencies as well)
Install-Package itextsharp.xfaworker
Use the following code:
static void Main(string[] args)
{
ValidateLicense();
var sourcePdfPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "<your_xfa_pdf_file>");
var destinationPdfPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "output.pdf");
FlattenPDF(sourcePdfPath, destinationPdfPath);
}
private static void ValidateLicense()
{
var licenseFileLocation = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "itextkey.xml");
iTextSharp.license.LicenseKey.LoadLicenseFile(licenseFileLocation);
}
private static void FlattenPDF(string sourcePdfPath, string destinationPdfPath)
{
using (var sourcePdfStream = File.OpenRead(sourcePdfPath))
{
var document = new iTextSharp.text.Document();
var writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(destinationPdfPath, FileMode.Create));
var xfaf = new iTextSharp.tool.xml.xtra.xfa.XFAFlattener(document, writer);
sourcePdfStream.Position = 0;
xfaf.Flatten(new iTextSharp.text.pdf.PdfReader(sourcePdfStream));
document.Close();
}
}
The trial will put a huge watermark on the resulting PDF, but at least you can get it working and see how the full license should work.
For IText 7 this could be done in the following way
LicenseKey.LoadLicenseFile(#"Path of the license file");
MemoryStream dest_File = new MemoryStream();
XFAFlattener xfaFlattener = new XFAFlattener();
xfaFlattener.Flatten(new MemoryStream( File.ReadAllBytes(#"C:\\Unflattened file")), dest_File);
File.WriteAllBytes("flatten.pdf", dest_File.ToArray());

AmyuniPDF prints PDF document in wrong font (special characters)

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.

Reading doc and docx files using C# without having MS Office installed on server

I'm working on a project (asp.net, c#, vb 2010, .net 4) and I need to read both DOC and DOCX files, that I've previosly uploaded (I've done uploading part). Tricky part is that I don't have MS Office installed on server and that I can't use it.
Is there any public library that I can include into my project without having to install anything?
Both docs are very simple:
NUMBER TAB STRING
NUMBER TAB STRING
NUMBER TAB STRING
...
I need to extract number and string for each row (paragraph).
May someone help with this? I should repeat once again that I'm limited in a way that I can't install anything on a server.
We can now use open source, NPOI (.NET port of Apache POI) library which also supports docx, xls & xlsx.
DocX is also another open source library for creating word docs.
For DOCX I'd suggest Open XML API, though Microsoft developed Open XML to create office files through the XML files communicating with this API, the latest version 2.5 was released in 2013 which is 5 years ago.
you can use Code7248.word_reader.dll
below is the sample code on how to use Code7248.word_reader.dll
add reference to this DLL in your project and copy below code.
using System;
using System.Collections.Generic;
using System.Text;
//add extra namespaces
using Code7248.word_reader;
namespace testWordRead
{
class Program
{
private void readFileContent(string path)
{
TextExtractor extractor = new TextExtractor(path);
string text = extractor.ExtractText();
Console.WriteLine(text);
}
static void Main(string[] args)
{
Program cs = new Program();
string path = "D:\Test\testdoc1.docx";
cs.readFileContent(path);
Console.ReadLine();
}
}
}
Update: NPOI supports docx now. Please try the latest release (NPOI 2.0 beta)
You can do like this:
using System.IO;
using System.Text;
using Spire.Doc;
namespace ReadTextLineByLine{
class Program {
static void Main(string[] args) {
//Create a Document object
Document doc = new Document();
//Load a Word file
doc.LoadFromFile(#"C:\Users\Administrator\Desktop\data.docx");
//Convert the text in Word line by line into a txt file
doc.SaveToTxt("result.text", Encoding.UTF8);
//Read all lines of txt file
string[] lines = File.ReadAllLines("result.text", System.Text.Encoding.Default);
}
}
}

Categories

Resources