I created a simple pdf with iText.
But why is the position of the text on the first page higher than on all the other pages.
Here is some test code to see where the problem situates:
MemoryStream PDFData = new MeMemoryStream PDFData = new MemoryStream();
Document document = new Document(PageSize.A4, 50, 50, 80, 50);
PdfWriter PDFWriter = PdfWriter.GetInstance(document, PDFData);
document.Open();
Moviecollection movCol = new Moviecollection();
foreach (Movie mov in movCol.Movies)
{
Phrase phr = new Phrase(mov.Description);
document.Add(phr);
document.Add(Chunk.NEWLINE);
}
document.Close();
Any ideas?
thanks,
Filip
I think its to do with Chunk.NEWLINE addition.
I'm guessing you are simulating a paragraph with that Phrase + Newline combination. If you switch to Paragraph object instead, the problem is solved (tested on my machine with your code).
using(MemoryStream PDFData = new MemoryStream())
using(Document document = new Document(PageSize.A4, 50, 50, 80, 50))
{
PdfWriter PDFWriter = PdfWriter.GetInstance(document, PDFData);
document.Open();
Moviecollection movCol = new Moviecollection();
foreach (Movie mov in movCol.Movies)
document.Add(new Paragraph(mov.Description));
}
Related
Ifound some example.
However, all of the examples use PdfReader.
I want to use PDFWriter.
Below is the code I wrote.
private void CreatePdf(string strPdfPath)
{
FileStream fs = new FileStream(strPdfPath, FileMode.Create, FileAccess.Write, FileShare.None);
Document document = new Document(PageSize.A4, 45, 45, 80, 80);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
document.Open();
document.AddTitle("This is Title");
document.AddCreationDate();
Paragraph content1 = new Paragraph("This is first Page");
document.Add(content1);
document.NewPage();
Paragraph content2 = new Paragraph("This is second Page");
document.Add(content2);
writer.Close();
fs.Close();
}
How to rotate the PDF?
You could do both:
document.open();
// Add some content in portrait
document.setPageSize(PageSize.A4.rotate());
document.newPage();
// Add some content in landscape
document.close();
Currently I have a I create a pdf with itextsharp:
using (MemoryStream stream = new System.IO.MemoryStream())
{
//Document pdfDoc = new Document(PageSize.A4, 25, 25, 65, 15);
float myWidth = 227f;
float myHeight = 842f;
var pgSize = new Rectangle(myWidth, myHeight);
using (var pdfDoc = new Document(pgSize, 2, 2, 1, 30))
{
var pdfWriter = PdfWriter.GetInstance(pdfDoc, stream);
//pdfWriter.PageEvent = new ITextEvents();
pdfDoc.Open();
//code
I need to know that if the whole page was written, do a line break, to add a footer at the end, without overwriting the last one. How could I do it?
I am opening a pdf template and creating a new pdf and write on it. On the loop I have to enter dynamic data from a list, I am subtracting the height variable to put one item after the other vertically. My issue is that when the list is too long and is approaching the end of the pdf page the code is not creating a new blank page to continue writing the rest items of the list. However, it is overwriting the rest items one above the other in last item of the page. I have try to increase my height variable but still the code is not working to produce new page.
string pdfpath = context.Server.MapPath(#"~/mypdf_new.pdf");
string oldFile = context.Server.MapPath(#"~/mypdftemplate.pdf");
string newFile = pdfpath;
PdfReader reader = new PdfReader(oldFile);
iTextSharp.text.Rectangle size = reader.GetPageSizeWithRotation(1);
Document document = new Document(size);
FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
document.Open();
string myFont = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts),
"Calibri.TTF");
iTextSharp.text.pdf.BaseFont bfR;
bfR = iTextSharp.text.pdf.BaseFont.CreateFont(myFont,
iTextSharp.text.pdf.BaseFont.IDENTITY_H,
iTextSharp.text.pdf.BaseFont.EMBEDDED);
BaseFont title = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
iTextSharp.text.Font titleFont = new iTextSharp.text.Font(title, 10, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.BLACK);
int myheight = 500;
foreach (var mypro in listofproducts)
{
ColumnText ct26 = new ColumnText(cb);
Phrase myText26 = new Phrase(mypro.ProductCode, titleFont);
ct26.SetSimpleColumn(myText26, 60, myheight, 580, 317, 15, Element.ALIGN_LEFT);
ct26.Go();
ColumnText ct28 = new ColumnText(cb);
Phrase myText28 = new Phrase(Convert.ToString(mypro.quantity), titleFont);
ct28.SetSimpleColumn(myText28, 340, myheight, 580, 317, 15, Element.ALIGN_LEFT);
ct28.Go();
myheight = myheight - 20;
}
PdfImportedPage page = writer.GetImportedPage(reader, 1);
cb.AddTemplate(page, 0, 0);
document.Close();
fs.Close();
writer.Close();
reader.Close();
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);
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.