iTextSharp | Embed xlsx file with export button in PDF in C# - c#

I want to attachment excel file in PDF with click_button, on click event it generate excel that contain PDF data.
Attaching a code that I'm using for this process, due to lack in knowledge of iTextSharp was not able to embed the file. I'm not using any kind of web service. Please help me out.
Thanks in Advance.
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileStream(dest, FileMode.Create));
PdfFileSpecification fs = PdfFileSpecification.FileEmbedded(stamper.Writer, null, "test.xlxs", null);
stamper.AddFileAttachment("Some Test File",fs);
stamper.Close();

Related

How to get binary data of PDF with out generating

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?

Generating PDF in windows forms using XML reader

My windows form contains a textbox in which we need to enter html tags,One button to generate PDF.
And we need to load the textbox content into XML Reader and process each element of XML recursively then we need to generate a PDF file.
The PDF file must contain the data i.e;
for example if I entered tag in the text box in the pdf file it must display a table.
I am very new to Windows forms and XML also can any one help me to complete this task
You would need to use a library to create PDF files. iTextSharp is a common library which can help. Take a look at this library and samples, you would be able to create PDF files easily from your application
https://sourceforge.net/projects/itextsharp/
iText is a PDF library that allows you to CREATE, ADAPT, INSPECT and MAINTAIN documents in the Portable Document Format (PDF):
iTextSharp is the .NET port.
Got Answer with this simple code
Document document = new Document();
PdfWriter.GetInstance(document, new FileStream(Request.PhysicalApplicationPath + "\\MySamplePDF.pdf", FileMode.Create));
document.Open();
iTextSharp.text.html.simpleparser.HTMLWorker hw =
new iTextSharp.text.html.simpleparser.HTMLWorker(document);
hw.Parse(new StringReader(htmlText));
document.Close();
but my problem is the path I want to select the path dynamically.Can any one help me how to set the path dynamically in the above code.
PdfWriter.GetInstance(document, new FileStream(Request.PhysicalApplicationPath + "\\MySamplePDF.pdf", FileMode.Create));
Changed this code by using a savefile dialog box
SaveFileDialog svg = new SaveFileDialog();
svg.ShowDialog();
PdfWriter.GetInstance(document, new FileStream(svg.FileName + ".pdf", FileMode.Create));

Unable to fill formfields in a PDF using iTextSharp

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

Using iTextSharp to write data to PDF works great, but Acrobat Reader asks 'Do you want to save changes' when closing file

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.

WPF/iTextSharp: How to send MemoryStream to Windows, for it to handle opening PDF?

I am using iTextSharp to generate PDF out of HTML. I can save the PDF file ok, but I want to handle the PDF for the OS to open it, without having to save it to disk first.
How can I do that? I am doing this from within a WPF application.
Here's my code so far:
MemoryStream memoryStream = new MemoryStream();
TextReader reader = new StringReader(tb.Text);
Document document = new Document(PageSize.A4, 30, 30, 30, 30);
PdfWriter pdfWriter = PdfWriter.GetInstance(document, memoryStream);
HTMLWorker worker = new HTMLWorker(document);
document.Open();
worker.StartDocument();
worker.Parse(reader);
worker.EndDocument();
worker.Close();
pdfWriter.CloseStream = false;
document.Close();
How can I "materialize" memoryStream.ToArray() into a .pdf file (in memory) and send it to Windows?
"Send to Windows" doesn't mean anything. Only a process knows how to deal with a PDF document. Like Adobe Acrobat. A process has no use for what you store in memory, it can't get to it. It needs a file. That's a non-issue in Windows, when you write a file you write to memory first. The file system cache. The difference between the disk and memory is very small in Windows, an important design feature of the operating system.

Categories