User Tools

Site Tools


zalozeni:experiment:pdfcreator

PDFCreator

Tohle nemam reseny vubec elegantne. marklar-fx-common obsahuje v balicku cz.jlochman.marklar.fxcommon.protocol nejake konstanty, ktere nastavuji velikost fontu, odsazeni stranek, poskytuji tridu usnadnujici vytvareni tabulky… Vetsinu prace je ale treba napsat striktne do vlastniho projektu.

K vytvareni PDF jsem pouzil knihovnu org.apache.pdfbox

Projekt dokuwiki nema protokol nijak implementovan, tak alespon kratce, jaka je filozofie vytvareni protokolu… Ukazky kodu prevzaty z projektu plastometer

Odstraneni Cestiny

private String normalizeString(String s) {
  if (s == null)
    return "";
  if (s.isEmpty())
    return "";
  String resultString;
  resultString = s.replaceAll("[^\\x00-\\x7F]", "");
  return resultString;
}

Titulek

@SuppressWarnings("deprecation")
private void printTitle(PDPageContentStream stream, String title) throws IOException {
  PDFont font = MyFont.FONT_BOLD;
  float fontSize = MyFont.SIZE_HUGE;
  float titleWidth = font.getStringWidth(title) / 1000 * fontSize;
 
  stream.beginText();
  stream.setFont(font, fontSize);
  stream.moveTextPositionByAmount((pageWidth - titleWidth) / 2, pageHeight - MyPage.MARGIN_TOP - vertPos);
  stream.drawString(title);
  stream.endText();
}

Podtitulek

@SuppressWarnings("deprecation")
private void printSubTitle(PDPageContentStream stream, String title) throws IOException {
  PDFont font = MyFont.FONT_BOLD;
  float fontSize = MyFont.SIZE_BIG;
  float titleWidth = font.getStringWidth(title) / 1000 * fontSize;
 
  stream.beginText();
  stream.setFont(font, fontSize);
  stream.moveTextPositionByAmount((pageWidth - titleWidth) / 2, pageHeight - MyPage.MARGIN_TOP - vertPos);
  stream.drawString(title);
  stream.endText();
}

Nazev: Hodnota

@SuppressWarnings("deprecation")
private void printTwoColls(PDPageContentStream stream, String leftText, String rightText) throws IOException {
  stream.beginText();
  stream.setFont(MyFont.FONT_BOLD, MyFont.SIZE_NORMAL);
  stream.moveTextPositionByAmount(MyPage.MARGIN_LEFT, pageHeight - MyPage.MARGIN_TOP - vertPos);
  stream.drawString(leftText);
  stream.setFont(MyFont.FONT, MyFont.SIZE_NORMAL);
  stream.moveTextPositionByAmount(pageWidth / 2 - MyPage.MARGIN_LEFT, 0);
  stream.drawString(rightText);
  stream.endText();
}

Tabulka

private void printTblRow(PDPageContentStream stream, TblRow tblRow) throws IOException {
  printTblRow(stream, tblRow, MyFont.FONT);
}
 
@SuppressWarnings("deprecation")
private void printTblRow(PDPageContentStream stream, TblRow tblRow, PDFont myFont) throws IOException {
  float colWidth = (pageWidth - MyTable.MARGIN_LEFT - MyTable.MARGIN_RIGHT) / (tblRow.getSize() + 1);
 
  stream.beginText();
  stream.setFont(myFont, MyFont.SIZE_NORMAL);
  stream.moveTextPositionByAmount(MyPage.MARGIN_LEFT + MyTable.MARGIN_RIGHT, pageHeight - MyPage.MARGIN_TOP - vertPos + 5);
  for (String s : tblRow.getList()) {
    stream.drawString(Normalizer.normalize(s, Form.NFKD));
    stream.moveTextPositionByAmount(colWidth, 0);
  }
  stream.endText();
}
 
@SuppressWarnings("deprecation")
private void printTblUnderscore(PDPageContentStream stream) throws IOException {
  stream.setStrokingColor(Color.BLACK);
  float tblLeft = MyPage.MARGIN_LEFT + MyTable.MARGIN_LEFT;
  float tblRight = pageWidth - MyTable.MARGIN_RIGHT - MyTable.MARGIN_RIGHT;
  float lineVertical = pageHeight - MyPage.MARGIN_TOP - vertPos - 2;
  stream.drawLine(tblLeft, lineVertical, tblRight, lineVertical);
}

Text

