Switch between multiple functions MATLAB - matlab

So I have an m. file script that contains 2 functions.
The first one (that's loaded when I run the script) and the second.
Each of them has a different GUI setup and different text in text boxes (sorry for tautology).
My program is about calculating stuff using economic formulas, and the final version of the program is going to have about 50 formulas in it. AND I don't want to make 50 separate scripts for each formula. What I want is to be able to switch between formulas inside one script.
So I made a specific push button for that purpose (code below) but when I press it nothing happens.
Can someone who's experienced enough tell me what did I do wrong? (I'm new to MATLAB).
Let me know if more information is needed, or the question is not clear enough. Thanks in advance!
uicontrol('Style','pushbutton','Position',[136,88,194,27],'String','Next formula','FontSize',10,'FontName','MS Reference Sans Serif','BackgroundColor',[0.756862759590149 0.866666674613953 0.776470601558685],'CallBack',#SecondScript);
Here's the whole script:
function FirstScript
clc
clear
close all
global ZatratyNaSozdanieProgProdukta hEditZzpspp hEditZmvspp hEditZobsh
ScreenSize = get(0,'ScreenSize');
set ( 0, 'DefaultFigureColor', [0.23137255012989 0.443137258291245 0.337254911661148] )
hFig = figure('Visible','off','Position',[ScreenSize(3)/2,ScreenSize(4)/2,550,450]);
uicontrol('Style','Pushbutton','Position',[371,136,98,27],'String','Рассчитать','FontSize',10,'FontName','MS Reference Sans Serif','Callback',#CalculateCallback,'BackgroundColor',[0.756862759590149 0.866666674613953 0.776470601558685]);
uicontrol('Style','pushbutton','Position',[136,88,194,27],'String','Next formula','FontSize',10,'FontName','MS Reference Sans Serif','BackgroundColor',[0.756862759590149 0.866666674613953 0.776470601558685],'CallBack',#SecondScript);
axes('units','pixels','position',[20 100 200 24],'visible','off');
message = sprintf('Формула определения затрат на\nсоздание программного продукта:\n \nЗ^З^П_С_П_П+З^М^В_С_П_П+З_О_Б_Щ');
text(0,4.6,message,'interpreter','tex','Position',[1.18 8.64166666666667 0],'HorizontalAlignment','center','FontSize',12,'FontName','MS Reference Sans Serif','BackgroundColor',[0.756862759590149 0.866666674613953 0.776470601558685]);
axes('units','pixels','position',[20 100 200 24],'visible','off');
hTextZzpspp = text(0,4.6,'З^З^П_С_П_П','interpreter','tex','Position',[0.55 4.14166666666666 0],'FontSize',12,'FontName','MS Reference Sans Serif','BackgroundColor',[0.756862759590149 0.866666674613953 0.776470601558685]);
hEditZzpspp = uicontrol('Style','Edit','Position',[117,150,72,25],'String','','FontSize',10,'FontName','MS Reference Sans Serif');
axes('units','pixels','position',[20 100 200 24],'visible','off');
hTextZmvspp = text(0,4.6,'З^М^В_С_П_П','interpreter','tex','Position',[0.935 4.14166666666666 0],'FontSize',12,'FontName','MS Reference Sans Serif','BackgroundColor',[0.756862759590149 0.866666674613953 0.776470601558685]);
hEditZmvspp = uicontrol('Style','Edit','Position',[195,150,72,25],'String','','FontSize',10,'FontName','MS Reference Sans Serif');
axes('units','pixels','position',[20 100 200 24],'visible','off');
hTextZobsh = text(0,4.6,'З_О_Б_Щ','interpreter','tex','Position',[1.32 4.05833333333332 0],'FontSize',12,'FontName','MS Reference Sans Serif','BackgroundColor',[0.756862759590149 0.866666674613953 0.776470601558685]);
hEditZobsh = uicontrol('Style','Edit','Position',[274,150,72,25],'String','','FontSize',10,'FontName','MS Reference Sans Serif');
uicontrol('Style','Text','Position',[370,191,100,29],'String','Результат:','FontSize',12,'FontName','MS Reference Sans Serif','BackgroundColor',[0.756862759590149 0.866666674613953 0.776470601558685]);
ZatratyNaSozdanieProgProdukta = uicontrol('Style','Text','Position',[370,168,100,23],'String','','FontSize',10,'FontName','MS Reference Sans Serif');
set(hFig,'Visible','on')
function CalculateCallback(~,~)
Zzpspp = str2double(get(hEditZzpspp,'String'));
Zmvspp = str2double(get(hEditZmvspp,'String'));
Zobsh = str2double(get(hEditZobsh,'String'));
Calculation = Zzpspp+Zmvspp+Zobsh;
set(ZatratyNaSozdanieProgProdukta,'String',sprintf('%0.2f',Calculation));
end
end
function SecondScript
clc
clear
close all
global RashodyNaOplatuTrudaRazrabotchikaProgrammy hEditZosnzp hEditZdopzp hEditZotchzp
ScreenSize = get(0,'ScreenSize');
set ( 0, 'DefaultFigureColor', [0.23137255012989 0.443137258291245 0.337254911661148] )
hFig = figure('Visible','off','Position',[ScreenSize(3)/2,ScreenSize(4)/2,550,450]);
uicontrol('Style','Pushbutton','Position',[371,136,98,27],'String','Рассчитать','FontSize',10,'FontName','MS Reference Sans Serif','Callback',#CalculateCallback,'BackgroundColor',[0.756862759590149 0.866666674613953 0.776470601558685]);
axes('units','pixels','position',[20 100 200 24],'visible','off');
message = sprintf('Формула определения расходов на\nоплату труда разработчика программы:\n \nЗ^З^П_С_П_П+З^М^В_С_П_П+З_О_Б_Щ');
text(0,4.6,message,'interpreter','tex','Position',[1.18 8.64166666666667 0],'HorizontalAlignment','center','FontSize',12,'FontName','MS Reference Sans Serif','BackgroundColor',[0.756862759590149 0.866666674613953 0.776470601558685]);
axes('units','pixels','position',[20 100 200 24],'visible','off');
hTextZosnzp = text(0,4.6,'З^З^П_С_П_П','interpreter','tex','Position',[0.55 4.14166666666666 0],'FontSize',12,'FontName','MS Reference Sans Serif','BackgroundColor',[0.756862759590149 0.866666674613953 0.776470601558685]);
hEditZosnzp = uicontrol('Style','Edit','Position',[117,150,72,25],'String','','FontSize',10,'FontName','MS Reference Sans Serif');
axes('units','pixels','position',[20 100 200 24],'visible','off');
hTextZdopzp = text(0,4.6,'З^М^В_С_П_П','interpreter','tex','Position',[0.935 4.14166666666666 0],'FontSize',12,'FontName','MS Reference Sans Serif','BackgroundColor',[0.756862759590149 0.866666674613953 0.776470601558685]);
hEditZdopzp = uicontrol('Style','Edit','Position',[195,150,72,25],'String','','FontSize',10,'FontName','MS Reference Sans Serif');
axes('units','pixels','position',[20 100 200 24],'visible','off');
hTextZotchzp = text(0,4.6,'З_О_Б_Щ','interpreter','tex','Position',[1.32 4.05833333333332 0],'FontSize',12,'FontName','MS Reference Sans Serif','BackgroundColor',[0.756862759590149 0.866666674613953 0.776470601558685]);
hEditZotchzp = uicontrol('Style','Edit','Position',[274,150,72,25],'String','','FontSize',10,'FontName','MS Reference Sans Serif');
uicontrol('Style','Text','Position',[370,191,100,29],'String','Результат:','FontSize',12,'FontName','MS Reference Sans Serif','BackgroundColor',[0.756862759590149 0.866666674613953 0.776470601558685]);
RashodyNaOplatuTrudaRazrabotchikaProgrammy = uicontrol('Style','Text','Position',[370,168,100,23],'String','','FontSize',10,'FontName','MS Reference Sans Serif');
set(hFig,'Visible','on')
function CalculateCallback(~,~)
Zosnzp = str2double(get(hEditZosnzp,'String'));
Zdopzp = str2double(get(hEditZdopzp,'String'));
Zotchzp = str2double(get(hEditZotchzp,'String'));
Calculation = Zosnzp+Zdopzp+Zotchzp;
set(RashodyNaOplatuTrudaRazrabotchikaProgrammy,'String',sprintf('%0.2f',Calculation));
end
end

I have a trick, although i myself find it not very good-looking:
Assuming you put all of these functions inside a file myforms.m without any blank line. Then, at the beginning of your GUI, open and read your file:
f1 = fopen('myforms.m');
alllines = textscan(f1, '%s', 'Delimiter', '');
fclose(f1);
alllines{1} is now a cell array, and each element is one line from myforms.m.
Now, when you want to switch to a formula, you need to know the line number of its section in myforms.m, for example starting from line 10, ending at line 15.
Create a new file, i.e callme.m which should be called by your button, and write these lines to the file:
f2 = fopen('callme.m', 'w');
for i = 10:15
fprintf(f2, '%s\n', alllines{1}{i});
end
fclose(f2);
Your callback function can then be #callme.

I've put source and eventdata in a set of parentheses directly after the function's name and it solved the problem! I can now easily switch between formulas as fast as I can push the button.
Here's how my function's name looked like before:
function SecondScript
Here's how it looks now:
function SecondScript(source,eventdata)

Related

i am trying to get a bokeh server to run, but when i type in the command in my terminal i get an error message

I am trying to run the code below in a bokeh server to visualize the plots. the final_imdb_dataframe is in the same directory as the python code. these are the steps i take to get the server to run:
open my terminal
type in "cd" then a space and then my directory in for the map containing my python script and the final_imdb_dataframe
press enter
type: bokeh serve --show "filename.py"
press enter
in my terminal i am getting this error message however:
bokeh : The term 'bokeh' is not recognized as the name of a cmdlet, function, script file, or operable program. (the complete error message is attached as a picture)
when i type in "pip show bokeh" in the command promt in python it gives me this:
pip show bokeh
Name: bokeh
Version: 2.4.3
Summary: Interactive plots and applications in the browser from Python
Home-page: https://github.com/bokeh/bokeh
Author: Bokeh Team
Author-email: info#bokeh.org
License: BSD-3-Clause
Location: c:\users\fazan\anaconda\lib\site-packages
Requires: Jinja2, numpy, packaging, pillow, PyYAML, tornado, typing-extensions
Required-by: hvplot, panel
Note: you may need to restart the kernel to use updated packages.
so it should work right?
i don't know what to do anymore...
code:
import pandas as pd
from bokeh.plotting import figure, show
import numpy as np
from bokeh.models import ColumnDataSource, HoverTool, Slider, RangeSlider, Div, Select
from bokeh.io import output_file, curdoc
from bokeh.layouts import column, row, layout
#EERSTE PLOT:
#line diagram of amount of movies released each year en avg rating displayed over the years
df = pd.read_csv("final_imdb_dataframe")
#"unnamed column weghalen
df.drop(df.columns[df.columns.str.contains('unnamed',case = False)],axis = 1, inplace = True)
#creating plot 2
plot2 = figure(plot_width = 1000,
plot_height = 400,
x_axis_label= "year",
title = "watchtime and movie rating over the years")
#creating a dropdown menu
source2 = ColumnDataSource(data={'x': df["release_date"], 'y': df["watchtime"]})
plot2.circle(x="x", y="y", size = 3, source = source2, alpha = 0.2)
def update_plot(attr, old, new):
if new == 'watchtime':
source2.data = {'x' : df["release_date"], 'y' : df["watchtime"]}
else:
source2.data = {'x' : df["release_date"], 'y' : df["movie_rating"]}
select = Select(title = "keuze menu", options=["watchtime", "movie_rating"], value = 'watchtime')
#code for updating the plot when the value is changed
select.on_change('value', update_plot)
#TWEEDE PLOT:
#creating a list of all the years a movie came out
dfrelease = df.release_date.drop_duplicates().tolist()
#sorting the list with release dates to get them in chronological order
dfrelease1 = sorted(dfrelease)
#creating an empty list
total_releases_per_year = []
#adding the amount of movies that came out every year to the list
for i in range(len(dfrelease1)):
total_releases_per_year.append(len(df[df["release_date"] == dfrelease1[i]]))
#creating a columndatasource of the two lists created above to make it possible for the hovertool to be used
source1 = ColumnDataSource(data=dict(year = dfrelease1, amount_of_movies_made = total_releases_per_year))
#making a hovertool
hover2 = HoverTool(tooltips=[("year", "#year"), ("amount of movies made", "#amount_of_movies_made")])
#creating plot 3
plot3 = figure(plot_width = 1000,
plot_height = 400,
x_axis_label= "year",
y_axis_label = "amount of movies",
title = "amount of movies per year")
plot3.line("year", "amount_of_movies_made", source = source1)
plot3.add_tools(hover2)
#DERDE PLOT:
#creating the ColumnDataSource
df_sel = df[df["release_date"] == 2020]
source1 = ColumnDataSource(df_sel)
#scatter plot
p1 = figure(title = "relation 'watchtime' and 'movie rating' over the years",
x_axis_label= "watchtime",
y_axis_label= "rating (0-10)",
plot_width = 1000,
plot_height = 400)
p1.circle(x="watchtime", y="movie_rating", alpha = 0.2, source=source1)
#making the rangeslider
slider = RangeSlider(title="release date", value= (1919, 2021), start=1919, end=2020, step=1)
#making the hover tool
hover1 = HoverTool(tooltips=[("title", "#movie_name"),("gross", "#gross_collection"),("votes", "#votes"),("genre", "#genre"), ("watchtime", "#watchtime"),("rating", "#movie_rating"),("release date", "#release_date")])
#making a def callback for the rangeslider to work when moved
def callback(attr, old, new):
year = slider.value
source1.data = df[(df["release_date"] >= year[0]) & (df["release_date"] <= year[1])]
slider.on_change("value", callback)
layout = column(slider, p1) #create a layout with slider on top of plot
p1.add_tools(hover1) #adding hover tool
layout3 = column(select, plot2, plot3, slider, p1) #making the layout for the dashboard
curdoc().add_root(layout3)*
i tried what i wrote above and expected acces to the bokeh server that would show me my plots. i have done this before on my previous computer (mac) and it worked then. now on my lenovo it is not working.`

A bug is encountered with the "Maskable PPO" training with custom Env setup

I encountered an error while using SB3-contrib Maskable PPO action masking algorithm.
File ~\anaconda3\lib\site-packages\sb3_contrib\common\maskable\distributions.py:231, in MaskableMultiCategoricalDistribution.apply_masking(self, masks)
228 masks = th.as_tensor(masks)
230 # Restructure shape to align with logits
--> 231 masks = masks.view(-1, sum(self.action_dims))
233 # Then split columnwise for each discrete action
234 split_masks = th.split(masks, tuple(self.action_dims), dim=1)
RuntimeError: shape '[-1, 1600]' is invalid for input of size 800
I am running learning progamme with an action being a MultiBinary space with 800 selections of 0, 1.
The action space is defined as below:
self.action_space = spaces.MultiBinary(800)
Within the custom environment class, an "action_mask" function was created such that it returns a List of 800 boolean values.
Now, when I follow the document and start to train the model, the error message pops:
from sb3_contrib import MaskablePPO
from Equities_RL_Env import Equities_RL_Env
import time
from sb3_contrib.common.maskable.utils import get_action_masks
models_dir = f"models/V1 31-Jul/"
logdir = f"logs/{time.strftime('%d %b %Y %H-%M',time.localtime())}/"
if not os.path.exists(models_dir):
os.makedirs(models_dir)
if not os.path.exists(logdir):
os.makedirs(logdir)
env = Equities_RL_Env(Normalize_frame(historical_frame), pf)
env.reset()
model = MaskablePPO('MlpPolicy', env, verbose=1, tensorboard_log=logdir)
TIMESTEPS = 1000
iters = 0
while iters <= 1000000:
iters += 1
model.learn(total_timesteps=TIMESTEPS, reset_num_timesteps=False, tb_log_name=f"PPO")
model.save(f"{models_dir}/{TIMESTEPS*iters}")
May I know is there a way to define that shape within the custom environment?

Loading fonts in Python/Pillow on a Mac

The Pillow ImageFont documentation has a few examples of using different fonts, but they don't work "out of the box" on a Mac. It's probably not too hard to figure out where my system fonts are located, but I'm lazy and would like a simple example that I can just edit.
So: how can I use Pillow on a Mac to write some text in a different font and size from the default?
Ok, "Lazy One" ;-) Here is an example:
from PIL import Image, ImageFont, ImageDraw
# Make a red canvas and get a drawing context to it
img = Image.new(mode='RGB', size=(500, 200), color=(255,0,0))
draw = ImageDraw.Draw(img)
# Get a "Chalkduster" TTF
chalk = ImageFont.truetype("Chalkduster.ttf",16)
# Get a "Keyboard" TTF
kbd = ImageFont.truetype("Keyboard.ttf",26)
# Make example for Lazy One - showing how to colour cyan too
draw.text((40,40),"Keyboard",font=kbd, fill=(0,255,255))
# And another
draw.text((20,140),"Chalkduster",font=chalk)
img.save("test.png")
If you want a list of all the TTF fonts on your system, you can look in both:
/System/Library/Fonts, and
/Library/Fonts
And, if you are very lazy, you can check in both at the same time from Terminal:
find {/System,}/Library/Fonts -name \*ttf
Sample Output
/System/Library/Fonts/SFNSDisplay.ttf
/System/Library/Fonts/Symbol.ttf
/System/Library/Fonts/ZapfDingbats.ttf
/System/Library/Fonts/Apple Braille.ttf
/System/Library/Fonts/SFNSText.ttf
/System/Library/Fonts/Apple Braille Outline 6 Dot.ttf
/System/Library/Fonts/Apple Braille Pinpoint 6 Dot.ttf
/System/Library/Fonts/Apple Symbols.ttf
/System/Library/Fonts/SFNSTextItalic.ttf
/System/Library/Fonts/SFNSRounded.ttf
/System/Library/Fonts/Apple Braille Pinpoint 8 Dot.ttf
/System/Library/Fonts/Keyboard.ttf
/System/Library/Fonts/Apple Braille Outline 8 Dot.ttf
/Library/Fonts/Webdings.ttf
/Library/Fonts/Zapfino.ttf
/Library/Fonts/Trebuchet MS Italic.ttf
/Library/Fonts/Georgia.ttf
/Library/Fonts/Verdana Bold.ttf
/Library/Fonts/Bodoni 72 Smallcaps Book.ttf
/Library/Fonts/Times New Roman Bold Italic.ttf
/Library/Fonts/Silom.ttf
/Library/Fonts/Verdana Italic.ttf
/Library/Fonts/Times New Roman Italic.ttf
/Library/Fonts/Bradley Hand Bold.ttf
/Library/Fonts/Arial Narrow Italic.ttf
/Library/Fonts/AppleGothic.ttf
/Library/Fonts/DIN Condensed Bold.ttf
/Library/Fonts/Farisi.ttf
/Library/Fonts/Arial Bold.ttf
/Library/Fonts/Trebuchet MS.ttf
/Library/Fonts/Mishafi.ttf
/Library/Fonts/Trattatello.ttf
/Library/Fonts/BigCaslon.ttf
/Library/Fonts/Courier New Bold.ttf
/Library/Fonts/NISC18030.ttf
/Library/Fonts/Lao Sangam MN.ttf
/Library/Fonts/Luminari.ttf
/Library/Fonts/Times New Roman.ttf
/Library/Fonts/Brush Script.ttf
/Library/Fonts/Georgia Italic.ttf
/Library/Fonts/Courier New Italic.ttf
/Library/Fonts/Arial Unicode.ttf
/Library/Fonts/Chalkduster.ttf
/Library/Fonts/Apple Chancery.ttf
/Library/Fonts/AppleMyungjo.ttf
/Library/Fonts/Arial Narrow Bold Italic.ttf
/Library/Fonts/Arial Narrow.ttf
/Library/Fonts/Courier New.ttf
/Library/Fonts/Wingdings 3.ttf
/Library/Fonts/Wingdings 2.ttf
/Library/Fonts/Hoefler Text Ornaments.ttf
/Library/Fonts/Bodoni Ornaments.ttf
/Library/Fonts/Skia.ttf
/Library/Fonts/Trebuchet MS Bold Italic.ttf
/Library/Fonts/Impact.ttf
/Library/Fonts/Kokonor.ttf
/Library/Fonts/Tahoma Bold.ttf
/Library/Fonts/Arial.ttf
/Library/Fonts/Diwan Thuluth.ttf
/Library/Fonts/Ayuthaya.ttf
/Library/Fonts/Khmer Sangam MN.ttf
/Library/Fonts/Trebuchet MS Bold.ttf
/Library/Fonts/Arial Black.ttf
/Library/Fonts/Courier New Bold Italic.ttf
/Library/Fonts/Comic Sans MS.ttf
/Library/Fonts/DIN Alternate Bold.ttf
/Library/Fonts/Wingdings.ttf
/Library/Fonts/Sathu.ttf
/Library/Fonts/Arial Bold Italic.ttf
/Library/Fonts/Tahoma.ttf
/Library/Fonts/PlantagenetCherokee.ttf
/Library/Fonts/Georgia Bold.ttf
/Library/Fonts/Verdana Bold Italic.ttf
/Library/Fonts/Microsoft Sans Serif.ttf
/Library/Fonts/Georgia Bold Italic.ttf
/Library/Fonts/Arial Rounded Bold.ttf
/Library/Fonts/Times New Roman Bold.ttf
/Library/Fonts/Krungthep.ttf
/Library/Fonts/Gurmukhi.ttf
/Library/Fonts/Andale Mono.ttf
/Library/Fonts/Mishafi Gold.ttf
/Library/Fonts/Herculanum.ttf
/Library/Fonts/Comic Sans MS Bold.ttf
/Library/Fonts/Arial Italic.ttf
/Library/Fonts/Verdana.ttf
/Library/Fonts/Arial Narrow Bold.ttf

Indentation misplaced while creating PDF using perl module PDF::API2

I have data in an array and I can write the data into a pdf format using PDF::API2 .But the problem is during the writing process the Indentation(spaces) is not exactly same as in the array
In array format:
ATOM 1 N MET A 0 24.277 8.374 -9.854 1.00 38.41 N 0.174
ATOM 38 OE2 GLU A 4 37.711 19.692 -12.684 1.00 28.70 O 0.150
In PDF format:
ATOM 1 N MET A 0 24.277 8.374-9.8541.0038.41 N 0.174
ATOM 38 OE2 GLU A 4 37.71119.692-12.684 1.00 28.70 O 0.150
My code:
my $pdf = PDF::API2->new(-file => "/home/httpd/cgi-bin/new.pdf");
$pdf->mediabox("A4");
my $page = $pdf->page;
my $fnt = $pdf->corefont('Arial',-encoding => 'latin1');
my $txt = $page->text;
$txt->textstart;
$txt->font($fnt, 8);
$txt->translate(100,800);
$j1=0;
for($i=0;$i<=scalar(#ar_velz);$i++) #Data input to write in PDF
{
$txt->lead(10);
$txt->section("$ar_velz[$i]", 500, 800); #writing each array index
if($j1 == 75) #To create a page for every 75 lines
{
$page = $pdf->page;
$fnt = $pdf->corefont('Arial',-encoding => 'latin1');
$txt = $page->text;
$txt->textstart;
$txt->font($fnt, 8);
$txt->lead(10);
$txt->translate(100,800);
$j1=0;
}
$j1++;
}
$txt->textend;
$pdf->save;
$pdf->end( );
}
That happens because Arial is not a mono-spaced font. The characters all have different widths. Especially a blank space is usually not very wide. If you want the spacing to stay intact, you need to use a mono-spaced font, such as Courier.
$fnt = $pdf->corefont('Courier',-encoding => 'latin1');
That fact is also why PDF::API2 includes a method advancewidth in its PDF::API2::Content class. You can use that to check if a block of text is too wide to fit into a line, and manually wrap it if needed. Of course for your table, that doesn't help.
An alternative to the mono-spaced font might be to use PDF::Table, which can create tables inside a PDF::API2 document.

PdfReaderContentParser.ProcessContent returns whitespace for clear text

I'd like to parse a pdf for texts containing both, binary and clear text data. When I try to do it with PdfReaderContentParser the GetResultantText method returns the right texts for the binary content but whitespaces for the clear text content. Here is the code I use:
byte[] binaryPdf = File.ReadAllBytes(this.fileName);
reader = new PdfReader(binaryPdf);
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
for (int i = 1; i <= reader.NumberOfPages; i++)
{
SimpleTextExtractionStrategy simpleStragety = parser.ProcessContent(i, new SimpleTextExtractionStrategy());
string contentText = simpleStragety.GetResultantText();
// Do something with the contentText
// ...
}
Any idea how to get all content?
Overview
In a comment the OP clarified which texts he was missing in his extracted text:
Basically for all descriptions on the left-hand side (e.g. Lifting moment) I get whitespaces instead of the actual text.
The reason for this is fairly simple: In the page content there are only spaces (if anything at all) on most of the left side. The labels you see actually are read-only form fields.
For example the "Lifting moment" is the value of the form field 13B141032.
If you want text extraction to include these fields, too, you should consider flattening the document in a first step (moving the field appearances into the regular page content stream) and extracting text from this flattened document.
Document analysis
It looks like the major part of the internationalization of the specification labels has been done using form fields.
For an overview I separated the original document
into its regular page content
and the form fields
There indeed are several strings of spaces in the page content under the form fields.
I would assume that there once was an earlier version of that document (or a template for it) which contained those labels (maybe in only one language or probably two) as page content.
Then there was a task of more dynamic internationalization, so someone replaced the existing labels in the page content by spaces and added new internationalized labels as read-only form-fields, probably because form fields are easier to manipulate.
Considering that the original labels seem to have been replaced by an equal number of spaces, though, one might speculate that there even is another program manipulating the page stream of this and similar documents at hard coded offsets, and to not break this program in the course of internationalization the actual labels had to be created outside the page content. Stranger things have happened...
Flatten and extract
As mentioned above, if you want text extraction to include these fields, too, you should consider flattening the document in a first step (moving the field appearances into the regular page content stream) and extracting text from this flattened document. This can be done like this:
[Test]
public void ExtractFlattenedTextTestSeeb()
{
FileInfo file = new FileInfo(#"PATH_TO_FILE\41851208.pdf");
Console.Out.Write("41851208.pdf, flattened before extraction\n\n");
using (MemoryStream memStream = new MemoryStream())
{
using (PdfReader readerOrig = new PdfReader(file.FullName))
using (PdfStamper stamper = new PdfStamper(readerOrig, memStream))
{
stamper.Writer.CloseStream = false;
stamper.FormFlattening = true;
}
memStream.Position = 0;
using (PdfReader readerFlat = new PdfReader(memStream))
{
PdfReaderContentParser parser = new PdfReaderContentParser(readerFlat);
for (int i = 1; i <= readerFlat.NumberOfPages; i++)
{
SimpleTextExtractionStrategy simpleStragety = parser.ProcessContent(i, new SimpleTextExtractionStrategy());
string contentText = simpleStragety.GetResultantText();
Console.Write("Page {0}:\n\n{1}\n\n", i, contentText);
}
}
}
}
The result StandardOutput:
41851208.pdf, flattened before extraction
Page 1:
90–120 l/min
(23.8–31.7 US gal./min)
60 kg
(132 lbs)
115 kg
(254 lbs)
350 l
(92.5 US gal.)
100 kg 105 kg
(220 lbs) (231 kg)
100 kg
(220 lbs)
250 l 300 l
(66.0 US gal.) (79.3 US gal.)
90 kg
(198 lbs)
180 l
(47.6 US gal.)
5305kg
(11695 lbs)
5265kg
(11607 lbs)
5395kg
(11894 lbs)
5205kg
(11475 lbs)
5010kg
(11045 lbs)
4780kg
(10538 lbs)
4470kg
(9854 lbs)
4190kg
(9237 lbs)
3930kg
(8664 lbs)
5215kg
(11497 lbs)
5045kg
(11122 lbs)
4860kg
(10714 lbs)
4650kg
(10251 lbs)
4350kg
(9590 lbs)
4100kg
(9039 lbs)
3850kg
(8488 lbs)
25.2 m
(82’ 8")
23.2 m
(76’ 1")
21.0 m
(68’ 11")
18.7 m
(61’ 4")
16.4 m
(53’ 10")
14.1 m
(46’ 3")
11.8 m
(38’ 9")
9.7 m
(31’ 10")
7.7 m
(25’ 3")
36.5 MPa (365 bar)
(5293 psi)
endlos
endless
sans finite
25.2 m
31.2 m
(82’ 8")
(102’ 4")
21.0 m
(68’ 11")
14900kg
(32848 lbs)
403.2 kNm (41.1 mt)
(297270 ft.lbs)
49.1 kNm (5.0 mt)
PK 42002–SH A–G
(36210 ft.lbs)
37.3 kNm (3.8 mt)
PK 42002–SH A–C
(27510 ft.lbs)
1GETR 2GETR
PK 42002–SH A – C
KT250 KT300 KT350 KT180
2GETR STZY
+V1
+V2
+2/4
7(F) 8(G) 6(E) 5(D) 4(C) 3(B) 2(A)
+V1
+V2
(S410–SK–D)
DTS410SHC/03
0100
11/2010
PK 42002–SH
Type Model Modell
Page Page Seite
Chapitre Chapter Kapitel
Edition Edition Ausgabe
Öltank
Mehrgewicht:
Alle Gewichtsangaben ohne Aufbauzubehör,Zusatzgeräte und Öl.
Hydr. Ausschübe:
Max. Reichweite + Fly-Jib:
Max. Reichweite:
Fördermenge der Pumpe:
Betriebsdruck:
Schwenkmoment:
Schwenkbereich:
Max. Reichweite:
Max. hydraulische Reichweite:
Max. Hubkraft:
Max. Hubmoment:
Gewicht +V ohne 2/4
Krangewicht (R3X,STZS):
Technische Daten
Konstruktionsänderungen vorbehalten, fertigungstechn. Toleranzen müssen berücksichtigt werden.
Oil tank
Excess weight:
All weights given without assembly accessory,additional devices and oil.
Hydr. boom extensions:
Max. outreach + Fly-Jib:
Max. outreach:
Pump capacity:
Operating pressure:
Slewing torque:
Slewing angle:
Max. outreach:
Max. hydraulic outreach:
Max. lifting capacity:
Lifting moment:
Weight +V without 2/4
Crane weight (R3X,STZS):
Specifications
Subject to change, production tolerances have to be taken into account.
Réservoir
Excessif poids:
Tous les poids sans huile ni accessoire de montage ni appareils accessoires
Extensions hydrauliques:
Portee maximale + Fly-Jib:
Max. portee:
Debit de pompe:
Pression d' utilisation:
Couple de rotation:
Angle de rotation:
Max. portee:
Portee hydraulique maximale:
Capacite maxi de levage:
Couple de levage:
Poids +V sans 2/4
Poids grue (R3X,STZS):
Données Techniques
Sous reserve de modifications de conception. Les tolerances relatives a la technique de production doivent etre prises en consideration.
As you see, "Lifting moment" and all the other missing labels are there now.