I am trying to do something really simple with ABCpdf. My goal is to add page numbers to existing PDFs (Generated through telerik - PDF export).
The problem I am having is that after reading a PDF and then saving it the file becomes corrupt.
My code:
Doc pdfDocument = new Doc();
pdfDocument.Read(path);
MemoryStream outputMemoryStream = new MemoryStream();
pdfDocument.Save(outputMemoryStream);
pdfDocument.Clear();
Response.BinaryWrite(outputMemoryStream.ToArray());
outputMemoryStream.Close();
Any advice would be greatly appreciated.
I am using ABCpdf 8.
Take a look at my answer Add page numbers to a Pdf document
This explains how to add page numbering to the pdf chain.
Looking at your code the issue seams to be that you are rewritting to the current file in memory. Try reading the file in creating a new file with the added page numbering then if you need to, delete the first document and rename the second.
Related
Well, after spending quite some time playing with this, I think I may have found a solution soon after I posted the question. Bit embarrassing but hey, it seems to work now.
I don't want to delete the question yet, in case someone wants the details. Basically, I tried again point (c) but in the XML I put back I used an embedded image. So, it seems I can change the XFA template master page using iText. More testing to follow ...
=========================
The problem: Apply watermark to a PDF XFA (dynamic or not). At the end the PDF should still be an XFA and should have all its security settings intact. I don't have control over the PDFs coming in.
The question: Can I use iText 5 to do that? If yes, is it via PdfStamper.getUnderContent() or via XfaForm.setXfa() or something else ? The first 2 didn't work for me yet.
I wonder if it's some XFA detail that I am missing (i.e. when I try to replace the XML), not sure how the XFA is actually protected from changes. Do I need to generate some UUID, encrypt some things, something about signatures ...
Btw, if I take the PDF generated by iText after replacing the XML, then open and save in LC, the watermark shows
thanks,
Cristian
========================
Anyway, that's the short version of my question. If you think you can help and/or are interested in more details ...
I know this or similar questions came up before but what I have tried so far didn't work for me. I also admit I am not a IText, XFA or PDF Standard expert. I browsed a number of forums posts, the Itext book, browsed the specs, no luck yet.
The PDF input has no regular usage rights, no security. When opened in Acrobat it will show the restrictions on Changing Document/Document Assembly/Adding templates but I have a feeling all XFAs have that. Nothing comes back from the following ...
System.out.println(" permissions " + reader.getPermissions());
System.out.println(" usage rights " + reader.hasUsageRights());
System.out.println(" viewer pref" + reader.getSimpleViewerPreferences());
Here are some of the things I have been through:
a) doing in it LC (https://forums.adobe.com/thread/496558)
This works. If I try to place an image on the master page, then it shows as watermark on all pages when saved from LC.
b) trying to write under using iText (https://sourceforge.net/p/itext/mailman/message/17225398/)
I found a post from '07 suggesting using pdfStamper.getUnderContent(). The thread does not seem to conclude if it did work eventually for the person asking
For me, the code works for a simple PDF, but not for XFA
PdfContentByte under = pdfStamper.getUnderContent(1);
under.beginText();
BaseFont FONT = BaseFont.createFont("c:/windows/fonts/times.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED);
under.setFontAndSize(FONT, 40);
under.showTextAligned(Element.ALIGN_CENTER, "TEST_TEXT", 200, 600, 45);
under.endText();
c) generate the XML and replace it using iText (Some pdf file watermark does not show using iText)
After reading the post above and section 8.6 from itext in action, it seemed this is the correct path, so:
I create a simple XFA using LiveCycle
Save as xfa1.pdf and extract xml (using iText) to xfa1.xml
in LC add an image to the masterpage and save as xfa2.pdf
open xfa2.pdf and notice the watermark is present
extract XML from xfa2 to xfa2.xml and compare to xfa1.xml - notice the image element
either place the image element in original XML or use xfa2.xml and replace the XFA in original xfa1.pdf using iText, let's call it xfa3_itext;
Relevant code:
XfaForm xfa = new XfaForm(reader);
DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
fact.setNamespaceAware(true);
DocumentBuilder db = fact.newDocumentBuilder();
Document doc = db.parse(new FileInputStream(xml));
xfa.setDomDocument(doc);
xfa.setChanged(true);
XfaForm.setXfa(xfa, stamper.getReader(), stamper.getWriter());
I have tried with a sample PDF that was provided by a customer and with a simple XFA form created in LiveCycle Designer, no luck. If I open xfa3_itext in LiveCycle it does the watermark and if I save it again from LiveCycle as dynamic XFA PDF, the new PDF shows the watermark.
phew, that is a long post ... sorry.
thanks for reading and for any feedback
In my iOS app, I would like to regenerate an existing pdf into another pdf after the users are done annotating on the existing pdf.
My regenerated pdf should be an exact replica of the existing pdf but should have embedded annotations and highlights etc which can be opened and viewed on desktops as well.
I have done some research on this including the solutions proposed on other SO posts. I have tried libharu etc.
But somehow I am not able to convert an existing pdf into a replica pdf. I am able to add annotations to a new pdf I create using libharu.
Now my problem is to copy the existing pdf as is to my regenerated pdf. Any pointers will be much helpful.
My understanding is that a library that can save back out a PDF with "true" annotations (those that can be hidden in Acrobat, for example) is not something that exists in a FOSS solution.
LibHaru, for example, only supports creating new PDFs, not editing or appending existing PDFs. From their homepage:
At this moment libHaru does not support reading and editing existing
PDF files and it's unlikely this support will ever appear.
You can render the PDF on a page by page basis, and then re-save it with some additional information. This S.O question has a reasonable looking piece of code. That will save any "annotations" more as an image in the PDF itself, though.
You might try a paid library like PDFNet.
I have a system that generates PDF-files using itextsharp and mails them to my users. And the files grows in a way that is not ok.
I start of with one 1 page word document 28 KB.
I print this one page word document using adobes printer and pdf file gets 73 KB.
I open the document in Adobe Acrobat X, insert my forms and save, 1055 KB.
I load the document in itextsharp and set the 30 different values and now my file is 2031 KB.
Is there any compression flags or tricks that can be set in itextsharp or in Adobe that keeps my file at ~73 KB. I don't add any images or any other media. just text.
BR
Andreas
I've had similar fun with PDF file size growing. I've gotten form template generation down to a process where I get my template down to the smallest size possible before I send it to itextsharp to have it populate it.
In your step 3 you are adding your acrofields for your form. After you've finished adding all your fields and saved the document, Used the "Reduce File Size..." option to help shrink the document a little bit. Then you can also use the PDF Optimizer to further reduce file size. I personally use a work around when I use the Create PDF from multiple files feature, but only add the one document I'm working on and then select the smaller file size option for lower quality optimized PDFs.
Then in step 4 it depends on how you are populating and generating the PDF files. Our process uses the template PDFs generated and copies each page onto a new document with the form fields filled. When copying instead of using the PDFCopy class we use the PDFSmartCopy class which will copy content to the new document, but will not duplicate content that is identical. After switching to the smart copy class we saw a significant reduction in file size generated by itextsharp.
Hope this helps.
I have been using iTextSharp to generate PDF documents for over a year. Unfortunately, with the release of Adobe Reader X, my PDFs now cause a "Do you want to Save?" dialog to appear when closing the PDF document. This does not happen with PDFs that are not generated with iTextSharp. It's really annoying for my users who are opening and closing PDF documents all day long. Are there any properties in iTextSharp that I can set to prevent this from happening?
If it helps, I am using a PdfReader to read data from an existing PDF document (this original document does not cause the Save dialog to appear). I then use a PdfWriter to create a new document and AddTemplate to copy a portion of the original document to the new one.
The problem is this line:
Response.OutputStream.Write(MS.GetBuffer(), 0, MS.GetBuffer().Length)
The GetBuffer method returns the entire internal buffer which is larger that the actual content. The bad PDF has about 10kb of garbage content at the end (bytes of zero), the good PDF has only a few garbage bytes. Use the ToArray() method of the memory stream to get the PDF file and the problem will be fixed. You will also get smaller files.
byte[] pdf = MS.ToArray();
Response.OutputStream.Write(pdf, 0, pdf.Length);
Also set the "Content-Length" with the length of the pdf array.
Also add
HttpContext.Current.Response.End();
After completion of your PDF file.
After concatenating 2 pdf's using PdfCopyFields, I am able to edit the PDF fields, but I can't save the merged PDF. Why am I not able to save the PDF after doing this?
A form that has been Reader Enabled must be saved in append mode in order to continue working as such. PdfCopyFields creates a new PDF from scratch.
To get this working, you'll have to duplicate most of PdfCopyField's code via a PdfStamper set to append. Hurray for open source.
And keep in mind that iText 5.x is AGPL. Anyone with access to the program output must also have access to the source.
(which is why quite a few people are still using 2.1.7)