@SuppressWarnings("deprecation")
private void printText(PDPageContentStream stream, String text) throws IOException {
  List<String> lines = breakLongString(text, MyFont.FONT, MyFont.SIZE_NORMAL);
 
  stream.beginText();
  stream.setFont(MyFont.FONT, MyFont.SIZE_NORMAL);
  stream.moveTextPositionByAmount(MyPage.MARGIN_LEFT + MyText.MARGIN_LEFT, pageHeight - MyPage.MARGIN_TOP - vertPos);
  for (String s : lines) {
    stream.drawString(Normalizer.normalize(s, Form.NFKD));
    stream.moveTextPositionByAmount(0, -20);
    vertPos += 20;
  }
 
  stream.endText();
}
 
private List<String> breakLongString(String s, PDFont pdfFont, float fontSize) throws IOException {
  List<String> lines = new ArrayList<String>();
 
  if (s == null)
    return lines;
 
  float width = pageWidth - MyPage.MARGIN_LEFT - MyPage.MARGIN_RIGHT - MyText.MARGIN_LEFT - MyText.MARGIN_RIGHT;
  int lastSpace = -1;
 
  while (s.length() > 0) {
    int spaceIndex = s.indexOf(' ', lastSpace + 1);
    if (spaceIndex < 0)
      spaceIndex = s.length();
    String subString = s.substring(0, spaceIndex);
    float size = fontSize * pdfFont.getStringWidth(subString) / 1000;
    if (size > width) {
      if (lastSpace < 0)
        lastSpace = spaceIndex;
      subString = s.substring(0, lastSpace);
      lines.add(subString);
      s = s.substring(lastSpace).trim();
      lastSpace = -1;
    } else if (spaceIndex == s.length()) {
      lines.add(s);
      s = "";
    } else {
      lastSpace = spaceIndex;
    }
  }
 
  return lines;
}

Graf

Graf je treba exportovat do .png a nasledne vlozit na aktualni stranku PDF

public class PlastGraphImageService {
 
  PlastExpInstanceDataService dataService;
  PlastExpInstanceSettingsService instanceSS;
  PlastExpTypeSettingsService expTypeSS;
 
  public PlastGraphImageService(ExperimentInstance expInstance) {
    if (expInstance == null)
      return;
 
    dataService = (PlastExpInstanceDataService) expInstance.getDataService();
    instanceSS = (PlastExpInstanceSettingsService) expInstance.getSettignsService();
    expTypeSS = (PlastExpTypeSettingsService) expInstance.getExperimentType().getExpDefaultSettingsService();
  }
 
  public void create(String imageName) throws IOException {
    PlastGraph graph = new PlastGraph(expTypeSS.isLogAxis(), false);
    graph.repaint(instanceSS, dataService);
    LineChart<Number, Number> chart = graph.getChart();
    chart.getStylesheets().clear();
    chart.getStylesheets().addAll(getClass().getResource("graph.css").toExternalForm());
 
    Scene scene = new Scene(chart, 1000, 700);
    WritableImage image = new WritableImage(1000, 700);
    scene.snapshot(image);
 
    File file = new File(imageName);
    RenderedImage renderedImage = SwingFXUtils.fromFXImage(image, null);
    ImageIO.write(renderedImage, "png", file);
  }
 
}
String imageName = fullFileName + "_" + counter + ".png";
PlastGraphImageService imgService = new PlastGraphImageService(expInstance);
imgService.create(imageName);
 
PDImageXObject pdImage = PDImageXObject.createFromFile(imageName, document);
PDPageContentStream contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, true);
 
float scale = (pageWidth - MyPage.MARGIN_LEFT - MyPage.MARGIN_RIGHT) / pdImage.getWidth();
contentStream.drawImage(pdImage, MyPage.MARGIN_LEFT, (pageHeight - pdImage.getHeight() * scale) / 2, pdImage.getWidth() * scale,
    pdImage.getHeight() * scale);
contentStream.close();

Titulni strana

