Hi there.

I'm using itextsharp to do exactly what someone else was trying to do a
week ago (just joined, otherwise I'd have answered of course). My code
works fine for "normal" PDF files, that is without trying to create PDF/A
files. It fails as soon as I add the snippets to make it a PDF/A at 
content.SetFontAndSize(font, fontSize);
with a PDFXConformanceException, "All fonts must be embedded".
Yes, sure. Makes sense. But actually I already want to embed the font in
the first place.

I'm pasting a slightly longer snipped for the guys that want to do the
same, marking the possible cause with a huge/caps comment. Again: The code
works as long as I don't try to create a PDF/A compliant PDF (i.e. the
parameter to my method is false).
Thanks in advance for any help!
Ben

private static void ConvertToPDF(RandomAccessFileOrArray sourceFile, string
destinationImageFile, bool searchable, bool pdfAcompliant) {
  // Create the PDF document
  Document document = new Document();
  PdfWriter pdfWriter = PdfWriter.GetInstance(document, new
FileStream(destinationImageFile, FileMode.Create));
  pdfWriter.StrictImageSequence = true;
  pdfWriter.SetFullCompression();
  pdfWriter.SetLinearPageMode();     

  if (pdfAcompliant) {
    pdfWriter.PDFXConformance = PdfWriter.PDFA1B;
  }

  // POSSIBLE CAUSE HERE:
  // This is the font I'm using. It's only used for invisible rendering
anyway
  // The "embedded" parameter is true, but Visual Studio shows me that the
resulting
  // BaseFont instance's "embedded" member is set to false when I debug
this?
  BaseFont font = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI,
pdfAcompliant);
        
  // I do this for multiple pages, leaving the loop out here
  Image pageImage = SomeConversionMethod();

    // Set the size of the next page to the image dimensions
  iTextSharp.text.Rectangle PageRectangle = new
iTextSharp.text.Rectangle(pageImage.Width, pageImage.Height);
  document.SetPageSize(PageRectangle);          

  // Compute the scaling factor for coordinate transformations
  double horizontalScaling = pageImage.ScaledWidth / pageImage.Width;
  double verticalScaling   = pageImage.ScaledHeight / pageImage.Height;

  // Prepare/start the next page in the PDF document
  if (document.IsOpen()) {
    document.NewPage();
  } else {
    document.Open();

    if (pdfAcompliant) {
      // Copied verbatim from the itext mailing list, see:
      // http://article.gmane.org/gmane.comp.java.lib.itext.general/31582/
        
      PdfDictionary outputIntent = new PdfDictionary(PdfName.OUTPUTINTENT);
      outputIntent.Put(PdfName.OUTPUTCONDITIONIDENTIFIER, new
PdfString("sRGB IEC61966-2.1"));
      outputIntent.Put(PdfName.INFO, new PdfString("sRGB IEC61966-2.1"));
      outputIntent.Put(PdfName.S, PdfName.GTS_PDFA1);
      PdfICCBased ib = new
PdfICCBased(ICC_Profile.GetInstance(Properties.Resources.ICC_Profile));
      ib.Remove(PdfName.ALTERNATE);
      outputIntent.Put(PdfName.DESTOUTPUTPROFILE,
pdfWriter.AddToBody(ib).IndirectReference);
      pdfWriter.ExtraCatalog.Put(PdfName.OUTPUTINTENTS, new
PdfArray(outputIntent));
    }
  }

  if (searchable) {                
    // Grab the OCR Data
    SomePageStructure ocrPage = GetPageFromSomewhere();
    if (ocrPage != null) {                
      // Read all words in all lines
      foreach (Line ocrLine in ocrPage.Lines) {
        foreach (Word ocrWord in ocrLine.Words) {                    
          // Calculate the font size for the OCR rectangle bounds
          float fontSize = CalculateFontSize(font, ocrWord.Rectangle.Width
* horizontalScaling, ocrWord.Data);
    
          // Special case: Some characters are non-printable: Continue with
the next word
          if (fontSize < 0.1f) continue;
    
          // Write the text to the PDF, on the layer below the default
content, invisble
          PdfContentByte content = pdfWriter.DirectContentUnder;
          content.BeginText();
          // EXCEPTION BELOW
          content.SetFontAndSize(font, fontSize); // EXCEPTION HERE, font
seems not to be embedded
          // EXCEPTION ABOVE
         
content.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_INVISIBLE);
          content.SetTextMatrix(
            (float)(ocrWord.Rectangle.Location.X * horizontalScaling),
            pageImage.ScaledHeight - (float)(ocrWord.Rectangle.Bottom *
verticalScaling)
          );
          content.ShowText(ocrWord.Data);
          content.EndText();
        }
      }
    }    
  }
                                 
  // Draw/add the image to the PDF page                  
  pageImage.SetAbsolutePosition(0, 0);
  document.Add(pageImage);

  if (pdfAcompliant) pdfWriter.CreateXmpMetadata();
  document.Close();
}

Can anyone help out?


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.1t3xt.com/docs/book.php

Reply via email to