I am trying to add a watermak on a pdf file with itextsharp 5.1.2.0 .
I want the watermark to be in the center of the page, with a 45° angle, with a border around these lines.
Here is my code :
for (int i = 1; i <= reader.NumberOfPages; i++)
{
iTextSharp.text.Rectangle pageSize = reader.GetPageSizeWithRotation(i);
PdfContentByte pdfPageContents;
pdfPageContents = pdfStamper.GetOverContent(i);
pdfPageContents.BeginText();
PdfGState gstate = new PdfGState();
gstate.FillOpacity = 0.4f;
gstate.StrokeOpacity = 0.4f;
pdfPageContents.SaveState();
pdfPageContents.SetGState(gstate);
BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, Encoding.ASCII.EncodingName, false);
pdfPageContents.SetRGBColorFill(255, 0, 0);
double radians = Math.Atan2(pageSize.Height, pageSize.Width);
float textAngle = radians * (180 / Math.PI);
pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "Phrase 1 This is Phrase 2 and must be centered below phrase 1" , pageSize.Width / 2, pageSize.Height / 2, textAngle);
//pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "Phrase 1", pageSize.Width / 2, pageSize.Height / 2, textAngle);
//pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "This is Phrase 2 and must be centered below phrase 1", pageSize.Width / 2 + 20 , pageSize.Height / 2 - 20, textAngle);
}
This adds the watermark on one line.
I am able to have two lines with the commented code.
What i dont like are the hardcoded values for the position for the second line.
I am sure there must be a better way of achieving this.
Regarding the border i did not manage to add it around the lines.
If someone can help me with this case .
Thanks
basically what I do for my project is as under... here font_size and style are the variables... also llx, lly, urx,ury are variable for the the corrdinates position and size.. in alignment variable, you can set the alignment easily....
for opacity your code will work fine(you can add state options)
Dim Font = New Font(arial, FONT_SIZE, STYLE, iTextSharp.text.Color.BLACK )
dim text = GetRowValue(row, "CONTROL_MAP")
Dim ct As ColumnText = New ColumnText(pdf_cb)
ct.SetSimpleColumn(LLX, LLY, URX, URY, FONT_SIZE, ALIGNMENT)
ct.SetText(New Paragraph(0, text, Font))
ct.Go()
Related
I have an application with GMap.Net showing various markers. I know how to take a screen shot of the current map and markers:
Dim sImageName As String = DateTime.Now.ToString(Format("yyyyMMdd-HHmmss")) & ".png"
Dim ThisMap As New Bitmap(Form2.myMap.Width, Form2.myMap.Height)
Form2.myMap.DrawToBitmap(ThisMap, New Rectangle(0, 0, Form2.myMap.Width, Form2.myMap.Height))
ThisMap.Save(sImagesFolder & sImageName)
What I would like to do is create an image for a selected marker. Instead of the image being the entire map shown on screen, it would center on the marker and show 100 pixels in each direction.
Does anyone know how to do that?
This is what I tried, but it gives me a blank image-- nothing shows up. I feel like this should be working...
Private Sub MyMap_OnMarkerClick(item As GMapMarker, e As Windows.Forms.MouseEventArgs) Handles myMap.OnMarkerClick
SelMarkerX = e.X
SelMarkerY = e.Y
Dim sImageName As String = DateTime.Now.ToString(Format("yyyyMMdd-HHmmss")) & ".png"
Dim ThisMap As New Bitmap(140,100)
myMap.DrawToBitmap(ThisMap, New Rectangle(SelMarkerX - 70, SelMarkerY - 50, 140, 100))
ThisMap.Save(sImagesFolder & sImageName)
End Sub
I just don't get it. If I write:
myMap.DrawToBitmap(ThisMap, New Rectangle(0, 0, 140, 100)
then I get what you might expect. I get the upper left corner of the existing map from 0 to 140 horizontally and 0 to 100 vertically. If I change it to this:
myMap.DrawToBitmap(ThisMap, New Rectangle(10, 0, 140, 100)
then I get 0 to 130 horizontally and not 10 to 140.
Well, I couldn't figure out how to do it with Gmap, so I wondered if I could crop it outside of Gmap and apparently that is common. Here is the code I used.
Dim ThisMap As New Bitmap(Form2.myMap.Width, Form2.myMap.Height)
Form2.myMap.DrawToBitmap(ThisMap, New Rectangle(0, 0, Form2.myMap.Width, Form2.myMap.Height))
ThisMap.Save(sImagesFolder & sImageName)
Dim LocX = SelMarkerX - 160 'x cord. of where crop starts
Dim LocY = SelMarkerY - 120 'y cord. of where crop starts
Dim CropW = 320 'Crop width
Dim CropH = 240 'Crop height
Dim CropRect As New Rectangle(LocX, LocY, CropW, CropH)
Dim OriginalImage = ThisMap
Dim CropImage = New Bitmap(CropRect.Width, CropRect.Height)
Using grp = Graphics.FromImage(CropImage)
grp.DrawImage(OriginalImage, New Rectangle(0, 0, CropRect.Width, CropRect.Height), CropRect, GraphicsUnit.Pixel)
CropImage.Save(sImagesFolder & sImageName)
End Using
I am using itextsharp for creating PDF . But when my content goes beyong 1 page it does not create and append text to next page. Below is my source code. Cannot identify whats goin wrong with this. Please guide.
Dim pdfTemplate As String = "C:\Program Files\mycrm\Documents\Client\Statement_.pdf"
Dim newFile As String = "D:\test.pdf"
Dim pdfReader As New PdfReader(pdfTemplate)
Dim pdfStamper As New PdfStamper(pdfReader, New FileStream(newFile, FileMode.Create))
Dim pdfFormFields As AcroFields = pdfStamper.AcroFields
pdfFormFields.SetField("[CLIENT NAME]", "siddhesh")
'For adding table
Dim PdfTable As New PdfPTable(2)
Dim PdfPCell As PdfPCell = Nothing
For column As Integer = 0 To 1
PdfPCell = New PdfPCell(New Phrase(New Chunk(column.ToString())))
PdfTable.AddCell(PdfPCell)
Next
For rows As Integer = 0 To 100
For column As Integer = 0 To 1
PdfPCell = New PdfPCell(New Phrase(New Chunk(rows.ToString() + column.ToString())))
PdfTable.AddCell(PdfPCell)
Next
Next
PdfTable.HeaderRows = 1
pdfStamper.FormFlattening = True
PdfTable.SetTotalWidth(New Single() {
(iTextSharp.text.PageSize.A4.Rotate().Height - 25) / 10,
(iTextSharp.text.PageSize.A4.Rotate().Height - 25) / 10
})
PdfTable.WriteSelectedRows(0, 50, 35, 460, pdfStamper.GetOverContent(1)) 'X Y départ en bas à gauche? plus yPos est au plus le texte est haut
PdfTable.CompleteRow()
pdfStamper.Close()
MsgBox("Exported")
PdfTable.WriteSelectedRows is documented as
/**
* Writes the selected rows to the document.
*
* #param rowStart the first row to be written, zero index
* #param rowEnd the last row to be written + 1. If it is -1 all the
* rows to the end are written
* #param xPos the x write coodinate
* #param yPos the y write coodinate
* #param canvas the <CODE>PdfContentByte</CODE> where the rows will
* be written to
* #return the y coordinate position of the bottom of the last row
*/
virtual public float WriteSelectedRows(int rowStart, int rowEnd, float xPos, float yPos, PdfContentByte canvas)
You use
PdfTable.WriteSelectedRows(0, 50, 35, 460, pdfStamper.GetOverContent(1)) 'X Y départ en bas à gauche? plus yPos est au plus le texte est haut
Thus, you explicitly only draw the first 50 rows of your table over page 1 and completely ignore the remaining rows.
Your observation, therefore,
when my content goes beyong 1 page it does not create and append text to next page.
is exactly what is to be expected.
To draw all the rows, you can iterate and draw rows 50..99 over page 2, 100..149 over page 3, etc. PdfPTable has a property Size returning the number of rows in the table; you can use this to determine how often you have to loop.
If the document you loaded into your PdfReader does not have enough pages, you can add extra blank pages using PdfStamper.InsertPage.
Once again, I'm stuck. I've tried figuring out how to print a form while having margins, a proper scaling, and certain objects not visible.
So far, I've tried .PaintPicture function which centers the form it prints but that's it (read: you can't disable some objects there), while using .PrintForm I can make some objects visible but I couldn't figure it out how to center and scale the form to fit and do some scaling so that the print result doesn't crop the form it prints. Playing with .Scale functions (e.g., .ScaleHeight, .ScaleWidth) apparently obviously don't help either as they give me errors after countless trial, error, and curiosity.
Here is my code so far:
Private Sub Command1_Click()
Dim NumCopies, I
'rem Set Cancel to True
dlgDialog.CancelError = False
dlgDialog.Flags = cdlPDUseDevModeCopies 'Enables multiple-copy printing
Dim MarginsLR As Single
Dim MarginsTB As Single
Dim PrintableWidth As Single
Dim PrintableHeight As Single
Dim ScaleFactor As Double
Dim ScaledWidth As Double
Dim ScaledHeight As Double
MarginsLR = Printer.ScaleX(0.5, vbInches, Printer.ScaleMode)
MarginsTB = Printer.ScaleY(0.5, vbInches, Printer.ScaleMode)
PrintableWidth = Printer.Width - 2 * MarginsLR
PrintableHeight = Printer.Height - 2 * MarginsTB
ScaleFactor = PrintableWidth / Printer.ScaleX(Form1.Width, vbHiMetric, Printer.ScaleMode)
If ScaleFactor * Printer.ScaleY(Form1.Height, vbHiMetric, Printer.ScaleMode) > PrintableHeight Then
ScaleFactor = PrintableHeight / Printer.ScaleY(Form1.Height, vbHiMetric, Printer.ScaleMode)
End If
ScaledWidth = ScaleFactor * Printer.ScaleX(Form1.Width, vbHiMetric, Printer.ScaleMode)
ScaledHeight = ScaleFactor * Printer.ScaleY(Form1.Height, vbHiMetric, Printer.ScaleMode)
MoveX = (Printer.Width - ScaledWidth) / 2
MoveY = (Printer.Height - ScaledHeight) / 2
On Error GoTo ErrorHandler
' Display the Print dialog box
dlgDialog.ShowPrinter
NumCopies = dlgDialog.Copies
For I = 1 To NumCopies
'The following was experimental
'Form1.ScaleHeight = ScaledHeight
'Form1.ScaleWidth = ScaledWidth
'Form1.ScaleX = MoveX
'Form1.ScaleY = MoveY
'For the .PrintForm function. I can't figure out how to do all the centering stuff sadly...
Label1.Visible = True
Command1.Visible = False
'This following code enables resizing and positioning but it doesn't make both Label1 and Command1 statements above work
Printer.PaintPicture CaptureClient(Me), (Printer.Width - Me.Width) / 2, _
(Printer.Height - Me.Height) / 2
'Form1.PrintForm
Printer.EndDoc
Next I
Label1.Visible = False
Command1.Visible = True
Exit Sub
ErrorHandler:
MsgBox "The following error has occurred:" & vbNewLine _
& "Err # " & Err.Number & " - " & Err.Description, _
vbCritical, _
"Print Error"
End Sub
Your help is appreciated. I'm currently updating my application's normal printing function with the new one.
Thanks in advance!
I am using Itextsharp.
doc.SetMargins(36, 36, 36, 36);
doc.NewPage();
writer.PageEvent = new PDFPageEvent();
int horizontalMarginForColumn = 54;
int bottomMarginForColumn = 72;
int topMarginForColumn = 112;
int interval = 8;
Rectangle pageNumberLeftLocation = new Rectangle(horizontalMarginForColumn, doc.PageSize.Height - 36, horizontalMarginForColumn + 36, doc.PageSize.Height);
paragraph = new Paragraph();
paragraph.SpacingBefore = 0;
paragraph.Alignment = Element.ALIGN_LEFT;
line = new LineSeparator(1f, 100f, BaseColor.BLACK, 0, 0f);
line.Alignment = Element.ALIGN_CENTER;
paragraph.Add(new Chunk(line));
paragraph.Add(Chunk.NEWLINE);
chunk = new Chunk(" Index", FontFactory.GetFont("Arial", 15, Font.BOLD));
chunk.setLineHeight(23);
paragraph.Add(chunk);
paragraph.Add(Chunk.NEWLINE);
paragraph.Add(new Chunk(line));
doc.Add(paragraph);
writer.DirectContent.SetColorStroke(BaseColor.BLUE);
writer.DirectContent.Rectangle(pageNumberLeftLocation.Left, pageNumberLeftLocation.Bottom, pageNumberLeftLocation.Width, pageNumberLeftLocation.Height);
writer.DirectContent.Stroke();
The top margin of the page is 36, and the height of the rectangle is 36. So I assume that the bottom line of the rectangle and the beginning of the paragraph which is a black line, sholud be in the same vertical position. But you can see, there is some space between the two. Why is this? Thank you.
I have seen answers to question of how to add text watermark to an existing PDF document using iTextSharp. My question is how can we do multiline text. Is there a way to do this without having multiple PdfContentByte defined. I have tried to insert a newline character with no luck.
Here is the code from the internet. I just added
pdfData.ShowTextAligned(Element.ALIGN_CENTER, editDate, (pageRectangle.Width / 2) + 100, (pageRectangle.Height / 2) - 100, 45);
as the second line to get the second line of the watermark, it works but uses same parameters (color, size, etc.) as the first.
iTextSharp.text.Rectangle pageRectangle = PDFreader.GetPageSizeWithRotation(1);
//pdfcontentbyte object contains graphics and text content of page returned by pdfstamper
PdfContentByte pdfData = stamper.GetOverContent(1);
//create fontsize for watermark
pdfData.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 120);
//create new graphics state and assign opacity
PdfGState graphicsState = new PdfGState();
graphicsState.FillOpacity = 0.2F;
//set graphics state to pdfcontentbyte
pdfData.SetGState(graphicsState);
//set color of watermark
pdfData.SetColorFill(iTextSharp.text.Color.BLUE);
//indicates start of writing of text
pdfData.BeginText();
//show text as per position and rotation
pdfData.ShowTextAligned(Element.ALIGN_CENTER, "E D I T E D" , (pageRectangle.Width / 2), (pageRectangle.Height / 2), 45);
pdfData.ShowTextAligned(Element.ALIGN_CENTER, editDate, (pageRectangle.Width / 2) + 100, (pageRectangle.Height / 2) - 100, 45);
//call endText to invalid font set
pdfData.EndText();