How would you programmatically embed a SWF in a PDF? - 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.

Related

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:

Opening generated PDF in a browser using iText

I am using PDFStamper to generate a PDF file and then I want to pass it to be opened in a Browser. My code is in a JSP file. My code to actually generate a PDF to Desktop works but not to route to a browser. Below is my code.
PdfReader reader = new PdfReader("/path/pdfs/raw.pdf");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfStamper stamper = new PdfStamper(reader, baos);
PdfContentByte canvas = stamper.getOverContent(1);
BaseFont font = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
canvas.setFontAndSize(font, 12);
canvas.beginText();
canvas.showTextAligned(Element.ALIGN_LEFT, "TEST! TEST! TEST! TEST! ", 80, 713, 0);
canvas.endText();
stamper.close();
reader.close();
String filename="test.pdf";
response.setContentType("application/pdf");
response.setHeader( "Content-Disposition", "filename=" + filename );
response.setContentType("application/pdf");
OutputStream os = response.getOutputStream();
baos.writeTo(os);
os.flush();
This currently opens a blank page - I am not sure what exactly I am doing wrong.
I can make this work using iText Document but since I am opening an existing document and adding stuff to it I have to use PDFStamper and that is where the issue comes. I've confirmed the PDF file in reader exists and can be accessed via a browser by directly going to the location.
Any help would be appreciated!
Using, Struts2, Tile2, Weblogic, Java, iText

Using ContentByteUtils for raw PDF manipulation

This is a follow up question to:
Programmatically change the color of a black box in a PDF file?
I have a pdf I created in Illustrator that has basically a black shape in the middle of the page and nothing else. I need to change the color of that shape dynamically.
From the response to the post above I am using iTextSharp (.NET C#) to get the raw contents of the PDF through ContentByteUtils.GetContentBytesForPage() and changing the color at the raw level.
Problem is that I can't find any way of saving the results back into either the original PDF or a new PDF file via iTextSharp. I'm currently stuck with a byte array of the raw contents but need to figure out how to save.
Help please!
Why are you using ContentByteUtils.GetContentBytesForPage()?
I would use:
PdfReader reader = new PdfReader(src);
byte[] content = reader.GetPageContent(pageNumber);
// do stuff with content
reader.SetPageContent(pageNumber, content);
using (FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
using (PdfStamper stamper = new PdfStamper(reader, fs)) {
}
}

Java Image to PDF

How can an image file be converted into a PDF file using java? I am taking output from a graphic library. the output that I am able to export is in image formats like JPEG and PNG.
I want to convert that image file to PDF file.
You can use Itext to add an Image to a PDF.
Use IText PDF API for Java you must first download the IText JAR file from the IText website
First a Document instance is created.
Second, a PDFWriter is created, passing the Document instance and an OutputStream to its constructor. The Document instance is the document we are currently adding content to. The OutputStream is where the generated PDF document is written to.
OutputStream file = newFileOutputStream(newFile("/path/JavaGeneratedPDF.pdf"));
Document document = new Document();
PdfWriter.getInstance(document, file);
Here make sure that you handle DocumentException
Inserting Image in PDF
Image image = Image.getInstance ("/Image.jpg");
image.scaleAbsolute(200f, 100f); //image width,height
Here make sure that you handle MalformedURLException
Now Open PDF document, add image and close document instance
document.open();
document.add(image);
document.close();
file.close();

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.