Switch (Title_TextBox.Foreground != Brushes.Silver & Title_() != null)ĬolumnText.ShowTextAligned(Stamper.GetUnderContent(1), Element.ALIGN_CENTER, new Phrase(LatinDigitToPersianDigit(Title_()), PageNumberFont), ((PageSize.Left + PageSize.Right) / 2), PageSize.Top - 10, 0, PdfWriter.RUN_DIRECTION_RTL, ColumnText.AR_LIG) Using (PdfStamper Stamper = new PdfStamper(PDF_Reader, Stream)) PdfReader PDF_Reader = new PdfReader(Bytes) Using (MemoryStream Stream = new MemoryStream()) PersianCalendar PersianCalendar = new PersianCalendar() Void AddTitleAndPageNumberRTL(string FileIn, string FileOut, string Date)īyte Bytes = File.ReadAllBytes(FileIn) īaseFont BaseFont = BaseFont.CreateFont(.StartupPath + Collection\Bahij Uthman Taha.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED) įont PageNumberFont = new Font(BaseFont, 12) These are my codes: public string LatinDigitToPersianDigit(string Text) ((PageSize.Left + PageSize.Right)/2)-((TextRenderer.MeasureText("تاریخ گزارش", font).Width)/2), but it's not exactly in the center. I can solve this problem with the class but I can't exactly put the text alignment in the center.
I want to add Persian text to the PDF headline, but when I do this through the class, I get into trouble. Where(x => x.Key = script.First and foremost, I apologize for my grammatical errors my first language is Persian (Iran).
if null needs to be passed for any other cases, use the commented lines instead Var parentField = PdfFormField.CreateTextField(pdfStamper.Writer, false, false, 0) įor (var pageNumber = PdfFirstPageNumber pageNumber Using (var pdfStamper = new PdfStamper(pdfReader, File.Create(GetOutputPath(filename)))) Using (var pdfReader = new PdfReader(filename)) Private void AddScriptToPdf(string filename, Script script) if the compiler complains that Rectangle is ambiguous, use this instead if you add this line on the top of the file You have some very-very-very-lonnnng name due to lack of using namespace : new (.) The switch can be replaced by a dictionary to remove the boilerplate breaks : var actionMapping = new Dictionary Using (var pdfStamper = new PdfStamper(pdfReader, new FileStream(GetOutputPath(filename), FileMode.Create))) When using two directly nested using, you can write it as the following to reduce the level of indentation : using (var pdfReader = new PdfReader(filename))
Together, it makes the code difficult to read when glancing over. At the same time, there is no new line between logical blocks of the method. There is an excessive amount of new line after parameter delimiter( ,) and assignment operator( =).Is there any way I could actually make it better (or shorter)? I know I could split it into several smaller methods, but considering they only ever run in sequence, I don't see the point of that. It's pretty much step-by-step instructions. The method is long, but requires very little logic. PdfStamper.AddAnnotation(parentField, 1) Var childField = textField.GetTextField() Var pdfContentByte = pdfStamper.GetOverContent( ParentField.FieldName = įor (var pageNumber = PdfFirstPageNumber The method to add the script to the PDF is as follows: private void AddScriptToPdf(string filename, Script script) The text is the JavaScript itself, the field is an object with a string Title and four integers representing the coordinates of the field on the document, and the timing is an enum value used to determine when the script should be executed. If you are not familiar, PDFs support JavaScript to perform various tasks such as changing the content of documents, and scripts can be executed on various events such as on printing or saving.Ī Script has a ScriptText, a ScriptField, and a ScriptTiming.
I have a method that takes the filename of a PDF file and a Script object, and applies the Script to the PDF. This is related to my (unanswered) question here, but the code has changed considerably since I asked that question.