Programmatically enable Adobe PDF usage rights - pdf

Is there any way to programmatically enable Adobe PDF usage rights from .net code ?
I'm using ITextSharp library to fill an XFA Form with XML Data (generated from app),
but the output PDF does not have usage rights enabled, thus the users cannot interact with it (that wouldn't normally be a problem, BUT the original PDF is gov supplied, and
the user must click some validation buttons, and that process is user/company specific)
This could be manually accomplished from Adobe Reader but you have to have an adobe acrobat professional licence..
Google is saying that "Only Adobe products can do that" ..
(http://old.nabble.com/Enable-Adobe-Reader-usage-rights-td14276927.html)
string pathPDF = #"C:\original.pdf";
string pathCreated = #"C:\created.pdf";
string pathXml = #"C:\data.xml";
using (PdfStamper stamper = new PdfStamper(new PdfReader(pathPDF), System.IO.File.OpenWrite(pathCreated)))
{
stamper.FormFlattening = false;
stamper.AcroFields.Xfa.FillXfaForm(pathXml);
stamper.Close();
}

The only way to do it programitically is to use Adobe Reader Extension Server. You can review Adobe whitepaper here: http://www.adobe.com/sea/products/server/readerextensions/pdfs/readerextensionsserver_ds.pdf
In the case above you would use iTextSharp to create Pdf document and then use Adobe Reader Extension Server to allow Pdf document to have extended functionality in Adobe Reader.
However, there is a small window that allows you to work with iTextSharp and fill-in already Reader-enabled PDF documents. If you have such Pdf document (Reader Enabled), then you can use iText/iTextSharp to fill in XFA data. You can check example here:
http://itextpdf.com/examples/iia.php?id=166
Good luck!

Currently only 2 products can enable usage rights:
Adobe Acrobat - for less that 500 users
Adobe LiveCycle Reader Extensions - more than 500 users
There have been some findings regarding this feature here.

No. Adobe uses Strong Crypto to ensure it... PPK I believe.
Google is saying that "Only Adobe products can do that"
That's because only Adobe products can do that. You can pay for some Acrobat server product or other... $$$... but that's it.

This worked for me:
string TempFilename = Path.GetTempFileName();
PdfReader pdfReader = new PdfReader(FileName);
//PdfStamper stamper = new PdfStamper(pdfReader, new FileStream(TempFilename, FileMode.Create));
PdfStamper stamper = new PdfStamper(pdfReader, new FileStream(TempFilename, FileMode.Create), '\0', true);
AcroFields fields = stamper.AcroFields;
AcroFields pdfFormFields = pdfReader.AcroFields;
foreach (KeyValuePair<string, AcroFields.Item> kvp in fields.Fields)
{
string FieldValue = GetXMLNode(XMLFile, kvp.Key);
if (FieldValue != "")
{
fields.SetField(kvp.Key, FieldValue);
}
}
stamper.FormFlattening = false;
stamper.Close();
pdfReader.Close()

you can complete it using PdfStamper
when using PdfStamper use thi code
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(
newPath, FileMode.CreateNew, FileAccess.Write), '\0', true);
if the form is Reader Extension enabled it will work

Related

Reader extensions not needed anymore for digital signature in Acrobat Reader DC?

In a business environment we have Adobe LiveCycle ES for several years.
A key feature is to enable "Reader extensions" in PDFs, which unlocks some features in Adobe Reader for reader extended PDFs. One of them was allowing user to digitally sign empty signature fields in Adobe Reader.
I remember nothing happened when clicking on the signature field in Adobe Reader if the PDF was not "reader extended". This is the case e.g. if the PDF was generated using iText. This limitation is still confirmed in iText FAQ which is supposed to be up-to-date as the project is active.
I recently reexecuted some old code and surprisingly the empty signature field could be signed in Acrobat Reader DC.
The code generating the PDF is the following :
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(filename));
document.open();
document.add(new Paragraph("Hello World!"));
document.close();
The code for adding the signature field :
PdfReader pdf = new PdfReader(inputstream);
PdfStamper stp = new PdfStamper(pdf, new FileOutputStream(filename));
PdfFormField sig = PdfFormField.createSignature(stp.getWriter());
sig.setWidget(new Rectangle(100, 100, 200, 200), null);
sig.setFlags(PdfAnnotation.FLAGS_PRINT);
sig.put(PdfName.DA, new PdfString("/Helv 0 Tf 0 g"));
sig.setFieldName("Signature1");
sig.setPage(1);
stp.addAnnotation(sig, 1);
stp.close();
What changed ? I guess Adobe Acrobat DC has removed some reader extensions requirements, but I could not find any release note explaining this.

Adobe Reader can't display unicode font of pdf added with iText

