i just want to see all the report on a pdf format on button click.
i use ...
protected void Button1_Click(object sender, EventArgs e)
{
//Create Document class object and set its size to letter and give space left, right, Top, Bottom Margin
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
//Write some content
Paragraph paragraph = new Paragraph("This is my first line using Paragraph.");
Phrase pharse = new Phrase("This is my second line using Pharse.");
Chunk chunk = new Chunk(" This is my third line using Chunk.");
// Now add the above created text using different class object to our pdf document
doc.Add(paragraph);
doc.Add(pharse);
doc.Add(chunk);
doc.Close(); //Close document
}
but not effective
Your button click is simply creating a document in memory, writing to it and closing it.
You need to output the document to the Response.Output stream.
This works for me:
protected void PrintButton_Click(object sender, EventArgs e)
{
if (!Page.IsValid) return;
Response.ContentType = "application/pdf";
using (var document = new Document())
{
PdfWriter.GetInstance(document, Response.OutputStream);
document.Open();
document.Add(new Paragraph("Hello PDF!"));
document.Close();
}
}
The main thing that you were missing was the PdfWriter which writes the document to the Response.OutputStream
Related
I don't know how to solve this because the document seem close before the actual command even I put command to open it again. Please help.
This is my code. When I click the button it will do this and the error will occur at doc.close() line. It shown "Cannot access a closed file." Even I put doc.open() above.
private void run_Click(object sender, EventArgs e)
{
Document doc = new Document(PageSize.A4);
using(FileStream op = new FileStream("text.pdf", FileMode.Create))
{
PdfWriter wri = PdfWriter.GetInstance(doc, op);
Paragraph p = new Paragraph("test");
doc.Open();
doc.Add(p);
}
using (FileStream op = new FileStream("text.pdf", FileMode.Append, FileAccess.Write))
{
PdfWriter wri = PdfWriter.GetInstance(doc, op);
Paragraph p = new Paragraph("test2");
doc.Open();
doc.Add(p);
doc.Close();
}
}
First of all, it is not a correct way to add some contents to an existing PDF by appending a PDF file. If you want to add contents to an existing PDF, please check ITextSharp insert text to an existing pdf.
However, if you just want it to work, you just need to create a new Document instance every time.
private void run_Click(object sender, EventArgs e)
{
Document doc = new Document(PageSize.A4);
using(FileStream op = new FileStream("text.pdf", FileMode.Create))
{
PdfWriter wri = PdfWriter.GetInstance(doc, op);
Paragraph p = new Paragraph("test");
doc.Open();
doc.Add(p);
}
using (FileStream op = new FileStream("text.pdf", FileMode.Append, FileAccess.Write))
{
doc = new Document(PageSize.A4); // this is the fix
PdfWriter wri = PdfWriter.GetInstance(doc, op);
Paragraph p = new Paragraph("test2");
doc.Open();
doc.Add(p);
doc.Close();
}
}
So I've been using itextsharp to create and download a file to the client. The PDF is created, but is created using the wrong file extension. It is downloaded as "webform1.aspx" But if I change the file extension it is correct. I need to learn how to change the file name when downloading using the memory stream, or a different method if needed. Code below, it is executed via a button on a blank webform.
protected void Button1_Click(object sender, EventArgs e)
{
// Create a Document object
Document document = new Document(PageSize.A4, 50, 50, 25, 25);
// Create a new PdfWriter object, specifying the output stream
MemoryStream output = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(document, output);
// Open the Document for writing
document.Open();
// Create a new Paragraph object with the text, "Hello, World!"
var welcomeParagraph = new Paragraph("Hello, World!");
// Add the Paragraph object to the document
document.Add(welcomeParagraph);
document.Close();
Response.ContentType = "pdf/application";
Response.BinaryWrite(output.ToArray());
}
You can add a content-disposition header with filename to Response object
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
I'm trying to pass image to PDF file, but what it does it takes image Size Mode (in PDF file while showing me on form as a stretchimage) as Normal even if I set it to stretchimage. Here is My code:
private void button14_Click(object sender, EventArgs e)
{
SaveFileDialog svg = new SaveFileDialog();
svg.ShowDialog();
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(svg.FileName + ".pdf", FileMode.Create));
doc.Open();
iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance(pictureBox1.Image, System.Drawing.Imaging.ImageFormat.Jpeg);
doc.Add(png);
doc.Close();
}
I am using c#.net and need for some data to be displayed in a .pdf file. I am able to do so by using a PdfTable but then it is displayed in a tabular format.
I want a simple format like:
I dont want the exact tabular format.I am using iTextSharp dll.
Is it possible to display data in the above metioned format?
Any suggestions are welcome..
You can use PdfParagraph class to render genreal text in PDF
So when you say that you don't want exact tabular format are you talking about the borders? If so, I'd recommend using a PdfPTable and turning the borders off then. Cells in a PdfPTable are all based on the table's DefaultCell, so to turn the borders off for a table called t you'd do this:
t.DefaultCell.Border = 0;
Below is a full working WinForms app targetting iTextSharp 5.1.1.0 that does exactly that:
//PdfPTable Example
using System;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//PDF file to output
string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Output.pdf");
//Create a basic stream to write to
using (FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
//Create a new PDF document
using (Document doc = new Document())
{
//Bind a the document to the stream
using (PdfWriter w = PdfWriter.GetInstance(doc, fs))
{
//Open our PDF for writing
doc.Open();
//Insert a new page into our output PDF
doc.NewPage();
//Create a three column table
PdfPTable t = new PdfPTable(3);
//Remove the border from the table
t.DefaultCell.Border = 0;
//For the first row we some extra padding
t.DefaultCell.PaddingBottom = 10;
t.AddCell("ColumnA");
t.AddCell("ColumnB");
t.AddCell("ColumnC");
//Reset the padding for the remaining cells
t.DefaultCell.PaddingBottom = 0;
t.AddCell("Value1");
t.AddCell("Value1");
t.AddCell("Value1");
t.AddCell("Value2");
t.AddCell("Value2");
t.AddCell("Value2");
//Add the table to the document
doc.Add(t);
//Close our output PDF
doc.Close();
}
}
}
this.Close();
}
}
}
If for some reason you don't want to do this with a PdfPTable you can try hacking it together with a Paragraph but unless alignment isn't important you are going to give yourself a headache. The sample below uses paragraphs and formats each "column" within the paragraph to be 20 characters each, padding spaces as necessary. NOTE: This is 20 characters of a variable-width font so things don't actually align. If you want that, either use the PdfPTable above or you are going to have to measure strings which is not fun.
//Paragraph Example
using System;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//PDF file to output
string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Output.pdf");
//Create a basic stream to write to
using (FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
//Create a new PDF document
using (Document doc = new Document())
{
//Bind a the document to the stream
using (PdfWriter w = PdfWriter.GetInstance(doc, fs))
{
//Open our PDF for writing
doc.Open();
//Insert a new page into our output PDF
doc.NewPage();
//Right-pad each "column" for a total of 20 characters
string FormatString = "{0,-20}{1,-20}{2,-20}";
//Add the first row
doc.Add(new Paragraph(String.Format(FormatString, "ColumnA", "ColumnB", "ColumnC")));
//Add a blank line
doc.Add(new Paragraph(""));
//Add the two data rows
doc.Add(new Paragraph(String.Format(FormatString, "Value1", "Value1", "Value1")));
doc.Add(new Paragraph(String.Format(FormatString, "Value2", "Value2", "Value2")));
//Close our output PDF
doc.Close();
}
}
}
this.Close();
}
}
}
In iTextSharp dll, you just pass your data by creating table/tr/td structure in string like this :
string strData = string.Emrty;
strData = "<table border='0' cellpadding='0' cellspacing='0' width='100%'>";
strData += "<tr>";
strData += "<td>";
strData += "</td>";
strData += "</tr>";
strData += "</table>";
by creating table/tr/td structure of your data, just pass this variable data to pdf, then you will get your resulted output in pdf.
How do one create PDF in memorystream instead of physical file using itextsharp.
The code below is creating actual pdf file.
Instead how can I create a byte[] and store it in the byte[] so that I can return it through a function
using iTextSharp.text;
using iTextSharp.text.pdf;
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("c:\\Test11.pdf", FileMode.Create));
doc.Open();//Open Document to write
Paragraph paragraph = new Paragraph("This is my first line using Paragraph.");
Phrase pharse = new Phrase("This is my second line using Pharse.");
Chunk chunk = new Chunk(" This is my third line using Chunk.");
doc.Add(paragraph);
doc.Add(pharse);
doc.Add(chunk);
doc.Close(); //Close document
Switch the filestream with a memorystream.
MemoryStream memStream = new MemoryStream();
PdfWriter wri = PdfWriter.GetInstance(doc, memStream);
...
return memStream.ToArray();
using iTextSharp.text;
using iTextSharp.text.pdf;
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
byte[] pdfBytes;
using(var mem = new MemoryStream())
{
using(PdfWriter wri = PdfWriter.GetInstance(doc, mem))
{
doc.Open();//Open Document to write
Paragraph paragraph = new Paragraph("This is my first line using Paragraph.");
Phrase pharse = new Phrase("This is my second line using Pharse.");
Chunk chunk = new Chunk(" This is my third line using Chunk.");
doc.Add(paragraph);
doc.Add(pharse);
doc.Add(chunk);
}
pdfBytes = mem.ToArray();
}
I've never used iTextPDF before but it sounded interesting so I took upon the challenge and did some research on my own. Here's how to stream the PDF document via memory.
protected void Page_Load(object sender, EventArgs e)
{
ShowPdf(CreatePDF2());
}
private byte[] CreatePDF2()
{
Document doc = new Document(PageSize.LETTER, 50, 50, 50, 50);
using (MemoryStream output = new MemoryStream())
{
PdfWriter wri = PdfWriter.GetInstance(doc, output);
doc.Open();
Paragraph header = new Paragraph("My Document") {Alignment = Element.ALIGN_CENTER};
Paragraph paragraph = new Paragraph("Testing the iText pdf.");
Phrase phrase = new Phrase("This is a phrase but testing some formatting also. \nNew line here.");
Chunk chunk = new Chunk("This is a chunk.");
doc.Add(header);
doc.Add(paragraph);
doc.Add(phrase);
doc.Add(chunk);
doc.Close();
return output.ToArray();
}
}
private void ShowPdf(byte[] strS)
{
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=" + DateTime.Now);
Response.BinaryWrite(strS);
Response.End();
Response.Flush();
Response.Clear();
}
Where your code has new FileStream, pass in a MemoryStream you've already created. (Don't just create it inline in the call to PdfWriter.GetInstance - you'll want to be able to refer to it later.)
Then call ToArray() on the MemoryStream when you've finished writing to it to get a byte[]:
using (MemoryStream output = new MemoryStream())
{
PdfWriter wri = PdfWriter.GetInstance(doc, output);
// Write to document
// ...
return output.ToArray();
}
I haven't used iTextSharp, but I suspect some of these types implement IDisposable - in which case you should be creating them in using statements too.