Skip to content

Commit

Permalink
New Update Commit:
Browse files Browse the repository at this point in the history
1.Fixed the issue where formatting HTML caused it to break.
2.Added ignoreCase support for tags, allowing usage of <c> or <C> interchangeably.
3.Application errors and UI have been translated into English.
4.The algorithm for creating justified tables has been rewritten.
5.Several minor changes were made.
  • Loading branch information
BeratARPA committed Jan 6, 2025
1 parent ff76eea commit d16fdfd
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 180 deletions.
6 changes: 3 additions & 3 deletions PrintHTML/Helpers/AsyncPrintTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public static void Exec(bool highPriority, Action action)
private static void InternalExec(Action action)
{
try
{
{
action.Invoke();
}
catch (Exception e)
catch (Exception exception)
{
MessageBox.Show(e.Message);
MessageBox.Show($"There is a problem while printing.\n\nError Message: {exception.Message}");
}
}
}
Expand Down
34 changes: 23 additions & 11 deletions PrintHTML/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -1,39 +1,51 @@
<Window x:Class="PrintHTML.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Yazdırma Şablonu" Height="768" Width="1024" WindowStartupLocation="CenterScreen">
Title="Flexible Print Application" Height="600" Width="800" WindowStartupLocation="CenterScreen">

<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>

<StackPanel Grid.Row="0" Margin="0,0,0,10">
<StackPanel Grid.Row="0">
<DockPanel Margin="0,0,0,5">
<Label Content="Printers:" Width="120"/>
<ComboBox x:Name="ComboBoxPrinters" SelectionChanged="ComboBoxPrinters_SelectionChanged"/>
</DockPanel>

<DockPanel Margin="0,0,0,5">
<Label Content="Yazıcı Seçin:" Width="100"/>
<ComboBox x:Name="cbPrinters" SelectionChanged="cbPrinters_SelectionChanged"/>
<Label Content="Characters Per Line:" Width="120"/>
<TextBox x:Name="TextBoxMaxWidth" Width="50" HorizontalAlignment="Left" TextChanged="TextBoxMaxWidth_TextChanged" PreviewTextInput="TextBoxMaxWidth_PreviewTextInput" Text="42"/>
<Label Content="58MM = 32 char, 80MM = 42 char" Width="200" HorizontalAlignment="Left"/>
</DockPanel>
</StackPanel>

<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="373"/>
<ColumnDefinition Width="331"/>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" x:Name="txtHtmlContent"

<TextBox Grid.Column="0" x:Name="TextBoxContent"
AcceptsReturn="True"
TextWrapping="Wrap"
FontSize="12"
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Auto"
FontFamily="Consolas"
Text="&lt;div style=&quot;text-align: center; color: red;&quot;&gt;YAZDIRMA ŞABLONU KULLANIM KILAVUZU&lt;/div&gt;&#x0a;&#x0a;&lt;F&gt;=&#x0a;&lt;T&gt;Metin Hizalama Örnekleri:&#x0a;&lt;L&gt;Bu metin sola hizalı&#x0a;&lt;C&gt;Bu metin ortalı&#x0a;&lt;R&gt;Bu metin sağa hizalı&#x0a;&#x0a;&lt;F&gt;=&#x0a;&#x0a;&lt;T&gt;Kalın Yazı Örnekleri:&#x0a;&lt;EB&gt;&#x0a;Bu metin kalın olacak&#x0a;&lt;DB&gt;&#x0a;&#x0a;&lt;F&gt;=&#x0a;&#x0a;&lt;T&gt;Tablo Örneği:&#x0a;&lt;J&gt;Ürün | Stok&#x0a;&lt;J&gt;Laptop | 25&#x0a;&lt;J&gt;Mouse | 100&#x0a;&lt;J&gt;Klavye | 50&#x0a;&#x0a;&lt;F&gt;=&#x0a;&#x0a;&lt;C&gt;beratarpa.com"/>
<FlowDocumentScrollViewer x:Name="flowDocumentScrollViewer" Grid.Column="1"/>
Text="&lt;div style=&quot;text-align: center; color: red;&quot;&gt;PRINT TEMPLATE USER GUIDE&lt;/div&gt;&#x0a;&#x0a;&lt;F&gt;=&#x0a;&lt;T&gt;Text Alignment Examples:&#x0a;&lt;L&gt;This text is left-aligned&#x0a;&lt;C&gt;This text is centered&#x0a;&lt;R&gt;This text is right-aligned&#x0a;&#x0a;&lt;F&gt;=&#x0a;&#x0a;&lt;T&gt;Bold Text Examples:&#x0a;&lt;EB&gt;&#x0a;This text will be bold&#x0a;&lt;DB&gt;&#x0a;&#x0a;&lt;F&gt;=&#x0a;&#x0a;&lt;T&gt;Table Example:&#x0a;&lt;J&gt;Product | Stock&#x0a;&lt;J&gt;Laptop | 25&#x0a;&lt;J&gt;Mouse | 100&#x0a;&lt;J&gt;Keyboard | 50&#x0a;&#x0a;&lt;F&gt;=&#x0a;&#x0a;&lt;C&gt;beratarpa.com"/>

<GridSplitter Grid.Column="0" Width="5" HorizontalAlignment="Right" VerticalAlignment="Stretch" Background="Gray"/>

<FlowDocumentScrollViewer x:Name="FlowDocumentScrollViewer" Grid.Column="1" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"/>
</Grid>

<StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,10,0,0">
<Button x:Name="btnPreview" Content="Önizleme" Width="100" Margin="0,0,10,0" Click="btnPreview_Click"/>
<Button x:Name="btnPrint" Content="Yazdır" Width="100" Click="btnPrint_Click"/>
<Button x:Name="ButtonPreview" Content="Preview" Width="100" Margin="0,0,10,0" Click="ButtonPreview_Click"/>
<Button x:Name="ButtonPrint" Content="Print" Width="100" Click="ButtonPrint_Click"/>
</StackPanel>
</Grid>
</Window>
61 changes: 37 additions & 24 deletions PrintHTML/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,87 +14,100 @@ namespace PrintHTML
public partial class MainWindow : Window
{
private string _selectedPrinter;
private readonly HtmlPrinterService _printerService;
private int _charactersPerLine;
private readonly PrinterService _printerService = new PrinterService();

public MainWindow()
{
InitializeComponent();

LoadPrinters();
_printerService = new HtmlPrinterService();
}

private void LoadPrinters()
{
var printers = PrinterInfo.GetPrinterNames();
cbPrinters.ItemsSource = printers;
ComboBoxPrinters.ItemsSource = printers;
if (printers.Any())
{
cbPrinters.SelectedIndex = 0;
ComboBoxPrinters.SelectedIndex = 0;
}
}

private void btnPrint_Click(object sender, RoutedEventArgs e)
private void ButtonPrint_Click(object sender, RoutedEventArgs e)
{
try
{
if (string.IsNullOrEmpty(_selectedPrinter))
{
MessageBox.Show("Lütfen bir yazıcı seçin.", "Uyarı", MessageBoxButton.OK, MessageBoxImage.Warning);
MessageBox.Show("Please select a printer.", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}

if (string.IsNullOrWhiteSpace(txtHtmlContent.Text))
if (string.IsNullOrWhiteSpace(TextBoxContent.Text))
{
MessageBox.Show("Yazdırılacak HTML içeriği boş olamaz.", "Uyarı", MessageBoxButton.OK, MessageBoxImage.Warning);
MessageBox.Show("The content to be printed cannot be empty.", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}

btnPrint.IsEnabled = false;
ButtonPrint.IsEnabled = false;
Mouse.OverrideCursor = Cursors.Wait;

var content = txtHtmlContent.Text.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
AsyncPrintTask.Exec(true, () => _printerService.PrintAsync(content, _selectedPrinter));
var content = TextBoxContent.Text;

AsyncPrintTask.Exec(true, () => _printerService.DoPrint(content, _selectedPrinter, _charactersPerLine));

MessageBox.Show("Yazdırma işlemi başarıyla tamamlandı.", "Bilgi", MessageBoxButton.OK, MessageBoxImage.Information);
MessageBox.Show("The printing process was completed successfully.", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
}
catch (Exception ex)
catch (Exception exception)
{
MessageBox.Show($"Yazdırma işlemi sırasında bir hata oluştu: {ex.Message}", "Hata", MessageBoxButton.OK, MessageBoxImage.Error);
MessageBox.Show($"An error occurred during printing: {exception.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
finally
{
btnPrint.IsEnabled = true;
ButtonPrint.IsEnabled = true;
Mouse.OverrideCursor = null;
}
}

private async void btnPreview_Click(object sender, RoutedEventArgs e)
private void ButtonPreview_Click(object sender, RoutedEventArgs e)
{
try
{
var htmlContent = txtHtmlContent.Text;
var htmlContent = TextBoxContent.Text;

if (string.IsNullOrWhiteSpace(htmlContent))
{
MessageBox.Show("Önizleme için HTML içeriği boş olamaz.", "Uyarı", MessageBoxButton.OK, MessageBoxImage.Warning);
MessageBox.Show("Content for preview cannot be empty.", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}

flowDocumentScrollViewer.Document = await _printerService.GeneratePreview(htmlContent);
FlowDocumentScrollViewer.Document = _printerService.GeneratePreview(htmlContent, _charactersPerLine);

}
catch (Exception ex)
{
MessageBox.Show($"Önizleme sırasında bir hata oluştu: {ex.Message}", "Hata", MessageBoxButton.OK, MessageBoxImage.Error);
MessageBox.Show($"An error occurred during preview: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}

private void cbPrinters_SelectionChanged(object sender, SelectionChangedEventArgs e)
private void ComboBoxPrinters_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (cbPrinters.SelectedItem != null)
if (ComboBoxPrinters.SelectedItem != null)
{
_selectedPrinter = cbPrinters.SelectedItem.ToString();
_selectedPrinter = ComboBoxPrinters.SelectedItem.ToString();
}
}

private void TextBoxMaxWidth_TextChanged(object sender, TextChangedEventArgs e)
{
_charactersPerLine = Convert.ToInt16(TextBoxMaxWidth.Text);
}

private void TextBoxMaxWidth_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
// Yalnızca rakamlara izin ver
e.Handled = !e.Text.All(char.IsDigit);
}
}
}
4 changes: 2 additions & 2 deletions PrintHTML/PrintHTML.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@
<Compile Include="MainWindow.xaml.cs">
<DependentUpon>MainWindow.xaml</DependentUpon>
</Compile>
<Compile Include="Services\DocumentFormatter.cs" />
<Compile Include="Services\HtmlPrinterService.cs" />
<Compile Include="Services\HtmlFormattedDocument.cs" />
<Compile Include="Services\PrinterService.cs" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs">
Expand Down
Loading

0 comments on commit d16fdfd

Please sign in to comment.