Пример #1
0
        public override void OnEndPage(PdfWriter writer, Document document)
        {
            base.OnEndPage(writer, document);

            //page number
            if (writer.PageNumber > 1)
            {
                String    text     = PublicMethods.convert_numbers_to_local((writer.PageNumber - 1) + "/");
                float     len      = font.BaseFont.GetWidthPoint(text, 8);
                Rectangle pageSize = document.PageSize;
                _pdfContentByte.SetRGBColorFill(130, 130, 130);

                _pdfContentByte.BeginText();
                _pdfContentByte.SetFontAndSize(font.BaseFont, 8);
                _pdfContentByte.SetTextMatrix(pageSize.GetLeft(pageSize.Width / 2), pageSize.GetBottom(20));
                _pdfContentByte.ShowText(text);
                _pdfContentByte.EndText();

                _pdfContentByte.AddTemplate(_template, pageSize.GetLeft(pageSize.Width / 2) + len, pageSize.GetBottom(20));
            }
            //end of page number

            //header
            if (writer.PageNumber > 1)
            {
                string hOddMessage  = RaaiVanSettings.Organization.Name(ApplicationID);
                string hEvenMessage = DocumentTitle;

                if (string.IsNullOrEmpty(hOddMessage))
                {
                    hOddMessage = hEvenMessage;
                }

                bool isOddPage = writer.PageNumber % 2 == 1;

                string headerMessage = isOddPage ? hOddMessage : hEvenMessage;

                bool isRTL = PublicMethods.has_rtl_characters(headerMessage);

                PdfPTable htbl = new PdfPTable(1);
                htbl.TotalWidth          = 519;
                htbl.WidthPercentage     = 100;
                htbl.HorizontalAlignment = isRTL ? Element.ALIGN_RIGHT : Element.ALIGN_LEFT;
                if (isRTL)
                {
                    htbl.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
                }

                Paragraph header = new Paragraph(headerMessage,
                                                 new Font(font.BaseFont, 8, Font.NORMAL, BaseColor.BLACK));

                PdfPCell hcell = new PdfPCell(header);
                hcell.Border        = 0;
                hcell.PaddingBottom = 6;

                PdfPCell lcell = new PdfPCell();
                lcell.PaddingTop      = 1;
                lcell.BackgroundColor = BaseColor.BLACK;

                htbl.AddCell(hcell);
                htbl.AddCell(lcell);

                htbl.WriteSelectedRows(0, -1, 38, 830, writer.DirectContent);
            }
            //end of header

            //footer
            if (writer.PageNumber > 1 && Meta != null)
            {
                PdfPTable htbl = new PdfPTable(1)
                {
                    TotalWidth          = 519,
                    WidthPercentage     = 100,
                    HorizontalAlignment = Element.ALIGN_CENTER,
                    RunDirection        = PdfWriter.RUN_DIRECTION_RTL
                };

                Paragraph footer = new Paragraph(Meta.toString(), new Font(font.BaseFont, 7, Font.NORMAL, BaseColor.DARK_GRAY));
                PdfPCell  hcell  = new PdfPCell(footer)
                {
                    Border = 0, PaddingBottom = 0
                };

                htbl.AddCell(hcell);

                htbl.WriteSelectedRows(0, -1, 38, 15, writer.DirectContent);
            }
            //end of footer
        }
Пример #2
0
        protected void send_file(DocFileInfo file, bool logNeeded, bool addPDFCover = false, bool addPDFFooter = false,
                                 Guid?coverId = null, string pdfPassword = null, string contentType = null, bool isAttachment = true)
        {
            byte[] fileContent = file.toByteArray(paramsContainer.ApplicationID);

            if (fileContent.Length == 0)
            {
                send_empty_response();
                return;
            }

            //Save Log
            if (logNeeded && paramsContainer.CurrentUserID.HasValue)
            {
                LogController.save_log(paramsContainer.Tenant.Id, new Log()
                {
                    UserID           = paramsContainer.CurrentUserID,
                    Date             = DateTime.Now,
                    HostAddress      = PublicMethods.get_client_ip(HttpContext.Current),
                    HostName         = PublicMethods.get_client_host_name(HttpContext.Current),
                    Action           = Modules.Log.Action.Download,
                    SubjectID        = file.FileID,
                    Info             = file.toJson(paramsContainer.Tenant.Id),
                    ModuleIdentifier = ModuleIdentifier.DCT
                });
            }
            //end of Save Log

            if (file.Extension.ToLower() == "pdf")
            {
                addPDFCover = addPDFCover && file.OwnerNodeID.HasValue && coverId.HasValue &&
                              paramsContainer.ApplicationID.HasValue && paramsContainer.CurrentUserID.HasValue;

                if (addPDFFooter || addPDFCover)
                {
                    bool invalidPassword = false;

                    fileContent = PDFUtil.get_pdf_content(paramsContainer.Tenant.Id, fileContent, pdfPassword, ref invalidPassword);

                    if (invalidPassword)
                    {
                        string responseText = "{\"InvalidPassword\":" + true.ToString().ToLower() + "}";
                        paramsContainer.return_response(ref responseText);
                        return;
                    }
                }

                if (addPDFFooter)
                {
                    User currentUser = !paramsContainer.CurrentUserID.HasValue ? null :
                                       UsersController.get_user(paramsContainer.Tenant.Id, paramsContainer.CurrentUserID.Value);

                    if (currentUser == null)
                    {
                        currentUser = new User()
                        {
                            UserID    = Guid.NewGuid(),
                            UserName  = "******",
                            FirstName = "[anonymous]",
                            LastName  = "[anonymous]"
                        };
                    }

                    DownloadedFileMeta meta = new DownloadedFileMeta(PublicMethods.get_client_ip(HttpContext.Current),
                                                                     currentUser.UserName, currentUser.FirstName, currentUser.LastName, null);

                    fileContent = PDFTemplates.append_footer(fileContent, meta.toString());
                }

                if (addPDFCover)
                {
                    fileContent = Wiki2PDF.add_custom_cover(paramsContainer.Tenant.Id,
                                                            paramsContainer.CurrentUserID.Value, fileContent, coverId.Value, file.OwnerNodeID.Value);
                }

                if (!string.IsNullOrEmpty(pdfPassword))
                {
                    fileContent = PDFUtil.set_password(fileContent, pdfPassword);
                }
            }

            string retFileName = file.FileName + (string.IsNullOrEmpty(file.Extension) ? string.Empty : "." + file.Extension);

            paramsContainer.file_response(fileContent, retFileName, contentType: contentType, isAttachment: isAttachment);
        }