Generated pdf using Flying Saucer and Itext PdfReader Exception: com.itextpdf.text.exceptions.InvalidPdfException: PDF header signature not found - pdf

I got the following Exception when I try to create and instance of PdfReader from a pdf file generated using flying saucer
Exception:
com.itextpdf.text.exceptions.InvalidPdfException: PDF header signature not found.
instance creation:
com.itextpdf.text.pdf.PdfReader reader = new com.itextpdf.text.pdf.PdfReader(new File(filesList.get(0)).getAbsolutePath());
seems the flying saucer does not generate a valid pdf that starts with %PDF,
can you help me
flying saucer code
OutputStream os = new FileOutputStream(path);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(html);
renderer.layout();
renderer.createPDF(os);
renderer.finishPDF();
os.close();

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)) {
}
}

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().

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.