Ciao a tutti,
ho provato con questa funzione:
public static byte[] WriteToPdf2(FileInfo sourceFile, string stringToWriteToPdf)
{
PdfReader reader = new PdfReader(sourceFile.FullName);
using (MemoryStream memoryStream = new MemoryStream())
{
//
// PDFStamper is the class we use from iTextSharp to alter an existing PDF.
//
PdfStamper pdfStamper = new PdfStamper(reader, memoryStream);
for (int i = 1; i <= reader.NumberOfPages; i++) // Must start at 1 because 0 is not an actual page.
{
//
// If you ask for the page size with the method getPageSize(), you always get a
// Rectangle object without rotation (rot. 0 degrees)?in other words, the paper size
// without orientation. That?s fine if that?s what you?re expecting; but if you reuse
// the page, you need to know its orientation. You can ask for it separately with
// getPageRotation(), or you can use getPageSizeWithRotation(). - (Manning Java iText Book)
//
//
Rectangle pageSize = reader.GetPageSizeWithRotation(i);
//
// Gets the content ABOVE the PDF, Another option is GetUnderContent(...)
// which will place the text below the PDF content.
//
PdfContentByte pdfPageContents = pdfStamper.GetUnderContent(i);
pdfPageContents.BeginText(); // Start working with text.
//
// Create a font to work with
//
BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, Encoding.ASCII.EncodingName, false);
pdfPageContents.SetFontAndSize(baseFont, 40); // 40 point font
pdfPageContents.SetRGBColorFill(255, 0, 0); // Sets the color of the font, RED in this instance
//
// Angle of the text. This will give us the angle so we can angle the text diagonally
// from the bottom left corner to the top right corner through the use of simple trigonometry.
//
float textAngle =
(float)FooTheoryMath.GetHypotenuseAngleInDegreesFrom(pageSize.Height, pageSize.Width);
//
// Note: The x,y of the Pdf Matrix is from bottom left corner.
// This command tells iTextSharp to write the text at a certain location with a certain angle.
// Again, this will angle the text from bottom left corner to top right corner and it will
// place the text in the middle of the page.
//
pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_CENTER, stringToWriteToPdf,
pageSize.Width / 2,
pageSize.Height / 2,
textAngle);
pdfPageContents.EndText(); // Done working with text
}
pdfStamper.FormFlattening = true; // enable this if you want the PDF flattened.
pdfStamper.Close(); // Always close the stamper or you'll have a 0 byte stream.
byte[] content = memoryStream.ToArray();
// Write out PDF from memory stream.
using (FileStream fs = File.Create(@"C:\Users\m.armanno\Desktop\C\booo.pdf"))
{
fs.Write(content, 0, (int)content.Length);
}
return memoryStream.ToArray();
}
}
o questa:
public static void WriteText()
{
string oldFile = @"C:\Users\m.armanno\Desktop\C\01ZI20120021.pdf";
string newFile = @"C:\Users\m.armanno\Desktop\C\output.pdf";
// open the reader
PdfReader reader = new PdfReader(oldFile);
Rectangle size = reader.GetPageSizeWithRotation(1);
Document document = new Document(size);
// open the writer
FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
document.Open();
// the pdf content
PdfContentByte cb = writer.DirectContent;
// select the font properties
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.SetColorFill(BaseColor.DARK_GRAY);
cb.SetFontAndSize(bf, 8);
// write the text in the pdf content
cb.BeginText();
string text = "Some random blablablabla...";
// put the alignment and coordinates here
cb.ShowTextAligned(1, text, 520, 640, 0);
cb.EndText();
cb.BeginText();
text = "Other random blabla...";
// put the alignment and coordinates here
cb.ShowTextAligned(2, text, 100, 200, 0);
cb.EndText();
// create the new page and add it to the pdf
PdfImportedPage page = writer.GetImportedPage(reader, 1);
cb.AddTemplate(page, 0, 0);
// close the streams and voilá the file should be changed :)
document.Close();
fs.Close();
writer.Close();
reader.Close();
}
Ma entrambe non generano errori ma non scrivono nulla sul pdf che gli ho dato come parametro.
Riesco solo ad inserire immagini con questa funzione:
public static void WriteImage()
{
using (Stream inputPdfStream = new FileStream(@"C:\Users\m.armanno\Desktop\C\01ZI20120021.pdf", FileMode.Open, FileAccess.Read, FileShare.Read))
using (Stream inputImageStream = new FileStream(@"C:\Users\m.armanno\Desktop\C\a.jpg", FileMode.Open, FileAccess.Read, FileShare.Read))
using (Stream outputPdfStream = new FileStream(@"C:\Users\m.armanno\Desktop\C\result.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
{
var reader = new PdfReader(inputPdfStream);
var stamper = new PdfStamper(reader, outputPdfStream);
var pdfContentByte = stamper.GetOverContent(1);
Image image = Image.GetInstance(inputImageStream);
image.SetAbsolutePosition(100, 100);
pdfContentByte.AddImage(image);
stamper.Close();
}
}
Ma il testo ho cercato ovunque ma non riesco proprio ad inserirlo.
Riuscite a dirmi cosa sbaglio per favore?
Grazie mille in anticipo!!!