private void createFirstPage(PDDocument document) throws IOException {
  PDPage page = new PDPage();
  pageWidth = page.getMediaBox().getWidth();
  pageHeight = page.getMediaBox().getHeight();
 
  document.addPage(page);
  PDPageContentStream stream = new PDPageContentStream(document, page);
 
  vertPos = MyPage.MARGIN_TOP;
  printTitle(stream, expTypeSS.getProtocolTitle());
  vertPos += 20;
  printSubTitle(stream, expTypeSS.getProtocolSubTitle());
  vertPos += 30;
  printTwoColls(stream, I18n.getGraphicsBundle().getString("measurementLog"), Long.toString(sample.getId()));
  vertPos += 20;
  printTwoColls(stream, I18n.getGraphicsBundle().getString("sample"), sample.getName());
  vertPos += 20;
  printTwoColls(stream, I18n.getGraphicsBundle().getString("results"), "");
  vertPos += 30;
 
  TblRow tblRow = new TblRow(6);
  tblRow.add("#", I18n.getGraphicsBundle().getString("t1"), I18n.getGraphicsBundle().getString("t2"), I18n.getGraphicsBundle()
      .getString("t3"), I18n.getGraphicsBundle().getString("ddMax"), I18n.getGraphicsBundle().getString("ddTotal"));
  printTblRow(stream, tblRow, MyFont.FONT_BOLD);
  printTblUnderscore(stream);
 
  int counter = 0;
  for (ExperimentInstance expInstance : expInstances) {
    vertPos += 20;
    counter++;
    tblRow.clear();
    tblRow.add(Integer.toString(counter));
    PlastExpInstanceDataService dataService = (PlastExpInstanceDataService) expInstance.getDataService();
    tblRow.add(DecimalHelper.toString(dataService.getT1(), 1));
    tblRow.add(DecimalHelper.toString(dataService.getT2(), 1));
    tblRow.add(DecimalHelper.toString(dataService.getT3(), 1));
    tblRow.add(Integer.toString(dataService.getDdMax()));
    tblRow.add(Integer.toString(dataService.getDdCelk()));
    printTblRow(stream, tblRow);
  }
  printTblUnderscore(stream);
 
  vertPos += 20;
  PlastMeanCalculator plastCalculator = new PlastMeanCalculator(expInstances);
  tblRow.clear();
  tblRow.add("");
  tblRow.add(plastCalculator.getT1Stat().asString(1, 1));
  tblRow.add(plastCalculator.getT2Stat().asString(1, 1));
  tblRow.add(plastCalculator.getT3Stat().asString(1, 1));
  tblRow.add(plastCalculator.getDdMaxStat().asString(0, 0));
  tblRow.add(plastCalculator.getDdCelkStat().asString(0, 0));
  printTblRow(stream, tblRow, MyFont.FONT_BOLD);
 
  vertPos += 30;
  printTwoColls(stream, I18n.getGraphicsBundle().getString("sampleInfo"), "");
 
  vertPos += 20;
  printText(stream, normalizeString(sample.getDescription()));
 
  stream.close();
}

Strana s grafem

private void createGraphPage(PDDocument document, ExperimentInstance expInstance, int counter) throws IOException {
  PDPage page = new PDPage();
  pageWidth = page.getMediaBox().getWidth();
  pageHeight = page.getMediaBox().getHeight();
 
  document.addPage(page);
  PDPageContentStream stream = new PDPageContentStream(document, page);
 
  PlastExpInstanceDataService dataService = (PlastExpInstanceDataService) expInstance.getDataService();
  vertPos = MyPage.MARGIN_TOP;
  printTwoColls(stream, I18n.getGraphicsBundle().getString("measurementNumber"), Integer.toString(counter));
  vertPos += 20;
  printTwoColls(stream, I18n.getGraphicsBundle().getString("t1"), DecimalHelper.toString(dataService.getT1(), 1));
  vertPos += 20;
  printTwoColls(stream, I18n.getGraphicsBundle().getString("t2"), DecimalHelper.toString(dataService.getT2(), 1));
  vertPos += 20;
  printTwoColls(stream, I18n.getGraphicsBundle().getString("t3"), DecimalHelper.toString(dataService.getT3(), 1));
  vertPos += 20;
  printTwoColls(stream, I18n.getGraphicsBundle().getString("ddMax"), Integer.toString(dataService.getDdMax()));
  vertPos += 20;
  printTwoColls(stream, I18n.getGraphicsBundle().getString("ddTotal"), Integer.toString(dataService.getDdCelk()));
  stream.close();
 
  String imageName = fullFileName + "_" + counter + ".png";
  PlastGraphImageService imgService = new PlastGraphImageService(expInstance);
  imgService.create(imageName);
 
  PDImageXObject pdImage = PDImageXObject.createFromFile(imageName, document);
  PDPageContentStream contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, true);
 
  float scale = (pageWidth - MyPage.MARGIN_LEFT - MyPage.MARGIN_RIGHT) / pdImage.getWidth();
  contentStream.drawImage(pdImage, MyPage.MARGIN_LEFT, (pageHeight - pdImage.getHeight() * scale) / 2, pdImage.getWidth() * scale,
      pdImage.getHeight() * scale);
  contentStream.close();
}
zalozeni/experiment/pdfcreator.txt · Last modified: 2016/08/01 11:07 by jlochman