Search code examples
javapdfitextpdf-manipulation

Text added to PDF appearing bold/rough


I'm adding/replacing some page numbers on an existing PDF's contents page, but the text is coming out bold, or rough. It's not right any ways, and I can't seem to fix it!

This is what I mean:

enter image description here

The numbers on the right are the existing page numbers I am replacing and the text is fine. The numbers on the left are the page numbers I have added using iText in Java.

Here is the code:

private static void fixTOCPageNumbers(int i, PdfContentByte content, List<Section> sections)
        throws DocumentException, IOException {

    int xPositionRec;
    int yPositionRec;
    int xPositionText;
    int yPositionText;
    int xOffset = 0;
    int yOffset = 0;

    content.saveState();
    content.setColorStroke(new Color(77,77,77));

    content.beginText();
    content.setFontAndSize(BaseFont.createFont("fonts/LTe50327.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED), 10f);

    int count = 5;

    for(int j = 4; j <= sections.size() - 2; j++)
    {           
        int startPageIndex = sections.get(j).GetStartPageIndex();
        int endPageIndex = sections.get(j).GetEndPageIndex();

        xPositionRec = 281;
        yPositionRec = 385;
        xPositionText = 266;
        yPositionText = 386;

        if(j > 6)
        {
            yPositionRec = 195;
            yPositionText = 196;
        }

        for(int k = startPageIndex; k <= endPageIndex; k++)
        {               
            content.rectangle(xPositionRec+xOffset,yPositionRec-yOffset,12,12);
            content.setRGBColorFill(255,255,255);
            content.showTextAligned(PdfContentByte.ALIGN_CENTER, String.format("%d", count), xPositionText+xOffset, yPositionText-yOffset, 0);
            content.setRGBColorFill(77,77,77);
            //content.fillStroke();
            yOffset += 18;

            count++;
        }

        yOffset = 0;

        if(j > 6)
        {
            xOffset += 229;
        }
        else if(j == 6)
        {
            xOffset = 0;
        }
        else
        {
            xOffset += 230;
        }
    }

    xOffset = 0;
    yOffset = 0;

    content.restoreState();
    content.endText();
}

Am I doing something wrong? This is the first time I've used iText and the code base wasn't originally mine.

Any help would be much appreciated!


Solution

  • You can simulate bold for example like this:

    C#

    cb.BeginText();
    cb.SetFontAndSize(font, 11F);
    
    cb.SetCharacterSpacing(1F);
    // Fill color (stroke fill)
    cb.SetRGBColorFill(0, 0, 0);  
    cb.SetLineWidth(0.5F);
    // Fill stroke simulate bold
    cb.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE);
    
    cb.SetTextMatrix(x, pageSize.Height - y);
    cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, s, (pageSize.Width / 2F), pageSize.Height - y, 0);
    
    cb.EndText();