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);
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();
I receive a pdf as a byte[]. When I save this binary as pdf, pageSize is too big. I want to change the pageSize in the code.
Currently I am trying it this way, based on what I found in other questions:
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using (MemoryStream stream = new MemoryStream(pdfAsBinary))
{
using (PdfReader reader = new PdfReader(pdfAsBinary))
{
using (Document doc = new Document(PageSize.A4))
{
PdfWriter writer = PdfWriter.GetInstance(doc, stream);
PdfImportedPage page = writer.GetImportedPage(reader, 1);
image = Image.GetInstance(page);
using (var pdfStream = new FileStream(tempPath, FileMode.Create, FileAccess.ReadWrite, FileShare.Read))
{
PdfWriter pdfwriter = PdfWriter.GetInstance(doc, pdfStream);
doc.Open();
doc.SetPageSize(PageSize.A4);
image.ScalePercent(30f);
doc.Add(image);
doc.Close();
}
}
}
File.Copy(tempPath, pathToFile);
}
Or I tried using this method:
private static byte[] resizeToA4(byte[] inputDoc)
{
using (MemoryStream out = new MemoryStream())
{
using (PdfReader reader = new PdfReader(inputDoc))
{
using (Document doc = new Document(PageSize.A4))
{
PdfWriter writer = PdfWriter.GetInstance(doc, out);
}
}
return outPDF.ToArray();
}
None of the above is working and it feels like I'm overcomplicating things. How can I achieve my resizing of the pageSize to A4?
This method works for one-page-files.
public static void ScaleToA4self(byte[] pdfAsBinary, string locationOfPdfOut)
{
PdfReader reader = new PdfReader(pdfAsBinary);
Rectangle originalSize = reader.GetPageSize(1);
float originalHeight = originalSize.Height;
float originalWidth = originalSize.Width;
Rectangle newSize = PageSize.A4;
float newHeight = newSize.Height;
float newWidth = newSize.Width;
float scaleHeight = newHeight / originalHeight;
float scaleWidth = newWidth / originalWidth;
Document doc = new Document(newSize, 0, 0, 0, 0);
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(locationOfPdfOut, FileMode.Create));
doc.Open();
PdfContentByte cb = writer.DirectContent;
PdfImportedPage page = writer.GetImportedPage(reader, 1);
cb.AddTemplate(page, scaleWidth, 0, 0, scaleHeight, 0, 0);
doc.Close();
}
I am trying to export a div to pdf with style using code:
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + Fullname.Text + ".pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
var cssText = System.IO.File.ReadAllText(Server.MapPath("/css/bootstrap.css"));
var memoryStream = new MemoryStream();
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
pfd.RenderControl(htmlTextWriter);
StringReader stringReader = new StringReader(stringWriter.ToString().Replace("<br>", "<br/>").Replace("<td>", "<td/>").Replace("<tr>", "<tr/>").Replace("<td>", "<td/>"));
Document Doc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
PdfWriter writer = PdfWriter.GetInstance(Doc, Response.OutputStream);
Doc.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(writer, Doc, stringReader);
Doc.Close();
Response.Write(Doc);
Response.End();
How i can attach css file to pdf to keep same appearance in HTML?
Try below code,
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
byte[] pdf; // result will be here
var cssText = File.ReadAllText(MapPath("~/css/style.css"));
var html = File.ReadAllText(MapPath("~/css/index.html"));
using (var memoryStream = new MemoryStream())
{
var document = new Document(PageSize.A4, 20, 20, 30, 30);
var writer = PdfWriter.GetInstance(document, memoryStream);
document.Open();
using (var cssMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(cssText)))
{
using (var htmlMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(html)))
{
XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, htmlMemoryStream, cssMemoryStream);
}
}
document.Close();
pdf = memoryStream.ToArray();
}
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));
}
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.