Save pdf file with user input filename (iTextSharp) - c#

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

Related

C#, iTextSharp 5.5.10 Shown "Cannot access a closed file." on document close command

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

Passing picturebox image into PDF file

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

C# Save Bitmap as PDF With iTextSharp

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

How to rotate text in iTextSharp?

I have searched the web but it looks like that it is not that easy, how can i rotate my text?
Document doc = new Document(new iTextSharp.text.Rectangle(600, 800), 0, 0, 0, 0);
PdfWriter.GetInstance(doc, new FileStream(Directory.GetCurrentDirectory() + "/genpdf.pdf", FileMode.Create));
doc.Open();
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(file);
Chunk c1 = new Chunk("~Comment~"); //rotate 270°
doc.Add(image);
doc.Add(text);
doc.Close();
The answer is in Rotate text answer
The writer is a PdfWriter type, you can get it like:
using (stream = new FileStream(temp_filename, FileMode.Create))
{
iTextSharp.text.Document document = new iTextSharp.text.Document();
PdfWriter writer = PdfWriter.GetInstance(document, stream);

Create PDF in memory instead of physical file

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.

Categories