24 messaggi dal 27 settembre 2012
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!!!

Tutto ciò che non sai è vero!!!
60 messaggi dal 08 aprile 2010
Ciao ,ti allego una mia procedura che ho realizzato in un azienda,
apre il file pdf di origine e sopra ci disegna un quadratino con del testo (solo che l'esempio è in vb):
Private Sub Stampagina(ByVal FileOrigine As String, ByVal FileDestinazione As String, Optional ByVal Qta As Integer = 1, Optional ByVal Codice As String = "", Optional ByVal DimL As Integer = 0, Optional ByVal DimA As Integer = 0)

Dim DeltaX As Integer = 0
Dim DeltaY As Integer = 0
Dim ContaBind As Integer = 1

Dim reader As PdfReader = New PdfReader(FileOrigine)
Dim n As Integer = reader.NumberOfPages
Dim psize As Rectangle = reader.GetPageSize(1)
Dim width As Single = psize.Width
Dim height As Single = psize.Height

Dim Document As Document = New Document(PageSize.A4, 10, 10, 10, 10)
Dim writer As PdfWriter = PdfWriter.GetInstance(Document, New FileStream(FileDestinazione, FileMode.Create))
Dim bf_times As BaseFont = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, "Cp1252", False)

Document.Open()

Dim page1 As PdfImportedPage = writer.GetImportedPage(reader, 1)
Dim cb As iTextSharp.text.pdf.PdfContentByte = writer.DirectContentUnder

' --------------Cordinate Base
DeltaX = 20
DeltaY = 530

' ------------------------------------------------------------------------------------------Aggiunge Il Foglio Stampato
cb.AddTemplate(page1, 1.0F, 0, 0, 1.0F, 0, 0)

' Disegno Layout Stampa
cb.Rectangle(DeltaX + 1, DeltaY + 268, 210, 30) '--- Numero Ordine
cb.Rectangle(DeltaX + 1, DeltaY + 238, 210, 30) '--- Tessuto

If Fornitore = "IMBOTT" Then
cb.Rectangle(DeltaX + 1, DeltaY + 208, 210, 30) '--- Qta
End If

' Scrivi Testo
cb.BeginText()
cb.SetFontAndSize(bf_times, 12)
cb.SetTextMatrix(DeltaX + 3, DeltaY + 288)
cb.ShowText("Numero Ordine:")
cb.SetTextMatrix(DeltaX + 3, DeltaY + 258)
cb.ShowText("Tessuto:")
If Fornitore = "IMBOTT" Then
cb.SetTextMatrix(DeltaX + 3, DeltaY + 228)
cb.ShowText("Qta:")
End If
cb.SetFontAndSize(bf_times, 22)
cb.SetTextMatrix(DeltaX + 110, DeltaY + 274)
cb.ShowText(Lista.Item("ordine", i).Value) ' --- Variabile Numero Ordine
cb.SetTextMatrix(DeltaX + 80, DeltaY + 244)
cb.ShowText(Lista.Item("Denominazione", i).Value) ' --- Variabile Tessuto
If Fornitore = "IMBOTT" Then
cb.SetTextMatrix(DeltaX + 80, DeltaY + 214)
cb.ShowText(Qta) ' --- Variabile Qta
End If

If Fornitore = "IMBOTT" Then
Dim code39 As Barcode39 = New Barcode39
code39.Code = "M." & Codice
code39.StartStopText = False
Dim image39 As Image = code39.CreateImageWithBarcode(cb, Nothing, Nothing)
image39.SetAbsolutePosition(DeltaX + 3, 700)
cb.AddImage(image39)

Dim AppoCorrettivi() As Integer = TrovaCorrettiviDimensione(Codice)
'If TrovaCorrettiviDimensione(Codice)(0) <> 0 And TrovaCorrettiviDimensione(Codice)(1) <> 0 Then
If AppoCorrettivi(0) <> 0 Or AppoCorrettivi(1) <> 0 Then '--- Riquadro Correttivi
'cb.Rectangle(DeltaX + 1, DeltaY + 208, 18, 30)
cb.SetTextMatrix(DeltaX + 3, DeltaY + 138)
cb.ShowText("DimL:" & DimL + AppoCorrettivi(0) & " " & "DimP:" & DimA + AppoCorrettivi(1)) ' --- Variabile Qta
End If

End If

cb.EndText()
cb.Stroke()
reader.Close()
Document.Close()

Threading.Thread.Sleep(2500)

End Sub
24 messaggi dal 27 settembre 2012
Grazie mille!!!! :-)

Funzionaaaaa :D

Troppo gentile e veloce grazie ancora!!!!

Tutto ciò che non sai è vero!!!
0 messaggi dal 06 dicembre 2024
Ciao Emiliano
ho letto il tuo codice e ti volevo chiedere una cortesia se ovviamente lo sai. Quando vado ad impostare un campo non mi fa la somma in automatico (campi degli importi presenti sul modello f24) esiste un comando per "forzare" o quando meno simulare l'invio sul campo in modo tale da aggiornare il totale?

GRAZIEEEEE

Gabriele

Torna al forum | Feed RSS

ASPItalia.com non è responsabile per il contenuto dei messaggi presenti su questo servizio, non avendo nessun controllo sui messaggi postati nei propri forum, che rappresentano l'espressione del pensiero degli autori.