Пример #1
0
        static void Main(string[] args)
        {
            if (args == null || args.Length < 2)
            {
                System.Console.WriteLine("Usage: " + AppDomain.CurrentDomain.FriendlyName + " <original PDF filename> <watermark text> [new PDF filename]" + Environment.NewLine +
                                         "  For example: " + AppDomain.CurrentDomain.FriendlyName + " myDoc.pdf \"This is a Draft\"");
            }
            else
            {
                string origName     = args[0];
                string watermarkTxt = args[1];
                if (!System.IO.File.Exists(origName))
                {
                    System.Console.WriteLine("Error: cannot find the original PDF file(" + origName + "). Please correct the filename or the path and try again.");
                }
                else
                {
                    PDDocument origDoc  = PDDocument.load(new java.io.File(origName)); // NOTE: PDDocument.load() only takes java.io.File, not System.IO.File from C#.Net
                    PDPageTree allPages = origDoc.getPages();
                    PDFont     font     = PDType1Font.HELVETICA_BOLD;
                    for (int i = 0, len = allPages.getCount(); i < len; ++i)
                    {
                        PDPage pg = (PDPage)allPages.get(i);
                        addWatermarkText(origDoc, pg, font, "This is a draft!!!");
                    }

                    origDoc.save("watermarked_" + origName);
                    origDoc.close();
                }
            }
        }
Пример #2
0
        /// <summary>
        /// The below method is an example from https://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/AddWatermarkText.java?revision=1873147&view=markup
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="page"></param>
        /// <param name="font"></param>
        /// <param name="text"></param>
        static void addWatermarkText(PDDocument doc, PDPage page, PDFont font, string text)
        {
            using (PDPageContentStream cs
                       = new PDPageContentStream(doc, page, PDPageContentStream.AppendMode.APPEND, true, true))
            {
                float fontHeight     = 100; // arbitrary for short text
                float width          = page.getMediaBox().getWidth();
                float height         = page.getMediaBox().getHeight();
                float stringWidth    = font.getStringWidth(text) / 1000 * fontHeight;
                float diagonalLength = (float)System.Math.Sqrt(width * width + height * height);
                float angle          = (float)System.Math.Atan2(height, width);
                float x = (diagonalLength - stringWidth) / 2; // "horizontal" position in rotated world
                float y = -fontHeight / 4;                    // 4 is a trial-and-error thing, this lowers the text a bit
                cs.transform(Matrix.getRotateInstance(angle, 0, 0));
                cs.setFont(font, fontHeight);
                // cs.setRenderingMode(RenderingMode.STROKE) // for "hollow" effect

                PDExtendedGraphicsState gs = new PDExtendedGraphicsState();
                gs.setNonStrokingAlphaConstant(new Float(0.2f));
                gs.setStrokingAlphaConstant(new Float(0.2f));
                gs.setBlendMode(BlendMode.MULTIPLY);
                gs.setLineWidth(new Float(3f));
                cs.setGraphicsStateParameters(gs);

                cs.setNonStrokingColor(Color.red);
                cs.setStrokingColor(Color.red);

                cs.beginText();
                cs.newLineAtOffset(x, y);
                cs.showText(text);
                cs.endText();
            }
        }
Пример #3
0
 internal static PDDocument AddTrialStampIfNecessary(PDDocument pdfFile, bool addJavaScript)
 {
     if (PDFHelper.AddStamp)
     {
         PDFont pDFont = PDTrueTypeFont.load(pdfFile, new java.io.File("C:\\Windows\\Fonts\\Arial.ttf"), new WinAnsiEncoding());
         foreach (PDPage page in pdfFile.getPages())
         {
             PDPageContentStream pDPageContentStream = new PDPageContentStream(pdfFile, page, PDPageContentStream.AppendMode.APPEND, true);
             pDPageContentStream.setFont(pDFont, 14f);
             pDPageContentStream.beginText();
             pDPageContentStream.newLineAtOffset(100f, page.getMediaBox().getHeight() - 100f);
             pDPageContentStream.showText("Created with a trial copy of Aquaforest PDF Toolkit");
             pDPageContentStream.endText();
             pDPageContentStream.close();
         }
     }
     return(pdfFile);
 }
Пример #4
0
        private void EmbedFonts(bool doType1)
        {
            bool                     flag               = false;
            PDTrueTypeFont           pDTrueTypeFont     = null;
            List <PDFAFontsEmbedded> pDFAFontsEmbeddeds = new List <PDFAFontsEmbedded>();

            foreach (PDPage page in this.doc.getPages())
            {
                PDResources resources = page.getResources();
                Iterator    iterator  = resources.getFontNames().iterator();
                while (iterator.hasNext())
                {
                    PDFont font = null;
                    try
                    {
                        font = resources.getFont((COSName)iterator.next());
                        PDFontDescriptor fontDescriptor = font.getFontDescriptor();
                        if (font.isEmbedded())
                        {
                            continue;
                        }
                        else if (font.getSubType().ToLower().Contains("type1") & doType1)
                        {
                            string fontFile = this.GetFontFile(font.getName());
                            if (fontFile != null)
                            {
                                if ((
                                        from t in pDFAFontsEmbeddeds
                                        where t.FontName == fontFile
                                        select t).Count <PDFAFontsEmbedded>() > 0)
                                {
                                    (
                                        from t in pDFAFontsEmbeddeds
                                        where t.FontName == fontFile
                                        select t.fontDecriptor).SingleOrDefault <PDFont>();
                                    font.getCOSObject().setName(COSName.SUBTYPE, "TrueType");
                                }
                                else
                                {
                                    InputStream fileInputStream = new FileInputStream(string.Concat("C:\\Windows\\Fonts\\", fontFile));
                                    fontDescriptor.setFontFile(new PDStream(this.doc, fileInputStream));
                                    PDFAFontsEmbedded pDFAFontsEmbedded = new PDFAFontsEmbedded()
                                    {
                                        FontName = fontFile
                                    };
                                    font.getCOSObject().setName(COSName.SUBTYPE, "TrueType");
                                    pDFAFontsEmbedded.fontDecriptor = pDTrueTypeFont;
                                    pDFAFontsEmbeddeds.Add(pDFAFontsEmbedded);
                                    flag = true;
                                }
                            }
                        }
                        else if (font.getSubType().ToLower().Contains("true"))
                        {
                            string readableFontName = this.GetReadableFontName(font.getName());
                            string str  = this.GetFontFile(readableFontName);
                            string str1 = string.Concat("C:\\Windows\\Fonts\\", str);
                            if (System.IO.File.Exists(str1))
                            {
                                if ((
                                        from t in pDFAFontsEmbeddeds
                                        where t.FontName == str
                                        select t).Count <PDFAFontsEmbedded>() <= 0)
                                {
                                    InputStream inputStream = new FileInputStream(str1);
                                    fontDescriptor.setFontFile2(new PDStream(this.doc, inputStream));
                                    PDFAFontsEmbedded pDFAFontsEmbedded1 = new PDFAFontsEmbedded()
                                    {
                                        FontName      = str,
                                        fontDecriptor = pDTrueTypeFont
                                    };
                                    pDFAFontsEmbeddeds.Add(pDFAFontsEmbedded1);
                                    flag = true;
                                }
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                    }
                }
            }
        }