I have my PDF code and existing PDF layout where I am adding the data and creating the new pdf file, but I am not able to create my new pdf file or not able to download.
I need to read the newly generate PDF file and need to written the binary data
Please review below code
MemoryStream pdfms = new MemoryStream();
PdfReader reader;
reader = new PdfReader(HttpContext.Current.Server.MapPath("20171010_BillTemplate.pdf"));
PdfStamper formFiller = new PdfStamper(reader, pdfms);
AcroFields pdfBillingFields = formFiller.AcroFields;
pdfBillingFields.SetField("CT_Mail_Block",MailBlock.ToUpper());// some data
pdfBillingFields.SetField("Cash_Only", Cash_Only);
formFiller.FormFlattening = true;
formFiller.Writer.CloseStream = false;
reader = new PdfReader(pdfms); // giving error
formFiller.Close();
pdfms.Dispose();
What I need is with or with out Creating newly created file I need to get the binary data of file and send in return.
I had implemented this code in webapi
Reader is giving error as
PDF header signature not found.
Please say is this a right way to get the binary data from above code?
Related
I have a PDF document already stored in the database. It's stored as an "image" datatype (I had no choice with that). I'm working in C# .NET with iText 7, and the database is MSSQL. I want to parse that database data into smaller sections of that file. I don't have the original PDF file, only the image stored in the database.
I want to separate the original PDF data (image) into pages. That is, if we had the original file, I want to split that large file by every 2 pages into new files (e.g., a 10 page PDF would become 5 files, 2 pages each). I want to then store those smaller "PDFs" into the databases as well.
Is there any way to do this entirely in code? Or do I need to create a PDF file in the file system, and then create new files based on the file, and then import the individual files back into the database?
Thanks for the help.
Read about MemoryStream, that can hold your data without creating the file. Example,
// database part here
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
byte[] pdf = (byte[])dr["image_pdf"];
MemoryStream ms = new MemoryStream(picarr);
ms.Seek(0, SeekOrigin.Begin);
// PDF part here
Document doc = new Document();
PdfWriter writer = PdfWriter.GetInstance(doc, ms);
doc.Open();
...
I am using iText to fill a pdf. This pdf contains a xfa from, I fill this form through a xml file . After filling the form user need to download it and sign it manually. So far so good everything works fine every field in form filled properly. If user sign the file adobe reader make a new copy of file with signature.
But when user try to sign that pdf it gives following error and the newly generated file with signature doesn't save data, It make all field blank.
At least one signature has problems
When user sign pdf adobe reader also give a popup on signature verification
But if i fill the same pdf manually without using itext it allow me to sign pdf successfully
this is the code that i am using to fill pdf with xml data :
public static byte[] FillXfaForm(byte[] byteArray, String xmlFilePath)
{
PdfReader reader = new PdfReader(byteArray);
using (MemoryStream ms = new MemoryStream())
{
using (PdfStamper stamper = new PdfStamper(reader, ms,'\0',true))
{
stamper.Writer.CloseStream = false;
stamper.AcroFields.Xfa.FillXfaForm(xmlFilePath);
}
if(File.Exists(xmlFilePath))
{
File.Delete(xmlFilePath);
}
return ms.ToArray();
}
}
And here is the screen shot of error message
Please help me to solve this issue.
Finally I fix this problem
The cause of this problem is that i update whole xml document to fill xfa from but when i only update the data part not the whole xml it works without any error.
I don't know what is the difference it really create as in this similar question "Bruno Lowagie" state that you can either use full xml replace or you can change data part only.
How can I set XFA data in a static XFA form in iTextSharp and get it to save?
But for me it allow me to sign the document only if I replace data part not the whole xml data.
I hope it will help someone facing similar problem.
I tried to use iTextSharp 5.5.9 to redact PDF files. The problem is when I redact a large rectangle field on a PDF, it can not save the file. This is the code:
PdfReader reader1 = new PdfReader(new FileStream(DesFile, FileMode.Open));
Stream fs = new FileStream(DesFile, FileMode.Open);
PdfStamper stamper = new PdfStamper(reader1, fs);
List<PdfCleanUpLocation> cleanUpLocations = new List<PdfCleanUpLocation>();
cleanUpLocations.Add(new PdfCleanUpLocation(1, new Rectangle(77f,77f,600f,600f), BaseColor.GRAY));
PdfCleanUpProcessor cleaner = new PdfCleanUpProcessor(cleanUpLocations, stamper);
cleaner.CleanUp();
stamper.Close();
reader1.Close();
I use the http://sox.sourceforge.net/sox.pdf to test, if I change the Rectangle to
new Rectangle(77f,77f,200f,200f)
It will work well... But when I change back the larger Rectangle:
new Rectangle(77f,77f,600f,600f)
It stops working. Please help!
iText development usually warns against stamping to the same file the underlying PdfReader reads from. If done as in the OP's code, reading and writing operations can get into each other's way, the results being unpredictable.
After using different files to read from and write to, the OP's solution started working.
If one first reads the source file into memory as a byte[] and then constructs the PdfReader from that array, it is possible to use the same file as output of a PdfStamper operating on that reader. But this pattern is not recommended either: If some problem occurs during stamping, the original file contents may already have been removed, so you have neither the unstamped original PDF nor a stamped result PDF.
It might be embarrassing to have to explain to the client that his documents are completely gone for good...
I tried to fill my pdf using iTextsharp library.I took reference from this tutorial fill pdf using iTextsharp. But when I tried this example with my pdf file it showed nothing. When I read my pdf file in pdf reader it contains null.
string pdfTemplate = #"c:\authform.pdf";
// create a new PDF reader based on the PDF template document
PdfReader pdfReader = new PdfReader(pdfTemplate);
In this I get null when I read my pdf template.Is there some special format in which the pdf template should be so that it can work with this example.
If you check the tutorial there are two examples.
one is to read fields from existing pdf.
second one is to print data to pdf.
The following is the way to print data to pdf.
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile, FileMode.Create));
AcroFields pdfFormFields = pdfStamper.AcroFields;
// set form pdfFormFields
// The first worksheet and W-4 form
pdfFormFields.SetField("f1_01(0)", "1");
pdfStamper.Close();
I'm using iTextSharp 5.3.2.0 to add information to an existing PDF file that contains a W-2 form. Everything is working perfectly and the PDF file looks great when written into the browser's response stream; however, when the user is done looking at the PDF, he is asked "Do you want to save changes to 'W2.pdf' before closing?" every time he views the document from the web page.
In trying to narrow the problem down, I've actually stripped out all of my modifications but the problem continues. Here's the simple version of my code, with my data-writing call commented out:
PdfReader pdfReader = new PdfReader(dataSource.ReportTemplate);
using(MemoryStream outputStream = new MemoryStream())
using (PdfStamper pdfStamper = new PdfStamper(pdfReader, outputStream))
{
//dataSource.DrawDataFields(pdfStamper);
pdfStamper.FormFlattening = true;
return outputStream;
}
In this case, the "empty" PDF is written to the browser and looks good, but I still get asked, "Do you want to save" when I close the Acrobat window.
At this point I was thinking that there was something wrong with the source PDF file. However, when I send back the PDF file's raw bytes to the browser, I am NOT asked the "Do you want to save" question when using the code below.
byte[] bytes = File.ReadAllBytes(dataSource.ReportTemplate);
using (MemoryStream outputStream = new MemoryStream())
{
outputStream.Write(bytes, 0, bytes.Length);
return outputStream;
}
My conclusion is that iTextSharp is doing something "bad" to the PDF in the process of opening it and writing the bytes to the stream, but I'm new to iTextSharp and could easily be missing something.
FWIW, this is Acobat Reader 10.1.4 that we're talking about.
EDIT: The original PDF used as a template is approximately 80K in size. If I look at the temporary file that's been streamed down through my browser, the PDF file written by iTextSharp is approximately 150K. However, when I answer "Yes" to the "Save Changes" question asked by Acrobat Reader, the resulting file is approximately 80K again. iTextSharp is definitely doing something unexpected to this file.
Non-working:
public byte[] MergeDataByDrawing(int copies)
{
PdfReader pdfReader = new PdfReader(reportTemplate);
using (MemoryStream outputStream = new MemoryStream())
using (PdfStamper pdfStamper = new PdfStamper(pdfReader, outputStream))
{
pdfStamper.FormFlattening = true;
return outputStream.GetBuffer();
}
}
Working:
public byte[] MergeDataByDrawing(int copies)
{
PdfReader pdfReader = new PdfReader(reportTemplate);
using (MemoryStream outputStream = new MemoryStream())
using (PdfStamper pdfStamper = new PdfStamper(pdfReader, outputStream))
{
pdfStamper.FormFlattening = true;
return outputStream.ToArray();
}
}
Seems the GetBuffer method is a problem. I don't understand why, but I'll take the result!
Props to MKL for giving me an idea and Fredrik for the right example at the right time.
See http://itextpdf.com/history/?branch=52&node=521
Bugfix AcroForms: In some cases, Adobe Reader X asks if you want to
"save changes" after closing a flattened PDF form. This was due to the
presence of some unnecessary entries in the /AcroForm dictionary (for
instance added when the form was created with OOo).
I'm the Bruno who fixed this bug. I remember that it occurred in Adobe Reader 10, but not in Adobe Reader 9. I was able to fix the bug because the person reporting it was a customer who sent me a PDF that showed this behavior.
If you would share your PDF, we could take a look and see what other entries should be removed from the /AcroForm dictionary. I only removed those that were added when the form is created using Open Office. If you don't want to share the PDF, the cause will always remain a mystery.