I'd like to stamp text to a certain pdf with iText. As this text can be cyrillic I use a unicode font and encoding.
The following sample code represents how I do it:
string inputFile = #"sampleStamped.pdf";
PdfReader reader;
PdfStamper stamper;
FileStream fs;
byte[] binaryPdf = File.ReadAllBytes(inputFile);
reader = new PdfReader(binaryPdf);
fs = new FileStream(inputFile, FileMode.Create, FileAccess.Write);
stamper = new PdfStamper(reader, fs);
BaseFont bf = BaseFont.CreateFont("c:/windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
PdfContentByte cb = stamper.GetOverContent(1);
Phrase p = new Phrase();
p.Font = new Font(bf, 25, Font.NORMAL, BaseColor.BLUE);
p.Add("Sample Text");
ColumnText.ShowTextAligned(cb, PdfContentByte.ALIGN_LEFT, p, 200, 200, 0);
if (stamper != null)
stamper.Close();
if (fs != null)
fs.Close();
if (reader != null)
reader.Close();
The program works as expected and without errors. But if I want to open the stamped pdf in Acrobat Reader 11 or DC on Windows it says that there are proplems to display content and the stamped text is not there.
I use itextsharp 5.5.8.
Any idea how to fix this problem?
Thanks
This is not an issue of iText(Sharp) but a quirk (a feature?) of Adobe Reader.
Your sample file claims to be a PDF 1.2 file. Adobe Reader seems to behave differently when confronted with composite fonts in PDF 1.2 files.
You can check this by patching your sampleStamped.pdf, simply replace the first bytes %PDF-1.2 by %PDF-1.3 and open the file in Adobe Reader... no problem anymore.
Thus, you should make sure that your stamped PDF claims to be at least PDF 1.3. If you stamp your PDF 1.2 file, you can do so by creating the PdfStamper like this:
stamper = new PdfStamper(reader, fs, (char)3);
The result:

iText embedded ttf font not visible in Adobe Reader

I'm stamping an existing PDF file with extra information using the iText library.
The extra information is text that should be rendered in a custom TTF font.
Problem is that the text is not visible in the Adobe Reader only.
Other PDF viewers, such as the default eVince reader in Ubuntu and the Google online PDF reader render the stamped text in the custom embedded font just fine.
I tried multiple encodings, such as Cp1251, BaseFont.Identity_H, ...
The code where the magic happens:
PdfReader pdfReader = new PdfReader(new FileInputStream(inputPdf));
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream("stamped.pdf"));
PdfContentByte canvas = pdfStamper.getOverContent(1);
String text = "The stamp";
BaseFont bf = BaseFont.createFont("assign.ttf", "Cp1251",BaseFont.EMBEDDED);
canvas.beginText();
canvas.setColorFill(BaseColor.BLUE);
canvas.setFontAndSize(bf, 13);
canvas.moveText(310, 600);
canvas.showText(text);
pdfStamper.close();
You have a syntax problem. Text state in PDF is marked with BT and ET. These operators are added using the beginText() and endText() methods. You have a BT, but no ET. Adobe Reader is more strict than the other viewers (that's why I prefer Adobe Reader over all other viewers: people should respect the syntax when writing code).
Add the following line before pdfStamper.close();
canvas.endText();
Better yet, read my book and you'll find out you can reduce the complexity of your code by using ColumnText.showTextAligned().

Dynamically Fill your PDF Forms using silverlight

string pdfTemplate = #"Trust App form.pdf";
string newFile = #"Trust App form Completed.pdf";
PdfReader pdfReader = new PdfReader(pdfTemplate);
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(
newFile, FileMode.Create));
AcroFields pdfFormFields = pdfStamper.AcroFields;
pdfFormFields.SetField("trust.trustee.entityname.line1", "Gulistan-e-Jauhar Karachi");
pdfStamper.FormFlattening = false;
pdfStamper.Close();
I am able to fill pdf forms using Itext sharp pdf.
But problem is this pdf is for .net. I WANT TO USE IT IN SILVERLIGHT.
Is there any alternative? for filling pdf form in silverlight... what i think itext sharp give silverpdf (http://silverpdf.codeplex.com/) but there is pdfstamper and acrofields classes in silverpdf.
SilverPDF looks to be inspired/based-on iTextSharp and PDFSharp, but it doesn't use an identical class layout by any means.
I just poked around in their code a bit (they have no docs that I could find), and it looks like you need to get the field's PdfAcroField object from the PdfAcroForm, which you get from a PdfDocument, which in turn you get from PdfReader.open(...).
When the docs aren't good enough, check the code if at all possible.

How would you programmatically embed a SWF in a PDF?

Is it possible to programmatically embed a SWF in a PDF from a C# application?
You can use the c# port of the iText library. It's called iTextSharp.
http://itextsharp.com/
An example of what the code could look like:
// create an output file stream which will be used to capture the generated file
string outputFileName = "output.pdf";
FileStream outputFile = new FileStream(outputFileName, FileMode.Create);
// open the original pdf file as a PdfReader
PdfReader reader = new PdfReader("inputfile.pdf");
// open the output pdf file as a PdfStamper
PdfStamper stamper = new PdfStamper(reader, outputFile);
// inserts a blank page at the start of the document
stamper.InsertPage(1, PageSize.A4);
// add the flash file to the output document as an annotation
PdfFileSpecification fs = PdfFileSpecification.FileEmbedded(stamper.Writer, flashFileTextBox.Text, flashFileTextBox.Text, null);
PdfAnnotation flashInsert = PdfAnnotation.CreateScreen(stamper.Writer, new Rectangle(50f,791f,545f,420f),"Flash Insert",fs, "application/x-shockwave-flash", true);
stamper.AddAnnotation(flashInsert,1);
stamper.Close();
reader.Close();
Flash SWF files can be embedded into a PDF document programmatically. Have a look at the PDF library from webSupergoo. The documentation includes examples in C# and VB.NET.