how to load one specific xps document into DocumentViewer for reading without loading it when application start
I only made a button to add a document. Is it possible to somehow load a document from a folder with a Visual Studio project or something else?
private void Load_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "XPS file (*.xps)|*.xps";
if (ofd.ShowDialog() == true)
{
XpsDocument doc = new XpsDocument(ofd.FileName, FileAccess.Read);
documentViewer.Document = doc.GetFixedDocumentSequence();
}
}
1 answer
-
answered 2022-01-23 12:12
nosferatu zodd
private void Book1_Click(object sender, RoutedEventArgs e) { var xpsDocument = new XpsDocument(@"C:documentPath", FileAccess.Read); documentViewer.Document = xpsDocument.GetFixedDocumentSequence(); }
do you know?
how many words do you know
See also questions close to this topic
-
C# - Adding condition to func results in stack overflow exception
I have a func as part of specification class which sorts the given iqueryable
Func<IQueryable<T>, IOrderedQueryable<T>>? Sort { get; set; }
When i add more than one condition to the func like below , it results in stack overflow exception.
spec.OrderBy(sc => sc.Case.EndTime).OrderBy(sc => sc.Case.StartTime);
The OrderBy method is implemented like this
public ISpecification<T> OrderBy<TProperty>(Expression<Func<T, TProperty>> property) { _ = Sort == null ? Sort = items => items.OrderBy(property) : Sort = items => Sort(items).ThenBy(property); return this; }
Chaining or using separate lines doesn't make a difference.
This problem gets resolved if I assign a new instance of the specification and set it's func, but i don't want to be assigning to a new instance everytime. Please suggest what am i missing here and how to reuse the same instance (if possible).
-
How to projection fields for a dictionary (C#, MongdoDB)
I am trying my luck here, I have a model which is like the following
public class RowData : BaseBsonDefinition { . [BsonExtraElements] [BsonDictionaryOptions(DictionaryRepresentation.ArrayOfDocuments)] public Dictionary<string, object> Rows { get; set; } = new(StringComparer.OrdinalIgnoreCase); . }
In result, the schema in the MongoDB looks like
{ "_id": { "$binary": { "base64": "HiuI1sgyT0OZmcgGUit2dw==", "subType": "03" } }, "c1": "AAA", "c8": "Fully Vac", "c10": "", }
Those c1, c8 and c10 fields are keys from the dictionary, my question is how to dynamic project those fields?
I tried
Builders<RowData>.Projection.Exclude(p => "c1")
It seems the MongoDB driver can not handle a value directly.
Anyone could point me in the correct direction?
Thanks,
-
How do I add new DataSource to an already Databinded CheckBoxList
i'm building a web form that show Database's item(Tables, Rows, FK,...)
I have a CheckBoxList of Tables (
chkListTable
) which will show a new CheckBoxList of Rows (chkListRow
) everytime I SelectedIndexChanged fromchkListTable
. The problem is i can show the items fromchkListTable
with 1 selected item. But i don't know how to showchkListRow
if multiple item fromchkListTable
are selected.Here are my codes:
aspx
:<div> <asp:Label ID="Label2" runat="server" Text="Table: "></asp:Label> <asp:CheckBoxList ID="chkListTable" runat="server" DataTextField="name" DataValueFeild="name" AutoPostBack="true" OnSelectedIndexChanged="chkListTable_SelectedIndexChanged"> </asp:CheckBoxList> </div> <div> <asp:CheckBoxList ID="chkListRow" runat="server" DataTextField="COLUMN_NAME" DataValueField="COLUMN_NAME" RepeatDirection="Horizontal"> </asp:CheckBoxList> </div>
aspx.cs
:protected void chkListTable_SelectedIndexChanged(object sender, EventArgs e) { tableName.Clear(); foreach (ListItem item in chkListTable.Items) { if(item.Selected) { tableName.Add(item.Text.Trim()); } } for(int i = 0; i < tableName.Count; i++) { String query = "USE " + dbname + " SELECT * FROM information_schema.columns" + " WHERE table_name = '" + tableName[i] + "'" + " AND COLUMN_NAME != 'rowguid'"; chkListRow.DataSource = Program.ExecSqlDataReader(query); chkListRow.DataBind(); Program.conn.Close(); } }
Program.cs
:public static bool Connect() { if (Program.conn != null && Program.conn.State == ConnectionState.Open) Program.conn.Close(); try { Program.conn.ConnectionString = Program.constr; Program.conn.Open(); return true; } catch (Exception e) { return false; } } public static SqlDataReader ExecSqlDataReader(String query) { SqlDataReader myreader; SqlCommand sqlcmd = new SqlCommand(query, Program.conn); sqlcmd.CommandType = CommandType.Text; if (Program.conn.State == ConnectionState.Closed) Program.conn.Open(); try { myreader = sqlcmd.ExecuteReader(); return myreader; myreader.Close(); } catch (SqlException ex) { Program.conn.Close(); return null; } }
I want my display to be like this:
[x]Table1 [x]Table2 [ ]Table3 [ ]Row1(Table1) [ ]Row2(Table1) [ ]Row3(Table1) [ ]Row1(Table2) [ ]Row2(Table2)
-
Implementing mouse stroke detection similar to InkCanvas
My problem effectively boils down to accurate mouse movement detection.
I need to create my own implementation of an InkCanvas and have succeeded for the most part, except for drawing strokes accurately.
void OnMouseMove(object sneder, MouseEventArgs e) { var position = e.GetPosition(this); if (!Rect.Contains(position)) return; var ratio = new Point(Width / PixelDisplay.Size.X, Height / PixelDisplay.Size.Y); var intPosition = new IntVector(Math2.FloorToInt(position.X / ratio.X), Math2.FloorToInt(position.Y / ratio.Y)); DrawBrush.Draw(intPosition, PixelDisplay); UpdateStroke(intPosition); // calls CaptureMouse }
This works. The Bitmap (PixelDisplay) is updated and all is well. However, any kind of quick mouse movement causes large skips in the drawing. I've narrowed down the problem to
e.GetPosition(this)
, which blocks the event long enough to be inaccurate.There's this question which is long beyond revival, and its answers are unclear or simply don't have a noticeable difference. After some more testing, the stated solution and similar ideas fail specifically because of
e.GetPosition
.I know InkCanvas uses similar methods after looking through the source; detect the device, if it's a mouse, get its position and capture. I see no reason for the same process to not work identically here.
-
WPF Textbox Validation Error binded to Variable
In a .xaml file, I have a number of Textbox objects that are linked to data paths as such:
<TextBox Text="{Binding Path=Var1}">
These variables (like Var1) are all linked to double values, and as such, if a user attempts to input a non-double value like a string or an empty value, the box is highlighted red. I assume this is the default validation error.
I'd like to know if it's possible to set a boolean to true in my code-behind whenever one of these Textbox objects are highlighted red, and then set back to false when it's not highlighted red. In other words, I'd want to set the boolean to true whenever a user causes the default Textbox validation error, and to false when the error is resolved.
Additionally, I notice that this validation error fires off when the Textbox loses focus; i.e. when the user clicks off of that Textbox or moves to a different Textbox. Is it possible for the validation error to fire off on each keystroke instead?
Thanks. I'm something of a WPF beginner, but it's been hard to find specifics about my issue, and I suspect it'll be useful information to understand data validation down the road.
-
How to nest formatted strings in XAML?
Is it possible to nest formatted strings in XAML without creating a custom class?
Here is an example of what I want to achieve:
- Format a string, e.g.
"inner start {0} inner end"
. - Format the previous resultant string, e.g.
"outer start {0} outer end"
. - Use the final string as the
Text
of aTextBlock
.
As a concrete example, if
"foo"
was the string to be formatted, I would require the final string to be:"outer start inner start foo inner end outer end"
I am able to format a single string with the following:
<TextBlock Text="{Binding Source={x:Static res:Resources.Foo}, StringFormat={x:Static res:Resources.Bar}}" />
However, attempting to bind one
Binding
to another results in an error:<TextBlock Text="{Binding Source={Binding Source={x:Static res:Resources.Foo}, StringFormat={x:Static res:Resources.Bar}}, StringFormat={x:Static res:Resources.Baz}}" />
I think this could be achieved with an
IMultiValueConverter
, but I don't know if there is a simpler way. - Format a string, e.g.
-
PrintTicket for every FixedPage lost when using XpsSerializationManager
Sample code:
public static void MakeXPS(FixedDocument fixedDoc, PrintTicket ticket, Stream os) { using (Package package = Package.Open(os, FileMode.Create)) { var inMemPackageName = $"memorystream://foo.xps"; Uri packageUri = new Uri(inMemPackageName); PackageStore.RemovePackage(packageUri); PackageStore.AddPackage(packageUri, package); using (XpsDocument xpsDoc = new XpsDocument(package, CompressionOption.Fast, inMemPackageName)) { var paginator = ((IDocumentPaginatorSource)fixedDoc).DocumentPaginator; /*paginator.ComputePageCount(); int pageCount = paginator.PageCount; for (int i = 0; i < pageCount; ++i) { DocumentPage docPage = paginator.GetPage(i); using (docPage) { // every FixedPage has its own PrintTicket //((FixedPage)docPage.Visual)... } }*/ XpsPackagingPolicy policy = new XpsPackagingPolicy(xpsDoc); using (var manager = new XpsSerializationManager(policy, false)) { policy.PersistPrintTicket(ticket); // original printticket for document with default settings manager.SaveAsXaml(paginator); // <-- does NOT save the printtickets attached to every FixedPage in the fixedDoc object manager.Commit(); } PackageStore.RemovePackage(packageUri); } } }
In the code above you see that we create a new XPS document from a given FixedDocument.
The FixedDocument
fixedDoc
contains severalFixedPage
s each page having its ownPrintTicket
. The PrintTicketticket
is a PrintTicket on document level (e.g. kind of 'default print settings').When we use the
XpsSerializationManager
to create the XPS file, the PrintTickets attached to every page (FixedPage) are gone.Why does
XpsSerializationManager
not save the PrintTickets at page level? Do I need to configure anything special?(+ additional small question: should
PackageStore.RemovePackage(packageUri);
be done before XpsDocument.Close() or after (outside theusing
-statement)? -
XPS text position -- what am I not understanding?
I have multiple examples, this is just the first one. I've been extracting some information from this file (I really wish there were a better way!) and something changed that broke my parser. Here's an example of the problem:
The first line displays the date over at the right margin, the second displays "Room 1" in a slightly larger, bolded font against the left margin farther down the page. Note, though, that the first line has a Y location about 250 units below the second. There's obviously something about the location I'm not understanding.
(I only need to be able to properly Y-sort the items. While this one is in the right order I have found them out of order in the past.)
<Glyphs RenderTransform="0.0403884,0,0,0.0403119,0,0" Fill="#ff000000" FontUri="/Documents/1/Resources/Fonts/6C15166D-E658-4B97-A6C0-E217017F767F.odttf" FontRenderingEmSize="327.68" StyleSimulations="None" OriginX="14079.4" OriginY="2270.24" Indices="26,60;17,61;3,59;48,63;68,60;85,60;70,60;75,61;3,62;21,60;19,60;21,61;21" UnicodeString="7. March 2022" /> <Glyphs RenderTransform="0.0646215,0,0,0.0644991,0,0" Fill="#ff000000" FontUri="/Documents/1/Resources/Fonts/FF418D14-C0F5-49FA-8F94-42C185369528.odttf" FontRenderingEmSize="327.68" StyleSimulations="None" OriginX="836.8" OriginY="1929.92" Indices="53,59;82,60;82,59;80,62;3,58;20" UnicodeString="Room 1" />