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();
}
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 want to allow user to enter his own file name, just like save file dialog and stream (Example: Stream s = File.Open(sfdPdf.FileName, FileMode.CreateNew)
Here is my code:
private void btnSave_Click(object sender, EventArgs e)
{
System.Drawing.Rectangle bounds = this.Bounds;
using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
{
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
}
bitmap.Save("Image.jpeg", ImageFormat.Jpeg);
}
Document doc = new Document(PageSize.LETTER, bounds.Left, bounds.Right, bounds.Top, bounds.Bottom);
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("ImageTest.pdf", FileMode.Create));
doc.Open();
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance("Image.jpeg");
doc.Add(image);
doc.Close();
}
I want the part "ImageTest.pdf" to be named as the user want with pdf extension (and .pdf filetype).
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream("ImageTest.pdf", FileMode.Create));
Can anyone help or does anyone has better solution for my problem?
I want to take screenshot of my windows form and export image to pdf file under user input name
EDIT:
With saveFileDialog (after bitmap.save) - Receiving error "Format Error: Not a PDF or corrupted."
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Pdf File |*.pdf";
if (sfd.ShowDialog() == DialogResult.OK)
{
using (Stream s = File.Open(sfd.FileName, FileMode.CreateNew))
{
Document doc = new Document(PageSize.LETTER, bounds.Left, bounds.Right, bounds.Top, bounds.Bottom);
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("ImageTest.pdf", FileMode.Create));
doc.Open();
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance("Image.jpeg");
doc.Add(image);
doc.Close();
s.Close();
s.Dispose();
}
}
I am not an expert of ITextSharp, but I think that your code should be something like this
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Pdf File |*.pdf";
if (sfd.ShowDialog() == DialogResult.OK)
{
Document doc = new Document(PageSize.LETTER, bounds.Left, bounds.Right, bounds.Top, bounds.Bottom);
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(sfd.FileName, FileMode.Create));
doc.Open();
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance("Image.jpeg");
doc.Add(image);
doc.Close();
}
In other words, just pass the FileName string choosen in SaveFileDialog to the PdfWriter.GetInstance method
The following code creates a bitmap from a control on the form, and then shows a save dialog to save as a JPEG. Can anyone help with the code to save the Bitmap bm as a PDF with iTextSharp?
Bitmap bm = null;
bm = new Bitmap(this.RCofactorTBS.SelectedTab.Width, this.RCofactorTBS.SelectedTab.Height);
this.RCofactorTBS.SelectedTab.DrawToBitmap(bm, this.RCofactorTBS.SelectedTab.ClientRectangle);
SaveFileDialog dialog = new SaveFileDialog();
dialog.Filter = "JPEG|*.jpeg";
dialog.Title = "Save Test As Jpeg";
dialog.ShowDialog();
if (dialog.FileName != "" && bm != null)
{
bm.Save(dialog.FileName);
}
You can try this
System.Drawing.Image image = System.Drawing.Image.FromFile("Your image file path");
Document doc = new Document(PageSize.A4);
PdfWriter.GetInstance(doc, new FileStream("image.pdf", FileMode.Create));
doc.Open();
iTextSharp.text.Image pdfImage = iTextSharp.text.Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Jpeg);
doc.Add(pdfImage);
doc.Close();
Referenced from here
public void exportarPDF(Bitmap img){
// System.Drawing.Image image = System.Drawing.Image.FromFile("C://snippetsource.jpg"); // Here it saves with a physical file
System.Drawing.Image image = img; //Here I passed a bitmap
Document doc = new Document(PageSize.A4);
PdfWriter.GetInstance(doc, new FileStream("C://image.pdf", FileMode.Create));
doc.Open();
iTextSharp.text.Image pdfImage = iTextSharp.text.Image.GetInstance(image,
System.Drawing.Imaging.ImageFormat.Jpeg);
doc.Add(pdfImage);
doc.Close();
}
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