Add Text in Existing PDF Document using Aspose.PDF for .NET

if i wan’t to add text to a existing pdf, is Aspose.Pdf.Kit the right product. I need to use certain truetype fonts. I can’t see any support in the PdfFileMend class for truetype fonts.

Hi Anton,

Thanks for using our products.

Please note that Aspose.Pdf.Kit for .NET has been discontinued as a separate product and all its classes and enumerations are present under Aspose.PDF namespace of Aspose.Pdf for .NET. However I would suggest you to use TextFragment instead of PdfFileMend class. Please visit the following link for further information on Add Text in an Existing PDF File

Hi Nayyer,


thanks for your quick response. But my question was related to Aspose PDF Kit for java. Can you move it to this section again.

Thanks.

Hi Anton,

In order to add text in truetype font, please provide the complete path of TTF file as an argument to the FormattedText object. Please take a look over the following code snippet and the resultant PDF which I have generated using Aspose.Pdf.Kit for Java 4.4.0.

[Java]

// add image in a PDF file
String inputFile = "c:/pdftest/Mail_Pdf_output.pdf";

String outputFile = "c:/pdftest/FileMend_kitOut.pdf";

int[] pages = { 1 };

FormattedText text = new FormattedText(
    "Sample Text in Rage",
    new FontColor(180, 0, 0),
    "c:/pdftest/RAGE.TTF", // path of the font, using the absolute path here
    EncodingType.MacRoman,
    false,
    40f
);

PdfFileMend mendor = new PdfFileMend(inputFile, outputFile);
mendor.addText(text, 1, 50, 400, 500, 700);

//lowerleftX < upperrightX,lowerleftY < upperrightY
mendor.addText(
    new FormattedText(
        "Hello World in Helvetica",
        new FontColor(180, 0, 0),
        FontStyle.Helvetica,
        EncodingType.Winansi,
        false,
        30
    ),
    pages,
    10,
    25,
    200,
    200
);
mendor.close();

PS, I am also moving this thread back to Aspose.Pdf.Kit product support forum.

thanks a lot.

Hi,


your example works fine. But is it also possible to insert text using CMYK-Colors (not RGB-colors)?

Thanks

Toni

Hi Anton,


Thanks for sharing the details and sorry for the delayed response.

I am afraid currently FormattedText does not support CMYK colors. For
the sake of correction, I have logged it in our issue tracking system as PDFKITJAVA-33261. We
will investigate this issue in details and will keep you updated on the status
of a correction.

We
apologize for your inconvenience.

Hi,


would it be possible to use the PdfContentEditor.replaceText - methode to replace an existing text (already formatted with CMYK-Color and a certain font) with a new text, but no loosing all the formatting?


Thanks Toni

Hi Anton,

Thanks for contacting support.

In order to replace text, PdfContentEditor class provides replaceText(..) method and in order to specify the font color for text being replaced, TextProperties.setColor method is used which accepts java.awt.Color object but I am afraid java.awt.Color does not directly support CMYK color. However, you may try using the approach specified over this link to see if it can be helpful while converting RGB color to CMYK. We are sorry for this inconvenience.

[Java]

//create PdfContentEditor object
PdfContentEditor editor = new PdfContentEditor();

//create TextProperties object with Font Style, Bold and Italic attributes
PdfContentEditor.TextProperties textProperties = new PdfContentEditor.TextProperties("Courier", true, true);

//set font size
textProperties.setTextSize(20);

 //set font color
textProperties.setColor(java.awt.Color.red);