0

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!

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
New Start
  • 1,401
  • 5
  • 21
  • 29
  • Review the LTe50327.ttf that you have the frutiger font with no bold on it. – icrovett Nov 18 '13 at 15:34
  • That font is used elsewhere in the program and it comes out perfectly! – New Start Nov 18 '13 at 15:35
  • I think that has to be something with the `setColorStroke(new Color(77,77,77));` line, may be you should try to remove it from the `PdfContentByte`...just a guess, but i think it changes the intensity, you can try to put something like `setColorStroke(Color.Black);` – icrovett Nov 18 '13 at 15:50
  • Please supply the PDF to inspect. – mkl Nov 18 '13 at 15:54
  • I'm sorry, I can't supply the PDF. But if it helps, I inspected the PDF using PitStop in Adobe Acrobat, and the text isn't *actually* bold, it just appears rough/bold looking in the PDF. – New Start Nov 18 '13 at 15:57
  • 1
    You probably want to take the file that you've created, find the content stream and see if the font really is the same one that is being used elsewhere or is a different one. I would expect the content stream to include something like "/someresourcename 10.0 Tf 196 266 Td (6) Tj". 'someresourcename' will be a name in the page's font resource dictionary. If it's not the same as the existing text, there's your problem. – plinth Nov 18 '13 at 17:17
  • Does "boldness" disappear when zooming in? Then `Tr = 2` i.e. text stroked and looks like with zero stroke. Maybe `setColorStroke` sets it... And, though irrelevant, `q BT ... Q ET` = not clean. – user2846289 Nov 19 '13 at 06:46

1 Answers1

1

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();
Krzysztof Radzimski
  • 3,785
  • 3
  • 12
  • 12