How to open a PDF automatically using itext in C#? - 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.

Related

how to print a pdf file without using adobe reader in 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.

iText7 usage rights and Adobe Reader

I'm revisiting some old code in an attempt to get this working again. I'm using the code from the iText KB here -> https://kb.itextpdf.com/home/it7kb/faq/how-to-fill-xfa-form-using-itext-without-breaking-usage-rights.
It seems even using that it somehow still breaks the pdf and not then editable with Adobe Reader. Last time I couldn't post the form but have now managed to strip out any important stuff so at least someone can test...hopefully.
I did create a super basic form in Livecycle and reader enable it in Acrobat DC and using the code below it worked fine, yet for some reason this form always breaks.
Here is the code I'm using.
String source = #"D:\Temp\SVTESTx.pdf";
String dest = #"D:\Temp\SVAx.PDF";
PdfReader preader = new PdfReader(source);
PdfDocument pdfDoc=new PdfDocument(preader, new PdfWriter(dest), new StampingProperties().UseAppendMode());
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true);
XfaForm xfa = form.GetXfaForm();
xfa.FillXfaForm(new FileStream(#"D:\Temp\SVTEST.xml", FileMode.Open, FileAccess.Read));
xfa.Write(pdfDoc);
pdfDoc.Close();
The files in question are here (for the non-working PDF) -> https://drive.google.com/drive/folders/1MET19PUubd-J9D4fbzkX1KJAUbn0aMCZ?usp=sharing
Cheers

Unable to create PDF from HTML in Xamarin Android

I am developing an Application in Xamarin for Android. I have already generated HTML file using StringBuilder. Now I have a HTML file in my External storage and the same template is required for PDF. So when I try to convert HTML to PDF using iTextSharp using XML Worker & PDFSharp libraries, I am getting build errors due to missing System.Drawing.dll. Then I found from Xamarin forums & Stackoverflow links that it is not supported for Xamarin.Android.
Can anyone please tell me other alternative about how to create template for PDF or any other working nuget package for Xamarin.Android which will convert html file to pdf.
NOTE: I am able to generate PDF but not able to convert HTML to PDF.
It would be of great help!. Thanks a ton!.
Use Nuget package Xam.iTextSharpLGPL
Below is the sample code
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using Android.Graphics;
string path = Android.OS.Environment.ExternalStorageDirectory.Path;
string pdfPath = System.IO.Path.Combine(path, "samplee.pdf");
System.IO.FileStream fs = new FileStream(pdfPath, FileMode.Create);
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
HTMLWorker worker = new HTMLWorker(document);
document.Open();
StringBuilder html = new StringBuilder();
html.Append("<? xml version='1.0' encoding='utf-8' ?><html><head><title></title></head>");
html.Append("<CENTER>Simple Sample html</H1>");
html.Append("<H4>By User1</H4>");
html.Append("<H2>Demonstrating a few HTML features</H2>");
html.Append("</CENTER>");
html.Append("<p>HTML doesn't normally use line breaks for ordinary text. A white space of any size is treated as a single space. This is because the author of the page has no way of knowing the size of the reader's screen, or what size type they will have their browser set for.");
html.Append("</p></body</html>");
TextReader reader = new StringReader(html.ToString());
worker.StartDocument();
worker.Parse(reader);
worker.EndDocument();
worker.Close();
document.Close();
writer.Close();
fs.Close();

Encrypt PDF document using iTextSharp

I want to make my PDF document protected by not allowing fill in and copy from it. I am using iTextSharp for this. I have following code:
PdfReader reader = new PdfReader(document, System.Text.Encoding.UTF8.GetBytes(PASSWORD));
using (MemoryStream ms = new MemoryStream())
{
using (PdfStamper stamper = new PdfStamper(reader, ms))
{
stamper.SetEncryption(
null,
Encoding.ASCII.GetBytes(PASSWORD),
PdfWriter.ALLOW_PRINTING,
PdfWriter.ENCRYPTION_AES_128);
}
}
reader.Close();
When the document is generated I use that code to encrypt the document. But later when I open the document in Adobe Reader (tested on 9 and 11) and check the 'File > Properties > Security' their are no restrictions applied on fill in and copy of the document and their status is Allowed.
Is there any issue in that code?
According to the ITextSharp documentation for PdfStamper, the second parameter to this method is an output stream representing the destination for the encrypted PDF document data. The code you show in the question simply disposes the MemoryStream after you setup the encryption so any changes this code could apply to your PDF document will never be saved to disk or otherwise be available outside your application.

Fill PDF Form with Itextsharp

I am trying to fill up a form with ITextsharp, and trying out the following code to get all the fields in the pdf:
string pdfTemplate = #"c:\Temp\questionnaire.pdf";
PdfReader pdfReader = new PdfReader(pdfTemplate);
StringBuilder sb = new StringBuilder();
foreach (var de in pdfReader.AcroFields.Fields)
{
sb.Append(de.Key.ToString() + Environment.NewLine);
}
But the foreach loop is always null count. Do I need to do something to file itself as I have tried the example from here and it works fine... this is an example of pdf I am trying to fill
any ideas?
Edit ::
As it turned out, the PDF "form" to fill in actually wasn't a form (in PDF terms) at all. Thus, your have two choices:
You add the text to the page contents directly using hardcoded or configured "field" positions and dimensions as described by #tschmit007 in comments to his answer.
You add actual PDF form fields to your PDF to generate a true PDF form which you take as template to fill in later.
You can add actual form fields either using some graphical tool allowing that, e.g. Adobe Acrobat, or you can use iText(Sharp). Have a look at chapter 8 of iText in Action — 2nd Edition and the samples available here for Java and here for .Net.
Those samples mostly add form fields to newly generated PDF documents. You can virtually use the same code, though, for adding form fields to a PdfStamper which exposes its inner PdfWriter using stamper.getWriter() in Java and the stamper.Writer in C#. Instead of writer.addAnnotation(field) you have to use stamper.addAnnotation(field, page), though.
try:
using (FileStream outFile = new FileStream("result.pdf", FileMode.Create)) {
PdfReader pdfReader = new PdfReader("file.pdf");
PdfStamper pdfStamper = new PdfStamper(pdfReader, outFile);
AcroFields fields = pdfStamper.AcroFields;
//rest of the code here
//fields.SetField("n°1", "value");
//...
pdfStamper.Close();
pdfReader.Close();
}

Categories

Resources